X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=blobdiff_plain;f=account%2Fbuilder_test.go;h=c5f2cbd8c6b56c108ac273a66283421d1612c77f;hp=9583bcc0220345c25ffea9d5fc8ac9d88b877b85;hb=51100c2a5afb320a9b16674f8c66b067fe760eb3;hpb=db158dcf09436b003defd333f1a665e7e051d820 diff --git a/account/builder_test.go b/account/builder_test.go index 9583bcc0..c5f2cbd8 100644 --- a/account/builder_test.go +++ b/account/builder_test.go @@ -1,200 +1,15 @@ package account import ( - "encoding/json" "testing" "time" - "github.com/vapor/blockchain/signers" "github.com/vapor/blockchain/txbuilder" "github.com/vapor/consensus" - "github.com/vapor/crypto/ed25519/chainkd" "github.com/vapor/protocol/bc" "github.com/vapor/testutil" ) -func TestReserveBtmUtxoChain(t *testing.T) { - chainTxUtxoNum = 3 - utxos := []*UTXO{} - m := mockAccountManager(t) - for i := uint64(1); i <= 20; i++ { - utxo := &UTXO{ - OutputID: bc.Hash{V0: i}, - AccountID: "TestAccountID", - AssetID: *consensus.BTMAssetID, - Amount: i * chainTxMergeGas, - } - utxos = append(utxos, utxo) - - data, err := json.Marshal(utxo) - if err != nil { - t.Fatal(err) - } - - m.db.Set(StandardUTXOKey(utxo.OutputID), data) - } - - cases := []struct { - amount uint64 - want []uint64 - err bool - }{ - { - amount: 1 * chainTxMergeGas, - want: []uint64{1}, - }, - { - amount: 888888 * chainTxMergeGas, - want: []uint64{}, - err: true, - }, - { - amount: 7 * chainTxMergeGas, - want: []uint64{4, 3, 1}, - }, - { - amount: 15 * chainTxMergeGas, - want: []uint64{5, 4, 3, 2, 1, 6}, - }, - { - amount: 163 * chainTxMergeGas, - want: []uint64{20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 2, 1, 3}, - }, - } - - for i, c := range cases { - m.utxoKeeper.expireReservation(time.Unix(999999999, 0)) - utxos, err := m.reserveBtmUtxoChain(&txbuilder.TemplateBuilder{}, "TestAccountID", c.amount, false) - - if err != nil != c.err { - t.Fatalf("case %d got err %v want err = %v", i, err, c.err) - } - - got := []uint64{} - for _, utxo := range utxos { - got = append(got, utxo.Amount/chainTxMergeGas) - } - - if !testutil.DeepEqual(got, c.want) { - t.Fatalf("case %d got %d want %d", i, got, c.want) - } - } - -} - -func TestBuildBtmTxChain(t *testing.T) { - chainTxUtxoNum = 3 - m := mockAccountManager(t) - cases := []struct { - inputUtxo []uint64 - wantInput [][]uint64 - wantOutput [][]uint64 - wantUtxo uint64 - }{ - { - inputUtxo: []uint64{5}, - wantInput: [][]uint64{}, - wantOutput: [][]uint64{}, - wantUtxo: 5 * chainTxMergeGas, - }, - { - inputUtxo: []uint64{5, 4}, - wantInput: [][]uint64{ - []uint64{5, 4}, - }, - wantOutput: [][]uint64{ - []uint64{8}, - }, - wantUtxo: 8 * chainTxMergeGas, - }, - { - inputUtxo: []uint64{5, 4, 1, 1}, - wantInput: [][]uint64{ - []uint64{5, 4, 1}, - []uint64{1, 9}, - }, - wantOutput: [][]uint64{ - []uint64{9}, - []uint64{9}, - }, - wantUtxo: 9 * chainTxMergeGas, - }, - { - inputUtxo: []uint64{22, 123, 53, 234, 23, 4, 2423, 24, 23, 43, 34, 234, 234, 24}, - wantInput: [][]uint64{ - []uint64{22, 123, 53}, - []uint64{234, 23, 4}, - []uint64{2423, 24, 23}, - []uint64{43, 34, 234}, - []uint64{234, 24, 197}, - []uint64{260, 2469, 310}, - []uint64{454, 3038}, - }, - wantOutput: [][]uint64{ - []uint64{197}, - []uint64{260}, - []uint64{2469}, - []uint64{310}, - []uint64{454}, - []uint64{3038}, - []uint64{3491}, - }, - wantUtxo: 3491 * chainTxMergeGas, - }, - } - - acct, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "testAccount", signers.BIP0044) - if err != nil { - t.Fatal(err) - } - - acp, err := m.CreateAddress(acct.ID, false) - if err != nil { - t.Fatal(err) - } - - for caseIndex, c := range cases { - utxos := []*UTXO{} - for _, amount := range c.inputUtxo { - utxos = append(utxos, &UTXO{ - Amount: amount * chainTxMergeGas, - AssetID: *consensus.BTMAssetID, - Address: acp.Address, - ControlProgram: acp.ControlProgram, - }) - } - - tpls, gotUtxo, err := m.buildBtmTxChain(utxos, acct.Signer) - if err != nil { - t.Fatal(err) - } - - for i, tpl := range tpls { - gotInput := []uint64{} - for _, input := range tpl.Transaction.Inputs { - gotInput = append(gotInput, input.Amount()/chainTxMergeGas) - } - - gotOutput := []uint64{} - for _, output := range tpl.Transaction.Outputs { - gotOutput = append(gotOutput, output.Amount/chainTxMergeGas) - } - - if !testutil.DeepEqual(c.wantInput[i], gotInput) { - t.Fatalf("case %d tx %d input got %d want %d", caseIndex, i, gotInput, c.wantInput[i]) - } - if !testutil.DeepEqual(c.wantOutput[i], gotOutput) { - t.Fatalf("case %d tx %d output got %d want %d", caseIndex, i, gotOutput, c.wantOutput[i]) - } - } - - if c.wantUtxo != gotUtxo.Amount { - t.Fatalf("case %d got utxo=%d want utxo=%d", caseIndex, gotUtxo.Amount, c.wantUtxo) - } - } - -} - func TestMergeSpendAction(t *testing.T) { testBTM := &bc.AssetID{} if err := testBTM.UnmarshalText([]byte("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); err != nil { @@ -586,3 +401,66 @@ func TestCalcMergeGas(t *testing.T) { } } } + +func TestReserveBtmUtxoChain(t *testing.T) { + chainTxUtxoNum = 3 + utxos := []*UTXO{} + m := mockAccountManager(t) + for i := uint64(1); i <= 20; i++ { + utxo := &UTXO{ + OutputID: bc.Hash{V0: i}, + AccountID: "TestAccountID", + AssetID: *consensus.BTMAssetID, + Amount: i * chainTxMergeGas, + } + utxos = append(utxos, utxo) + + m.store.SetStandardUTXO(utxo.OutputID, utxo) + } + + cases := []struct { + amount uint64 + want []uint64 + err bool + }{ + { + amount: 1 * chainTxMergeGas, + want: []uint64{1}, + }, + { + amount: 888888 * chainTxMergeGas, + want: []uint64{}, + err: true, + }, + { + amount: 7 * chainTxMergeGas, + want: []uint64{4, 3, 1}, + }, + { + amount: 15 * chainTxMergeGas, + want: []uint64{5, 4, 3, 2, 1, 6}, + }, + { + amount: 163 * chainTxMergeGas, + want: []uint64{20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 2, 1, 3}, + }, + } + + for i, c := range cases { + m.utxoKeeper.expireReservation(time.Unix(999999999, 0)) + utxos, err := m.reserveBtmUtxoChain(&txbuilder.TemplateBuilder{}, "TestAccountID", c.amount, false) + + if err != nil != c.err { + t.Fatalf("case %d got err %v want err = %v", i, err, c.err) + } + + got := []uint64{} + for _, utxo := range utxos { + got = append(got, utxo.Amount/chainTxMergeGas) + } + + if !testutil.DeepEqual(got, c.want) { + t.Fatalf("case %d got %d want %d", i, got, c.want) + } + } +}