X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=wallet%2Futxo_test.go;h=dadd834579809bd7d4f54714fe468ea5f996c930;hb=HEAD;hp=e5fb1b88d908c1394a93c4753649960f7485e135;hpb=6f7fe6fd7442ddcec8ee959c09b6d45639ef045f;p=bytom%2Fvapor.git diff --git a/wallet/utxo_test.go b/wallet/utxo_test.go index e5fb1b88..dadd8345 100644 --- a/wallet/utxo_test.go +++ b/wallet/utxo_test.go @@ -8,16 +8,17 @@ import ( "sort" "testing" - "github.com/vapor/account" - "github.com/vapor/consensus" - dbm "github.com/vapor/database/leveldb" - "github.com/vapor/protocol/bc" - "github.com/vapor/protocol/bc/types" - "github.com/vapor/testutil" + "github.com/bytom/vapor/account" + "github.com/bytom/vapor/consensus" + dbm "github.com/bytom/vapor/database/leveldb" + "github.com/bytom/vapor/protocol/bc" + "github.com/bytom/vapor/protocol/bc/types" + "github.com/bytom/vapor/testutil" ) func TestGetAccountUtxos(t *testing.T) { testDB := dbm.NewDB("testdb", "leveldb", "temp") + testStore := NewMockWalletStore(testDB) defer func() { testDB.Close() os.RemoveAll("temp") @@ -40,16 +41,16 @@ func TestGetAccountUtxos(t *testing.T) { }, { dbUtxos: map[string]*account.UTXO{ - string(account.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{ + string(StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{ OutputID: bc.Hash{V0: 1}, }, - string(account.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{ + string(StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{ OutputID: bc.Hash{V0: 2}, }, - string(account.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{ + string(StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{ OutputID: bc.Hash{V0: 3}, }, - string(account.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{ + string(ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{ OutputID: bc.Hash{V0: 4}, }, }, @@ -64,16 +65,16 @@ func TestGetAccountUtxos(t *testing.T) { }, { dbUtxos: map[string]*account.UTXO{ - string(account.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{ + string(StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{ OutputID: bc.Hash{V0: 1}, }, - string(account.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{ + string(StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{ OutputID: bc.Hash{V0: 2}, }, - string(account.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{ + string(StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{ OutputID: bc.Hash{V0: 3}, }, - string(account.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{ + string(ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{ OutputID: bc.Hash{V0: 4}, }, }, @@ -92,16 +93,16 @@ func TestGetAccountUtxos(t *testing.T) { }, { dbUtxos: map[string]*account.UTXO{ - string(account.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{ + string(StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{ OutputID: bc.Hash{V0: 1}, }, - string(account.StandardUTXOKey(bc.Hash{V0: 1, V1: 2})): &account.UTXO{ + string(StandardUTXOKey(bc.Hash{V0: 1, V1: 2})): &account.UTXO{ OutputID: bc.Hash{V0: 1, V1: 2}, }, - string(account.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{ + string(StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{ OutputID: bc.Hash{V0: 2}, }, - string(account.StandardUTXOKey(bc.Hash{V0: 2, V1: 2})): &account.UTXO{ + string(StandardUTXOKey(bc.Hash{V0: 2, V1: 2})): &account.UTXO{ OutputID: bc.Hash{V0: 2, V1: 2}, }, }, @@ -121,10 +122,10 @@ func TestGetAccountUtxos(t *testing.T) { }, { dbUtxos: map[string]*account.UTXO{ - string(account.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{ + string(StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{ OutputID: bc.Hash{V0: 3}, }, - string(account.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{ + string(ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{ OutputID: bc.Hash{V0: 4}, }, }, @@ -153,10 +154,10 @@ func TestGetAccountUtxos(t *testing.T) { }, { dbUtxos: map[string]*account.UTXO{ - string(account.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{ + string(StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{ OutputID: bc.Hash{V0: 3}, }, - string(account.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{ + string(ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{ OutputID: bc.Hash{V0: 4}, }, }, @@ -185,7 +186,7 @@ func TestGetAccountUtxos(t *testing.T) { }, } - w := &Wallet{DB: testDB} + w := &Wallet{Store: testStore} for i, c := range cases { for k, u := range c.dbUtxos { data, err := json.Marshal(u) @@ -195,9 +196,10 @@ func TestGetAccountUtxos(t *testing.T) { testDB.Set([]byte(k), data) } - w.AccountMgr = account.NewManager(testDB, nil) + acccountStore := NewMockAccountStore(testDB) + w.AccountMgr = account.NewManager(acccountStore, nil) w.AccountMgr.AddUnconfirmedUtxo(c.unconfirmedUtxos) - gotUtxos := w.GetAccountUtxos("", c.id, c.unconfirmed, c.isSmartContract) + gotUtxos := w.GetAccountUtxos("", c.id, c.unconfirmed, c.isSmartContract, false) if !testutil.DeepEqual(gotUtxos, c.wantUtxos) { t.Errorf("case %d: got %v want %v", i, gotUtxos, c.wantUtxos) } @@ -210,6 +212,7 @@ func TestGetAccountUtxos(t *testing.T) { func TestFilterAccountUtxo(t *testing.T) { testDB := dbm.NewDB("testdb", "leveldb", "temp") + testStore := NewMockWalletStore(testDB) defer func() { testDB.Close() os.RemoveAll("temp") @@ -227,7 +230,7 @@ func TestFilterAccountUtxo(t *testing.T) { }, { dbPrograms: map[string]*account.CtrlProgram{ - "436f6e74726163743a2a37a64a4e15a772ab43bf3f5956d0d1f353946496788e7f40d0ff1796286a6f": &account.CtrlProgram{ + "41533a013a2a37a64a4e15a772ab43bf3f5956d0d1f353946496788e7f40d0ff1796286a6f": &account.CtrlProgram{ AccountID: "testAccount", Address: "testAddress", KeyIndex: 53, @@ -287,13 +290,13 @@ func TestFilterAccountUtxo(t *testing.T) { }, { dbPrograms: map[string]*account.CtrlProgram{ - "436f6e74726163743a2a37a64a4e15a772ab43bf3f5956d0d1f353946496788e7f40d0ff1796286a6f": &account.CtrlProgram{ + "41533a013a2a37a64a4e15a772ab43bf3f5956d0d1f353946496788e7f40d0ff1796286a6f": &account.CtrlProgram{ AccountID: "testAccount", Address: "testAddress", KeyIndex: 53, Change: true, }, - "436f6e74726163743adb4d86262c12ba70d50b3ca3ae102d5682436243bd1e8c79569603f75675036a": &account.CtrlProgram{ + "41533a013adb4d86262c12ba70d50b3ca3ae102d5682436243bd1e8c79569603f75675036a": &account.CtrlProgram{ AccountID: "testAccount2", Address: "testAddress2", KeyIndex: 72, @@ -349,7 +352,12 @@ func TestFilterAccountUtxo(t *testing.T) { }, } - w := &Wallet{DB: testDB} + accountStore := NewMockAccountStore(testDB) + accountManager := account.NewManager(accountStore, nil) + w := &Wallet{ + Store: testStore, + AccountMgr: accountManager, + } for i, c := range cases { for s, p := range c.dbPrograms { data, err := json.Marshal(p) @@ -484,7 +492,7 @@ func TestTxInToUtxos(t *testing.T) { { tx: types.NewTx(types.TxData{ Inputs: []*types.TxInput{ - types.NewUnvoteInput([][]byte{}, bc.Hash{V0: 1}, bc.AssetID{V0: 1}, 1, 1, []byte{0x51}, []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269")), + types.NewVetoInput([][]byte{}, bc.Hash{V0: 1}, bc.AssetID{V0: 1}, 1, 1, []byte{0x51}, []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269")), }, Outputs: []*types.TxOutput{ types.NewIntraChainOutput(bc.AssetID{V0: 1}, 1, []byte{0x51}), @@ -493,7 +501,7 @@ func TestTxInToUtxos(t *testing.T) { statusFail: false, wantUtxos: []*account.UTXO{ &account.UTXO{ - OutputID: bc.NewHash([32]byte{0x95, 0x23, 0x06, 0xa5, 0x2f, 0xc4, 0xe2, 0x36, 0x03, 0x0f, 0xe3, 0xe6, 0xb8, 0x0b, 0xcc, 0x3c, 0x1e, 0x17, 0x3e, 0x25, 0x95, 0xd0, 0xbf, 0x08, 0x11, 0x73, 0x06, 0xd4, 0x64, 0x9c, 0xfb, 0x3b}), + OutputID: bc.NewHash([32]byte{0x7c, 0x75, 0x7f, 0x03, 0x67, 0x9b, 0xc2, 0x8f, 0x8f, 0xbd, 0x04, 0x25, 0x72, 0x42, 0x4b, 0x0b, 0x2a, 0xa4, 0x0e, 0x10, 0x0a, 0x6e, 0x99, 0x0e, 0x6d, 0x58, 0x92, 0x1d, 0xdd, 0xbe, 0xeb, 0x1a}), AssetID: bc.AssetID{V0: 1}, Amount: 1, ControlProgram: []byte{0x51}, @@ -524,7 +532,7 @@ func TestTxOutToUtxos(t *testing.T) { cases := []struct { tx *types.Tx statusFail bool - vaildHeight uint64 + blockHeight uint64 wantUtxos []*account.UTXO }{ { @@ -537,7 +545,7 @@ func TestTxOutToUtxos(t *testing.T) { }, }), statusFail: false, - vaildHeight: 98, + blockHeight: 98, wantUtxos: []*account.UTXO{ &account.UTXO{ OutputID: bc.Hash{V0: 1728735075694344097, V1: 884766857607786922, V2: 12293210594955921685, V3: 11109045974561998790}, @@ -546,7 +554,7 @@ func TestTxOutToUtxos(t *testing.T) { ControlProgram: []byte{0x51}, SourceID: bc.NewHash([32]byte{0xb4, 0x7e, 0x94, 0x31, 0x88, 0xfe, 0xd3, 0xe9, 0xac, 0x99, 0x7c, 0xfc, 0x99, 0x6d, 0xd7, 0x4d, 0x04, 0x10, 0x77, 0xcb, 0x1c, 0xf8, 0x95, 0x14, 0x00, 0xe3, 0x42, 0x00, 0x8d, 0x05, 0xec, 0xdc}), SourcePos: 0, - ValidHeight: 98, + ValidHeight: consensus.MainNetParams.CoinbasePendingBlockNumber + 98, }, }, }, @@ -564,7 +572,7 @@ func TestTxOutToUtxos(t *testing.T) { }, }), statusFail: false, - vaildHeight: 0, + blockHeight: 0, wantUtxos: []*account.UTXO{ &account.UTXO{ OutputID: bc.Hash{V0: 8675398163687045889, V1: 7549510466747714094, V2: 13693077838209211470, V3: 6878568403630757599}, @@ -614,7 +622,7 @@ func TestTxOutToUtxos(t *testing.T) { }, }), statusFail: true, - vaildHeight: 0, + blockHeight: 0, wantUtxos: []*account.UTXO{ &account.UTXO{ OutputID: bc.Hash{V0: 7067560744282869147, V1: 8991714784298240423, V2: 2595857933262917893, V3: 11490631006811252506}, @@ -648,7 +656,7 @@ func TestTxOutToUtxos(t *testing.T) { }, }), statusFail: false, - vaildHeight: 0, + blockHeight: 0, wantUtxos: []*account.UTXO{ &account.UTXO{ OutputID: bc.Hash{V0: 8675398163687045889, V1: 7549510466747714094, V2: 13693077838209211470, V3: 6878568403630757599}, @@ -671,8 +679,8 @@ func TestTxOutToUtxos(t *testing.T) { { tx: types.NewTx(types.TxData{ Inputs: []*types.TxInput{ - types.NewCrossChainInput([][]byte{}, bc.Hash{V0: 1}, bc.AssetID{V0: 1}, 5, 1, []byte{0x51}, []byte("asset1")), - types.NewCrossChainInput([][]byte{}, bc.Hash{V0: 2}, *consensus.BTMAssetID, 7, 1, []byte{0x51}, []byte("assetbtm"))}, + types.NewCrossChainInput([][]byte{}, bc.Hash{V0: 1}, bc.AssetID{V0: 1}, 5, 1, 1, []byte("asset1"), []byte("IssuanceProgram")), + types.NewCrossChainInput([][]byte{}, bc.Hash{V0: 2}, *consensus.BTMAssetID, 7, 1, 1, []byte("assetbtm"), []byte("IssuanceProgram"))}, Outputs: []*types.TxOutput{ types.NewIntraChainOutput(bc.AssetID{V0: 1}, 2, []byte{0x51}), types.NewIntraChainOutput(bc.AssetID{V0: 1}, 3, []byte{0x52}), @@ -681,46 +689,70 @@ func TestTxOutToUtxos(t *testing.T) { }, }), statusFail: false, - vaildHeight: 0, + blockHeight: 0, wantUtxos: []*account.UTXO{ &account.UTXO{ - OutputID: bc.Hash{V0: 396952592194652166, V1: 9806684391645699244, V2: 484243382648745315, V3: 16988093808435014689}, + OutputID: bc.Hash{15099088327605875240, 9219883424533839002, 14610773420520931246, 14899393216621986426}, AssetID: bc.AssetID{V0: 1}, Amount: 2, ControlProgram: []byte{0x51}, - SourceID: bc.Hash{V0: 15256474482236132139, V1: 14615963227748152009, V2: 1392768713126269609, V3: 3435801067785833027}, + SourceID: bc.Hash{16280523637332892554, 3627898494554775182, 16212395834831293013, 3511838375364469081}, SourcePos: 0, }, &account.UTXO{ - OutputID: bc.Hash{V0: 10880631720641638863, V1: 7783872056988487492, V2: 10925792818362846534, V3: 16483659407709834456}, + OutputID: bc.Hash{3610727630628260133, 13088239834060115701, 14968571476177322101, 7529789620153710893}, AssetID: bc.AssetID{V0: 1}, Amount: 3, ControlProgram: []byte{0x52}, - SourceID: bc.Hash{V0: 15256474482236132139, V1: 14615963227748152009, V2: 1392768713126269609, V3: 3435801067785833027}, + SourceID: bc.Hash{16280523637332892554, 3627898494554775182, 16212395834831293013, 3511838375364469081}, SourcePos: 1, }, &account.UTXO{ - OutputID: bc.Hash{V0: 6688768820716928311, V1: 7640171490156205612, V2: 6082620342644961312, V3: 6194446985740174532}, + OutputID: bc.Hash{2034718018519539988, 16893043149780417913, 11926903829554245570, 3446441680088007327}, AssetID: *consensus.BTMAssetID, Amount: 2, ControlProgram: []byte{0x53}, - SourceID: bc.Hash{V0: 15256474482236132139, V1: 14615963227748152009, V2: 1392768713126269609, V3: 3435801067785833027}, + SourceID: bc.Hash{16280523637332892554, 3627898494554775182, 16212395834831293013, 3511838375364469081}, SourcePos: 2, }, &account.UTXO{ - OutputID: bc.Hash{V0: 13540722642395030514, V1: 15412939347183859286, V2: 9545016219428105666, V3: 11940603522975438116}, + OutputID: bc.Hash{7296157888262317106, 5789265653020263821, 1170213393196090227, 7665081318694049454}, AssetID: *consensus.BTMAssetID, Amount: 5, ControlProgram: []byte{0x54}, - SourceID: bc.Hash{V0: 15256474482236132139, V1: 14615963227748152009, V2: 1392768713126269609, V3: 3435801067785833027}, + SourceID: bc.Hash{16280523637332892554, 3627898494554775182, 16212395834831293013, 3511838375364469081}, SourcePos: 3, }, }, }, + { + tx: types.NewTx(types.TxData{ + Inputs: []*types.TxInput{ + types.NewCoinbaseInput([]byte{0x51}), + }, + Outputs: []*types.TxOutput{ + types.NewIntraChainOutput(*consensus.BTMAssetID, 0, []byte{0x51}), + types.NewIntraChainOutput(*consensus.BTMAssetID, 3, []byte{0x52}), + }, + }), + statusFail: false, + blockHeight: 0, + wantUtxos: []*account.UTXO{ + &account.UTXO{ + OutputID: bc.Hash{V0: 17248080803965266442, V1: 11280159100206427956, V2: 14296992668077839045, V3: 10053986081220066749}, + AssetID: *consensus.BTMAssetID, + Amount: 3, + ControlProgram: []byte{0x52}, + SourceID: bc.Hash{V0: 14680680172533616824, V1: 32429899179491316, V2: 15399988966960786775, V3: 17411722803888206567}, + SourcePos: 1, + ValidHeight: consensus.MainNetParams.CoinbasePendingBlockNumber, + }, + }, + }, } for i, c := range cases { - if gotUtxos := txOutToUtxos(c.tx, c.statusFail, c.vaildHeight); !testutil.DeepEqual(gotUtxos, c.wantUtxos) { + if gotUtxos := txOutToUtxos(c.tx, c.statusFail, c.blockHeight); !testutil.DeepEqual(gotUtxos, c.wantUtxos) { t.Errorf("case %d: got %v want %v", i, gotUtxos, c.wantUtxos) for j, u := range gotUtxos {