|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | +from eth_bloom import BloomFilter |
| 5 | +from eth_utils import abi, big_endian_to_int |
| 6 | +from hexbytes import HexBytes |
| 7 | +from web3.datastructures import AttributeDict |
| 8 | + |
| 9 | +from .network import setup_custom_cronos |
| 10 | +from .utils import ( |
| 11 | + ADDRS, |
| 12 | + CONTRACTS, |
| 13 | + KEYS, |
| 14 | + deploy_contract, |
| 15 | + sign_transaction, |
| 16 | + wait_for_new_blocks, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(scope="module") |
| 21 | +def cronos(request, tmp_path_factory): |
| 22 | + """start-cronos |
| 23 | + params: enable_auto_deployment |
| 24 | + """ |
| 25 | + yield from setup_custom_cronos( |
| 26 | + tmp_path_factory.mktemp("pruned"), |
| 27 | + 26900, |
| 28 | + Path(__file__).parent / "configs/pruned-node.yaml", |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def test_pruned_node(cronos): |
| 33 | + """ |
| 34 | + test basic json-rpc apis works in pruned node |
| 35 | + """ |
| 36 | + w3 = cronos.w3 |
| 37 | + erc20 = deploy_contract( |
| 38 | + w3, |
| 39 | + CONTRACTS["TestERC20A"], |
| 40 | + key=KEYS["validator"], |
| 41 | + ) |
| 42 | + tx = erc20.functions.transfer(ADDRS["community"], 10).buildTransaction( |
| 43 | + {"from": ADDRS["validator"]} |
| 44 | + ) |
| 45 | + signed = sign_transaction(w3, tx, KEYS["validator"]) |
| 46 | + txhash = w3.eth.send_raw_transaction(signed.rawTransaction) |
| 47 | + |
| 48 | + print("wait for prunning happens") |
| 49 | + wait_for_new_blocks(cronos.cosmos_cli(0), 10) |
| 50 | + |
| 51 | + txreceipt = w3.eth.wait_for_transaction_receipt(txhash) |
| 52 | + assert len(txreceipt.logs) == 1 |
| 53 | + expect_log = { |
| 54 | + "address": erc20.address, |
| 55 | + "topics": [ |
| 56 | + HexBytes( |
| 57 | + abi.event_signature_to_log_topic("Transfer(address,address,uint256)") |
| 58 | + ), |
| 59 | + HexBytes(b"\x00" * 12 + HexBytes(ADDRS["validator"])), |
| 60 | + HexBytes(b"\x00" * 12 + HexBytes(ADDRS["community"])), |
| 61 | + ], |
| 62 | + "data": "0x000000000000000000000000000000000000000000000000000000000000000a", |
| 63 | + "transactionIndex": 0, |
| 64 | + "logIndex": 0, |
| 65 | + "removed": False, |
| 66 | + } |
| 67 | + assert expect_log.items() <= txreceipt.logs[0].items() |
| 68 | + |
| 69 | + # check get_balance and eth_call don't work on pruned state |
| 70 | + with pytest.raises(Exception): |
| 71 | + w3.eth.get_balance(ADDRS["validator"], block_identifier=txreceipt.blockNumber) |
| 72 | + with pytest.raises(Exception): |
| 73 | + erc20.caller(block_identifier=txreceipt.blockNumber).balanceOf( |
| 74 | + ADDRS["validator"] |
| 75 | + ) |
| 76 | + |
| 77 | + # check block bloom |
| 78 | + block = w3.eth.get_block(txreceipt.blockNumber) |
| 79 | + assert "baseFeePerGas" in block |
| 80 | + assert block.miner == "0x0000000000000000000000000000000000000000" |
| 81 | + bloom = BloomFilter(big_endian_to_int(block.logsBloom)) |
| 82 | + assert HexBytes(erc20.address) in bloom |
| 83 | + for topic in expect_log["topics"]: |
| 84 | + assert topic in bloom |
| 85 | + |
| 86 | + tx1 = w3.eth.get_transaction(txhash) |
| 87 | + tx2 = w3.eth.get_transaction_by_block( |
| 88 | + txreceipt.blockNumber, txreceipt.transactionIndex |
| 89 | + ) |
| 90 | + exp_tx = AttributeDict( |
| 91 | + { |
| 92 | + "from": "0x57f96e6B86CdeFdB3d412547816a82E3E0EbF9D2", |
| 93 | + "gas": 51503, |
| 94 | + "input": ( |
| 95 | + "0xa9059cbb000000000000000000000000378c50d9264c63f3f92b806d4ee56e" |
| 96 | + "9d86ffb3ec000000000000000000000000000000000000000000000000000000" |
| 97 | + "000000000a" |
| 98 | + ), |
| 99 | + "nonce": 2, |
| 100 | + "to": erc20.address, |
| 101 | + "transactionIndex": 0, |
| 102 | + "value": 0, |
| 103 | + "type": "0x2", |
| 104 | + "accessList": [], |
| 105 | + "chainId": "0x309", |
| 106 | + } |
| 107 | + ) |
| 108 | + assert tx1 == tx2 |
| 109 | + for name in exp_tx.keys(): |
| 110 | + assert tx1[name] == tx2[name] == exp_tx[name] |
| 111 | + |
| 112 | + print( |
| 113 | + w3.eth.get_logs( |
| 114 | + {"fromBlock": txreceipt.blockNumber, "toBlock": txreceipt.blockNumber} |
| 115 | + ) |
| 116 | + ) |
0 commit comments