-
Notifications
You must be signed in to change notification settings - Fork 202
/
e2e_test.go
559 lines (465 loc) · 19.6 KB
/
e2e_test.go
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package e2e_test
import (
"context"
"errors"
"fmt"
"math/big"
"os"
"os/exec"
"strings"
"sync"
"testing"
"time"
"github.com/celo-org/celo-blockchain/common"
"github.com/celo-org/celo-blockchain/common/hexutil"
"github.com/celo-org/celo-blockchain/core/types"
"github.com/celo-org/celo-blockchain/log"
"github.com/celo-org/celo-blockchain/node"
"github.com/celo-org/celo-blockchain/rpc"
"github.com/celo-org/celo-blockchain/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
println("====== RUNNING e2e tests with original consensus protocol")
code1 := m.Run()
// Activate V2 consensus block
test.BaseEthConfig.Istanbul.V2Block = big.NewInt(0)
println("====== RUNNING e2e tests with v2 consensus protocol")
code2 := m.Run()
os.Exit(code1 | code2)
}
func init() {
// This statement is commented out but left here since its very useful for
// debugging problems and its non trivial to construct.
//
// log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stdout, log.TerminalFormat(true))))
// This disables all logging which in general we want, because there is a lot
log.Root().SetHandler(log.DiscardHandler())
}
// This test starts a network submits a transaction and waits for the whole
// network to process the transaction.
func TestSendCelo(t *testing.T) {
ac := test.AccountConfig(3, 2)
gc, ec, err := test.BuildConfig(ac)
require.NoError(t, err)
network, shutdown, err := test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
defer shutdown()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
accounts := test.Accounts(ac.DeveloperAccounts(), gc.ChainConfig())
// Send one celo from external account 0 to 1 via node 0.
tx, err := accounts[0].SendCelo(ctx, accounts[1].Address, 1, network[0])
require.NoError(t, err)
// Wait for the whole network to process the transaction.
err = network.AwaitTransactions(ctx, tx)
require.NoError(t, err)
}
// This test verifies correct behavior in a network of size one, in the case that
// this fails we know that the problem does not lie with our network code.
func TestSingleNodeNetworkManyTxs(t *testing.T) {
iterations := 5
txsPerIteration := 5
ac := test.AccountConfig(1, 1)
gc, ec, err := test.BuildConfig(ac)
require.NoError(t, err)
gc.Istanbul.Epoch = uint64(iterations) * 50 // avoid the epoch for this test
network, shutdown, err := test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
defer shutdown()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
accounts := test.Accounts(ac.DeveloperAccounts(), gc.ChainConfig())
for r := 0; r < iterations; r++ {
txs := make([]*types.Transaction, 0, txsPerIteration)
for j := 0; j < txsPerIteration; j++ {
tx, err := accounts[0].SendCelo(ctx, common.Address{}, 1, network[0])
require.NoError(t, err)
require.NotNil(t, tx)
}
err = network.AwaitTransactions(ctx, txs...)
require.NoError(t, err)
}
}
// This test is intended to ensure that epoch blocks can be correctly marshalled.
// We previously had an open bug for this https://github.com/celo-org/celo-blockchain/issues/1574
func TestEpochBlockMarshaling(t *testing.T) {
accounts := test.AccountConfig(1, 0)
gc, ec, err := test.BuildConfig(accounts)
require.NoError(t, err)
// Configure the shortest possible epoch, uptimeLookbackWindow minimum is 3
// and it needs to be < (epoch -2).
ec.Istanbul.Epoch = 6
ec.Istanbul.DefaultLookbackWindow = 3
network, shutdown, err := test.NewNetwork(accounts, gc, ec)
require.NoError(t, err)
defer shutdown()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
// Wait for the whole network to process the transaction.
err = network.AwaitBlock(ctx, 6)
require.NoError(t, err)
b := network[0].Tracker.GetProcessedBlock(6)
// Check that epoch snark data was actually unmarshalled, I.E there was
// something there.
assert.True(t, len(b.EpochSnarkData().Signature) > 0)
assert.True(t, b.EpochSnarkData().Bitmap.Uint64() > 0)
}
// This test checks that a network can have validators shut down mid operation
// and that it can continue to function, it also checks that if more than f
// validators are shut down, when they restart the network is able to continue.
func TestStartStopValidators(t *testing.T) {
ac := test.AccountConfig(4, 2)
gc, ec, err := test.BuildConfig(ac)
require.NoError(t, err)
network, _, err := test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
// We define our own shutdown function because we don't want to print
// errors about already stopped nodes. Since this test can fail while we
// have stopped nodes.
defer func() {
for _, err := range network.Shutdown() {
if !errors.Is(err, test.ErrTrackerAlreadyStopped) && !errors.Is(err, node.ErrNodeStopped) {
fmt.Println(err.Error())
}
}
}()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*40)
defer cancel()
var txs []*types.Transaction
accounts := test.Accounts(ac.DeveloperAccounts(), gc.ChainConfig())
// Send one celo from external account 0 to 1 via node 0.
tx, err := accounts[0].SendCelo(ctx, accounts[1].Address, 1, network[0])
require.NoError(t, err)
txs = append(txs, tx)
// Wait for the whole network to process the transaction.
err = network.AwaitTransactions(ctx, txs...)
require.NoError(t, err)
// Stop one node, the rest of the network should still be able to progress
err = network[3].Close()
require.NoError(t, err)
// Send one celo from external account 0 to 1 via node 0.
tx, err = accounts[0].SendCelo(ctx, accounts[1].Address, 1, network[0])
require.NoError(t, err)
txs = append(txs, tx)
// Check that the remaining network can still process this transction.
err = network[:3].AwaitTransactions(ctx, txs...)
require.NoError(t, err)
// Stop another node, the network should now be stuck
err = network[2].Close()
require.NoError(t, err)
// Now we will check that the network does not process transactions in this
// state, by waiting for a reasonable amount of time for it to process a
// transaction and assuming it is not processing transactions if we time out.
shortCtx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
// Send one celo from external account 0 to 1 via node 0.
tx, err = accounts[0].SendCelo(ctx, accounts[1].Address, 1, network[0])
require.NoError(t, err)
txs = append(txs, tx)
err = network[:2].AwaitTransactions(shortCtx, txs...)
// Expect DeadlineExceeded error
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expecting %q, instead got: %v ", context.DeadlineExceeded.Error(), err)
}
// Start the last stopped node
err = network[2].Start()
require.NoError(t, err)
// Connect last stopped node to running nodes
network[2].AddPeers(network[:2]...)
time.Sleep(25 * time.Millisecond)
for _, n := range network[:3] {
err = n.GossipEnodeCertificatge()
require.NoError(t, err)
}
// Check that the network now processes the previous transaction.
err = network[:3].AwaitTransactions(ctx, txs...)
require.NoError(t, err)
// Check that the network now quickly processes incoming transactions.
// Send one celo from external account 0 to 1 via node 0.
tx, err = accounts[0].SendCelo(ctx, accounts[1].Address, 1, network[0])
require.NoError(t, err)
txs = append(txs, tx)
err = network[:3].AwaitTransactions(ctx, txs...)
require.NoError(t, err)
// Start the first stopped node
err = network[3].Start()
require.NoError(t, err)
// Connect final node to rest of network
network[3].AddPeers(network[:3]...)
time.Sleep(25 * time.Millisecond)
for _, n := range network {
err = n.GossipEnodeCertificatge()
require.NoError(t, err)
}
// Check that the network continues to quickly processes incoming transactions.
// Send one celo from external account 0 to 1 via node 0.
tx, err = accounts[0].SendCelo(ctx, accounts[1].Address, 1, network[0])
require.NoError(t, err)
txs = append(txs, tx)
err = network.AwaitTransactions(ctx, txs...)
require.NoError(t, err)
}
// This test was created to reproduce the concurrent map access error in
// https://github.com/celo-org/celo-blockchain/issues/1799
//
// It does this by calling debug_traceBlockByNumber a number of times since the
// trace block code was the source of the concurrent map access.
func TestBlockTracingConcurrentMapAccess(t *testing.T) {
ac := test.AccountConfig(1, 2)
gc, ec, err := test.BuildConfig(ac)
require.NoError(t, err)
network, shutdown, err := test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
defer shutdown()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
defer cancel()
n := network[0]
accounts := test.Accounts(ac.DeveloperAccounts(), gc.ChainConfig())
var txs []*types.Transaction
// Send one celo from external account 0 to 1 via node 0.
for i := 0; i < 10; i++ {
tx, err := accounts[0].SendCelo(ctx, accounts[1].Address, 1, n)
require.NoError(t, err)
txs = append(txs, tx)
}
// Wait for the whole network to process the transactions.
err = network.AwaitTransactions(ctx, txs...)
require.NoError(t, err)
lastTx := txs[len(txs)-1]
b := n.Tracker.GetProcessedBlockForTx(lastTx.Hash())
var wg sync.WaitGroup
for i := 1; i < +int(b.NumberU64()); i++ {
wg.Add(1)
num := i
go func() {
defer wg.Done()
c, err := rpc.DialContext(ctx, n.WSEndpoint())
require.NoError(t, err)
var result []interface{}
err = c.CallContext(ctx, &result, "debug_traceBlockByNumber", hexutil.EncodeUint64(uint64(num)))
require.NoError(t, err)
}()
}
wg.Wait()
}
type rpcCustomTransaction struct {
BlockNumber *hexutil.Big `json:"blockNumber"`
BlockHash *common.Hash `json:"blockHash"`
GasPrice *hexutil.Big `json:"gasPrice"`
}
// TestRPCDynamicTxGasPriceWithBigFeeCap test that after a dynamic tx
// was added to a block, the rpc sends in the gasPrice the actual consumed
// price by the tx, which could be less than the feeCap (as in this example)
func TestRPCDynamicTxGasPriceWithBigFeeCap(t *testing.T) {
ac := test.AccountConfig(3, 2)
gc, ec, err := test.BuildConfig(ac)
require.NoError(t, err)
network, shutdown, err := test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
defer shutdown()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
accounts := test.Accounts(ac.DeveloperAccounts(), gc.ChainConfig())
suggestedGasPrice, err := network[0].WsClient.SuggestGasPrice(ctx)
require.NoError(t, err)
gasFeeCap := big.NewInt(0).Mul(suggestedGasPrice, big.NewInt(90))
gasTipCap := big.NewInt(0).Mul(suggestedGasPrice, big.NewInt(2))
// Send one celo from external account 0 to 1 via node 0.
tx, err := accounts[0].SendCeloWithDynamicFee(ctx, accounts[1].Address, 1, gasFeeCap, gasTipCap, network[0])
require.NoError(t, err)
// Wait for the whole network to process the transaction.
err = network.AwaitTransactions(ctx, tx)
require.NoError(t, err)
var json *rpcCustomTransaction
err = network[0].WsClient.GetRPCClient().CallContext(ctx, &json, "eth_getTransactionByHash", tx.Hash())
require.NoError(t, err)
require.NotNil(t, json.BlockNumber)
gasPrice := json.GasPrice.ToInt()
require.NotNil(t, json.GasPrice)
require.Greater(t, gasPrice.Int64(), gasTipCap.Int64())
require.Less(t, gasPrice.Int64(), gasFeeCap.Int64())
}
// TestRPCDynamicTxGasPriceWithState aims to test the scenario where a
// an old dynamic tx is requested via rpc, to an archive node.
// As right now on Celo, we are not storing the baseFee in the header (as ethereum does),
// to know the exactly gasPrice expent in a dynamic tx, depends on consuming the
// GasPriceMinimum contract
func TestRPCDynamicTxGasPriceWithState(t *testing.T) {
ac := test.AccountConfig(3, 2)
gc, ec, err := test.BuildConfig(ac)
require.NoError(t, err)
ec.TxLookupLimit = 0
ec.NoPruning = true
network, shutdown, err := test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
defer shutdown()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
accounts := test.Accounts(ac.DeveloperAccounts(), gc.ChainConfig())
suggestedGasPrice, err := network[0].WsClient.SuggestGasPrice(ctx)
require.NoError(t, err)
gasFeeCap := big.NewInt(0).Mul(suggestedGasPrice, big.NewInt(90))
gasTipCap := big.NewInt(0).Mul(suggestedGasPrice, big.NewInt(2))
// Send one celo from external account 0 to 1 via node 0.
tx, err := accounts[0].SendCeloWithDynamicFee(ctx, accounts[1].Address, 1, gasFeeCap, gasTipCap, network[0])
require.NoError(t, err)
// Wait for the whole network to process the transaction.
err = network.AwaitTransactions(ctx, tx)
require.NoError(t, err)
var json *rpcCustomTransaction
// Check that the transaction can be retrieved via the rpc api
err = network[0].WsClient.GetRPCClient().CallContext(ctx, &json, "eth_getTransactionByHash", tx.Hash())
require.NoError(t, err)
// Blocknumber != nil it means that it eas already processed
require.NotNil(t, json.BlockNumber)
// Create a block containing a transaction, we will prune the state of this block.
_, err = accounts[0].SendCeloTracked(ctx, accounts[1].Address, 1, network[0])
require.NoError(t, err)
// Prune state
err = pruneStateOfBlock(ctx, network[0], *json.BlockHash)
require.NoError(t, err)
var json2 *rpcCustomTransaction
// Check that the transaction can still be retrieved via the rpc api
err = network[0].WsClient.GetRPCClient().CallContext(ctx, &json2, "eth_getTransactionByHash", tx.Hash())
require.NoError(t, err)
// if the object is nil, it means that was not found
require.NotNil(t, json2)
// Blocknumber != nil it means that it eas already processed
require.NotNil(t, json2.BlockNumber)
require.Equal(t, json.GasPrice, json2.GasPrice)
}
// TestRPCDynamicTxGasPriceWithoutState aims to test the scenario where a
// an old dynamic tx is requested via rpc, to a full node that does not have
// the state anymore.
// As right now on Celo, we are not storing the baseFee in the header (as ethereum does),
// to know the exactly gasPrice expent in a dynamic tx, depends on consuming the
// GasPriceMinimum contract
func TestRPCDynamicTxGasPriceWithoutState(t *testing.T) {
ac := test.AccountConfig(3, 2)
gc, ec, err := test.BuildConfig(ac)
ec.TrieDirtyCache = 5
require.NoError(t, err)
network, shutdown, err := test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
defer shutdown()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
defer cancel()
accounts := test.Accounts(ac.DeveloperAccounts(), gc.ChainConfig())
suggestedGasPrice, err := network[0].WsClient.SuggestGasPrice(ctx)
require.NoError(t, err)
gasFeeCap := big.NewInt(0).Mul(suggestedGasPrice, big.NewInt(90))
gasTipCap := big.NewInt(0).Mul(suggestedGasPrice, big.NewInt(2))
// Send one celo from external account 0 to 1 via node 0.
tx, err := accounts[0].SendCeloWithDynamicFee(ctx, accounts[1].Address, 1, gasFeeCap, gasTipCap, network[0])
require.NoError(t, err)
// Wait for the whole network to process the transaction.
err = network.AwaitTransactions(ctx, tx)
require.NoError(t, err)
var json *rpcCustomTransaction
// Check that the transaction can be retrieved via the rpc api
err = network[0].WsClient.GetRPCClient().CallContext(ctx, &json, "eth_getTransactionByHash", tx.Hash())
require.NoError(t, err)
// Blocknumber != nil it means that it eas already processed
require.NotNil(t, json.BlockNumber)
// Create one block to be able to prune the last state
_, err = accounts[0].SendCeloTracked(ctx, accounts[1].Address, 1, network[0])
require.NoError(t, err)
// Prune state
err = pruneStateOfBlock(ctx, network[0], *json.BlockHash)
require.NoError(t, err)
var json2 *rpcCustomTransaction
// Check that the transaction can still be retrieved via the rpc api
err = network[0].WsClient.GetRPCClient().CallContext(ctx, &json2, "eth_getTransactionByHash", tx.Hash())
require.NoError(t, err)
// if the object is nil, it means that was not found
require.NotNil(t, json2)
// Blocknumber != nil it means that it eas already processed
require.NotNil(t, json2.BlockNumber)
require.Nil(t, json2.GasPrice)
}
func pruneStateOfBlock(ctx context.Context, node *test.Node, blockHash common.Hash) error {
var block *types.Block
block, err := node.WsClient.BlockByHash(ctx, blockHash)
if err != nil {
return err
}
root := block.Root()
node.Eth.BlockChain().StateCache().TrieDB().Dereference(root)
return nil
}
func TestEthersJSCompatibility(t *testing.T) {
ac := test.AccountConfig(1, 1)
gc, ec, err := test.BuildConfig(ac)
require.NoError(t, err)
network, shutdown, err := test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
defer shutdown()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
defer cancel()
num, err := network[0].WsClient.BlockNumber(ctx)
require.NoError(t, err)
// Execute typescript tests to check ethers.js compatibility.
//
// The '--networkaddr' and '--blocknum' flags are npm config variables, the
// values become available under 'process.env.npm_config_networkaddr' and
// 'process.env.npm_config_blocknum' in typescript test. Everything after
// '--' are flags that are passed to mocha and these flags are controlling
// which tests to run.
// The tests don't seem to work on CI with IPV6 addresses so we convert to IPV4 here
addr := strings.Replace(network[0].Node.HTTPEndpoint(), "[::]", "127.0.0.1", 1)
cmd := exec.Command("npm", "run", "test", "--networkaddr="+addr, "--blocknum="+hexutil.Uint64(num).String(), "--", "--grep", "ethers.js compatibility tests with state")
cmd.Dir = "./ethersjs-api-check/"
println("executing mocha test with", cmd.String())
output, err := cmd.CombinedOutput()
println(string(output))
require.NoError(t, err)
err = network[0].Tracker.AwaitBlock(ctx, num+1)
require.NoError(t, err)
block := network[0].Tracker.GetProcessedBlock(num)
// Prune state
err = pruneStateOfBlock(ctx, network[0], block.Hash())
require.NoError(t, err)
// Execute typescript tests to check what happens with a pruned block.
cmd = exec.Command("npm", "run", "test", "--networkaddr="+addr, "--blocknum="+hexutil.Uint64(num).String(), "--", "--grep", "ethers.js compatibility tests with no state")
cmd.Dir = "./ethersjs-api-check/"
println("executing mocha test with", cmd.String())
output, err = cmd.CombinedOutput()
println(string(output))
require.NoError(t, err)
}
// This test checks the functionality of the configuration to enable/disable
// returning the 'gasLimit' and 'baseFeePerGas' fields on RPC blocks.
func TestEthersJSCompatibilityDisable(t *testing.T) {
ac := test.AccountConfig(1, 1)
gc, ec, err := test.BuildConfig(ac)
require.NoError(t, err)
// Check fields present (compatibility set by default)
network, shutdown, err := test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
defer shutdown()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
defer cancel()
result := make(map[string]interface{})
err = network[0].WsClient.GetRPCClient().CallContext(ctx, &result, "eth_getBlockByNumber", "latest", true)
require.NoError(t, err)
_, ok := result["gasLimit"]
assert.True(t, ok, "gasLimit field should be present on RPC block")
_, ok = result["baseFeePerGas"]
assert.True(t, ok, "baseFeePerGas field should be present on RPC block")
// Turn of compatibility and check fields are not present
ec.RPCEthCompatibility = false
network, shutdown, err = test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
defer shutdown()
ctx, cancel = context.WithTimeout(context.Background(), time.Second*20)
defer cancel()
result = make(map[string]interface{})
err = network[0].WsClient.GetRPCClient().CallContext(ctx, &result, "eth_getBlockByNumber", "latest", true)
require.NoError(t, err)
_, ok = result["gasLimit"]
assert.False(t, ok, "gasLimit field should not be present on RPC block")
_, ok = result["baseFeePerGas"]
assert.False(t, ok, "baseFeePerGas field should not be present on RPC block")
}