class Ethereum::BlockHeader
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
Attributes
Public Class Methods
# File lib/ethereum/block_header.rb, line 59 def find(db, hash) bh = from_block_rlp db.get(hash) raise ValidationError, "BlockHeader.hash is broken" if bh.full_hash != hash bh end
# File lib/ethereum/block_header.rb, line 54 def from_block_rlp(rlp_data) block_data = RLP.decode_lazy rlp_data deserialize block_data[0] end
# File lib/ethereum/block_header.rb, line 68 def initialize(options={}) 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(options) 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
Public Instance Methods
Two blockheader are equal iff they have the same hash.
# File lib/ethereum/block_header.rb, line 182 def ==(other) other.instance_of?(BlockHeader) && full_hash == other.full_hash end
# File lib/ethereum/block_header.rb, line 97 def _state_root @state_root end
Check if the proof-of-work of the block is valid.
@param nonce [String] if given the proof of work function will be
evaluated with this nonce instead of the one already present in the header
@return [Bool]
# File lib/ethereum/block_header.rb, line 146 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
# File lib/ethereum/block_header.rb, line 125 def full_hash Utils.keccak256_rlp self end
# File lib/ethereum/block_header.rb, line 129 def full_hash_hex Utils.encode_hex full_hash end
# File lib/ethereum/block_header.rb, line 187 def hash Utils.big_endian_to_int full_hash end
# File lib/ethereum/block_header.rb, line 133 def mining_hash Utils.keccak256 RLP.encode(self, sedes: self.class.exclude(%i(mixhash nonce))) end
# File lib/ethereum/block_header.rb, line 117 def receipts_root get_with_block :receipts_root end
# File lib/ethereum/block_header.rb, line 121 def receipts_root=(v) set_with_block :receipts_root, v end
# File lib/ethereum/block_header.rb, line 101 def state_root get_with_block :state_root end
# File lib/ethereum/block_header.rb, line 105 def state_root=(v) set_with_block :state_root, v end
Serialize the header to a readable hash.
# File lib/ethereum/block_header.rb, line 154 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
# File lib/ethereum/block_header.rb, line 174 def to_s "#<#{self.class.name}:#{object_id} ##{number} #{full_hash_hex[0,8]}>" end
# File lib/ethereum/block_header.rb, line 109 def tx_list_root get_with_block :tx_list_root end
# File lib/ethereum/block_header.rb, line 113 def tx_list_root=(v) set_with_block :tx_list_root, v end
Private Instance Methods
# File lib/ethereum/block_header.rb, line 197 def get_with_block(attr) block ? block.send(attr) : instance_variable_get(:"@#{attr}") end
# File lib/ethereum/block_header.rb, line 193 def logger @logger ||= Logger.new 'eth.block.header' end
# File lib/ethereum/block_header.rb, line 201 def set_with_block(attr, v) if block block.send :"#{attr}=", v else _set_field attr, v end end