aboutsummaryrefslogtreecommitdiffstats
path: root/dex/app_test.go
blob: 34b31d3d3db0f8752c5ed1b8ace1c115a9cbfe94 (plain) (blame)
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
package dex

import (
    "crypto/ecdsa"
    "fmt"
    "math/big"
    "strings"
    "testing"
    "time"

    coreCommon "github.com/dexon-foundation/dexon-consensus/common"
    coreTypes "github.com/dexon-foundation/dexon-consensus/core/types"

    "github.com/dexon-foundation/dexon/accounts/abi"
    "github.com/dexon-foundation/dexon/common"
    "github.com/dexon-foundation/dexon/consensus/dexcon"
    "github.com/dexon-foundation/dexon/core"
    "github.com/dexon-foundation/dexon/core/types"
    "github.com/dexon-foundation/dexon/core/vm"
    "github.com/dexon-foundation/dexon/crypto"
    "github.com/dexon-foundation/dexon/ethdb"
    "github.com/dexon-foundation/dexon/params"
    "github.com/dexon-foundation/dexon/rlp"
)

func TestPreparePayload(t *testing.T) {
    key, err := crypto.GenerateKey()
    if err != nil {
        t.Fatalf("hex to ecdsa error: %v", err)
    }

    dex, err := newTestDexonWithGenesis(key)
    if err != nil {
        t.Fatalf("new test dexon error: %v", err)
    }

    signer := types.NewEIP155Signer(dex.chainConfig.ChainID)

    var expectTx types.Transactions
    for i := 0; i < 5; i++ {
        tx, err := addTx(dex, i, signer, key)
        if err != nil {
            t.Fatalf("add tx error: %v", err)
        }
        expectTx = append(expectTx, tx)
    }

    // This transaction will not be included.
    _, err = addTx(dex, 100, signer, key)
    if err != nil {
        t.Fatalf("add tx error: %v", err)
    }

    chainID := new(big.Int).Mod(crypto.PubkeyToAddress(key.PublicKey).Big(),
        big.NewInt(int64(dex.chainConfig.Dexcon.NumChains)))
    var chainNum uint32
    for chainNum = 0; chainNum < dex.chainConfig.Dexcon.NumChains; chainNum++ {

        root := dex.blockchain.CurrentBlock().Root()
        dex.app.chainRoot.Store(chainNum, &root)
        payload, err := dex.app.PreparePayload(coreTypes.Position{ChainID: chainNum})
        if err != nil {
            t.Fatalf("prepare payload error: %v", err)
        }

        var transactions types.Transactions
        err = rlp.DecodeBytes(payload, &transactions)
        if err != nil {
            t.Fatalf("rlp decode error: %v", err)
        }

        // Only one chain id allow prepare transactions.
        if chainID.Uint64() == uint64(chainNum) && len(transactions) != 5 {
            t.Fatalf("incorrect transaction num expect %v but %v", 5, len(transactions))
        } else if chainID.Uint64() != uint64(chainNum) && len(transactions) != 0 {
            t.Fatalf("expect empty slice but %v", len(transactions))
        }

        for i, tx := range transactions {
            if expectTx[i].Gas() != tx.Gas() {
                t.Fatalf("unexpected gas expect %v but %v", expectTx[i].Gas(), tx.Gas())
            }

            if expectTx[i].Hash() != tx.Hash() {
                t.Fatalf("unexpected hash expect %v but %v", expectTx[i].Hash(), tx.Hash())
            }

            if expectTx[i].Nonce() != tx.Nonce() {
                t.Fatalf("unexpected nonce expect %v but %v", expectTx[i].Nonce(), tx.Nonce())
            }

            if expectTx[i].GasPrice().Uint64() != tx.GasPrice().Uint64() {
                t.Fatalf("unexpected gas price expect %v but %v",
                    expectTx[i].GasPrice().Uint64(), tx.GasPrice().Uint64())
            }
        }
    }
}

