Skip to content

Commit

Permalink
feat: limit collisions per address
Browse files Browse the repository at this point in the history
  • Loading branch information
Otto-AA committed Jul 18, 2024
1 parent 3fbc5f7 commit 2c0328a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
43 changes: 18 additions & 25 deletions tests/integration/snapshots/snap_test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
"types": ["balance"],
}

snapshots["test_tod_attack_miner_e2e num_candidates"] = 369
snapshots["test_tod_attack_miner_e2e num_candidates"] = 161

snapshots["test_tod_attack_miner_e2e stats"] = {
"accesses": {"balance": 4663, "code": 2248, "nonce": 4332, "storage": 8239},
"candidates": 369,
"candidates": 161,
"candidates_filters": {
"candidates": {
"before_filters": 2195,
"final": 369,
"final": 161,
"original_without_same_value": 264234,
},
"filtered": {
Expand All @@ -31,27 +31,20 @@
"codes": 0,
"indirect_dependencies_quick": 47,
"indirect_dependencies_recursive": 38,
"limited_collisions_per_address": 208,
"nonces": 658,
"recipient_eth_transfer": 61,
"same_sender": 174,
},
},
"candidates_transactions_unique": 399,
"candidates_transactions_unique": 238,
"collision_addresses_unique": 75,
"collisions": {"balance": 229, "storage": 385},
"collisions": {"balance": 69, "storage": 209},
"collisions_before_filters": {"balance": 1526, "nonce": 879, "storage": 710},
"frequencies": {
"candidates_transactions": [(5, 2), (4, 5), (3, 30), (2, 256), (1, 106)],
"candidates_transactions": [(3, 1), (2, 82), (1, 155)],
"collisions_addresses": [
(184, 1),
(90, 1),
(30, 1),
(27, 1),
(18, 1),
(16, 5),
(14, 1),
(12, 1),
(11, 1),
(10, 13),
(9, 1),
(6, 2),
(5, 5),
Expand All @@ -63,18 +56,18 @@
},
"samples": {
"candidates_transactions_frequent": [
("0xe91cfe5d2cd6d2f4fc353ed11b595f2ec2f32d01933dbdccc2f2bf0616bfdcbc", 5),
("0x6e97688f22ccd10ca137f292f806632fe08abb0dff3743f769ba517e6255ffc7", 5),
("0x6a6441644f1511f988446bd3bc0950677594b6830189fe4bb8e57c0eb90c45f7", 4),
("0x618f4b0392ac8ee3cb3c447c6dbe1ec4b472e8e49d302a393fd23e6d36164ec6", 4),
("0x1574e1c50915720812699177d7adbfc4ceb0aacf22dd0f259496ad6a8563d2cb", 4),
("0x618f4b0392ac8ee3cb3c447c6dbe1ec4b472e8e49d302a393fd23e6d36164ec6", 3),
("0x767e0cc6d695c96fb2b7be8a28f075c5a0729cdd646e3892230a6138a33925df", 2),
("0x4fa5690f54408827f28253342d0bf8c616b9377a9917ab7706be23c79079df9a", 2),
("0x7d98a6b0fa1b22bdd88bbaed71178105c121010dae17a6d96084e7576c055316", 2),
("0x367b13927c05d804d67b2809daade8baba114e6d5a0f2d95cf7318ffdb656f97", 2),
],
"collision_addresses_frequent": [
("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", 184),
("0xdac17f958d2ee523a2206206994597c13d831ec7", 90),
("0x3328f7f4a1d1c57c35df56bbf0c9dcafca309c49", 30),
("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", 27),
("0x8390a1da07e376ef7add4be859ba74fb83aa02d5", 18),
("0x0d7e906bd9cafa154b048cfa766cc1e54e39af9b", 10),
("0x0ec68c5b10f21effb74f2a5c61dfe6b08c0db6cb", 10),
("0x3328f7f4a1d1c57c35df56bbf0c9dcafca309c49", 10),
("0x6774bcbd5cecef1336b5300fb5186a12ddd8b367", 10),
("0x7777777f279eba3d3ad8f4e708545291a6fdba8b", 10),
],
},
"state_diffs": {"balance": 2577, "code": 3, "nonce": 880, "storage": 2594},
Expand Down
1 change: 1 addition & 0 deletions tod_attack_miner/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ def main():
miner.find_collisions()
miner.filter_candidates(args.window_size)
print(f"Found {miner.count_candidates()} candidates")
print(json.dumps(miner.get_stats()))
19 changes: 19 additions & 0 deletions tod_attack_miner/db/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,22 @@ def filter_indirect_dependencies_recursive(db: DB):
db._con.commit()
db.remove_collisions_without_candidate()
return deleted


def limit_collisions_per_address(db: DB, limit=10):
sql = f"""
DELETE FROM collisions c
USING (
SELECT *, ROW_NUMBER() OVER (PARTITION BY SUBSTR(key, 1, 42) ORDER BY RANDOM()) AS n
FROM collisions
) grouped
WHERE c.tx_write_hash = grouped.tx_write_hash
AND c.tx_access_hash = grouped.tx_access_hash
AND c.type = grouped.type
AND c.key = grouped.key
AND n > {limit}
"""
with db._con.cursor() as cursor:
cursor.execute("SELECT setseed(0)")
cursor.execute(sql) # type: ignore
return db.remove_candidates_without_collision()
4 changes: 4 additions & 0 deletions tod_attack_miner/miner/miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
filter_same_sender,
filter_second_tx_ether_transfer,
filter_block_producers,
limit_collisions_per_address,
)
from tod_attack_miner.fetcher.fetcher import BlockRange, fetch_block_range
from tod_attack_miner.rpc.rpc import RPC
Expand Down Expand Up @@ -49,6 +50,9 @@ def filter_candidates(self, window_size: int | None) -> None:
self._filter_stats["filtered"]["recipient_eth_transfer"] = (
filter_second_tx_ether_transfer(self.db)
)
self._filter_stats["filtered"]["limited_collisions_per_address"] = (
limit_collisions_per_address(self.db)
)
self._filter_stats["candidates"]["final"] = self.db.count_candidates()

def count_candidates(self) -> int:
Expand Down

0 comments on commit 2c0328a

Please sign in to comment.