class Ethereum::Index
Collection of indexes.
-
children - needed to get the uncles of a block
-
blocknumbers - needed to mark the longest chain (path to top)
-
transactions - optional to resolve txhash to block:tx
Attributes
db[R]
Public Class Methods
new(env, index_transactions=true)
click to toggle source
# File lib/ethereum/index.rb, line 15 def initialize(env, index_transactions=true) @env = env @db = env.db @index_transactions = index_transactions end
Public Instance Methods
add_block(blk)
click to toggle source
# File lib/ethereum/index.rb, line 21 def add_block(blk) add_child blk.prevhash, blk.full_hash add_transactions blk if @index_transactions end
add_child(parent_hash, child_hash)
click to toggle source
# File lib/ethereum/index.rb, line 26 def add_child(parent_hash, child_hash) children = (get_children(parent_hash) + [child_hash]).uniq @db.put_temporarily child_db_key(parent_hash), RLP.encode(children) end
get_block_by_number(number)
click to toggle source
# File lib/ethereum/index.rb, line 52 def get_block_by_number(number) @db.get block_by_number_key(number) end
get_children(blk_hash)
click to toggle source
# File lib/ethereum/index.rb, line 56 def get_children(blk_hash) key = child_db_key blk_hash @db.has_key?(key) ? RLP.decode(@db.get(key)) : [] end
get_transaction(txhash)
click to toggle source
@param txhash [String] transaction hash
@return [[Transaction, Block
, Integer]] transaction, block, and tx number
# File lib/ethereum/index.rb, line 66 def get_transaction(txhash) blockhash, tx_num_enc = RLP.decode @db.get(txhash) blk = RLP.decode(@db.get(blockhash), sedes: Block, env: @env) num = Utils.decode_int tx_num_enc tx_data = blk.get_transaction num [tx_data, blk, num] end
has_block_by_number(number)
click to toggle source
# File lib/ethereum/index.rb, line 48 def has_block_by_number(number) @db.has_key? block_by_number_key(number) end
update_blocknumbers(blk)
click to toggle source
start from head and update until the existing indices match the block
# File lib/ethereum/index.rb, line 32 def update_blocknumbers(blk) loop do if blk.number > 0 @db.put_temporarily block_by_number_key(blk.number), blk.full_hash else @db.put block_by_number_key(blk.number), blk.full_hash end @db.commit_refcount_changes blk.number break if blk.number == 0 blk = blk.get_parent() break if has_block_by_number(blk.number) && get_block_by_number(blk.number) == blk.full_hash end end
Private Instance Methods
add_transactions(blk)
click to toggle source
# File lib/ethereum/index.rb, line 82 def add_transactions(blk) blk.get_transactions.each_with_index do |tx, i| @db.put_temporarily tx.full_hash, RLP.encode([blk.full_hash, i]) end @db.commit_refcount_changes blk.number end
block_by_number_key(number)
click to toggle source
# File lib/ethereum/index.rb, line 90 def block_by_number_key(number) "blocknumber:#{number}" end
child_db_key(blk_hash)
click to toggle source
# File lib/ethereum/index.rb, line 78 def child_db_key(blk_hash) "ci:#{blk_hash}" end