This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
forked from portis-project/provider-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnonce.js
174 lines (143 loc) · 5.84 KB
/
nonce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const test = require('tape')
const Transaction = require('ethereumjs-tx')
const ethUtil = require('ethereumjs-util')
const ProviderEngine = require('../index.js')
const FixtureProvider = require('../subproviders/fixture.js')
const NonceTracker = require('../subproviders/nonce-tracker.js')
const HookedWalletProvider = require('../subproviders/hooked-wallet.js')
const TestBlockProvider = require('./util/block.js')
const createPayload = require('../util/create-payload.js')
const injectMetrics = require('./util/inject-metrics')
test('basic nonce tracking', function(t){
t.plan(11)
var privateKey = Buffer.from('cccd8f4d88de61f92f3747e4a9604a0395e6ad5138add4bec4a2ddf231ee24f9', 'hex')
var address = Buffer.from('1234362ef32bcd26d3dd18ca749378213625ba0b', 'hex')
var addressHex = '0x'+address.toString('hex')
// sign all tx's
var providerA = injectMetrics(new HookedWalletProvider({
signTransaction: function(txParams, cb){
var tx = new Transaction(txParams)
tx.sign(privateKey)
var rawTx = '0x'+tx.serialize().toString('hex')
cb(null, rawTx)
},
}))
// handle nonce requests
var providerB = injectMetrics(new NonceTracker())
// handle all bottom requests
var providerC = injectMetrics(new FixtureProvider({
eth_gasPrice: '0x1234',
eth_getTransactionCount: '0x00',
eth_sendRawTransaction: function(payload, next, done){
var rawTx = ethUtil.toBuffer(payload.params[0])
var tx = new Transaction(rawTx)
var hash = '0x'+tx.hash().toString('hex')
done(null, hash)
},
}))
// handle block requests
var providerD = injectMetrics(new TestBlockProvider())
var engine = new ProviderEngine()
engine.addProvider(providerA)
engine.addProvider(providerB)
engine.addProvider(providerC)
engine.addProvider(providerD)
var txPayload = {
method: 'eth_sendTransaction',
params: [{
from: addressHex,
to: addressHex,
value: '0x01',
gas: '0x1234567890',
}]
}
engine.start()
engine.sendAsync(createPayload(txPayload), function(err, response){
t.ifError(err, 'did not error')
t.ok(response, 'has response')
// tx nonce
t.equal(providerB.getWitnessed('eth_getTransactionCount').length, 1, 'providerB did see "eth_getTransactionCount"')
t.equal(providerB.getHandled('eth_getTransactionCount').length, 0, 'providerB did NOT handle "eth_getTransactionCount"')
t.equal(providerC.getWitnessed('eth_getTransactionCount').length, 1, 'providerC did see "eth_getTransactionCount"')
t.equal(providerC.getHandled('eth_getTransactionCount').length, 1, 'providerC did handle "eth_getTransactionCount"')
// send raw tx
t.equal(providerC.getWitnessed('eth_sendRawTransaction').length, 1, 'providerC did see "eth_sendRawTransaction"')
t.equal(providerC.getHandled('eth_sendRawTransaction').length, 1, 'providerC did handle "eth_sendRawTransaction"')
engine.sendAsync(createPayload({
method: 'eth_getTransactionCount',
params: [addressHex, 'pending'],
}), function(err, response){
t.ifError(err, 'did not error')
t.ok(response, 'has response')
// tx nonce did increment
t.equal(response.result, '0x01', 'the provider gives the correct pending nonce')
engine.stop()
t.end()
})
})
})
test('nonce tracking - on error', function(t){
t.plan(11)
var privateKey = Buffer.from('cccd8f4d88de61f92f3747e4a9604a0395e6ad5138add4bec4a2ddf231ee24f9', 'hex')
var address = Buffer.from('1234362ef32bcd26d3dd18ca749378213625ba0b', 'hex')
var addressHex = '0x'+address.toString('hex')
// sign all tx's
var providerA = injectMetrics(new HookedWalletProvider({
signTransaction: function(txParams, cb){
var tx = new Transaction(txParams)
tx.sign(privateKey)
var rawTx = '0x'+tx.serialize().toString('hex')
cb(null, rawTx)
},
}))
// handle nonce requests
var providerB = injectMetrics(new NonceTracker())
// handle all bottom requests
var providerC = injectMetrics(new FixtureProvider({
eth_gasPrice: '0x1234',
eth_getTransactionCount: '0x00',
eth_sendRawTransaction: function(payload, next, done){
done(new Error('Always fail.'))
},
}))
// handle block requests
var providerD = injectMetrics(new TestBlockProvider())
var engine = new ProviderEngine()
engine.addProvider(providerA)
engine.addProvider(providerB)
engine.addProvider(providerC)
engine.addProvider(providerD)
var txPayload = {
method: 'eth_sendTransaction',
params: [{
from: addressHex,
to: addressHex,
value: '0x01',
gas: '0x1234567890',
}]
}
engine.start()
engine.sendAsync(createPayload(txPayload), function(err, response){
t.ok(err, 'did not error')
t.ok(response.error, 'has response')
// tx nonce
t.equal(providerB.getWitnessed('eth_getTransactionCount').length, 1, 'providerB did see "eth_getTransactionCount"')
t.equal(providerB.getHandled('eth_getTransactionCount').length, 0, 'providerB did NOT handle "eth_getTransactionCount"')
t.equal(providerC.getWitnessed('eth_getTransactionCount').length, 1, 'providerC did see "eth_getTransactionCount"')
t.equal(providerC.getHandled('eth_getTransactionCount').length, 1, 'providerC did handle "eth_getTransactionCount"')
// send raw tx
t.equal(providerC.getWitnessed('eth_sendRawTransaction').length, 1, 'providerC did see "eth_sendRawTransaction"')
t.equal(providerC.getHandled('eth_sendRawTransaction').length, 1, 'providerC did handle "eth_sendRawTransaction"')
engine.sendAsync(createPayload({
method: 'eth_getTransactionCount',
params: [addressHex, 'pending'],
}), function(err, response){
t.ifError(err, 'did not error')
t.ok(response, 'has response')
// tx nonce did NOT increment
t.equal(response.result, '0x00', 'the provider gives the correct pending nonce')
engine.stop()
t.end()
})
})
})