func TestPrepareWitness(t *testing.T) {
    key, err := crypto.GenerateKey()
    if err != nil {
        t.Fatalf("hex to ecdsa error: %v", err)
    }

    dex, err := newTestDexonWithGenesis(key)
    if err != nil {
        t.Fatalf("new test dexon error: %v", err)
    }

    currentBlock := dex.blockchain.CurrentBlock()

    witness, err := dex.app.PrepareWitness(0)
    if err != nil {
        t.Fatalf("prepare witness error: %v", err)
    }

    if witness.Height != currentBlock.NumberU64() {
        t.Fatalf("unexpeted witness height %v", witness.Height)
    }

    var witnessBlockHash common.Hash
    err = rlp.DecodeBytes(witness.Data, &witnessBlockHash)
    if err != nil {
        t.Fatalf("rlp decode error: %v", err)
    }

    if witnessBlockHash != currentBlock.Hash() {
        t.Fatalf("expect root %v but %v", currentBlock.Hash(), witnessBlockHash)
    }

    if _, err := dex.app.PrepareWitness(999); err == nil {
        t.Fatalf("it must be get error from prepare")
    } else {
        t.Logf("Nice error: %v", err)
    }
}

func TestVerifyBlock(t *testing.T) {
    key, err := crypto.GenerateKey()
    if err != nil {
        t.Fatalf("hex to ecdsa error: %v", err)
    }

    dex, err := newTestDexonWithGenesis(key)
    if err != nil {
        t.Fatalf("new test dexon error: %v", err)
    }

    chainID := new(big.Int).Mod(crypto.PubkeyToAddress(key.PublicKey).Big(),
        big.NewInt(int64(dex.chainConfig.Dexcon.NumChains)))

    root := dex.blockchain.CurrentBlock().Root()
    dex.app.chainRoot.Store(uint32(chainID.Uint64()), &root)

    // Prepare first confirmed block.
    _, err = prepareConfirmedBlocks(dex, []*ecdsa.PrivateKey{key}, 0)
    if err != nil {
        t.Fatalf("prepare confirmed blocks error: %v", err)
    }

    // Prepare normal block.
    block := &coreTypes.Block{}
    block.Hash = coreCommon.NewRandomHash()
    block.Position.ChainID = uint32(chainID.Uint64())
    block.Position.Height = 1
    block.ProposerID = coreTypes.NodeID{coreCommon.Hash{1, 2, 3}}
    block.Payload, block.Witness, err = prepareDataWithoutTxPool(dex, key, 0, 100)
    if err != nil {
        t.Fatalf("prepare data error: %v", err)
    }

    // Expect ok.
    status := dex.app.VerifyBlock(block)
    if status != coreTypes.VerifyOK {
        t.Fatalf("verify fail: %v", status)
    }

    // Prepare invalid nonce tx.
    block = &coreTypes.Block{}
    block.Hash = coreCommon.NewRandomHash()
    block.Position.ChainID = uint32(chainID.Uint64())
    block.Position.Height = 1
    block.ProposerID = coreTypes.NodeID{coreCommon.Hash{1, 2, 3}}
    block.Payload, block.Witness, err = prepareDataWithoutTxPool(dex, key, 1, 100)
    if err != nil {
        t.Fatalf("prepare data error: %v", err)
    }

    // Expect invalid block.
    status = dex.app.VerifyBlock(block)
    if status != coreTypes.VerifyInvalidBlock {
        t.Fatalf("verify fail: %v", status)
    }

    // Prepare invalid block height.
    block = &coreTypes.Block{}
    block.Hash = coreCommon.NewRandomHash()
    block.Position.ChainID = uint32(chainID.Uint64())
    block.Position.Height = 2
    block.ProposerID = coreTypes.NodeID{coreCommon.Hash{1, 2, 3}}
    block.Payload, block.Witness, err = prepareDataWithoutTxPool(dex, key, 0, 100)
    if err != nil {
        t.Fatalf("prepare data error: %v", err)
    }

    // Expect retry later.
    status = dex.app.VerifyBlock(block)
    if status != coreTypes.VerifyRetryLater {
        t.Fatalf("verify fail expect retry later but get %v", status)
    }

    // Prepare reach block limit.
    block = &coreTypes.Block{}
    block.Hash = coreCommon.NewRandomHash()
    block.Position.ChainID = uint32(chainID.Uint64())
    block.Position.Height = 1
    block.ProposerID = coreTypes.NodeID{coreCommon.Hash{1, 2, 3}}
    block.Payload, block.Witness, err = prepareDataWithoutTxPool(dex, key, 0, 10000)
    if err != nil {
        t.Fatalf("prepare data error: %v", err)
    }

    // Expect invalid block.
    status = dex.app.VerifyBlock(block)
    if status != coreTypes.VerifyInvalidBlock {
        t.Fatalf("verify fail expect invalid block but get %v", status)
    }

    // Prepare insufficient funds.
    block = &coreTypes.Block{}
    block.Hash = coreCommon.NewRandomHash()
    block.Position.ChainID = uint32(chainID.Uint64())
    block.Position.Height = 1
    block.ProposerID = coreTypes.NodeID{coreCommon.Hash{1, 2, 3}}
    _, block.Witness, err = prepareDataWithoutTxPool(dex, key, 0, 0)
    if err != nil {
        t.Fatalf("prepare data error: %v", err)
    }

    signer := types.NewEIP155Signer(dex.chainConfig.ChainID)
    tx := types.NewTransaction(
        0,
        common.BytesToAddress([]byte{9}),
        big.NewInt(50000000000000001),
        params.TxGas,
        big.NewInt(1),
        nil)
    tx, err = types.SignTx(tx, signer, key)
    if err != nil {
        return
    }

    block.Payload, err = rlp.EncodeToBytes(types.Transactions{tx})
    if err != nil {
        return
    }

    // expect invalid block
    status = dex.app.VerifyBlock(block)
    if status != coreTypes.VerifyInvalidBlock {
        t.Fatalf("verify fail expect invalid block but get %v", status)
    }

    // Prepare invalid intrinsic gas.
    block = &coreTypes.Block{}
    block.Hash = coreCommon.NewRandomHash()
    block.Position.ChainID = uint32(chainID.Uint64())
    block.Position.Height = 1
    block.ProposerID = coreTypes.NodeID{coreCommon.Hash{1, 2, 3}}
    _, block.Witness, err = prepareDataWithoutTxPool(dex, key, 0, 0)
    if err != nil {
        t.Fatalf("prepare data error: %v", err)
    }

    signer = types.NewEIP155Signer(dex.chainConfig.ChainID)
    tx = types.NewTransaction(
        0,
        common.BytesToAddress([]byte{9}),
        big.NewInt(1),
        params.TxGas,
        big.NewInt(1),
        make([]byte, 1))
    tx, err = types.SignTx(tx, signer, key)
    if err != nil {
        return
    }

    block.Payload, err = rlp.EncodeToBytes(types.Transactions{tx})
    if err != nil {
        return
    }

    // Expect invalid block.
    status = dex.app.VerifyBlock(block)
    if status != coreTypes.VerifyInvalidBlock {
        t.Fatalf("verify fail expect invalid block but get %v", status)
    }

    // Prepare invalid transactions with nonce.
    block = &coreTypes.Block{}
    block.Hash = coreCommon.NewRandomHash()
    block.Position.ChainID = uint32(chainID.Uint64())
    block.Position.Height = 1
    block.ProposerID = coreTypes.NodeID{coreCommon.Hash{1, 2, 3}}
    _, block.Witness, err = prepareDataWithoutTxPool(dex, key, 0, 0)
    if err != nil {
        t.Fatalf("prepare data error: %v", err)
    }

    signer = types.NewEIP155Signer(dex.chainConfig.ChainID)
    tx1 := types.NewTransaction(
        0,
        common.BytesToAddress([]byte{9}),
        big.NewInt(1),
        params.TxGas,
        big.NewInt(1),
        make([]byte, 1))
    tx1, err = types.SignTx(tx, signer, key)
    if err != nil {
        return
    }

    // Invalid nonce.
    tx2 := types.NewTransaction(
        2,
        common.BytesToAddress([]byte{9}),
        big.NewInt(1),
        params.TxGas,
        big.NewInt(1),
        make([]byte, 1))
    tx2, err = types.SignTx(tx, signer, key)
    if err != nil {
        return
    }

    block.Payload, err = rlp.EncodeToBytes(types.Transactions{tx1, tx2})
    if err != nil {
        return
    }

    // Expect invalid block.
    status = dex.app.VerifyBlock(block)
    if status != coreTypes.VerifyInvalidBlock {
        t.Fatalf("verify fail expect invalid block but get %v", status)
    }
}

