class Node

Attributes

exchange[R]
id[R]
peers[R]
wallet[R]

Public Class Methods

new( address: ) click to toggle source
# File lib/tulipmania/node.rb, line 7
def initialize( address: )
  @id       = SecureRandom.uuid
  @peers    = []
  @wallet   = Wallet.new( address )
  @exchange = Exchange.new @wallet.address
end

Public Instance Methods

on_add_peer( host, port ) click to toggle source
# File lib/tulipmania/node.rb, line 15
def on_add_peer( host, port )
  @peers << [host, port]
  @peers.uniq!
  # TODO/FIX: no need to send to every peer, just the new one
  send_chain_to_peers
  @exchange.pending.each { |tx| send_transaction_to_peers( tx ) }
end
on_add_transaction( from, to, qty, what, id ) click to toggle source
# File lib/tulipmania/node.rb, line 28
def on_add_transaction( from, to, qty, what, id )
  ## note: for now must always pass in id - why? why not? possible tx without id???
  tx = Tx.new( from, to, qty, what, id )
  if @exchange.sufficient_tulips?( tx.from, tx.qty, tx.what ) && @exchange.add_transaction( tx )
    send_transaction_to_peers( tx )
    return true
  else
    return false
  end
end
on_delete_peer( index ) click to toggle source
# File lib/tulipmania/node.rb, line 23
def on_delete_peer( index )
  @peers.delete_at( index )
end
on_mine!() click to toggle source
# File lib/tulipmania/node.rb, line 50
def on_mine!
  @exchange.mine_block!
  send_chain_to_peers
end
on_resolve( data ) click to toggle source
# File lib/tulipmania/node.rb, line 55
def on_resolve( data )
  chain_new = Blockchain.from_json( data )
  if @exchange.resolve!( chain_new )
    send_chain_to_peers
    return true
  else
    return false
  end
end
on_send( to, qty, what ) click to toggle source
# File lib/tulipmania/node.rb, line 39
def on_send( to, qty, what )
  tx = @wallet.generate_transaction( to, qty, what )
  if @exchange.sufficient_tulips?( tx.from, tx.qty, tx.what ) && @exchange.add_transaction( tx )
    send_transaction_to_peers( tx )
    return true
  else
    return false
  end
end

Private Instance Methods

send_chain_to_peers() click to toggle source
# File lib/tulipmania/node.rb, line 69
def send_chain_to_peers
  data = JSON.pretty_generate( @exchange.as_json )   ## payload in json
  @peers.each do |(host, port)|
    Net::HTTP.post(URI::HTTP.build(host: host, port: port, path: '/resolve'), data )
  end
end
send_transaction_to_peers( tx ) click to toggle source
# File lib/tulipmania/node.rb, line 76
def send_transaction_to_peers( tx )
  @peers.each do |(host, port)|
    Net::HTTP.post_form(URI::HTTP.build(host: host, port: port, path: '/transactions'), tx.to_h )
  end
end