Class: Exchange

Inherits:
Object
  • Object
show all
Defined in:
lib/tulipmania/exchange.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ Exchange

Returns a new instance of Exchange.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tulipmania/exchange.rb', line 9

def initialize( address )
  @address = address

  @cache = Cache.new( "data.#{address.downcase}.json" )
  h = @cache.read
  if h
    ## restore blockchain
    @chain = Blockchain.from_json( h['chain'] )
    ## restore pending transactions too
    @pending = Pool.from_json( h['transactions'] )
  else
    @chain   = Blockchain.new
    @chain  << [Tx.new( Tulipmania.config.rand_coinbase,
                        @address,
                        Tulipmania.config.mining_reward,
                        Tulipmania.config.rand_tulip )]    # genesis (big bang!) starter block
    @pending = Pool.new
  end

  ## update ledger (balances) with confirmed transactions
  @ledger = Ledger.new( @chain )
end

Instance Attribute Details

#chainObject (readonly)

Returns the value of attribute chain.



6
7
8
# File 'lib/tulipmania/exchange.rb', line 6

def chain
  @chain
end

#ledgerObject (readonly)

Returns the value of attribute ledger.



6
7
8
# File 'lib/tulipmania/exchange.rb', line 6

def ledger
  @ledger
end

#pendingObject (readonly)

Returns the value of attribute pending.



6
7
8
# File 'lib/tulipmania/exchange.rb', line 6

def pending
  @pending
end

Instance Method Details

#add_transaction(tx) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/tulipmania/exchange.rb', line 59

def add_transaction( tx )
  if tx.valid? && transaction_is_new?( tx )
    @pending << tx
    @cache.write as_json
    return true
  else
    return false
  end
end

#as_jsonObject



94
95
96
97
98
# File 'lib/tulipmania/exchange.rb', line 94

def as_json
  { chain:        @chain.as_json,
    transactions: @pending.as_json
  }
end

#mine_block!Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tulipmania/exchange.rb', line 34

def mine_block!
  add_transaction( Tx.new( Tulipmania.config.rand_coinbase,
                           @address,
                           Tulipmania.config.mining_reward,
                           Tulipmania.config.rand_tulip ))

  ## add mined (w/ computed/calculated hash) block
  @chain << @pending.transactions
  @pending = Pool.new

  ## update ledger (balances) with new confirmed transactions
  @ledger = Ledger.new( @chain )

  @cache.write as_json
end

#resolve!(chain_new) ⇒ Object

check - how to name incoming chain - chain_new, chain_candidate - why? why not? what's an intuitive name - what's gets used most often???



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tulipmania/exchange.rb', line 74

def resolve!( chain_new )
  # TODO this does not protect against invalid block shapes (bogus COINBASE transactions for example)

  if !chain_new.empty? && chain_new.last.valid? && chain_new.size > @chain.size
    @chain  = chain_new
    ## update ledger (balances) with new confirmed transactions
    @ledger = Ledger.new( @chain )

    ##  document - keep only pending (unconfirmed) transaction not yet in blockchain ????
    @pending.update!( @chain.transactions)

    @cache.write as_json
    return true
  else
    return false
  end
end

#sufficient_tulips?(wallet, qty, what) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/tulipmania/exchange.rb', line 51

def sufficient_tulips?( wallet, qty, what )
  ## (convenience) delegate for ledger
  ##  todo/check: use address instead of wallet - why? why not?
  ##   for now single address wallet (that is, wallet==address)
  @ledger.sufficient_tulips?( wallet, qty, what )
end