Module: Z4BC

Defined in:
lib/z4blockchain.rb

Defined Under Namespace

Classes: Bank, Error, Game, Lvls, User

Constant Summary collapse

BLOCKCHAIN =
Blockchain.new
MINES =
PStore.new("db/mines.pstore")
BANKS =
PStore.new("db/banks.pstore")
BANK =
Hash.new { |h,k| h[k] = Bank.new(k) }
GAMES =
PStore.new("db/games.pstore")
GAME =
Hash.new { |h,k| h[k] = Game.new(k) }
LVLS =
PStore.new("db/lvls.pstore")
LVL =
Hash.new { |h,k| h[k] = Lvls.new(k) }
USER =
Hash.new { |h,k| h[k] = User.new(k) }

Class Method Summary collapse

Class Method Details

.<<(h = {}) ⇒ Object



53
54
55
# File 'lib/z4blockchain.rb', line 53

def self.<< h={}
  BLOCKCHAIN << JSON.generate(h)
end

.balance(u) ⇒ Object



89
90
91
92
93
# File 'lib/z4blockchain.rb', line 89

def self.balance u
  x = 0
  BANK.each_pair { |k,v| x += v[u] }
  return x
end

.bank(b) ⇒ Object



95
96
97
# File 'lib/z4blockchain.rb', line 95

def self.bank b
  BANK[b]
end

.chainObject



45
46
47
# File 'lib/z4blockchain.rb', line 45

def self.chain
  BLOCKCHAIN
end

.digest(k) ⇒ Object



41
42
43
# File 'lib/z4blockchain.rb', line 41

def self.digest k
  Digest::SHA256.hexdigest(k)
end

.freeObject

Economy

Each channel controls it’s own “Bank”. Users within the channel fund it through payment for channel services. Users may reconcile obligations between each other for a fee.



64
65
66
67
68
# File 'lib/z4blockchain.rb', line 64

def self.free
  x = 0
  BANKS.transaction { |db| db.keys.each { |e| x += db[e] } }
  return x
end

.gameObject



118
119
120
# File 'lib/z4blockchain.rb', line 118

def self.game
  GAME
end

.heldObject



76
77
78
79
80
81
# File 'lib/z4blockchain.rb', line 76

def self.held
  x = 0
  BANKS.transaction { |db| db.keys.each { |e| BANK[e] } }
  BANK.each_pair { |k,v| x += v.holdings }
  return x
end

.lastObject



49
50
51
# File 'lib/z4blockchain.rb', line 49

def self.last
  BLOCKCHAIN[-1]
end

.levelObject



126
127
128
# File 'lib/z4blockchain.rb', line 126

def self.level
  LVLS
end

.lvlsObject



130
131
132
# File 'lib/z4blockchain.rb', line 130

def self.lvls
  LVL
end

.minedObject



70
71
72
73
74
# File 'lib/z4blockchain.rb', line 70

def self.mined
  x = 0
  MINES.transaction { |db| db.keys.each { |e| x += db[e] } }
  return x
end

.transaction(h = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/z4blockchain.rb', line 99

def self.transaction h={}
  bank = BANK[h[:bank]]
  if !h.has_key? :from
    bank.credit h
    h[:action] = :tx
    h[:description] = %[#{h[:bank]} credit #{h[:amount]} to #{h[:to]}.]
  elsif !h.has_key? :to
    bank.debit h
    h[:action] = :rx
    h[:description] = %[#{h[:bank]} debit #{h[:amount]} from #{h[:from]}.]
  else
    bank.xfer h
    h[:action] = :dx
    h[:description] = %[#{h[:bank]} xfer #{h[:amount]} from #{h[:from]} to #{h[:to]}.]
  end
  BLOCKCHAIN << JSON.generate(h)
  return nil
end

.userObject



122
123
124
# File 'lib/z4blockchain.rb', line 122

def self.user
  USER
end

.wallet(u) ⇒ Object



83
84
85
86
87
# File 'lib/z4blockchain.rb', line 83

def self.wallet u
  h = {}
  BANK.each_pair { |k,v| h[k] = v[u] }
  return h
end