func TestBlockConfirmed(t *testing.T) {
    key, err := crypto.GenerateKey()
    if err != nil {
        t.Fatalf("hex to ecdsa error: %v", err)
    }

    dex, err := newTestDexonWithGenesis(key)
    if err != nil {
        t.Fatalf("new test dexon error: %v", err)
    }

    chainID := new(big.Int).Mod(crypto.PubkeyToAddress(key.PublicKey).Big(),
        big.NewInt(int64(dex.chainConfig.Dexcon.NumChains)))

    root := dex.blockchain.CurrentBlock().Root()
    dex.app.chainRoot.Store(uint32(chainID.Uint64()), &root)

    var (
        expectCost    big.Int
        expectNonce   uint64
        expectCounter uint64
    )
    for i := 0; i < 10; i++ {
        var startNonce int
        if expectNonce != 0 {
            startNonce = int(expectNonce) + 1
        }
        payload, witness, cost, nonce, err := prepareData(dex, key, startNonce, 50)
        if err != nil {
            t.Fatalf("prepare data error: %v", err)
        }
        expectCost.Add(&expectCost, &cost)
        expectNonce = nonce

        block := &coreTypes.Block{}
        block.Hash = coreCommon.NewRandomHash()
        block.Witness = witness
        block.Payload = payload
        block.ProposerID = coreTypes.NodeID{coreCommon.Hash{1, 2, 3}}
        block.Position.ChainID = uint32(chainID.Uint64())

        dex.app.BlockConfirmed(*block)
        expectCounter++
    }

    info := dex.app.blockchain.GetAddressInfo(uint32(chainID.Uint64()),
        crypto.PubkeyToAddress(key.PublicKey))

    if info.Counter != expectCounter {
        t.Fatalf("expect address counter is %v but %v", expectCounter, info.Counter)
    }

    if info.Cost.Cmp(&expectCost) != 0 {
        t.Fatalf("expect address cost is %v but %v", expectCost.Uint64(), info.Cost.Uint64())
    }

    if info.Nonce != expectNonce {
        t.Fatalf("expect address nonce is %v but %v", expectNonce, info.Nonce)
    }
}

