/ EIPS / eip-210.md
eip-210.md
 1  ---
 2  eip: 210
 3  title: Blockhash refactoring
 4  author: Vitalik Buterin (@vbuterin)
 5  type: Standards Track
 6  category: Core
 7  status: Draft
 8  created: 2017-02-10
 9  ---
10  
11  ### Summary
12  
13  Stores blockhashes in the state, reducing the protocol complexity and the need for client implementation complexity in order to process the BLOCKHASH opcode. Also extends the range of how far back blockhash checking can go, with the side effect of creating direct links between blocks with very distant block numbers, facilitating much more efficient initial light client syncing.
14  
15  ### Parameters
16  
17  * `CONSTANTINOPLE_FORK_BLKNUM`: TBD
18  * `SUPER_USER`: 2**160 - 2
19  * `BLOCKHASH_CONTRACT_ADDR`: 0xf0 (ie. 240)
20  * `BLOCKHASH_CONTRACT_CODE`: see below
21  
22  ### Specification
23  
24  If `block.number == CONSTANTINOPLE_FORK_BLKNUM`, then when processing the block, before processing any transactions set the code of BLOCKHASH_CONTRACT_ADDR to BLOCKHASH_CONTRACT_CODE.
25  
26  If `block.number >= CONSTANTINOPLE_FORK_BLKNUM`, then when processing a block, before processing any transactions execute a call with the parameters:
27  
28  * `SENDER`: SUPER_USER
29  * `GAS`: 1000000
30  * `TO`: BLOCKHASH_CONTRACT_ADDR
31  * `VALUE`: 0
32  * `DATA`: <32 bytes corresponding to the block's prevhash>
33  
34  If `block.number >= CONSTANTINOPLE_FORK_BLKNUM + 256`, then the BLOCKHASH opcode instead returns the result of executing a call (NOT a transaction) with the parameters:
35  
36  * `SENDER`: <account from which the opcode was called>
37  * `GAS`: 1000000
38  * `TO`: BLOCKHASH_CONTRACT_ADDR
39  * `VALUE`: 0
40  * `DATA`: 32 byte zero-byte-leftpadded integer representing the stack argument with which the opcode was called
41  
42  Also, for blocks where `block.number >= CONSTANTINOPLE_FORK_BLKNUM`, the gas cost is increased from 20 to 800 to reflect the higher costs of processing the algorithm in the contract code.
43  
44  ### BLOCKHASH_CONTRACT_CODE
45  
46  The Serpent source code is:
47  
48  ```python
49  with offset = 0:
50      if msg.sender == 0xfffffffffffffffffffffffffffffffffffffffe:
51          with bn = block.number - 1:
52              while bn:
53                  ~sstore(offset + ~mod(bn, 256), ~calldataload(0))
54                  if ~mod(bn, 256):
55                      ~stop()
56                  bn = ~div(bn, 256)
57                  offset += 256
58      elif ~calldataload(0) >= 0 and ~calldataload(0) < block.number:
59          with tbn = ~calldataload(0):
60              with dist_minus_one = block.number - tbn - 1:
61                  while dist_minus_one >= 256 && ~mod(tbn, 256) == 0:
62                      offset += 256
63                      tbn = ~div(tbn, 256) 
64                      dist_minus_one = ~div(dist_minus_one, 256)
65                  if dist_minus_one >= 256:
66                      return(0)
67                  return(~sload(offset + ~mod(tbn, 256)))
68      else:
69          return(0)
70  ```
71  
72  The EVM init code is:
73  
74  ```
75  0x6100f58061000e60003961010356600073fffffffffffffffffffffffffffffffffffffffe33141561005857600143035b801561005257600035610100820683015561010081061561003f57005b6101008104905061010082019150610022565b506100f3565b600060003512151561006e574360003512610071565b60005b156100e7576000356001814303035b6101008112151561009857600061010083061461009b565b60005b156100ba57610100830192506101008204915061010081049050610080565b610100811215156100d057600060a052602060a0f35b610100820683015460c052602060c0f350506100f2565b600060e052602060e0f35b5b505b6000f3
76  ```
77  
78  The EVM bytecode that the contract code should be set to is:
79  
80  ```
81  0x600073fffffffffffffffffffffffffffffffffffffffe33141561005857600143035b801561005257600035610100820683015561010081061561003f57005b6101008104905061010082019150610022565b506100f3565b600060003512151561006e574360003512610071565b60005b156100e7576000356001814303035b6101008112151561009857600061010083061461009b565b60005b156100ba57610100830192506101008204915061010081049050610080565b610100811215156100d057600060a052602060a0f35b610100820683015460c052602060c0f350506100f2565b600060e052602060e0f35b5b50
82  ```
83  
84  ### Rationale
85  
86  This removes the need for implementations to have an explicit way to look into historical block hashes, simplifying the protocol definition and removing a large component of the "implied state" (information that is technically state but is not part of the state tree) and thereby making the protocol more "pure". Additionally, it allows blocks to directly point to blocks far behind them, which enables extremely efficient and secure light client protocols.