Skip to content

Commit

Permalink
fix nonce tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kristinaNikolaevaa committed Mar 7, 2024
1 parent 743f238 commit 9b87b1e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
7 changes: 2 additions & 5 deletions integration/tests/basic/evm/test_precompiled_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,8 @@ def test_call_via_send_trx(
amount = random.choice([0, 10])
balance_before = self.web3_client.get_balance(address)

instruction_tx = self.web3_client.make_raw_tx(sender_account, amount=amount, estimate_gas=True)
instruction_tx["data"] = input_data
instruction_tx["chainId"] = self.web3_client.eth.chain_id
instruction_tx["to"] = address
instruction_tx["from"] = sender_account.address
instruction_tx = self.web3_client.make_raw_tx(sender_account, address, data=input_data,
amount=amount, estimate_gas=True)
if request.node.callspec.id not in [
"modexp-nagydani-5-square0",
"modexp-nagydani-5-square1",
Expand Down
55 changes: 40 additions & 15 deletions integration/tests/basic/test_nonce.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ def test_get_receipt_sequence(self):

def test_reverse_sequence(self):
sender_account = self.accounts[0]
recipient_account = self.accounts[1]
nonce = self.web3_client.get_nonce(sender_account.address)
nonce_list = [i for i in range(nonce + self.TRANSFER_CNT - 1, nonce - 1, -1)]

tx_hash_list = []
for nonce in nonce_list:
transaction = self.web3_client.make_raw_tx(sender_account, nonce=nonce, estimate_gas=True)
transaction = self.web3_client.make_raw_tx(
sender_account, recipient_account, nonce=nonce, estimate_gas=True
)
signed_tx = self.web3_client.eth.account.sign_transaction(transaction, sender_account.key)
tx = self.web3_client.eth.send_raw_transaction(signed_tx.rawTransaction)
tx_hash_list.append(tx.hex())
Expand All @@ -50,12 +53,16 @@ def test_reverse_sequence(self):

def test_random_sequence(self):
sender_account = self.accounts[0]
recipient_account = self.accounts[1]
nonce = self.web3_client.get_nonce(sender_account.address)
nonce_list = [i for i in range(nonce, nonce + self.TRANSFER_CNT)]
random.shuffle(nonce_list)
tx_hash_list = []
for nonce in nonce_list:
transaction = self.web3_client.make_raw_tx(sender_account, nonce=nonce, estimate_gas=True)
transaction = self.web3_client.make_raw_tx(
sender_account, recipient_account, nonce=nonce, estimate_gas=True
)
print(transaction)
signed_tx = self.web3_client.eth.account.sign_transaction(transaction, sender_account.key)
tx = self.web3_client.eth.send_raw_transaction(signed_tx.rawTransaction)
tx_hash_list.append(tx.hex())
Expand All @@ -65,18 +72,19 @@ def test_random_sequence(self):
def test_send_transaction_with_low_nonce_after_several_high(self, json_rpc_client):
"""Check that transaction with a higher nonce is waiting for its turn in the mempool"""
sender_account = self.accounts[0]
recipient_account = self.accounts[1]
nonce = self.web3_client.eth.get_transaction_count(sender_account.address)
trx = {}
for n in [nonce + 3, nonce + 1, nonce]:
transaction = self.web3_client.make_raw_tx(sender_account, nonce=n, estimate_gas=True)
transaction = self.web3_client.make_raw_tx(sender_account, recipient_account, nonce=n, estimate_gas=True)
signed_tx = self.web3_client.eth.account.sign_transaction(transaction, sender_account.key)
response_trx = json_rpc_client.send_rpc("eth_sendRawTransaction", [signed_tx.rawTransaction.hex()])
trx[n] = response_trx

receipt_trx1 = json_rpc_client.send_rpc(method="eth_getTransactionReceipt", params=[trx[n + 3]["result"]])
assert receipt_trx1["result"] is None, "Transaction shouldn't be accepted"

transaction = self.web3_client.make_raw_tx(sender_account, nonce=n + 2, estimate_gas=True)
transaction = self.web3_client.make_raw_tx(sender_account, recipient_account, nonce=n + 2, estimate_gas=True)
signed_tx = self.web3_client.eth.account.sign_transaction(transaction, sender_account.key)
json_rpc_client.send_rpc("eth_sendRawTransaction", [signed_tx.rawTransaction.hex()])

Expand All @@ -86,31 +94,37 @@ def test_send_transaction_with_low_nonce_after_several_high(self, json_rpc_clien
def test_send_transaction_with_low_nonce_after_high(self, json_rpc_client):
"""Check that transaction with a higher nonce is waiting for its turn in the mempool"""
sender_account = self.accounts[0]
recipient_account = self.accounts[1]
nonce = self.web3_client.eth.get_transaction_count(sender_account.address) + 1
transaction = self.web3_client.make_raw_tx(sender_account, nonce=nonce, estimate_gas=True)
transaction = self.web3_client.make_raw_tx(sender_account, recipient_account, nonce=nonce, estimate_gas=True)
signed_tx = self.web3_client.eth.account.sign_transaction(transaction, sender_account.key)
response_trx1 = json_rpc_client.send_rpc("eth_sendRawTransaction", [signed_tx.rawTransaction.hex()])

time.sleep(10) # transaction with n+1 nonce should wait when transaction with nonce = n will be accepted
receipt_trx1 = json_rpc_client.send_rpc(method="eth_getTransactionReceipt", params=[response_trx1["result"]])
assert receipt_trx1["result"] is None, "Transaction shouldn't be accepted"

transaction = self.web3_client.make_raw_tx(sender_account, nonce=nonce - 1, estimate_gas=True)
transaction = self.web3_client.make_raw_tx(
sender_account, recipient_account, nonce=nonce - 1, estimate_gas=True
)
signed_tx = self.web3_client.eth.account.sign_transaction(transaction, sender_account.key)
response_trx2 = json_rpc_client.send_rpc("eth_sendRawTransaction", [signed_tx.rawTransaction.hex()])
for result in (response_trx2["result"], response_trx1["result"]):
self.web3_client.wait_for_transaction_receipt(result)
assert rpc_checks.is_hex(result)

@pytest.mark.only_stands # This doesn't work on devnet because sticky session is not enabled
@pytest.mark.only_stands # This doesn't work on devnet because sticky session is not enabled
def test_send_transaction_with_the_same_nonce_and_lower_gas(self, json_rpc_client, new_account):
"""Check that transaction with a low gas and the same nonce can't be sent"""
sender_account = self.accounts[3]
recipient_account = self.accounts[4]
nonce = self.web3_client.eth.get_transaction_count(sender_account.address) + 1
gas = self.web3_client.gas_price()
tx1 = self.web3_client.make_raw_tx(sender_account, nonce=nonce, gas_price=gas, estimate_gas=True)
tx1 = self.web3_client.make_raw_tx(
sender_account, recipient_account, nonce=nonce, gas_price=gas, estimate_gas=True
)
signed_tx1 = self.web3_client.eth.account.sign_transaction(tx1, sender_account.key)
tx2 = self.web3_client.make_raw_tx(sender_account, nonce=nonce, gas_price=gas - 1000, estimate_gas=True)
tx2 = self.web3_client.make_raw_tx(sender_account, recipient_account, nonce=nonce, gas_price=gas - 1000, estimate_gas=True)
signed_tx2 = self.web3_client.eth.account.sign_transaction(tx2, sender_account.key)
resp1 = json_rpc_client.send_rpc("eth_sendRawTransaction", [signed_tx1.rawTransaction.hex()])
response = json_rpc_client.send_rpc("eth_sendRawTransaction", [signed_tx2.rawTransaction.hex()])
Expand All @@ -122,12 +136,18 @@ def test_send_transaction_with_the_same_nonce_and_lower_gas(self, json_rpc_clien
def test_send_transaction_with_the_same_nonce_and_higher_gas(self, json_rpc_client):
"""Check that transaction with higher gas and the same nonce can be sent"""
sender_account = self.accounts[2]
recipient_account = self.accounts[3]
nonce = self.web3_client.eth.get_transaction_count(sender_account.address) + 1
gas = self.web3_client.gas_price()
transaction = self.web3_client.make_raw_tx(sender_account, nonce=nonce, gas_price=gas, estimate_gas=True)
transaction = self.web3_client.make_raw_tx(
sender_account, recipient_account, nonce=nonce, gas_price=gas, estimate_gas=True
)
signed_tx = self.web3_client.eth.account.sign_transaction(transaction, sender_account.key)
json_rpc_client.send_rpc("eth_sendRawTransaction", [signed_tx.rawTransaction.hex()])
transaction = self.web3_client.make_raw_tx(sender_account, nonce=nonce, gas_price=gas * 10, estimate_gas=True)
transaction = self.web3_client.make_raw_tx(
sender_account, recipient_account, nonce=nonce, gas_price=gas * 10, estimate_gas=True
)

signed_tx = self.web3_client.eth.account.sign_transaction(transaction, sender_account.key)
response = json_rpc_client.send_rpc("eth_sendRawTransaction", [signed_tx.rawTransaction.hex()])
assert "error" not in response
Expand All @@ -136,7 +156,8 @@ def test_send_transaction_with_the_same_nonce_and_higher_gas(self, json_rpc_clie
def test_send_the_same_transactions_if_accepted(self, json_rpc_client):
"""Transaction cannot be sent again if it was accepted"""
sender_account = self.accounts[0]
transaction = self.web3_client.make_raw_tx(sender_account, estimate_gas=True)
recipient_account = self.accounts[1]
transaction = self.web3_client.make_raw_tx(sender_account, recipient_account, estimate_gas=True)
signed_tx = self.web3_client.eth.account.sign_transaction(transaction, sender_account.key)
params = [signed_tx.rawTransaction.hex()]
response = json_rpc_client.send_rpc("eth_sendRawTransaction", params)
Expand All @@ -151,7 +172,8 @@ def test_send_the_same_transactions_if_accepted(self, json_rpc_client):
def test_send_the_same_transactions_if_not_accepted(self, json_rpc_client):
"""Transaction can be sent again if it was not accepted"""
sender_account = self.accounts[0]
transaction = self.web3_client.make_raw_tx(sender_account, estimate_gas=True)
recipient_account = self.accounts[1]
transaction = self.web3_client.make_raw_tx(sender_account, recipient_account, estimate_gas=True)
signed_tx = self.web3_client.eth.account.sign_transaction(transaction, sender_account.key)
params = [signed_tx.rawTransaction.hex()]
json_rpc_client.send_rpc("eth_sendRawTransaction", params)
Expand All @@ -163,16 +185,19 @@ def test_send_the_same_transactions_if_not_accepted(self, json_rpc_client):
def test_send_transaction_with_old_nonce(self, json_rpc_client, new_account):
"""Check that transaction with old nonce can't be sent"""
sender_account = new_account
recipient_account = self.accounts[1]
nonce = self.web3_client.eth.get_transaction_count(sender_account.address)
transaction = self.web3_client.make_raw_tx(sender_account, amount=1, nonce=nonce, estimate_gas=True)
transaction = self.web3_client.make_raw_tx(
sender_account, recipient_account, amount=1, nonce=nonce, estimate_gas=True
)
signed_tx = self.web3_client.eth.account.sign_transaction(transaction, sender_account.key)
response = json_rpc_client.send_rpc("eth_sendRawTransaction", [signed_tx.rawTransaction.hex()])
assert "result" in response and response["result"], f"Response doesn't have result field: {response}"
receipt = self.web3_client.wait_for_transaction_receipt(response["result"])
block_num = receipt["blockNumber"]
wait_finalized_block(json_rpc_client, block_num)

transaction = self.web3_client.make_raw_tx(sender_account, amount=2, nonce=nonce, estimate_gas=True)
transaction = self.web3_client.make_raw_tx(sender_account, recipient_account, amount=2, nonce=nonce, estimate_gas=True)
signed_tx = self.web3_client.eth.account.sign_transaction(transaction, sender_account.key)
response = json_rpc_client.send_rpc("eth_sendRawTransaction", [signed_tx.rawTransaction.hex()])
assert ErrorMessage.NONCE_TOO_LOW.value in response["error"]["message"]
Expand Down

0 comments on commit 9b87b1e

Please sign in to comment.