Class: Ethereum::Index
Overview
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
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
- #add_block(blk) ⇒ Object
- #add_child(parent_hash, child_hash) ⇒ Object
- #get_block_by_number(number) ⇒ Object
- #get_children(blk_hash) ⇒ Object
-
#get_transaction(txhash) ⇒ [Transaction, Block, Integer]
Transaction, block, and tx number.
- #has_block_by_number(number) ⇒ Object
-
#initialize(env, index_transactions = true) ⇒ Index
constructor
A new instance of Index.
-
#update_blocknumbers(blk) ⇒ Object
start from head and update until the existing indices match the block.
Constructor Details
#initialize(env, index_transactions = true) ⇒ Index
Returns a new instance of Index.
16 17 18 19 20 |
# File 'lib/ethereum/index.rb', line 16 def initialize(env, index_transactions=true) @env = env @db = env.db @index_transactions = index_transactions end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
14 15 16 |
# File 'lib/ethereum/index.rb', line 14 def db @db end |
Instance Method Details
#add_block(blk) ⇒ Object
22 23 24 25 |
# File 'lib/ethereum/index.rb', line 22 def add_block(blk) add_child blk.prevhash, blk.full_hash add_transactions blk if @index_transactions end |
#add_child(parent_hash, child_hash) ⇒ Object
27 28 29 30 |
# File 'lib/ethereum/index.rb', line 27 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) ⇒ Object
53 54 55 |
# File 'lib/ethereum/index.rb', line 53 def get_block_by_number(number) @db.get block_by_number_key(number) end |
#get_children(blk_hash) ⇒ Object
57 58 59 60 |
# File 'lib/ethereum/index.rb', line 57 def get_children(blk_hash) key = child_db_key blk_hash @db.has_key?(key) ? RLP.decode(@db.get(key)) : [] end |
#get_transaction(txhash) ⇒ [Transaction, Block, Integer]
Returns transaction, block, and tx number.
67 68 69 70 71 72 73 74 75 |
# File 'lib/ethereum/index.rb', line 67 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) ⇒ Object
49 50 51 |
# File 'lib/ethereum/index.rb', line 49 def has_block_by_number(number) @db.has_key? block_by_number_key(number) end |
#update_blocknumbers(blk) ⇒ Object
start from head and update until the existing indices match the block
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ethereum/index.rb', line 33 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 |