Class: Ethereum::BlockHeader

Inherits:
Object
  • Object
show all
Extended by:
Sedes
Includes:
RLP::Sedes::Serializable
Defined in:
lib/ethereum/block_header.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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.

Raises:

  • (ArgumentError)


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(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

Instance Attribute Details

#blockObject

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

Raises:



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_rootObject



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.

Parameters:

  • nonce (String) (defaults to: nil)

    if given the proof of work function will be evaluated with this nonce instead of the one already present in the header

Returns:

  • (Bool)


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_hashObject



126
127
128
# File 'lib/ethereum/block_header.rb', line 126

def full_hash
  Utils.keccak256_rlp self
end

#full_hash_hexObject



130
131
132
# File 'lib/ethereum/block_header.rb', line 130

def full_hash_hex
  Utils.encode_hex full_hash
end

#hashObject



188
189
190
# File 'lib/ethereum/block_header.rb', line 188

def hash
  Utils.big_endian_to_int full_hash
end

#mining_hashObject



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_rootObject



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_rootObject



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_hObject

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_sObject 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_rootObject



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