OSDN Git Service

4328dbc2cb8bbdb78ae8879d7e18e17680003f18
[bytom/vapor.git] / test / builder_test.go
1 package test
2
3 import (
4         "testing"
5
6         acc "github.com/vapor/account"
7         "github.com/vapor/blockchain/signers"
8         "github.com/vapor/consensus"
9         "github.com/vapor/crypto/ed25519/chainkd"
10         "github.com/vapor/testutil"
11 )
12
13 var (
14         //chainTxUtxoNum maximum utxo quantity in a tx
15         chainTxUtxoNum = 5
16         //chainTxMergeGas chain tx gas
17         chainTxMergeGas = uint64(10000000)
18 )
19
20 func TestBuildBtmTxChain(t *testing.T) {
21         chainTxUtxoNum = 3
22         m := mockAccountManager(t)
23         cases := []struct {
24                 inputUtxo  []uint64
25                 wantInput  [][]uint64
26                 wantOutput [][]uint64
27                 wantUtxo   uint64
28         }{
29                 {
30                         inputUtxo:  []uint64{5},
31                         wantInput:  [][]uint64{},
32                         wantOutput: [][]uint64{},
33                         wantUtxo:   5 * chainTxMergeGas,
34                 },
35                 {
36                         inputUtxo: []uint64{5, 4},
37                         wantInput: [][]uint64{
38                                 []uint64{5, 4},
39                         },
40                         wantOutput: [][]uint64{
41                                 []uint64{8},
42                         },
43                         wantUtxo: 8 * chainTxMergeGas,
44                 },
45                 {
46                         inputUtxo: []uint64{5, 4, 1, 1},
47                         wantInput: [][]uint64{
48                                 []uint64{5, 4, 1, 1},
49                                 []uint64{1, 9},
50                         },
51                         wantOutput: [][]uint64{
52                                 []uint64{10},
53                                 []uint64{9},
54                         },
55                         wantUtxo: 10 * chainTxMergeGas,
56                 },
57                 {
58                         inputUtxo: []uint64{22, 123, 53, 234, 23, 4, 2423, 24, 23, 43, 34, 234, 234, 24},
59                         wantInput: [][]uint64{
60                                 []uint64{22, 123, 53, 234, 23},
61                                 []uint64{4, 2423, 24, 23, 43},
62                                 []uint64{34, 234, 234, 24, 454},
63                                 []uint64{2516, 979},
64                                 []uint64{234, 24, 197},
65                                 []uint64{260, 2469, 310},
66                                 []uint64{454, 3038},
67                         },
68                         wantOutput: [][]uint64{
69                                 []uint64{454},
70                                 []uint64{2516},
71                                 []uint64{979},
72                                 []uint64{3494},
73                                 []uint64{454},
74                                 []uint64{3038},
75                                 []uint64{3491},
76                         },
77                         wantUtxo: 3494 * chainTxMergeGas,
78                 },
79         }
80
81         acct, err := m.Manager.Create([]chainkd.XPub{testutil.TestXPub}, 1, "testAccount", signers.BIP0044)
82         if err != nil {
83                 t.Fatal(err)
84         }
85
86         acp, err := m.Manager.CreateAddress(acct.ID, false)
87         if err != nil {
88                 t.Fatal(err)
89         }
90
91         for caseIndex, c := range cases {
92                 utxos := []*acc.UTXO{}
93                 for _, amount := range c.inputUtxo {
94                         utxos = append(utxos, &acc.UTXO{
95                                 Amount:         amount * chainTxMergeGas,
96                                 AssetID:        *consensus.BTMAssetID,
97                                 Address:        acp.Address,
98                                 ControlProgram: acp.ControlProgram,
99                         })
100                 }
101
102                 tpls, gotUtxo, err := m.Manager.BuildBtmTxChain(utxos, acct.Signer)
103                 if err != nil {
104                         t.Fatal(err)
105                 }
106
107                 for i, tpl := range tpls {
108                         gotInput := []uint64{}
109                         for _, input := range tpl.Transaction.Inputs {
110                                 gotInput = append(gotInput, input.Amount()/chainTxMergeGas)
111                         }
112
113                         gotOutput := []uint64{}
114                         for _, output := range tpl.Transaction.Outputs {
115                                 gotOutput = append(gotOutput, output.AssetAmount().Amount/chainTxMergeGas)
116                         }
117
118                         if !testutil.DeepEqual(c.wantInput[i], gotInput) {
119                                 t.Errorf("case %d tx %d input got %d want %d", caseIndex, i, gotInput, c.wantInput[i])
120                         }
121                         if !testutil.DeepEqual(c.wantOutput[i], gotOutput) {
122                                 t.Errorf("case %d tx %d output got %d want %d", caseIndex, i, gotOutput, c.wantOutput[i])
123                         }
124                 }
125
126                 if c.wantUtxo != gotUtxo.Amount {
127                         t.Errorf("case %d got utxo=%d want utxo=%d", caseIndex, gotUtxo.Amount, c.wantUtxo)
128                 }
129         }
130 }