near / near-workspaces-js

Write tests once, run them both on NEAR TestNet and a controlled NEAR Sandbox local environment
https://near.github.io/near-workspaces-js/
GNU General Public License v3.0
42 stars 22 forks source link

[enhancement] Tx signing doesn't support concurrent testing #172

Open mohamedalichelbi opened 2 years ago

mohamedalichelbi commented 2 years ago

Problem description: I have a contract with many owner-only methods, so I made a test with workspaces-js 2.0 that calls all those methods at the same time. Something like:

await Promise.all([
 alice.call( "my-contract", "method_1", {} ),
 alice.call( "my-contract", "method_2", {} ),
 alice.call( "my-contract", "method_3", {} ),
 ]);

However with many calls, the tests would take an unusually long time: 12 calls took ~1.5 minutes to run, and going over 14 calls will fail the test.

Problem cause: Every time we run account.call(), the RPC is queried for the user's nonce in order to sign the transaction. Since we're running all 3 calls concurrently, the RPC would return the same nonce for all transactions, which means all transactions will be signed with the same nonce. The first signed transaction to arrive at the the RPC will be accepted, the other 2 will be rejected. Since the library has up to 10 automatic retries, it will retry to send the failed 2 transactions and the same will happen again. The first of the two will be accepted, the other one will be rejected. As you can see, this means there will be a lot of delay until all transactions get eventually accepted. Things get worse when trying to do this for a lot of transactions (>14 in my experience) since the library will run out of the 10 automatic retries and simply fail the test.

Possible solution: I think there should be a method that allows to sign & send multiple transactions from the same user. I remember seeing something similar last year in near-api-js but I couldn't find it anymore, so I made this method for my own tests and it's running fine. See this test for an example usage.