OSDN Git Service

update
[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 TestReserveBtmUtxoChain(t *testing.T) {
21 //      dirPath, err := ioutil.TempDir(".", "")
22 //      if err != nil {
23 //              t.Fatal(err)
24 //      }
25 //      defer os.RemoveAll(dirPath)
26 //      testDB := dbm.NewDB("testdb", "memdb", dirPath)
27 //      store := database.NewAccountStore(testDB)
28 //      chainTxUtxoNum = 3
29 //      utxos := []*acc.UTXO{}
30 //      m := mockAccountManager(t)
31 //      for i := uint64(1); i <= 20; i++ {
32 //              utxo := &acc.UTXO{
33 //                      OutputID:  bc.Hash{V0: i},
34 //                      AccountID: "TestAccountID",
35 //                      AssetID:   *consensus.BTMAssetID,
36 //                      Amount:    i * chainTxMergeGas,
37 //              }
38 //              utxos = append(utxos, utxo)
39
40 //              data, err := json.Marshal(utxo)
41 //              if err != nil {
42 //                      t.Fatal(err)
43 //              }
44
45 //              store.SetStandardUTXO(utxo.OutputID, data)
46 //      }
47
48 //      cases := []struct {
49 //              amount uint64
50 //              want   []uint64
51 //              err    bool
52 //      }{
53 //              {
54 //                      amount: 1 * chainTxMergeGas,
55 //                      want:   []uint64{1},
56 //              },
57 //              {
58 //                      amount: 888888 * chainTxMergeGas,
59 //                      want:   []uint64{},
60 //                      err:    true,
61 //              },
62 //              {
63 //                      amount: 7 * chainTxMergeGas,
64 //                      want:   []uint64{4, 3, 1},
65 //              },
66 //              {
67 //                      amount: 15 * chainTxMergeGas,
68 //                      want:   []uint64{5, 4, 3, 2, 1, 6},
69 //              },
70 //              {
71 //                      amount: 163 * chainTxMergeGas,
72 //                      want:   []uint64{20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 2, 1, 3},
73 //              },
74 //      }
75
76 //      for i, c := range cases {
77 //              m.utxoKeeper.expireReservation(time.Unix(999999999, 0))
78 //              utxos, err := m.Manager.ReserveBtmUtxoChain(&txbuilder.TemplateBuilder{}, "TestAccountID", c.amount, false)
79
80 //              if err != nil != c.err {
81 //                      t.Fatalf("case %d got err %v want err = %v", i, err, c.err)
82 //              }
83
84 //              got := []uint64{}
85 //              for _, utxo := range utxos {
86 //                      got = append(got, utxo.Amount/chainTxMergeGas)
87 //              }
88
89 //              if !testutil.DeepEqual(got, c.want) {
90 //                      t.Fatalf("case %d got %d want %d", i, got, c.want)
91 //              }
92 //      }
93 // }
94
95 func TestBuildBtmTxChain(t *testing.T) {
96         chainTxUtxoNum = 3
97         m := mockAccountManager(t)
98         cases := []struct {
99                 inputUtxo  []uint64
100                 wantInput  [][]uint64
101                 wantOutput [][]uint64
102                 wantUtxo   uint64
103         }{
104                 {
105                         inputUtxo:  []uint64{5},
106                         wantInput:  [][]uint64{},
107                         wantOutput: [][]uint64{},
108                         wantUtxo:   5 * chainTxMergeGas,
109                 },
110                 {
111                         inputUtxo: []uint64{5, 4},
112                         wantInput: [][]uint64{
113                                 []uint64{5, 4},
114                         },
115                         wantOutput: [][]uint64{
116                                 []uint64{8},
117                         },
118                         wantUtxo: 8 * chainTxMergeGas,
119                 },
120                 {
121                         inputUtxo: []uint64{5, 4, 1, 1},
122                         wantInput: [][]uint64{
123                                 []uint64{5, 4, 1, 1},
124                                 []uint64{1, 9},
125                         },
126                         wantOutput: [][]uint64{
127                                 []uint64{10},
128                                 []uint64{9},
129                         },
130                         wantUtxo: 10 * chainTxMergeGas,
131                 },
132                 {
133                         inputUtxo: []uint64{22, 123, 53, 234, 23, 4, 2423, 24, 23, 43, 34, 234, 234, 24},
134                         wantInput: [][]uint64{
135                                 []uint64{22, 123, 53, 234, 23},
136                                 []uint64{4, 2423, 24, 23, 43},
137                                 []uint64{34, 234, 234, 24, 454},
138                                 []uint64{2516, 979},
139                                 []uint64{234, 24, 197},
140                                 []uint64{260, 2469, 310},
141                                 []uint64{454, 3038},
142                         },
143                         wantOutput: [][]uint64{
144                                 []uint64{454},
145                                 []uint64{2516},
146                                 []uint64{979},
147                                 []uint64{3494},
148                                 []uint64{454},
149                                 []uint64{3038},
150                                 []uint64{3491},
151                         },
152                         wantUtxo: 3494 * chainTxMergeGas,
153                 },
154         }
155
156         acct, err := m.Manager.Create([]chainkd.XPub{testutil.TestXPub}, 1, "testAccount", signers.BIP0044)
157         if err != nil {
158                 t.Fatal(err)
159         }
160
161         acp, err := m.Manager.CreateAddress(acct.ID, false)
162         if err != nil {
163                 t.Fatal(err)
164         }
165
166         for caseIndex, c := range cases {
167                 utxos := []*acc.UTXO{}
168                 for _, amount := range c.inputUtxo {
169                         utxos = append(utxos, &acc.UTXO{
170                                 Amount:         amount * chainTxMergeGas,
171                                 AssetID:        *consensus.BTMAssetID,
172                                 Address:        acp.Address,
173                                 ControlProgram: acp.ControlProgram,
174                         })
175                 }
176
177                 tpls, gotUtxo, err := m.Manager.BuildBtmTxChain(utxos, acct.Signer)
178                 if err != nil {
179                         t.Fatal(err)
180                 }
181
182                 for i, tpl := range tpls {
183                         gotInput := []uint64{}
184                         for _, input := range tpl.Transaction.Inputs {
185                                 gotInput = append(gotInput, input.Amount()/chainTxMergeGas)
186                         }
187
188                         gotOutput := []uint64{}
189                         for _, output := range tpl.Transaction.Outputs {
190                                 gotOutput = append(gotOutput, output.AssetAmount().Amount/chainTxMergeGas)
191                         }
192
193                         if !testutil.DeepEqual(c.wantInput[i], gotInput) {
194                                 t.Errorf("case %d tx %d input got %d want %d", caseIndex, i, gotInput, c.wantInput[i])
195                         }
196                         if !testutil.DeepEqual(c.wantOutput[i], gotOutput) {
197                                 t.Errorf("case %d tx %d output got %d want %d", caseIndex, i, gotOutput, c.wantOutput[i])
198                         }
199                 }
200
201                 if c.wantUtxo != gotUtxo.Amount {
202                         t.Errorf("case %d got utxo=%d want utxo=%d", caseIndex, gotUtxo.Amount, c.wantUtxo)
203                 }
204         }
205 }