func TestBlockDelivered(t *testing.T) {
    key, err := crypto.GenerateKey()
    if err != nil {
        t.Fatalf("hex to ecdsa error: %v", err)
    }

    dex, err := newTestDexonWithGenesis(key)
    if err != nil {
        t.Fatalf("new test dexon error: %v", err)
    }

    chainID := new(big.Int).Mod(crypto.PubkeyToAddress(key.PublicKey).Big(),
        big.NewInt(int64(dex.chainConfig.Dexcon.NumChains)))

    root := dex.blockchain.CurrentBlock().Root()
    dex.app.chainRoot.Store(uint32(chainID.Uint64()), &root)

    address := crypto.PubkeyToAddress(key.PublicKey)
    firstBlocksInfo, err := prepareConfirmedBlocks(dex, []*ecdsa.PrivateKey{key}, 50)
    if err != nil {
        t.Fatalf("preapare confirmed block error: %v", err)
    }

    dex.app.BlockDelivered(firstBlocksInfo[0].Block.Hash, firstBlocksInfo[0].Block.Position,
        coreTypes.FinalizationResult{
            Timestamp: time.Now(),
            Height:    1,
        })

    currentState, err := dex.blockchain.State()
    if err != nil {
        t.Fatalf("get state error: %v", err)
    }
    currentBlock := dex.blockchain.CurrentBlock()
    if currentBlock.NumberU64() != 1 {
        t.Fatalf("unexpected current block number %v", currentBlock.NumberU64())
    }

    currentNonce := currentState.GetNonce(address)
    if currentNonce != firstBlocksInfo[0].Nonce+1 {
        t.Fatalf("unexpected pending state nonce %v", currentNonce)
    }

    balance := currentState.GetBalance(address)
    if new(big.Int).Add(balance, &firstBlocksInfo[0].Cost).Cmp(big.NewInt(50000000000000000)) != 0 {
        t.Fatalf("unexpected pending state balance %v", balance)
    }
}

