Skip to content

Commit 1864a45

Browse files
author
Matija Petrunic
committed
Remove inserting gap nonce
1 parent 316feee commit 1864a45

File tree

3 files changed

+26
-74
lines changed

3 files changed

+26
-74
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ npm-debug.log*
1212
yarn-debug.log*
1313
yarn-error.log*
1414
lerna-debug.log*
15+
coverage

src/serverWallet.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,6 @@ export class ServerWeb3Wallet extends Wallet {
9494

9595
let nonce = transactionCount;
9696

97-
const gapNonce = this.findGapNonce(transactions, transactionCount);
98-
if(gapNonce) {
99-
defaultLogger.debug("Found gap nonce " + gapNonce);
100-
return BigNumber.from(gapNonce);
101-
}
102-
10397
if(transactions.length) {
10498
const storedNonce = transactions[transactions.length - 1].nonce + 1;
10599
//if stored nonce is lower than transaction count, we didn't store all transactions
@@ -108,24 +102,8 @@ export class ServerWeb3Wallet extends Wallet {
108102
nonce = storedNonce;
109103
}
110104
}
111-
return BigNumber.from(nonce);
112-
}
113-
114-
private findGapNonce(
115-
transactions: SavedTransactionResponse[],
116-
lastNonce: number
117-
): number | undefined {
118-
if(transactions[0] && transactions[0].nonce - lastNonce > 0) {
119-
return lastNonce;
120-
}
121-
122-
for(let i=0; i < transactions.length - 1; i++) {
123-
if(transactions[i+1].nonce - (transactions[i].nonce + 1) > 0) {
124-
return transactions[i].nonce + 1;
125-
}
126-
}
127105

128-
return;
106+
return BigNumber.from(nonce);
129107
}
130108

