Skip to content

Commit 7ff5c1c

Browse files
authored
fix issue with account addresses sometimes being null (#15)
1 parent 85849b9 commit 7ff5c1c

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

chainbench/test_data/evm.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55

66
class EVMTestData(BaseTestData):
77
TXS_REQUIRED = 50
8+
ACCOUNTS_REQUIRED = 100
89
SAVE_LAST_BLOCKS = 100
910

1011
@staticmethod
1112
def _parse_hex_to_int(value: str) -> int:
1213
return int(value, 16)
1314

15+
@staticmethod
16+
def _append_if_not_none(data_list, val):
17+
if val is not None:
18+
data_list.append(val)
19+
1420
def _fetch_block(
1521
self, block_number: int | str, return_txs: bool = True
1622
) -> tuple[int, dict]:
@@ -32,25 +38,25 @@ def _fetch_random_block(self, start, end, return_txs=True) -> tuple[int, dict]:
3238
# get initial data from blockchain
3339
def _get_init_data(self) -> BlockchainData:
3440
latest_block_number, latest_block = self._fetch_block("latest")
35-
txs = []
36-
tx_hashes = []
37-
accounts = []
41+
txs: list[dict] = []
42+
tx_hashes: list[str] = []
43+
accounts: list[str] = []
3844
blocks = [(latest_block_number, latest_block["hash"])]
3945
# extract data from block
4046
for tx in latest_block["transactions"]:
41-
txs.append(tx)
42-
tx_hashes.append(tx["hash"])
43-
accounts.append(tx["from"])
44-
accounts.append(tx["to"])
47+
self._append_if_not_none(txs, tx)
48+
self._append_if_not_none(tx_hashes, tx["hash"])
49+
self._append_if_not_none(accounts, tx["from"])
50+
self._append_if_not_none(accounts, tx["to"])
4551

46-
while self.TXS_REQUIRED > len(txs):
52+
while self.TXS_REQUIRED > len(txs) and self.ACCOUNTS_REQUIRED > len(accounts):
4753
block_number, block = self._fetch_random_block(0, latest_block_number)
4854
blocks.append((block_number, block["hash"]))
4955
for tx in block["transactions"]:
50-
txs.append(tx)
51-
tx_hashes.append(tx["hash"])
52-
accounts.append(tx["from"])
53-
accounts.append(tx["to"])
56+
self._append_if_not_none(txs, tx)
57+
self._append_if_not_none(tx_hashes, tx["hash"])
58+
self._append_if_not_none(accounts, tx["from"])
59+
self._append_if_not_none(accounts, tx["to"])
5460

5561
return BlockchainData(
5662
latest_block_number=latest_block_number,

0 commit comments

Comments
 (0)