Skip to content

Commit

Permalink
Merge pull request #516 from near/fix-nonce-again
Browse files Browse the repository at this point in the history
Fix nonce error handling
  • Loading branch information
vgrichina authored Feb 25, 2021
2 parents 5265f06 + f858819 commit 7ada88f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/account.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/account.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/utils/rpc_errors.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/utils/rpc_errors.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/utils/rpc_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export function getErrorTypeFromErrorMessage(errorMessage) {
return 'AccessKeyDoesNotExist';
case /wasm execution failed with error: FunctionCallError\(CompilationError\(CodeDoesNotExist/.test(errorMessage):
return 'CodeDoesNotExist';
case /Transaction nonce \d+ must be larger than nonce of the used access key \d+/.test(errorMessage):
return 'InvalidNonce';
default:
return 'UntypedError';
}
Expand Down
24 changes: 17 additions & 7 deletions test/account.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

const nearApi = require('../lib/index');
const { Account, Contract, providers } = require('../lib/index');
const testUtils = require('./test-utils');
const fs = require('fs');
const BN = require('bn.js');
Expand Down Expand Up @@ -35,7 +35,7 @@ test('create account and then view account returns the created account', async (
const { amount } = await workingAccount.state();
const newAmount = new BN(amount).div(new BN(10));
await workingAccount.createAccount(newAccountName, newAccountPublicKey, newAmount);
const newAccount = new nearApi.Account(nearjs.connection, newAccountName);
const newAccount = new Account(nearjs.connection, newAccountName);
const state = await newAccount.state();
expect(state.amount).toEqual(newAmount.toString());
});
Expand All @@ -55,10 +55,20 @@ test('delete account', async() => {
const sender = await testUtils.createAccount(nearjs);
const receiver = await testUtils.createAccount(nearjs);
await sender.deleteAccount(receiver.accountId);
const reloaded = new nearApi.Account(sender.connection, sender);
const reloaded = new Account(sender.connection, sender);
await expect(reloaded.state()).rejects.toThrow();
});

test('multiple parallel transactions', async () => {
const PARALLEL_NUMBER = 5;
await Promise.all([...Array(PARALLEL_NUMBER).keys()].map(async (_, i) => {
const account = new Account(workingAccount.connection, workingAccount.accountId);
// NOTE: Need to have different transactions outside of nonce, or they all succeed by being identical
// TODO: Check if randomization of exponential back off helps to do more transactions without exceeding retries
await account.sendMoney(account.accountId, new BN(i));
}));
});

describe('errors', () => {
let oldLog;
let logs;
Expand Down Expand Up @@ -97,7 +107,7 @@ describe('with deploy contract', () => {
const newPublicKey = await nearjs.connection.signer.createKey(contractId, testUtils.networkId);
const data = [...fs.readFileSync(HELLO_WASM_PATH)];
await workingAccount.createAndDeployContract(contractId, newPublicKey, data, HELLO_WASM_BALANCE);
contract = new nearApi.Contract(workingAccount, contractId, {
contract = new Contract(workingAccount, contractId, {
viewMethods: ['hello', 'getValue', 'returnHiWithLogs'],
changeMethods: ['setValue', 'generateLogs', 'triggerAssert', 'testSetRemove', 'crossContract']
});
Expand Down Expand Up @@ -138,7 +148,7 @@ describe('with deploy contract', () => {

const setCallValue = testUtils.generateUniqueString('setCallPrefix');
const result2 = await workingAccount.functionCall(contractId, 'setValue', { value: setCallValue });
expect(nearApi.providers.getTransactionLastResult(result2)).toEqual(setCallValue);
expect(providers.getTransactionLastResult(result2)).toEqual(setCallValue);
expect(await workingAccount.viewFunction(contractId, 'getValue', {})).toEqual(setCallValue);
});

Expand Down Expand Up @@ -219,14 +229,14 @@ describe('with deploy contract', () => {
});

test('can have view methods only', async () => {
const contract = new nearApi.Contract(workingAccount, contractId, {
const contract = new Contract(workingAccount, contractId, {
viewMethods: ['hello'],
});
expect(await contract.hello({ name: 'world' })).toEqual('hello world');
});

test('can have change methods only', async () => {
const contract = new nearApi.Contract(workingAccount, contractId, {
const contract = new Contract(workingAccount, contractId, {
changeMethods: ['hello'],
});
expect(await contract.hello({ name: 'world' })).toEqual('hello world');
Expand Down
2 changes: 2 additions & 0 deletions test/utils/rpc-errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ describe('rpc-errors', () => {
const err2 = 'Account random2.testnet doesn\'t exist';
const err3 = 'access key ed25519:DvXowCpBHKdbD2qutgfhG6jvBMaXyUh7DxrDSjkLxMHp does not exist while viewing';
const err4 = 'wasm execution failed with error: FunctionCallError(CompilationError(CodeDoesNotExist { account_id: "random.testnet" }))';
const err5 = '[-32000] Server error: Invalid transaction: Transaction nonce 1 must be larger than nonce of the used access key 1';
expect(getErrorTypeFromErrorMessage(err1)).toEqual('AccountDoesNotExist');
expect(getErrorTypeFromErrorMessage(err2)).toEqual('AccountDoesNotExist');
expect(getErrorTypeFromErrorMessage(err3)).toEqual('AccessKeyDoesNotExist');
expect(getErrorTypeFromErrorMessage(err4)).toEqual('CodeDoesNotExist');
expect(getErrorTypeFromErrorMessage(err5)).toEqual('InvalidNonce');
expect(getErrorTypeFromErrorMessage('random string')).toEqual('UntypedError');
expect(getErrorTypeFromErrorMessage(undefined)).toEqual('UntypedError');
expect(getErrorTypeFromErrorMessage('')).toEqual('UntypedError');
Expand Down

0 comments on commit 7ada88f

Please sign in to comment.