OSDN Git Service

fix log (#388)
[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, 11, 16, 33, 59, 73, 89, 66},
59                         wantInput: [][]uint64{
60                                 []uint64{22, 123, 53, 234, 23, 4, 2423, 24, 23, 43, 34, 234, 234, 24, 11, 16, 33, 59, 73, 89},
61                                 []uint64{66, 3778},
62                         },
63                         wantOutput: [][]uint64{
64                                 []uint64{3778},
65                                 []uint64{3843},
66                         },
67                         wantUtxo: 3843 * chainTxMergeGas,
68                 },
69         }
70
71         acct, err := m.Manager.Create([]chainkd.XPub{testutil.TestXPub}, 1, "testAccount", signers.BIP0044)
72         if err != nil {
73                 t.Fatal(err)
74         }
75
76         acp, err := m.Manager.CreateAddress(acct.ID, false)
77         if err != nil {
78                 t.Fatal(err)
79         }
80
81         for caseIndex, c := range cases {
82                 utxos := []*acc.UTXO{}
83                 for _, amount := range c.inputUtxo {
84                         utxos = append(utxos, &acc.UTXO{
85                                 Amount:         amount * chainTxMergeGas,
86                                 AssetID:        *consensus.BTMAssetID,
87                                 Address:        acp.Address,
88                                 ControlProgram: acp.ControlProgram,
89                         })
90                 }
91
92                 tpls, gotUtxo, err := m.Manager.BuildBtmTxChain(utxos, acct.Signer)
93                 if err != nil {
94                         t.Fatal(err)
95                 }
96
97                 for i, tpl := range tpls {
98                         gotInput := []uint64{}
99                         for _, input := range tpl.Transaction.Inputs {
100                                 gotInput = append(gotInput, input.Amount()/chainTxMergeGas)
101                         }
102
103                         gotOutput := []uint64{}
104                         for _, output := range tpl.Transaction.Outputs {
105                                 gotOutput = append(gotOutput, output.AssetAmount().Amount/chainTxMergeGas)
106                         }
107
108                         if !testutil.DeepEqual(c.wantInput[i], gotInput) {
109                                 t.Errorf("case %d tx %d input got %d want %d", caseIndex, i, gotInput, c.wantInput[i])
110                         }
111                         if !testutil.DeepEqual(c.wantOutput[i], gotOutput) {
112                                 t.Errorf("case %d tx %d output got %d want %d", caseIndex, i, gotOutput, c.wantOutput[i])
113                         }
114                 }
115
116                 if c.wantUtxo != gotUtxo.Amount {
117                         t.Errorf("case %d got utxo=%d want utxo=%d", caseIndex, gotUtxo.Amount, c.wantUtxo)
118                 }
119         }
120 }