OSDN Git Service

try to fix ban peer bug (#273)
[bytom/vapor.git] / wallet / utxo_test.go
index 4f76dd5..2df108c 100644 (file)
@@ -18,6 +18,7 @@ import (
 
 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,7 +196,8 @@ 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, false)
                if !testutil.DeepEqual(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)
@@ -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:    198,
                                },
                        },
                },
@@ -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:    100,
+                               },
+                       },
+               },
        }
 
        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 {