You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
829 lines
28 KiB
829 lines
28 KiB
4 years ago
|
import { strict as assert } from 'assert';
|
||
|
import sinon from 'sinon';
|
||
|
import BN from 'bn.js';
|
||
4 years ago
|
import { TRANSACTION_STATUSES } from '../../../../shared/constants/transaction';
|
||
|
import PendingTransactionTracker from './pending-tx-tracker';
|
||
5 years ago
|
|
||
|
describe('PendingTransactionTracker', function () {
|
||
|
describe('#resubmitPendingTxs', function () {
|
||
|
it('should return early if there are no pending transactions', async function () {
|
||
4 years ago
|
const getPendingTransactions = sinon.stub().returns([]);
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: {
|
||
|
getTransactionReceipt: sinon.stub(),
|
||
|
},
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions,
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
approveTransaction: sinon.spy(),
|
||
|
publishTransaction: sinon.spy(),
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
|
const resubmitTx = sinon.stub(pendingTxTracker, '_resubmitTx').rejects();
|
||
|
const warningListener = sinon.spy();
|
||
5 years ago
|
|
||
4 years ago
|
pendingTxTracker.on('tx:warning', warningListener);
|
||
|
await pendingTxTracker.resubmitPendingTxs('0x1');
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
getPendingTransactions.calledOnceWithExactly(),
|
||
|
'should call getPendingTransaction',
|
||
4 years ago
|
);
|
||
|
assert.ok(resubmitTx.notCalled, 'should NOT call _resubmitTx');
|
||
|
assert.ok(warningListener.notCalled, "should NOT emit 'tx:warning'");
|
||
|
});
|
||
5 years ago
|
|
||
|
it('should resubmit each pending transaction', async function () {
|
||
4 years ago
|
const getPendingTransactions = sinon.stub().returns([
|
||
|
{
|
||
|
id: 1,
|
||
|
},
|
||
|
{
|
||
|
id: 2,
|
||
|
},
|
||
4 years ago
|
]);
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: {
|
||
|
getTransactionReceipt: sinon.stub(),
|
||
|
},
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions,
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
approveTransaction: sinon.spy(),
|
||
|
publishTransaction: sinon.spy(),
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
|
const resubmitTx = sinon.stub(pendingTxTracker, '_resubmitTx').resolves();
|
||
|
const warningListener = sinon.spy();
|
||
5 years ago
|
|
||
4 years ago
|
pendingTxTracker.on('tx:warning', warningListener);
|
||
|
await pendingTxTracker.resubmitPendingTxs('0x1');
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
getPendingTransactions.calledOnceWithExactly(),
|
||
|
'should call getPendingTransaction',
|
||
4 years ago
|
);
|
||
|
assert.ok(resubmitTx.calledTwice, 'should call _resubmitTx');
|
||
|
assert.ok(warningListener.notCalled, "should NOT emit 'tx:warning'");
|
||
|
});
|
||
5 years ago
|
|
||
|
it("should NOT emit 'tx:warning' for known failed resubmission", async function () {
|
||
4 years ago
|
const getPendingTransactions = sinon.stub().returns([
|
||
|
{
|
||
|
id: 1,
|
||
|
},
|
||
4 years ago
|
]);
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: {
|
||
|
getTransactionReceipt: sinon.stub(),
|
||
|
},
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions,
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
approveTransaction: sinon.spy(),
|
||
|
publishTransaction: sinon.spy(),
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
4 years ago
|
const resubmitTx = sinon
|
||
|
.stub(pendingTxTracker, '_resubmitTx')
|
||
4 years ago
|
.rejects({ message: 'known transaction' });
|
||
|
const warningListener = sinon.spy();
|
||
5 years ago
|
|
||
4 years ago
|
pendingTxTracker.on('tx:warning', warningListener);
|
||
|
await pendingTxTracker.resubmitPendingTxs('0x1');
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
getPendingTransactions.calledOnceWithExactly(),
|
||
|
'should call getPendingTransaction',
|
||
4 years ago
|
);
|
||
|
assert.ok(resubmitTx.calledOnce, 'should call _resubmitTx');
|
||
|
assert.ok(warningListener.notCalled, "should NOT emit 'tx:warning'");
|
||
|
});
|
||
5 years ago
|
|
||
|
it("should emit 'tx:warning' for unknown failed resubmission", async function () {
|
||
4 years ago
|
const getPendingTransactions = sinon.stub().returns([
|
||
|
{
|
||
|
id: 1,
|
||
|
},
|
||
4 years ago
|
]);
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: {
|
||
|
getTransactionReceipt: sinon.stub(),
|
||
|
},
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions,
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
approveTransaction: sinon.spy(),
|
||
|
publishTransaction: sinon.spy(),
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
4 years ago
|
const resubmitTx = sinon
|
||
|
.stub(pendingTxTracker, '_resubmitTx')
|
||
4 years ago
|
.rejects({ message: 'who dis' });
|
||
|
const warningListener = sinon.spy();
|
||
5 years ago
|
|
||
4 years ago
|
pendingTxTracker.on('tx:warning', warningListener);
|
||
|
await pendingTxTracker.resubmitPendingTxs('0x1');
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
getPendingTransactions.calledOnceWithExactly(),
|
||
|
'should call getPendingTransaction',
|
||
4 years ago
|
);
|
||
|
assert.ok(resubmitTx.calledOnce, 'should call _resubmitTx');
|
||
|
assert.ok(warningListener.calledOnce, "should emit 'tx:warning'");
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('#updatePendingTxs', function () {
|
||
|
it('should call _checkPendingTx for each pending transaction', async function () {
|
||
|
const txMeta = {
|
||
|
id: 1,
|
||
4 years ago
|
hash:
|
||
|
'0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb',
|
||
4 years ago
|
status: TRANSACTION_STATUSES.SIGNED,
|
||
5 years ago
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x1',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
|
history: [{}],
|
||
4 years ago
|
rawTx:
|
||
|
'0xf86c808504a817c800827b0d940c62bb85faa3311a998d3aba8098c1235c564966880de0b6b3a7640000802aa08ff665feb887a25d4099e40e11f0fef93ee9608f404bd3f853dd9e84ed3317a6a02ec9d3d1d6e176d4d2593dd760e74ccac753e6a0ea0d00cc9789d0d7ff1f471d',
|
||
4 years ago
|
};
|
||
|
const txList = [1, 2, 3].map((id) => ({ ...txMeta, id }));
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: {
|
||
|
getTransactionReceipt: sinon.stub(),
|
||
|
},
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: async () => {
|
||
4 years ago
|
return { releaseLock: () => undefined };
|
||
5 years ago
|
},
|
||
|
},
|
||
|
getPendingTransactions: () => txList,
|
||
|
getCompletedTransactions: () => {
|
||
4 years ago
|
return [];
|
||
5 years ago
|
},
|
||
4 years ago
|
publishTransaction: () => undefined,
|
||
|
confirmTransaction: () => undefined,
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
const checkPendingTxStub = sinon
|
||
|
.stub(pendingTxTracker, '_checkPendingTx')
|
||
4 years ago
|
.resolves();
|
||
|
await pendingTxTracker.updatePendingTxs();
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(checkPendingTxStub.calledThrice);
|
||
4 years ago
|
assert.ok(
|
||
|
checkPendingTxStub.firstCall.calledWithExactly(
|
||
|
sinon.match.has('id', 1),
|
||
|
),
|
||
4 years ago
|
);
|
||
4 years ago
|
assert.ok(
|
||
|
checkPendingTxStub.secondCall.calledWithExactly(
|
||
|
sinon.match.has('id', 2),
|
||
|
),
|
||
4 years ago
|
);
|
||
4 years ago
|
assert.ok(
|
||
|
checkPendingTxStub.thirdCall.calledWithExactly(
|
||
|
sinon.match.has('id', 3),
|
||
|
),
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('#_resubmitTx', function () {
|
||
|
it('should publish a new transaction', async function () {
|
||
|
const txMeta = {
|
||
|
id: 1,
|
||
4 years ago
|
hash:
|
||
|
'0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb',
|
||
4 years ago
|
status: TRANSACTION_STATUSES.SIGNED,
|
||
5 years ago
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x1',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
|
history: [{}],
|
||
4 years ago
|
rawTx:
|
||
|
'0xf86c808504a817c80086a02ec9d3d1d6e176d4d2593dd760e74ccac753e6a0ea0d00cc9789d0d7ff1f471d',
|
||
4 years ago
|
};
|
||
|
const approveTransaction = sinon.spy();
|
||
|
const publishTransaction = sinon.spy();
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: {
|
||
|
getTransactionReceipt: sinon.stub(),
|
||
|
},
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions: sinon.stub().returns([]),
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
approveTransaction,
|
||
|
publishTransaction,
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
await pendingTxTracker._resubmitTx(txMeta);
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
publishTransaction.calledOnceWithExactly(txMeta.rawTx),
|
||
|
'should call publish transaction with the rawTx',
|
||
4 years ago
|
);
|
||
4 years ago
|
assert.ok(
|
||
|
approveTransaction.notCalled,
|
||
|
'should NOT try to approve transaction',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('should publish the given transaction if more than 2**retryCount blocks have passed', async function () {
|
||
|
const txMeta = {
|
||
|
id: 1,
|
||
4 years ago
|
hash:
|
||
|
'0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb',
|
||
4 years ago
|
status: TRANSACTION_STATUSES.SIGNED,
|
||
5 years ago
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x1',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
|
history: [{}],
|
||
4 years ago
|
rawTx:
|
||
|
'0xf86c808504a817c800827b0d940c62bb85faa3311a996e176d4d2593dd760e74ccac753e6a0ea0d00cc9789d0d7ff1f471d',
|
||
5 years ago
|
retryCount: 4,
|
||
|
firstRetryBlockNumber: '0x1',
|
||
4 years ago
|
};
|
||
|
const approveTransaction = sinon.spy();
|
||
|
const publishTransaction = sinon.spy();
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: {
|
||
|
getTransactionReceipt: sinon.stub(),
|
||
|
},
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions: sinon.stub().returns([]),
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
publishTransaction,
|
||
|
approveTransaction,
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
await pendingTxTracker._resubmitTx(txMeta, '0x11' /* 16 */);
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
publishTransaction.calledOnceWithExactly(txMeta.rawTx),
|
||
|
'should try to publish transaction',
|
||
4 years ago
|
);
|
||
4 years ago
|
assert.ok(
|
||
|
approveTransaction.notCalled,
|
||
|
'should NOT try to approve transaction',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('should NOT publish the given transaction if fewer than 2**retryCount blocks have passed', async function () {
|
||
|
const txMeta = {
|
||
|
id: 1,
|
||
4 years ago
|
hash:
|
||
|
'0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb',
|
||
4 years ago
|
status: TRANSACTION_STATUSES.SIGNED,
|
||
5 years ago
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x1',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
|
history: [{}],
|
||
4 years ago
|
rawTx:
|
||
|
'0xf86c808504a817c800827b0d940c62bb85faa3311a996e176d4d2593dd760e74ccac753e6a0ea0d00cc9789d0d7ff1f471d',
|
||
5 years ago
|
retryCount: 4,
|
||
|
firstRetryBlockNumber: '0x1',
|
||
4 years ago
|
};
|
||
|
const approveTransaction = sinon.spy();
|
||
|
const publishTransaction = sinon.spy();
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: {
|
||
|
getTransactionReceipt: sinon.stub(),
|
||
|
},
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions: sinon.stub().returns([]),
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
publishTransaction,
|
||
|
approveTransaction,
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
await pendingTxTracker._resubmitTx(txMeta, '0x5');
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
publishTransaction.notCalled,
|
||
|
'should NOT try to publish transaction',
|
||
4 years ago
|
);
|
||
4 years ago
|
assert.ok(
|
||
|
approveTransaction.notCalled,
|
||
|
'should NOT try to approve transaction',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('should call approveTransaction if the tx is not yet signed', async function () {
|
||
4 years ago
|
const approveTransaction = sinon.spy();
|
||
|
const publishTransaction = sinon.spy();
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: {
|
||
|
getTransactionReceipt: sinon.stub(),
|
||
|
},
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions: sinon.stub().returns([]),
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
approveTransaction,
|
||
|
publishTransaction,
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
await pendingTxTracker._resubmitTx({ id: 40 });
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
approveTransaction.calledOnceWithExactly(40),
|
||
|
'should call approveTransaction with the tx ID',
|
||
4 years ago
|
);
|
||
4 years ago
|
assert.ok(
|
||
|
publishTransaction.notCalled,
|
||
|
'should NOT try to publish transaction',
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('#_checkIfTxWasDropped', function () {
|
||
|
it('should return true when the given nonce is lower than the network nonce', async function () {
|
||
4 years ago
|
const nonceBN = new BN(2);
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: {
|
||
|
getTransactionReceipt: sinon.stub(),
|
||
5 years ago
|
getTransactionCount: sinon.stub().resolves(nonceBN),
|
||
5 years ago
|
},
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions: sinon.stub().returns([]),
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
publishTransaction: sinon.spy(),
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
pendingTxTracker.DROPPED_BUFFER_COUNT = 0;
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
await pendingTxTracker._checkIfTxWasDropped({
|
||
|
id: 1,
|
||
|
hash:
|
||
|
'0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb',
|
||
4 years ago
|
status: TRANSACTION_STATUSES.SUBMITTED,
|
||
4 years ago
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x1',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
|
rawTx: '0xf86c808504a817c800827b0d940c62bba0ea0d00cc9789d0d7ff1f471d',
|
||
|
}),
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('should return false when the given nonce is the network nonce', async function () {
|
||
4 years ago
|
const nonceBN = new BN(1);
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: {
|
||
|
getTransactionReceipt: sinon.stub(),
|
||
5 years ago
|
getTransactionCount: sinon.stub().resolves(nonceBN),
|
||
5 years ago
|
},
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions: sinon.stub().returns([]),
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
publishTransaction: sinon.spy(),
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
|
const dropped = await pendingTxTracker._checkIfTxWasDropped({
|
||
|
id: 1,
|
||
4 years ago
|
hash:
|
||
|
'0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb',
|
||
4 years ago
|
status: TRANSACTION_STATUSES.SUBMITTED,
|
||
5 years ago
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x1',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
4 years ago
|
rawTx:
|
||
|
'0xf86c808504a89e84ed3317a6a02ec9d3d1d6e176d4d2593dd760e74ccac753e6a0ea0d00cc9789d0d7ff1f471d',
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(!dropped, 'should be false');
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('#_checkIfNonceIsTaken', function () {
|
||
|
it('should return false if the given nonce is not taken', async function () {
|
||
4 years ago
|
const confirmedTxList = [
|
||
|
{
|
||
|
id: 1,
|
||
|
hash:
|
||
|
'0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb',
|
||
4 years ago
|
status: TRANSACTION_STATUSES.CONFIRMED,
|
||
4 years ago
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x1',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
|
rawTx:
|
||
|
'0xf86c808504a817c800827b0d940c62bb85fa3320e74ccac753e6a0ea0d00cc9789d0d7ff1f471d',
|
||
|
},
|
||
|
{
|
||
|
id: 2,
|
||
|
hash:
|
||
|
'0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb',
|
||
4 years ago
|
status: TRANSACTION_STATUSES.CONFIRMED,
|
||
4 years ago
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x2',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
|
rawTx:
|
||
|
'0xf86c808507a6a02ec9d3d1d6e176d4d2593dd760e74ccac753e6a0ea0d00cc9789d0d7ff1f471d',
|
||
5 years ago
|
},
|
||
4 years ago
|
];
|
||
|
const getCompletedTransactions = sinon.stub().returns(confirmedTxList);
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: sinon.spy(),
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions: sinon.stub().returns([]),
|
||
|
getCompletedTransactions,
|
||
|
publishTransaction: sinon.spy(),
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
|
const taken = await pendingTxTracker._checkIfNonceIsTaken({
|
||
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x3',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
getCompletedTransactions.calledOnceWithExactly(
|
||
|
'0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
),
|
||
4 years ago
|
);
|
||
|
assert.ok(!taken);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('should return true if the nonce is taken', async function () {
|
||
4 years ago
|
const confirmedTxList = [
|
||
|
{
|
||
|
id: 1,
|
||
|
hash:
|
||
|
'0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb',
|
||
4 years ago
|
status: TRANSACTION_STATUSES.CONFIRMED,
|
||
4 years ago
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x1',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
|
rawTx:
|
||
|
'0xf86c808504a817c80082ec9d3d1d6e176d4d2593dd760e74ccac753e6a0ea0d00cc9789d0d7ff1f471d',
|
||
|
},
|
||
|
{
|
||
|
id: 2,
|
||
|
hash:
|
||
|
'0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb',
|
||
4 years ago
|
status: TRANSACTION_STATUSES.CONFIRMED,
|
||
4 years ago
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x2',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
|
rawTx:
|
||
|
'0xf86c808504a817c800827b0d940c62bb760e74ccac753e6a0ea0d00cc9789d0d7ff1f471d',
|
||
5 years ago
|
},
|
||
4 years ago
|
];
|
||
|
const getCompletedTransactions = sinon.stub().returns(confirmedTxList);
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: sinon.spy(),
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions: sinon.stub().returns([]),
|
||
|
getCompletedTransactions,
|
||
|
publishTransaction: sinon.spy(),
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
|
const taken = await pendingTxTracker._checkIfNonceIsTaken({
|
||
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x2',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
getCompletedTransactions.calledOnceWithExactly(
|
||
|
'0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
),
|
||
4 years ago
|
);
|
||
|
assert.ok(taken);
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('#_checkPendingTx', function () {
|
||
5 years ago
|
it("should emit 'tx:warning' if getTransactionReceipt rejects", async function () {
|
||
|
const txMeta = {
|
||
|
id: 1,
|
||
4 years ago
|
hash:
|
||
|
'0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb',
|
||
4 years ago
|
status: TRANSACTION_STATUSES.SUBMITTED,
|
||
5 years ago
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x1',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
|
history: [{}],
|
||
|
rawTx: '0xf86c808504a817c80082471d',
|
||
4 years ago
|
};
|
||
|
const nonceBN = new BN(2);
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: {
|
||
|
getTransactionReceipt: sinon.stub().rejects(),
|
||
5 years ago
|
getTransactionCount: sinon.stub().resolves(nonceBN),
|
||
5 years ago
|
},
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions: sinon.stub().returns([]),
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
publishTransaction: sinon.spy(),
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
5 years ago
|
const listeners = {
|
||
|
confirmed: sinon.spy(),
|
||
|
dropped: sinon.spy(),
|
||
|
failed: sinon.spy(),
|
||
|
warning: sinon.spy(),
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
pendingTxTracker.once('tx:confirmed', listeners.confirmed);
|
||
|
pendingTxTracker.once('tx:dropped', listeners.dropped);
|
||
|
pendingTxTracker.once('tx:failed', listeners.failed);
|
||
|
pendingTxTracker.once('tx:warning', listeners.warning);
|
||
|
await pendingTxTracker._checkPendingTx(txMeta);
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(listeners.dropped.notCalled, "should not emit 'tx:dropped");
|
||
|
assert.ok(
|
||
|
listeners.confirmed.notCalled,
|
||
|
"should not emit 'tx:confirmed'",
|
||
|
);
|
||
|
assert.ok(listeners.failed.notCalled, "should not emit 'tx:failed'");
|
||
|
assert.ok(listeners.warning.calledOnce, "should emit 'tx:warning'");
|
||
|
});
|
||
5 years ago
|
|
||
5 years ago
|
it('should NOT emit anything if the tx is already not submitted', async function () {
|
||
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: sinon.spy(),
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions: sinon.stub().returns([]),
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
publishTransaction: sinon.spy(),
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
5 years ago
|
const listeners = {
|
||
|
confirmed: sinon.spy(),
|
||
|
dropped: sinon.spy(),
|
||
|
failed: sinon.spy(),
|
||
5 years ago
|
warning: sinon.spy(),
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
pendingTxTracker.once('tx:confirmed', listeners.confirmed);
|
||
|
pendingTxTracker.once('tx:dropped', listeners.dropped);
|
||
|
pendingTxTracker.once('tx:failed', listeners.failed);
|
||
|
pendingTxTracker.once('tx:warning', listeners.warning);
|
||
5 years ago
|
await pendingTxTracker._checkPendingTx({
|
||
4 years ago
|
status: TRANSACTION_STATUSES.CONFIRMED,
|
||
4 years ago
|
history: [{}],
|
||
|
txParams: { nonce: '0x1' },
|
||
|
id: '456',
|
||
|
value: '0x01',
|
||
|
hash: '0xbad',
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(listeners.failed.notCalled, "should not emit 'tx:failed'");
|
||
|
assert.ok(
|
||
|
listeners.confirmed.notCalled,
|
||
|
"should not emit 'tx:confirmed'",
|
||
|
);
|
||
|
assert.ok(listeners.dropped.notCalled, "should not emit 'tx:dropped'");
|
||
|
assert.ok(listeners.warning.notCalled, "should not emit 'tx:warning'");
|
||
|
});
|
||
5 years ago
|
|
||
|
it("should emit 'tx:failed' if the txMeta does NOT have a hash", async function () {
|
||
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: sinon.spy(),
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions: sinon.stub().returns([]),
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
publishTransaction: sinon.spy(),
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
5 years ago
|
const listeners = {
|
||
|
confirmed: sinon.spy(),
|
||
|
dropped: sinon.spy(),
|
||
|
failed: sinon.spy(),
|
||
5 years ago
|
warning: sinon.spy(),
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
pendingTxTracker.once('tx:confirmed', listeners.confirmed);
|
||
|
pendingTxTracker.once('tx:dropped', listeners.dropped);
|
||
|
pendingTxTracker.once('tx:failed', listeners.failed);
|
||
|
pendingTxTracker.once('tx:warning', listeners.warning);
|
||
5 years ago
|
await pendingTxTracker._checkPendingTx({
|
||
|
id: '2',
|
||
|
history: [{}],
|
||
4 years ago
|
status: TRANSACTION_STATUSES.SUBMITTED,
|
||
5 years ago
|
txParams: { from: '0x1678a085c290ebd122dc42cba69373b5953b831d' },
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
listeners.failed.calledOnceWithExactly(
|
||
|
'2',
|
||
|
sinon.match.instanceOf(Error),
|
||
|
),
|
||
|
"should pass txId to 'tx:failed' listener",
|
||
4 years ago
|
);
|
||
|
assert.ok(
|
||
|
listeners.confirmed.notCalled,
|
||
|
"should not emit 'tx:confirmed'",
|
||
|
);
|
||
|
assert.ok(listeners.dropped.notCalled, "should not emit 'tx:dropped'");
|
||
|
assert.ok(listeners.warning.notCalled, "should not emit 'tx:warning'");
|
||
|
});
|
||
5 years ago
|
|
||
|
it("should emit 'tx:dropped' if another tx with the same nonce succeeds", async function () {
|
||
4 years ago
|
const txs = [
|
||
|
{
|
||
4 years ago
|
status: TRANSACTION_STATUSES.CONFIRMED,
|
||
4 years ago
|
history: [{}],
|
||
|
txParams: { nonce: '0x1' },
|
||
|
id: '456',
|
||
|
value: '0x01',
|
||
|
hash: '0xbad',
|
||
|
},
|
||
|
{
|
||
4 years ago
|
status: TRANSACTION_STATUSES.SUBMITTED,
|
||
4 years ago
|
history: [{}],
|
||
|
txParams: { nonce: '0x1' },
|
||
|
id: '123',
|
||
|
value: '0x02',
|
||
|
hash:
|
||
|
'0x2a919d2512ec963f524bfd9730fb66b6d5a2e399d1dd957abb5e2b544a12644b',
|
||
|
},
|
||
4 years ago
|
];
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
5 years ago
|
query: {
|
||
|
getTransactionReceipt: sinon.stub().resolves(null),
|
||
|
},
|
||
5 years ago
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions: sinon.stub().returns([]),
|
||
|
getCompletedTransactions: sinon.stub().returns(txs),
|
||
|
publishTransaction: sinon.spy(),
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
5 years ago
|
const listeners = {
|
||
|
confirmed: sinon.spy(),
|
||
|
dropped: sinon.spy(),
|
||
|
failed: sinon.spy(),
|
||
5 years ago
|
warning: sinon.spy(),
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
pendingTxTracker.once('tx:confirmed', listeners.confirmed);
|
||
|
pendingTxTracker.once('tx:dropped', listeners.dropped);
|
||
|
pendingTxTracker.once('tx:failed', listeners.failed);
|
||
|
pendingTxTracker.once('tx:warning', listeners.warning);
|
||
|
await pendingTxTracker._checkPendingTx(txs[1]);
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(listeners.dropped.calledOnceWithExactly('123'));
|
||
|
assert.ok(
|
||
|
listeners.confirmed.notCalled,
|
||
|
"should not emit 'tx:confirmed'",
|
||
|
);
|
||
|
assert.ok(listeners.failed.notCalled, "should not emit 'tx:failed'");
|
||
|
assert.ok(listeners.warning.notCalled, "should not emit 'tx:warning'");
|
||
|
});
|
||
5 years ago
|
|
||
|
it("should emit 'tx:dropped' with the txMetas id only after the fourth call", async function () {
|
||
4 years ago
|
const nonceBN = new BN(2);
|
||
5 years ago
|
const txMeta = {
|
||
|
id: 1,
|
||
4 years ago
|
hash:
|
||
|
'0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb',
|
||
4 years ago
|
status: TRANSACTION_STATUSES.SUBMITTED,
|
||
5 years ago
|
txParams: {
|
||
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
||
|
nonce: '0x1',
|
||
|
value: '0xfffff',
|
||
|
},
|
||
|
history: [{}],
|
||
|
rawTx: '0xf86c808504a817c80082471d',
|
||
4 years ago
|
};
|
||
5 years ago
|
const pendingTxTracker = new PendingTransactionTracker({
|
||
|
query: {
|
||
5 years ago
|
getTransactionReceipt: sinon.stub().resolves(null),
|
||
5 years ago
|
getTransactionCount: sinon.stub().resolves(nonceBN),
|
||
5 years ago
|
},
|
||
|
nonceTracker: {
|
||
|
getGlobalLock: sinon.stub().resolves({
|
||
|
releaseLock: sinon.spy(),
|
||
|
}),
|
||
|
},
|
||
|
getPendingTransactions: sinon.stub().returns([]),
|
||
|
getCompletedTransactions: sinon.stub().returns([]),
|
||
|
publishTransaction: sinon.spy(),
|
||
|
confirmTransaction: sinon.spy(),
|
||
4 years ago
|
});
|
||
5 years ago
|
const listeners = {
|
||
|
confirmed: sinon.spy(),
|
||
|
dropped: sinon.spy(),
|
||
|
failed: sinon.spy(),
|
||
5 years ago
|
warning: sinon.spy(),
|
||
4 years ago
|
};
|
||
|
|
||
|
pendingTxTracker.once('tx:confirmed', listeners.confirmed);
|
||
|
pendingTxTracker.once('tx:dropped', listeners.dropped);
|
||
|
pendingTxTracker.once('tx:failed', listeners.failed);
|
||
|
pendingTxTracker.once('tx:warning', listeners.warning);
|
||
|
await pendingTxTracker._checkPendingTx(txMeta);
|
||
|
await pendingTxTracker._checkPendingTx(txMeta);
|
||
|
await pendingTxTracker._checkPendingTx(txMeta);
|
||
|
await pendingTxTracker._checkPendingTx(txMeta);
|
||
|
|
||
|
assert.ok(listeners.dropped.calledOnceWithExactly(1));
|
||
|
assert.ok(
|
||
|
listeners.confirmed.notCalled,
|
||
|
"should not emit 'tx:confirmed'",
|
||
|
);
|
||
|
assert.ok(listeners.failed.notCalled, "should not emit 'tx:failed'");
|
||
|
assert.ok(listeners.warning.notCalled, "should not emit 'tx:warning'");
|
||
|
});
|
||
|
});
|
||
|
});
|