@@ -80,3 +80,61 @@ def test_blocked_address(cronos_mempool):
8080 rsp = cli .transfer ("signer1" , cli .address ("validator" ), "1basecro" )
8181 assert rsp ["code" ] != 0
8282 assert "signer is blocked" in rsp ["raw_log" ]
83+
84+
85+ @pytest .mark .flaky (max_runs = 3 )
86+ def test_mempool_nonce (cronos_mempool ):
87+ """
88+ test the nonce logic in check-tx after new block is created.
89+
90+ we'll insert several transactions into mempool with increasing nonces,
91+ the tx body is so large that they won't be included in next block at the same time,
92+ then we'll try to send a new tx with local nonce to see if it still get accepted
93+ even if check-tx state get reset.
94+
95+ the expected behavior is when mempool.recheck=true, this test should pass, because
96+ although check-tx state get reset when new blocks generated, but recheck logic will
97+ bring it back in sync with pending txs, so the client can keep sending new
98+ transactions with local nonce.
99+ """
100+ w3 : Web3 = cronos_mempool .w3
101+ cli = cronos_mempool .cosmos_cli (0 )
102+ wait_for_new_blocks (cli , 1 , sleep = 0.1 )
103+ sender = ADDRS ["validator" ]
104+ orig_nonce = w3 .eth .get_transaction_count (sender )
105+ height = w3 .eth .get_block_number ()
106+ local_nonce = orig_nonce
107+ tx_bytes = 1000000 # can only include one tx at a time
108+
109+ def send_with_nonce (nonce ):
110+ tx = {
111+ "to" : ADDRS ["community" ],
112+ "value" : 1 ,
113+ "gas" : 4121000 ,
114+ "data" : "0x" + "00" * tx_bytes ,
115+ "nonce" : nonce ,
116+ }
117+ signed = sign_transaction (w3 , tx , KEYS ["validator" ])
118+ txhash = w3 .eth .send_raw_transaction (signed .rawTransaction )
119+ return txhash
120+
121+ for i in range (3 ):
122+ txhash = send_with_nonce (local_nonce )
123+ print (f"txhash: { txhash .hex ()} " )
124+ local_nonce += 1
125+
126+ new_height = wait_for_new_blocks (cli , 1 , sleep = 0.1 )
127+ assert orig_nonce + (new_height - height ) == w3 .eth .get_transaction_count (sender )
128+ assert orig_nonce + 3 == local_nonce
129+
130+ for i in range (3 ):
131+ # send a new tx with the next nonce
132+ txhash = send_with_nonce (local_nonce )
133+ print (f"txhash: { txhash .hex ()} " )
134+ local_nonce += 1
135+
136+ new_height = wait_for_new_blocks (cli , 1 , sleep = 0.1 )
137+ assert orig_nonce + (new_height - height ) == w3 .eth .get_transaction_count (
138+ sender
139+ )
140+ assert orig_nonce + 4 + i == local_nonce
0 commit comments