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

Commit bab5916

Browse files
committed
fix: test_chain
1 parent 2ffdf64 commit bab5916

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

ethereum/miner.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,26 @@ def mine(self, steps=1000):
8989
BE(X) evaluates to the value equal to X when interpreted as a
9090
big-endian-encoded integer.
9191
"""
92-
b = self.block
93-
sz = blocks.get_cache_size(b.number)
94-
cache = blocks.get_cache_memoized(b.db, b.header.seed, sz)
95-
fsz = blocks.get_full_size(b.number)
96-
nonce = utils.big_endian_to_int(b.nonce)
92+
#b = self.block
93+
sz = blocks.get_cache_size(self.block.number)
94+
cache = blocks.get_cache_memoized(self.block.db, self.block.header.seed, sz)
95+
fsz = blocks.get_full_size(self.block.number)
96+
nonce = utils.big_endian_to_int(self.block.nonce)
9797
TT64M1 = 2**64 - 1
98-
target = utils.zpad(utils.int_to_big_endian(2**256 // (b.difficulty or 1)), 32)
98+
target = utils.zpad(utils.int_to_big_endian(2**256 // (self.block.difficulty or 1)), 32)
99+
found = False
99100
for i in range(1, steps + 1):
100-
b.nonce = utils.zpad(utils.int_to_big_endian((nonce + i) & TT64M1), 8)
101-
o = blocks.hashimoto_light(fsz, cache, b.mining_hash, b.nonce)
101+
self.block.nonce = utils.zpad(utils.int_to_big_endian((nonce + i) & TT64M1), 8)
102+
o = blocks.hashimoto_light(fsz, cache, self.block.mining_hash, self.block.nonce)
102103
if o["result"] <= target:
103-
b.mixhash = o["mix digest"]
104+
self.block.mixhash = o["mix digest"]
105+
found = True
104106
break
105107
steps -= 1
106-
if b.header.check_pow():
107-
return self.block
108+
if not found:
109+
return False
108110

109-
self.nonce = nonce
110-
return False
111+
assert len(self.block.nonce) == 8
112+
assert len(self.block.mixhash) == 32
113+
assert self.block.header.check_pow()
114+
return self.block

ethereum/tests/test_chain.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,20 @@ def mine_next_block(parent, uncles=[], coinbase=None, transactions=[]):
4545
m = miner.Miner(parent, uncles, coinbase or parent.coinbase)
4646
for t in transactions:
4747
m.add_transaction(t)
48-
b = m.mine()
48+
while True:
49+
b = m.mine()
50+
if b:
51+
break
4952
assert b.header.check_pow()
5053
return b
5154

5255

56+
def test_mining():
57+
blk = mkgenesis()
58+
for i in range(2):
59+
blk = mine_next_block(blk)
60+
61+
5362
@pytest.fixture(scope="module")
5463
def get_transaction(gasprice=0, nonce=0):
5564
k, v, k2, v2 = accounts()

0 commit comments

Comments
 (0)