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.

Attributes

block[RW]

Public Class Methods

find(db, hash) click to toggle source
# 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
from_block_rlp(rlp_data) click to toggle source
# 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
new(options={}) click to toggle source
Calls superclass method
# 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

==(other) click to toggle source

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
Also aliased as: eql?
_state_root() click to toggle source
# File lib/ethereum/block_header.rb, line 97
def _state_root
  @state_root
end
check_pow(nonce=nil) click to toggle source

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
eql?(other)
Alias for: ==
full_hash() click to toggle source
# File lib/ethereum/block_header.rb, line 125
def full_hash
  Utils.keccak256_rlp self
end
full_hash_hex() click to toggle source
# File lib/ethereum/block_header.rb, line 129
def full_hash_hex
  Utils.encode_hex full_hash
end
hash() click to toggle source
# File lib/ethereum/block_header.rb, line 187
def hash
  Utils.big_endian_to_int full_hash
end
inspect()
Alias for: to_s
mining_hash() click to toggle source
# File lib/ethereum/block_header.rb, line 133
def mining_hash
  Utils.keccak256 RLP.encode(self, sedes: self.class.exclude(%i(mixhash nonce)))
end
receipts_root() click to toggle source
# File lib/ethereum/block_header.rb, line 117
def receipts_root
  get_with_block :receipts_root
end
receipts_root=(v) click to toggle source
# File lib/ethereum/block_header.rb, line 121
def receipts_root=(v)
  set_with_block :receipts_root, v
end
state_root() click to toggle source
# File lib/ethereum/block_header.rb, line 101
def state_root
  get_with_block :state_root
end
state_root=(v) click to toggle source
# File lib/ethereum/block_header.rb, line 105
def state_root=(v)
  set_with_block :state_root, v
end
to_h() click to toggle source

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
to_s() click to toggle source
# File lib/ethereum/block_header.rb, line 174
def to_s
  "#<#{self.class.name}:#{object_id} ##{number} #{full_hash_hex[0,8]}>"
end
Also aliased as: inspect
tx_list_root() click to toggle source
# File lib/ethereum/block_header.rb, line 109
def tx_list_root
  get_with_block :tx_list_root
end
tx_list_root=(v) click to toggle source
# File lib/ethereum/block_header.rb, line 113
def tx_list_root=(v)
  set_with_block :tx_list_root, v
end

Private Instance Methods

get_with_block(attr) click to toggle source
# File lib/ethereum/block_header.rb, line 197
def get_with_block(attr)
  block ? block.send(attr) : instance_variable_get(:"@#{attr}")
end
logger() click to toggle source
# File lib/ethereum/block_header.rb, line 193
def logger
  @logger ||= Logger.new 'eth.block.header'
end
set_with_block(attr, v) click to toggle source
# 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