func TestNumChainsChange(t *testing.T) {
    key, err := crypto.GenerateKey()
    if err != nil {
        t.Fatalf("hex to ecdsa error: %v", err)
    }

    params.TestnetChainConfig.Dexcon.Owner = crypto.PubkeyToAddress(key.PublicKey)

    dex, err := newTestDexonWithGenesis(key)
    if err != nil {
        t.Fatalf("new test dexon error: %v", err)
    }

    chainID := new(big.Int).Mod(crypto.PubkeyToAddress(key.PublicKey).Big(),
        big.NewInt(int64(dex.chainConfig.Dexcon.NumChains)))

    root := dex.blockchain.CurrentBlock().Root()
    dex.app.chainRoot.Store(uint32(chainID.Uint64()), &root)

    abiObject, err := abi.JSON(strings.NewReader(vm.GovernanceABIJSON))
    if err != nil {
        t.Fatalf("get abi object fail: %v", err)
    }

    // Update config in round 1 and height 1.
    // Config will affect in round 3.
    input, err := abiObject.Pack("updateConfiguration",
        new(big.Int).Mul(big.NewInt(1e18), big.NewInt(1e4)),
        big.NewInt(2000),
        big.NewInt(0.1875*1e8),
        big.NewInt(1e18),
        big.NewInt(1e18),
        big.NewInt(9000000),
        big.NewInt(3),
        big.NewInt(500),
        big.NewInt(5000),
        big.NewInt(1),
        big.NewInt(700000),
        big.NewInt(5),
        big.NewInt(5),
        big.NewInt(700000),
        big.NewInt(1000),
        []*big.Int{big.NewInt(1), big.NewInt(1), big.NewInt(1)})
    if err != nil {
        t.Fatalf("updateConfiguration abiObject pack error: %v", err)
    }

    block, err := prepareConfirmedBlockWithTxAndData(dex, key, [][]byte{input}, 1)
    if err != nil {
        t.Fatalf("prepare block error: %v", err)
    }
    dex.app.BlockDelivered(block.Hash, block.Position,
        coreTypes.FinalizationResult{
            Timestamp: time.Now(),
            Height:    1,
        })

    // Snapshot round on round 1 and height 2.
    input, err = abiObject.Pack("snapshotRound", big.NewInt(1), big.NewInt(1))
    if err != nil {
        t.Fatalf("abiObject pack error: %v", err)
    }

    block, err = prepareConfirmedBlockWithTxAndData(dex, key, [][]byte{input}, 1)
    if err != nil {
        t.Fatalf("prepare block error: %v", err)
    }
    dex.app.BlockDelivered(block.Hash, block.Position,
        coreTypes.FinalizationResult{
            Timestamp: time.Now(),
            Height:    2,
        })

    // Make round 1 height 2 block write into chain.
    block, err = prepareConfirmedBlockWithTxAndData(dex, key, nil, 1)
    if err != nil {
        t.Fatalf("prepare block error: %v", err)
    }
    dex.app.BlockDelivered(block.Hash, block.Position,
        coreTypes.FinalizationResult{
            Timestamp: time.Now(),
            Height:    3,
        })

    // Round 2 will keep prepare tx as usual.
    block, err = prepareConfirmedBlockWithTxAndData(dex, key, [][]byte{{1}}, 2)
    if err != nil {
        t.Fatalf("prepare block error: %v", err)
    }
    if block.Payload == nil {
        t.Fatalf("payload should not be nil")
    }
    dex.app.BlockDelivered(block.Hash, block.Position,
        coreTypes.FinalizationResult{
            Timestamp: time.Now(),
            Height:    4,
        })

    // It will prepare empty payload until witness any block in round 3.
    block, err = prepareConfirmedBlockWithTxAndData(dex, key, [][]byte{{1}}, 3)
    if err != nil {
        t.Fatalf("prepare block error: %v", err)
    }
    if block.Payload != nil {
        t.Fatalf("payload should be nil but %v", block.Payload)
    }

    // Test non-empty payload.
    block.Payload = []byte{1}
    if status := dex.app.VerifyBlock(block); status != coreTypes.VerifyInvalidBlock {
        t.Fatalf("unexpected verify status %v", status)
    }
    block.Payload = nil

    dex.app.BlockDelivered(block.Hash, block.Position,
        coreTypes.FinalizationResult{
            Timestamp: time.Now(),
            Height:    5,
        })

    // Empty block in round 3 has been delivered.
    // Now can prepare payload as usual.
    block, err = prepareConfirmedBlockWithTxAndData(dex, key, [][]byte{{1}}, 3)
    if err != nil {
        t.Fatalf("prepare block error: %v", err)
    }
    if block.Payload == nil {
        t.Fatalf("payload should not be nil")
    }
    dex.app.BlockDelivered(block.Hash, block.Position,
        coreTypes.FinalizationResult{
            Timestamp: time.Now(),
            Height:    6,
        })
}