131109
private async submitTransaction(tx: providers.TransactionRequest): Promise<providers.TransactionResponse> {

test/serverWallet.test.ts

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ describe("Server wallet sendTransaction", function () {
1515

1616
beforeEach(async function () {
1717
sinon.stub(providers.Provider, "isProvider").returns(true)
18-
walletStorage = sinon.stub() as IWalletTransactionStorage;
19-
walletSource = sinon.stub() as IWalletSourceStorage;
20-
providerStub = sinon.stub() as providers.Provider;
18+
walletStorage = sinon.stub() as unknown as IWalletTransactionStorage;
19+
walletSource = sinon.stub() as unknown as IWalletSourceStorage;
20+
providerStub = sinon.createStubInstance(providers.Provider);
2121
signingKey = new utils.SigningKey(
2222
"0xE5B21F1D68386B32407F2B63F49EE74CDAE4A80EE346EB90205B62D8BCDE9920"
2323
)
@@ -69,7 +69,7 @@ describe("Server wallet sendTransaction", function () {
6969
it("Uses provided gas price if sent", async function () {
7070
const transactionResponseStub = sinon.stub(
7171
web3Wallet as any, "submitTransaction"
72-
).resolves(sinon.stub() as providers.TransactionResponse)
72+
).resolves(sinon.stub() as unknown as providers.TransactionResponse)
7373
const tx = {
7474
to: "to-address",
7575
nonce: 0,
@@ -79,7 +79,7 @@ describe("Server wallet sendTransaction", function () {
7979
value: 121,
8080
}
8181

82-
const txResponse = await web3Wallet.sendTransaction(tx);
82+
await web3Wallet.sendTransaction(tx);
8383

8484
expect(transactionResponseStub.args[0][0].gasPrice).to.be.equal(20.00);
8585
});
@@ -88,7 +88,7 @@ describe("Server wallet sendTransaction", function () {
8888
sinon.stub(utilsModule, "estimateGasPrice").resolves(BigNumber.from(10.0))
8989
const transactionResponseStub = sinon.stub(
9090
web3Wallet as any, "submitTransaction"
91-
).resolves(sinon.stub() as providers.TransactionResponse)
91+
).resolves(sinon.stub() as unknown as providers.TransactionResponse)
9292
const tx = {
9393
to: "to-address",
9494
nonce: 0,
@@ -98,15 +98,15 @@ describe("Server wallet sendTransaction", function () {
9898
chainId: 1
9999
}
100100

101-
const txResponse = await web3Wallet.sendTransaction(tx);
101+
await web3Wallet.sendTransaction(tx);
102102

103103
expect(transactionResponseStub.args[0][0].gasPrice.toNumber()).to.be.equal(10.0);
104104
});
105105

106106
it("Uses limit gas price if gas price higher", async function () {
107107
const transactionResponseStub = sinon.stub(
108108
web3Wallet as any, "submitTransaction"
109-
).resolves(sinon.stub() as providers.TransactionResponse)
109+
).resolves(sinon.stub() as unknown as providers.TransactionResponse)
110110
const tx = {
111111
to: "to-address",
112112
nonce: 0,
@@ -117,15 +117,15 @@ describe("Server wallet sendTransaction", function () {
117117
gasPrice: 51000000000
118118
}
119119

120-
const txResponse = await web3Wallet.sendTransaction(tx);
120+
await web3Wallet.sendTransaction(tx);
121121

122122
expect(transactionResponseStub.args[0][0].gasPrice.toNumber()).to.be.equal(50000000000);
123123
});
124124

125125
it("Uses default nonce if sent", async function () {
126126
const transactionResponseStub = sinon.stub(
127127
web3Wallet as any, "submitTransaction"
128-
).resolves(sinon.stub() as providers.TransactionResponse)
128+
).resolves(sinon.stub() as unknown as providers.TransactionResponse)
129129
const tx = {
130130
to: "to-address",
131131
gasLimit: 21000,
@@ -136,24 +136,23 @@ describe("Server wallet sendTransaction", function () {
136136
nonce: 6
137137
}
138138

139-
const txResponse = await web3Wallet.sendTransaction(tx);
139+
await web3Wallet.sendTransaction(tx);
140140

141141
expect(transactionResponseStub.args[0][0].nonce).to.be.equal(6);
142142
});
143143

144-
it("Uses gap nonce if it exists", async function () {
144+
it("Assigns highest nonce + 1 if transactions exist", async function () {
145145
walletStorage.getTransactions = async function getTransactions(){
146146
return [
147-
{nonce: 2} as unknown as SavedTransactionResponse,
148-
{nonce: 4} as unknown as SavedTransactionResponse
147+
{nonce: 2} as unknown as SavedTransactionResponse
149148
]
150149
}
151150
sinon.stub(web3Wallet, "getTransactionCount").resolves(
152151
2
153152
);
154153
const transactionResponseStub = sinon.stub(
155154
web3Wallet as any, "submitTransaction"
156-
).resolves(sinon.stub() as providers.TransactionResponse)
155+
).resolves(sinon.stub() as unknown as providers.TransactionResponse)
157156
const tx = {
158157
to: "to-address",
159158
gasLimit: 21000,
@@ -163,49 +162,23 @@ describe("Server wallet sendTransaction", function () {
163162
chainId: 1
164163
}
165164

166-
const txResponse = await web3Wallet.sendTransaction(tx);
165+
await web3Wallet.sendTransaction(tx);
167166

168167
expect(transactionResponseStub.args[0][0].nonce.toNumber()).to.be.equal(3);
169168
});
170169

171-
it("Uses gap between first transaction and transaction count if it exists", async function () {
172-
walletStorage.getTransactions = async function getTransactions(){
173-
return [
174-
{nonce: 3} as unknown as SavedTransactionResponse,
175-
]
176-
}
170+
it("Uses get transaction count if no transactions in storage", async function () {
177171
sinon.stub(web3Wallet, "getTransactionCount").resolves(
178-
2
172+
4
179173
);
180-
const transactionResponseStub = sinon.stub(
181-
web3Wallet as any, "submitTransaction"
182-
).resolves(sinon.stub() as providers.TransactionResponse)
183-
const tx = {
184-
to: "to-address",
185-
gasLimit: 21000,
186-
gasPrice: 10.00,
187-
data: "data",
188-
value: 121,
189-
chainId: 1
190-
}
191-
192-
const txResponse = await web3Wallet.sendTransaction(tx);
193-
194-
expect(transactionResponseStub.args[0][0].nonce.toNumber()).to.be.equal(2);
195-
});
196-
197-
it("Assigns highest nonce + 1 if transactions exist", async function () {
198174
walletStorage.getTransactions = async function getTransactions(){
199175
return [
200176
{nonce: 2} as unknown as SavedTransactionResponse
201177
]
202178
}
203-
sinon.stub(web3Wallet, "getTransactionCount").resolves(
204-
2
205-
);
206179
const transactionResponseStub = sinon.stub(
207180
web3Wallet as any, "submitTransaction"
208-
).resolves(sinon.stub() as providers.TransactionResponse)
181+
).resolves(sinon.stub() as unknown as providers.TransactionResponse)
209182
const tx = {
210183
to: "to-address",
211184
gasLimit: 21000,
@@ -215,12 +188,12 @@ describe("Server wallet sendTransaction", function () {
215188
chainId: 1
216189
}
217190

218-
const txResponse = await web3Wallet.sendTransaction(tx);
191+
await web3Wallet.sendTransaction(tx);
219192

220-
expect(transactionResponseStub.args[0][0].nonce.toNumber()).to.be.equal(3);
193+
expect(transactionResponseStub.args[0][0].nonce.toNumber()).to.be.equal(4);
221194
});
222195

223-
it("Uses get transaction count if no transactions in storage", async function () {
196+
it("Uses transaction count if nonce in transactions lower", async function () {
224197
sinon.stub(web3Wallet, "getTransactionCount").resolves(
225198
4
226199
);
@@ -229,7 +202,7 @@ describe("Server wallet sendTransaction", function () {
229202
}
230203
const transactionResponseStub = sinon.stub(
231204
web3Wallet as any, "submitTransaction"
232-
).resolves(sinon.stub() as providers.TransactionResponse)
205+
).resolves(sinon.stub() as unknown as providers.TransactionResponse)
233206
const tx = {
234207
to: "to-address",
235208
gasLimit: 21000,
@@ -239,7 +212,7 @@ describe("Server wallet sendTransaction", function () {
239212
chainId: 1
240213
}
241214

242-
const txResponse = await web3Wallet.sendTransaction(tx);
215+
await web3Wallet.sendTransaction(tx);
243216

244217
expect(transactionResponseStub.args[0][0].nonce.toNumber()).to.be.equal(4);
245218
});
@@ -298,7 +271,7 @@ describe("Server wallet sendTransaction", function () {
298271
chainId: 1
299272
}
300273

301-
const txResponse = await web3Wallet.sendTransaction(tx);
274+
await web3Wallet.sendTransaction(tx);
302275

303276
expect(spy.calledOnce).to.be.deep.equal(true);
304277
});

0 commit comments

Comments
 (0)