Class: Ethereum::BlockHeader
Overview
A block header.
If the block with this header exists as an instance of Block, the connection can be made explicit by setting ‘BlockHeader.block`. Then, `BlockHeader.state_root`, `BlockHeader.tx_list_root` and `BlockHeader.receipts_root` always refer to the up-to-date value in the block instance.
-
‘@block` - an instance of Block or `nil`
-
‘@prevhash` - the 32 byte hash of the previous block
-
‘@uncles_hash` - the 32 byte hash of the RLP encoded list of uncle headers
-
‘@coinbase` - the 20 byte coinbase address
-
‘@state_root` - the root of the block’s state trie
-
‘@tx_list_root` - the root of the block’s transaction trie
-
‘@receipts_root` - the root of the block’s receipts trie
-
‘@bloom` - bloom filter
-
‘@difficulty` - the block’s difficulty
-
‘@number` - the number of ancestors of this block (0 for the genesis block)
-
‘@gas_limit` - the block’s gas limit
-
‘@gas_used` - the total amount of gas used by all transactions in this block
-
‘@timestamp` - a UNIX timestamp
-
‘@extra_data` - up to 1024 bytes of additional data
-
‘@nonce` - a 8 byte nonce constituting a proof-of-work, or the empty string as a placeholder
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Two blockheader are equal iff they have the same hash.
- #_state_root ⇒ Object
-
#check_pow(nonce = nil) ⇒ Bool
Check if the proof-of-work of the block is valid.
- #full_hash ⇒ Object
- #full_hash_hex ⇒ Object
- #hash ⇒ Object
-
#initialize(options = {}) ⇒ BlockHeader
constructor
A new instance of BlockHeader.
- #mining_hash ⇒ Object
- #receipts_root ⇒ Object
- #receipts_root=(v) ⇒ Object
- #state_root ⇒ Object
- #state_root=(v) ⇒ Object
-
#to_h ⇒ Object
Serialize the header to a readable hash.
- #to_s ⇒ Object (also: #inspect)
- #tx_list_root ⇒ Object
- #tx_list_root=(v) ⇒ Object
Methods included from Sedes
address, big_endian_int, binary, hash32, int20, int256, int32, trie_root
Constructor Details
#initialize(options = {}) ⇒ BlockHeader
Returns a new instance of BlockHeader.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ethereum/block_header.rb', line 69 def initialize(={}) fields = { prevhash: Env::DEFAULT_CONFIG[:genesis_prevhash], uncles_hash: Utils.keccak256_rlp([]), coinbase: Env::DEFAULT_CONFIG[:genesis_coinbase], state_root: PruningTrie::BLANK_ROOT, tx_list_root: PruningTrie::BLANK_ROOT, receipts_root: PruningTrie::BLANK_ROOT, bloom: 0, difficulty: Env::DEFAULT_CONFIG[:genesis_difficulty], number: 0, gas_limit: Env::DEFAULT_CONFIG[:genesis_gas_limit], gas_used: 0, timestamp: 0, extra_data: '', mixhash: Env::DEFAULT_CONFIG[:genesis_mixhash], nonce: '' }.merge() fields[:coinbase] = Utils.decode_hex(fields[:coinbase]) if fields[:coinbase].size == 40 raise ArgumentError, "invalid coinbase #{fields[:coinbase]}" unless fields[:coinbase].size == 20 raise ArgumentError, "invalid difficulty" unless fields[:difficulty] > 0 self.block = nil @fimxe_hash = nil super(**fields) end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
67 68 69 |
# File 'lib/ethereum/block_header.rb', line 67 def block @block end |
Class Method Details
.find(db, hash) ⇒ Object
60 61 62 63 64 |
# File 'lib/ethereum/block_header.rb', line 60 def find(db, hash) bh = from_block_rlp db.get(hash) raise ValidationError, "BlockHeader.hash is broken" if bh.full_hash != hash bh end |
.from_block_rlp(rlp_data) ⇒ Object
55 56 57 58 |
# File 'lib/ethereum/block_header.rb', line 55 def from_block_rlp(rlp_data) block_data = RLP.decode_lazy rlp_data deserialize block_data[0] end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Two blockheader are equal iff they have the same hash.
183 184 185 |
# File 'lib/ethereum/block_header.rb', line 183 def ==(other) other.instance_of?(BlockHeader) && full_hash == other.full_hash end |
#_state_root ⇒ Object
98 99 100 |
# File 'lib/ethereum/block_header.rb', line 98 def _state_root @state_root end |
#check_pow(nonce = nil) ⇒ Bool
Check if the proof-of-work of the block is valid.
147 148 149 150 |
# File 'lib/ethereum/block_header.rb', line 147 def check_pow(nonce=nil) logger.debug "checking pow", block: full_hash_hex[0,8] Miner.check_pow(number, mining_hash, mixhash, nonce || self.nonce, difficulty) end |
#full_hash ⇒ Object
126 127 128 |
# File 'lib/ethereum/block_header.rb', line 126 def full_hash Utils.keccak256_rlp self end |
#full_hash_hex ⇒ Object
130 131 132 |
# File 'lib/ethereum/block_header.rb', line 130 def full_hash_hex Utils.encode_hex full_hash end |
#hash ⇒ Object
188 189 190 |
# File 'lib/ethereum/block_header.rb', line 188 def hash Utils.big_endian_to_int full_hash end |
#mining_hash ⇒ Object
134 135 136 |
# File 'lib/ethereum/block_header.rb', line 134 def mining_hash Utils.keccak256 RLP.encode(self, sedes: self.class.exclude(%i(mixhash nonce))) end |
#receipts_root ⇒ Object
118 119 120 |
# File 'lib/ethereum/block_header.rb', line 118 def receipts_root get_with_block :receipts_root end |
#receipts_root=(v) ⇒ Object
122 123 124 |
# File 'lib/ethereum/block_header.rb', line 122 def receipts_root=(v) set_with_block :receipts_root, v end |
#state_root ⇒ Object
102 103 104 |
# File 'lib/ethereum/block_header.rb', line 102 def state_root get_with_block :state_root end |
#state_root=(v) ⇒ Object
106 107 108 |
# File 'lib/ethereum/block_header.rb', line 106 def state_root=(v) set_with_block :state_root, v end |
#to_h ⇒ Object
Serialize the header to a readable hash.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/ethereum/block_header.rb', line 155 def to_h h = {} %i(prevhash uncles_hash extra_data nonce mixhash).each do |field| h[field] = "0x#{Utils.encode_hex(send field)}" end %i(state_root tx_list_root receipts_root coinbase).each do |field| h[field] = Utils.encode_hex send(field) end %i(number difficulty gas_limit gas_used timestamp).each do |field| h[field] = send(field).to_s end h[:bloom] = Utils.encode_hex Sedes.int256.serialize(bloom) h end |
#to_s ⇒ Object Also known as: inspect
175 176 177 |
# File 'lib/ethereum/block_header.rb', line 175 def to_s "#<#{self.class.name}:#{object_id} ##{number} #{full_hash_hex[0,8]}>" end |
#tx_list_root ⇒ Object
110 111 112 |
# File 'lib/ethereum/block_header.rb', line 110 def tx_list_root get_with_block :tx_list_root end |
#tx_list_root=(v) ⇒ Object
114 115 116 |
# File 'lib/ethereum/block_header.rb', line 114 def tx_list_root=(v) set_with_block :tx_list_root, v end |