func BenchmarkBlockDeliveredFlow(b *testing.B) {
    key, err := crypto.GenerateKey()
    if err != nil {
        b.Fatalf("hex to ecdsa error: %v", err)
        return
    }

    dex, err := newTestDexonWithGenesis(key)
    if err != nil {
        b.Fatalf("new test dexon error: %v", err)
    }

    b.ResetTimer()
    for i := 1; i <= b.N; i++ {
        blocksInfo, err := prepareConfirmedBlocks(dex, []*ecdsa.PrivateKey{key}, 100)
        if err != nil {
            b.Fatalf("preapare confirmed block error: %v", err)
            return
        }

        dex.app.BlockDelivered(blocksInfo[0].Block.Hash, blocksInfo[0].Block.Position,
            coreTypes.FinalizationResult{
                Timestamp: time.Now(),
                Height:    uint64(i),
            })
    }
}

func newTestDexonWithGenesis(allocKey *ecdsa.PrivateKey) (*Dexon, error) {
    db := ethdb.NewMemDatabase()

    key, err := crypto.GenerateKey()
    if err != nil {
        panic(err)
    }

    testBankAddress := crypto.PubkeyToAddress(allocKey.PublicKey)
    genesis := core.DefaultTestnetGenesisBlock()
    genesis.Alloc = core.GenesisAlloc{
        testBankAddress: {
            Balance:   big.NewInt(100000000000000000),
            Staked:    big.NewInt(50000000000000000),
            PublicKey: crypto.FromECDSAPub(&key.PublicKey),
        },
    }
    chainConfig, _, err := core.SetupGenesisBlock(db, genesis)
    if err != nil {
        return nil, err
    }

    config := Config{PrivateKey: key}
    vmConfig := vm.Config{IsBlockProposer: true}

    engine := dexcon.New()

    dex := &Dexon{
        chainDb:     db,
        chainConfig: chainConfig,
        networkID:   config.NetworkId,
        engine:      engine,
    }

    dex.blockchain, err = core.NewBlockChain(db, nil, chainConfig, engine, vmConfig, nil)
    if err != nil {
        return nil, err
    }

    txPoolConfig := core.DefaultTxPoolConfig
    dex.txPool = core.NewTxPool(txPoolConfig, chainConfig, dex.blockchain, true)

    dex.APIBackend = &DexAPIBackend{dex, nil}
    dex.governance = NewDexconGovernance(dex.APIBackend, dex.chainConfig, config.PrivateKey)
    engine.SetGovStateFetcher(dex.governance)
    dex.app = NewDexconApp(dex.txPool, dex.blockchain, dex.governance, db, &config)

    return dex, nil
}

// Add tx into tx pool.
func addTx(dex *Dexon, nonce int, signer types.Signer, key *ecdsa.PrivateKey) (
    *types.Transaction, error) {
    tx := types.NewTransaction(
        uint64(nonce),
        common.BytesToAddress([]byte{9}),
        big.NewInt(int64(nonce*1)),
        params.TxGas,
        big.NewInt(1),
        nil)
    tx, err := types.SignTx(tx, signer, key)
    if err != nil {
        return nil, err
    }

    if err := dex.txPool.AddRemote(tx); err != nil {
        return nil, err
    }

    return tx, nil
}

// Prepare data with given transaction number and start nonce.
func prepareData(dex *Dexon, key *ecdsa.PrivateKey, startNonce, txNum int) (
    payload []byte, witness coreTypes.Witness, cost big.Int, nonce uint64, err error) {
    signer := types.NewEIP155Signer(dex.chainConfig.ChainID)
    chainID := new(big.Int).Mod(crypto.PubkeyToAddress(key.PublicKey).Big(),
        big.NewInt(int64(dex.chainConfig.Dexcon.NumChains)))

    for n := startNonce; n < startNonce+txNum; n++ {
        var tx *types.Transaction
        tx, err = addTx(dex, n, signer, key)
        if err != nil {
            return
        }

        cost.Add(&cost, tx.Cost())
        nonce = uint64(n)
    }

    payload, err = dex.app.PreparePayload(coreTypes.Position{ChainID: uint32(chainID.Uint64())})
    if err != nil {
        return
    }

    witness, err = dex.app.PrepareWitness(0)
    if err != nil {
        return
    }

    return
}

