OSDN Git Service

Set chain Tx Gas 0 (#409)
[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 )
17
18 func TestBuildBtmTxChain(t *testing.T) {
19         chainTxUtxoNum = 3
20         m := mockAccountManager(t)
21         cases := []struct {
22                 inputUtxo  []uint64
23                 wantInput  [][]uint64
24                 wantOutput [][]uint64
25                 wantUtxo   uint64
26         }{
27                 {
28                         inputUtxo:  []uint64{5},
29                         wantInput:  [][]uint64{},
30                         wantOutput: [][]uint64{},
31                         wantUtxo:   5,
32                 },
33                 {
34                         inputUtxo: []uint64{5, 4},
35                         wantInput: [][]uint64{
36                                 []uint64{5, 4},
37                         },
38                         wantOutput: [][]uint64{
39                                 []uint64{9},
40                         },
41                         wantUtxo: 9,
42                 },
43                 {
44                         inputUtxo: []uint64{5, 4, 1, 1},
45                         wantInput: [][]uint64{
46                                 []uint64{5, 4, 1, 1},
47                                 []uint64{1, 9},
48                         },
49                         wantOutput: [][]uint64{
50                                 []uint64{11},
51                                 []uint64{10},
52                         },
53                         wantUtxo: 11,
54                 },
55                 {
56                         inputUtxo: []uint64{22, 123, 53, 234, 23, 4, 2423, 24, 23, 43, 34, 234, 234, 24, 11, 16, 33, 59, 73, 89, 66},
57                         wantInput: [][]uint64{
58                                 []uint64{22, 123, 53, 234, 23, 4, 2423, 24, 23, 43, 34, 234, 234, 24, 11, 16, 33, 59, 73, 89},
59                                 []uint64{66, 3779},
60                         },
61                         wantOutput: [][]uint64{
62                                 []uint64{3779},
63                                 []uint64{3845},
64                         },
65                         wantUtxo: 3845,
66                 },
67         }
68
69         acct, err := m.Manager.Create([]chainkd.XPub{testutil.TestXPub}, 1, "testAccount", signers.BIP0044)
70         if err != nil {
71                 t.Fatal(err)
72         }
73
74         acp, err := m.Manager.CreateAddress(acct.ID, false)
75         if err != nil {
76                 t.Fatal(err)
77         }
78
79         for caseIndex, c := range cases {
80                 utxos := []*acc.UTXO{}
81                 for _, amount := range c.inputUtxo {
82                         utxos = append(utxos, &acc.UTXO{
83                                 Amount:         amount,
84                                 AssetID:        *consensus.BTMAssetID,
85                                 Address:        acp.Address,
86                                 ControlProgram: acp.ControlProgram,
87                         })
88                 }
89
90                 tpls, gotUtxo, err := m.Manager.BuildBtmTxChain(utxos, acct.Signer)
91                 if err != nil {
92                         t.Fatal(err)
93                 }
94
95                 for i, tpl := range tpls {
96                         gotInput := []uint64{}
97                         for _, input := range tpl.Transaction.Inputs {
98                                 gotInput = append(gotInput, input.Amount())
99                         }
100
101                         gotOutput := []uint64{}
102                         for _, output := range tpl.Transaction.Outputs {
103                                 gotOutput = append(gotOutput, output.AssetAmount().Amount)
104                         }
105
106                         if !testutil.DeepEqual(c.wantInput[i], gotInput) {
107                                 t.Errorf("case %d tx %d input got %d want %d", caseIndex, i, gotInput, c.wantInput[i])
108                         }
109                         if !testutil.DeepEqual(c.wantOutput[i], gotOutput) {
110                                 t.Errorf("case %d tx %d output got %d want %d", caseIndex, i, gotOutput, c.wantOutput[i])
111                         }
112                 }
113
114                 if c.wantUtxo != gotUtxo.Amount {
115                         t.Errorf("case %d got utxo=%d want utxo=%d", caseIndex, gotUtxo.Amount, c.wantUtxo)
116                 }
117         }
118 }