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"
14 //chainTxUtxoNum maximum utxo quantity in a tx
16 //chainTxMergeGas chain tx gas
17 chainTxMergeGas = uint64(10000000)
20 // func TestReserveBtmUtxoChain(t *testing.T) {
21 // dirPath, err := ioutil.TempDir(".", "")
25 // defer os.RemoveAll(dirPath)
26 // testDB := dbm.NewDB("testdb", "memdb", dirPath)
27 // store := database.NewAccountStore(testDB)
29 // utxos := []*acc.UTXO{}
30 // m := mockAccountManager(t)
31 // for i := uint64(1); i <= 20; i++ {
33 // OutputID: bc.Hash{V0: i},
34 // AccountID: "TestAccountID",
35 // AssetID: *consensus.BTMAssetID,
36 // Amount: i * chainTxMergeGas,
38 // utxos = append(utxos, utxo)
40 // data, err := json.Marshal(utxo)
45 // store.SetStandardUTXO(utxo.OutputID, data)
48 // cases := []struct {
54 // amount: 1 * chainTxMergeGas,
58 // amount: 888888 * chainTxMergeGas,
63 // amount: 7 * chainTxMergeGas,
64 // want: []uint64{4, 3, 1},
67 // amount: 15 * chainTxMergeGas,
68 // want: []uint64{5, 4, 3, 2, 1, 6},
71 // amount: 163 * chainTxMergeGas,
72 // want: []uint64{20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 2, 1, 3},
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)
80 // if err != nil != c.err {
81 // t.Fatalf("case %d got err %v want err = %v", i, err, c.err)
85 // for _, utxo := range utxos {
86 // got = append(got, utxo.Amount/chainTxMergeGas)
89 // if !testutil.DeepEqual(got, c.want) {
90 // t.Fatalf("case %d got %d want %d", i, got, c.want)
95 func TestBuildBtmTxChain(t *testing.T) {
97 m := mockAccountManager(t)
101 wantOutput [][]uint64
105 inputUtxo: []uint64{5},
106 wantInput: [][]uint64{},
107 wantOutput: [][]uint64{},
108 wantUtxo: 5 * chainTxMergeGas,
111 inputUtxo: []uint64{5, 4},
112 wantInput: [][]uint64{
115 wantOutput: [][]uint64{
118 wantUtxo: 8 * chainTxMergeGas,
121 inputUtxo: []uint64{5, 4, 1, 1},
122 wantInput: [][]uint64{
123 []uint64{5, 4, 1, 1},
126 wantOutput: [][]uint64{
130 wantUtxo: 10 * chainTxMergeGas,
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},
139 []uint64{234, 24, 197},
140 []uint64{260, 2469, 310},
143 wantOutput: [][]uint64{
152 wantUtxo: 3494 * chainTxMergeGas,
156 acct, err := m.Manager.Create([]chainkd.XPub{testutil.TestXPub}, 1, "testAccount", signers.BIP0044)
161 acp, err := m.Manager.CreateAddress(acct.ID, false)
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,
177 tpls, gotUtxo, err := m.Manager.BuildBtmTxChain(utxos, acct.Signer)
182 for i, tpl := range tpls {
183 gotInput := []uint64{}
184 for _, input := range tpl.Transaction.Inputs {
185 gotInput = append(gotInput, input.Amount()/chainTxMergeGas)
188 gotOutput := []uint64{}
189 for _, output := range tpl.Transaction.Outputs {
190 gotOutput = append(gotOutput, output.AssetAmount().Amount/chainTxMergeGas)
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])
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])
201 if c.wantUtxo != gotUtxo.Amount {
202 t.Errorf("case %d got utxo=%d want utxo=%d", caseIndex, gotUtxo.Amount, c.wantUtxo)