func prepareConfirmedBlockWithTxAndData(dex *Dexon, key *ecdsa.PrivateKey, data [][]byte, round uint64) (
    Block *coreTypes.Block, err error) {
    address := crypto.PubkeyToAddress(key.PublicKey)
    numChains := dex.governance.GetGovStateHelperAtRound(round).Configuration().NumChains
    chainID := new(big.Int).Mod(address.Big(), big.NewInt(int64(numChains)))

    for _, d := range data {
        // Prepare one block for pending.
        nonce := dex.txPool.State().GetNonce(address)
        signer := types.NewEIP155Signer(dex.chainConfig.ChainID)
        tx := types.NewTransaction(uint64(nonce), vm.GovernanceContractAddress, big.NewInt(0), params.TxGas*2,
            big.NewInt(1), d)
        tx, err = types.SignTx(tx, signer, key)
        if err != nil {
            return nil, err
        }

        dex.txPool.AddRemote(tx)
        if err != nil {
            return nil, err
        }
    }
    var (
        payload []byte
        witness coreTypes.Witness
    )

    payload, err = dex.app.PreparePayload(coreTypes.Position{Round: round, ChainID: uint32(chainID.Uint64())})
    if err != nil {
        return
    }

    witness, err = dex.app.PrepareWitness(0)
    if err != nil {
        return nil, err
    }

    block := &coreTypes.Block{}
    block.Hash = coreCommon.NewRandomHash()
    block.Witness = witness
    block.Payload = payload
    block.Position.ChainID = uint32(chainID.Uint64())
    block.Position.Round = round
    block.ProposerID = coreTypes.NodeID{coreCommon.Hash{1, 2, 3}}

    status := dex.app.VerifyBlock(block)
    if status != coreTypes.VerifyOK {
        err = fmt.Errorf("verify fail: %v", status)
        return nil, err
    }

    dex.app.BlockConfirmed(*block)
    return block, nil
}

func prepareDataWithoutTxPool(dex *Dexon, key *ecdsa.PrivateKey, startNonce, txNum int) (
    payload []byte, witness coreTypes.Witness, err error) {
    signer := types.NewEIP155Signer(dex.chainConfig.ChainID)

    var transactions types.Transactions
    for n := startNonce; n < startNonce+txNum; n++ {
        tx := types.NewTransaction(
            uint64(n),
            common.BytesToAddress([]byte{9}),
            big.NewInt(int64(n*1)),
            params.TxGas,
            big.NewInt(1),
            nil)
        tx, err = types.SignTx(tx, signer, key)
        if err != nil {
            return
        }
        transactions = append(transactions, tx)
    }

    payload, err = rlp.EncodeToBytes(&transactions)
    if err != nil {
        return
    }

    witness, err = dex.app.PrepareWitness(0)
    if err != nil {
        return
    }

    return
}

func prepareConfirmedBlocks(dex *Dexon, keys []*ecdsa.PrivateKey, txNum int) (blocksInfo []struct {
    Block *coreTypes.Block
    Cost  big.Int
    Nonce uint64
}, err error) {
    for _, key := range keys {
        address := crypto.PubkeyToAddress(key.PublicKey)
        chainID := new(big.Int).Mod(address.Big(),
            big.NewInt(int64(dex.chainConfig.Dexcon.NumChains)))

        // Prepare one block for pending.
        var (
            payload []byte
            witness coreTypes.Witness
            cost    big.Int
            nonce   uint64
        )
        startNonce := dex.txPool.State().GetNonce(address)
        payload, witness, cost, nonce, err = prepareData(dex, key, int(startNonce), txNum)
        if err != nil {
            err = fmt.Errorf("prepare data error: %v", err)
            return
        }

        block := &coreTypes.Block{}
        block.Hash = coreCommon.NewRandomHash()
        block.Witness = witness
        block.Payload = payload
        block.Position.ChainID = uint32(chainID.Uint64())
        block.ProposerID = coreTypes.NodeID{coreCommon.Hash{1, 2, 3}}

        status := dex.app.VerifyBlock(block)
        if status != coreTypes.VerifyOK {
            err = fmt.Errorf("verify fail: %v", status)
            return
        }

        dex.app.BlockConfirmed(*block)

        blocksInfo = append(blocksInfo, struct {
            Block *coreTypes.Block
            Cost  big.Int
            Nonce uint64
        }{Block: block, Cost: cost, Nonce: nonce})
    }

    return
}