Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 2209f33

Browse files
author
Jan Xie
committed
abstract check_pow to header_check in consensus strategy
1 parent a608b2b commit 2209f33

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

ethereum/block.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from ethereum.trie import Trie
88
from ethereum.securetrie import SecureTrie
99
from config import default_config
10-
from ethereum.ethpow import check_pow
1110
from ethereum.transactions import Transaction
1211
from db import BaseDB
1312
import sys
@@ -107,18 +106,6 @@ def mining_hash(self):
107106
def signing_hash(self):
108107
return utils.sha3(rlp.encode(self, BlockHeader.exclude(['extra_data'])))
109108

110-
def check_pow(self, nonce=None):
111-
"""Check if the proof-of-work of the block is valid.
112-
113-
:param nonce: if given the proof of work function will be evaluated
114-
with this nonce instead of the one already present in
115-
the header
116-
:returns: `True` or `False`
117-
"""
118-
# log.debug('checking pow', block=encode_hex(self.hash())[:8])
119-
return check_pow(self.number, self.mining_hash, self.mixhash, nonce or self.nonce,
120-
self.difficulty)
121-
122109
def to_dict(self):
123110
"""Serialize the header to a readable dictionary."""
124111
d = {}

ethereum/consensus_strategy.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class ConsensusStrategy(object):
2-
def __init__(self, header_validate, uncle_validate, block_setup, block_pre_finalize, block_post_finalize, state_initialize):
2+
def __init__(self, header_check, header_validate, uncle_validate, block_setup, block_pre_finalize, block_post_finalize, state_initialize):
3+
self.header_check=header_check
34
self.header_validate=header_validate
45
self.uncle_validate=uncle_validate
56
self.block_setup=block_setup
@@ -9,8 +10,11 @@ def __init__(self, header_validate, uncle_validate, block_setup, block_pre_final
910

1011
def get_consensus_strategy(config):
1112
if config['CONSENSUS_STRATEGY'] in ('pow', 'ethpow', 'ethash', 'ethereum1'):
12-
from ethpow_utils import ethereum1_validate_header, ethereum1_validate_uncle, ethereum1_pre_finalize_block, ethereum1_post_finalize_block, ethereum1_setup_block
13+
from ethpow_utils import ethereum1_check_header, ethereum1_validate_header, \
14+
ethereum1_validate_uncle, ethereum1_pre_finalize_block, \
15+
ethereum1_post_finalize_block, ethereum1_setup_block
1316
return ConsensusStrategy(
17+
header_check=ethereum1_check_header,
1418
header_validate=ethereum1_validate_header,
1519
uncle_validate=ethereum1_validate_uncle,
1620
block_setup=ethereum1_setup_block,
@@ -21,6 +25,7 @@ def get_consensus_strategy(config):
2125
elif config['CONSENSUS_STRATEGY'] == 'casper':
2226
from casper_utils import casper_validate_header, casper_state_initialize, casper_post_finalize_block, casper_setup_block
2327
return ConsensusStrategy(
28+
header_check=None,
2429
header_validate=casper_validate_header,
2530
uncle_validate=None,
2631
block_setup=casper_setup_block,

ethereum/ethpow_utils.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
from ethereum import ethash, ethash_utils, utils
2-
from ethereum.block import Block, BlockHeader
31
import time
42
import sys
5-
from ethereum.utils import sha3
63
import warnings
4+
import rlp
75
from collections import OrderedDict
6+
from ethereum import ethash, ethash_utils, utils
7+
from ethereum.block import Block, BlockHeader
8+
from ethereum.utils import sha3
89
from ethereum.slogging import get_logger
9-
import rlp
10-
from ethereum.state_transition import calc_difficulty, check_gaslimit, \
11-
initialize
10+
from ethereum.ethpow import check_pow
11+
from ethereum.state_transition import calc_difficulty, check_gaslimit, initialize, \
12+
check_block_header
1213
from ethereum.config import default_config
1314
from ethereum.exceptions import VerificationFailed
1415

@@ -62,8 +63,20 @@ def ethereum1_setup_block(chain, state=None, timestamp=None, coinbase='\x35'*20,
6263
return blk
6364

6465

66+
def ethereum1_check_header(header, nonce=None):
67+
"""Check if the proof-of-work of the block is valid.
68+
69+
:param nonce: if given the proof of work function will be evaluated
70+
with this nonce instead of the one already present in
71+
the header
72+
:returns: `True` or `False`
73+
"""
74+
# log.debug('checking pow', block=encode_hex(self.hash())[:8])
75+
return check_pow(header.number, header.mining_hash, header.mixhash,
76+
nonce or header.nonce, header.difficulty)
77+
6578
def ethereum1_validate_header(state, header):
66-
assert header.check_pow()
79+
assert check_block_header(state, header)
6780
parent = state.prev_headers[0]
6881
if parent:
6982
if header.prevhash != parent.hash:
@@ -91,7 +104,7 @@ def ethereum1_validate_header(state, header):
91104
return True
92105

93106
def ethereum1_validate_uncle(state, uncle):
94-
if not uncle.check_pow():
107+
if not check_block_header(state, uncle):
95108
raise VerificationFailed('pow mismatch')
96109
return True
97110

ethereum/state_transition.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,19 @@ def mk_receipt_sha(receipts):
295295
mk_transaction_sha = mk_receipt_sha
296296

297297

298+
def check_block_header(state, header, **kwargs):
299+
""" Check header's internal validity """
300+
cs = get_consensus_strategy(state.config)
301+
if cs.header_check:
302+
return cs.header_check(header, **kwargs)
303+
return True
304+
305+
298306
def validate_block_header(state, header):
307+
""" Check header's validity in block context """
299308
cs = get_consensus_strategy(state.config)
300-
cs.header_validate(state, header)
309+
if cs.header_validate:
310+
cs.header_validate(state, header)
301311
return True
302312

303313

0 commit comments

Comments
 (0)