OSDN Git Service

move TestReserveBtmUtxoChain to account
authorChengcheng Zhang <943420582@qq.com>
Mon, 1 Jul 2019 13:27:12 +0000 (21:27 +0800)
committerChengcheng Zhang <943420582@qq.com>
Mon, 1 Jul 2019 13:27:12 +0000 (21:27 +0800)
account/builder.go
account/builder_test.go
account/utxo_keeper_test.go

index ef89fcf..ff5098a 100644 (file)
@@ -73,7 +73,7 @@ func calcMergeGas(num int) uint64 {
        return gas
 }
 
-func (m *Manager) ReserveBtmUtxoChain(builder *txbuilder.TemplateBuilder, accountID string, amount uint64, useUnconfirmed bool) ([]*UTXO, error) {
+func (m *Manager) reserveBtmUtxoChain(builder *txbuilder.TemplateBuilder, accountID string, amount uint64, useUnconfirmed bool) ([]*UTXO, error) {
        reservedAmount := uint64(0)
        utxos := []*UTXO{}
        for gasAmount := uint64(0); reservedAmount < gasAmount+amount; gasAmount = calcMergeGas(len(utxos)) {
@@ -170,7 +170,7 @@ func SpendAccountChain(ctx context.Context, builder *txbuilder.TemplateBuilder,
                return nil, errors.New("spend chain action only support BTM")
        }
 
-       utxos, err := act.accounts.ReserveBtmUtxoChain(builder, act.AccountID, act.Amount, act.UseUnconfirmed)
+       utxos, err := act.accounts.reserveBtmUtxoChain(builder, act.AccountID, act.Amount, act.UseUnconfirmed)
        if err != nil {
                return nil, err
        }
index a0bef7a..845c243 100644 (file)
@@ -2,9 +2,12 @@ package account
 
 import (
        "testing"
+       "time"
 
        "github.com/vapor/blockchain/txbuilder"
+       "github.com/vapor/consensus"
        "github.com/vapor/protocol/bc"
+       "github.com/vapor/testutil"
 )
 
 func TestMergeSpendAction(t *testing.T) {
@@ -399,71 +402,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)
-
-//             data, err := json.Marshal(utxo)
-//             if err != nil {
-//                     t.Fatal(err)
-//             }
+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.db.Set(StandardUTXOKey(utxo.OutputID), data)
-//     }
+               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},
-//             },
-//     }
+       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)
+       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)
-//             }
+               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)
-//             }
+               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)
-//             }
-//     }
+               if !testutil.DeepEqual(got, c.want) {
+                       t.Fatalf("case %d got %d want %d", i, got, c.want)
+               }
+       }
 
-// }
+}
index 0f62651..f5f97bf 100644 (file)
@@ -2,14 +2,21 @@ package account
 
 import (
        "encoding/json"
+       "io/ioutil"
        "os"
        "testing"
        "time"
 
+       "github.com/golang/groupcache/lru"
        log "github.com/sirupsen/logrus"
+       "github.com/vapor/blockchain/txbuilder"
        "github.com/vapor/crypto/ed25519/chainkd"
        dbm "github.com/vapor/database/leveldb"
+       "github.com/vapor/database/storage"
+       "github.com/vapor/protocol"
        "github.com/vapor/protocol/bc"
+       "github.com/vapor/protocol/bc/types"
+       "github.com/vapor/protocol/state"
        "github.com/vapor/testutil"
 )
 
@@ -1411,3 +1418,88 @@ func (store *mockAccountStore) SetStandardUTXO(outputID bc.Hash, utxo *UTXO) err
        }
        return nil
 }
+
+func mockAccountManager(t *testing.T) *Manager {
+       dirPath, err := ioutil.TempDir(".", "")
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer os.RemoveAll(dirPath)
+
+       testDB := dbm.NewDB("testdb", "memdb", dirPath)
+       accountStore := newMockAccountStore(testDB)
+
+       // dispatcher := event.NewDispatcher()
+       // // store := database.NewStore(testDB)
+       // store := newMockStore(testDB)
+       // // accountStore := database.NewAccountStore(testDB)
+       // accountStore := newMockAccountStore(testDB)
+       // txPool := protocol.NewTxPool(store, dispatcher)
+       // config.CommonConfig = config.DefaultConfig()
+       // chain, err := protocol.NewChain(store, txPool, dispatcher)
+       // if err != nil {
+       //      t.Fatal(err)
+       // }
+
+       // return NewManager(accountStore, chain)
+
+       bestBlockHeight := func() uint64 { return 9527 }
+
+       return &Manager{
+               store:       accountStore,
+               chain:       nil,
+               utxoKeeper:  newUtxoKeeper(bestBlockHeight, accountStore),
+               cache:       lru.New(maxAccountCache),
+               aliasCache:  lru.New(maxAccountCache),
+               delayedACPs: make(map[*txbuilder.TemplateBuilder][]*CtrlProgram),
+       }
+}
+
+type mockStore struct {
+       db dbm.DB
+       // cache cache
+}
+
+// newStore creates and returns a new Store object.
+func newMockStore(db dbm.DB) *mockStore {
+       // fillBlockHeaderFn := func(hash *bc.Hash) (*types.BlockHeader, error) {
+       //      return GetBlockHeader(db, hash)
+       // }
+       // fillBlockTxsFn := func(hash *bc.Hash) ([]*types.Tx, error) {
+       //      return GetBlockTransactions(db, hash)
+       // }
+
+       // fillBlockHashesFn := func(height uint64) ([]*bc.Hash, error) {
+       //      return GetBlockHashesByHeight(db, height)
+       // }
+
+       // fillMainChainHashFn := func(height uint64) (*bc.Hash, error) {
+       //      return GetMainChainHash(db, height)
+       // }
+
+       // fillVoteResultFn := func(seq uint64) (*state.VoteResult, error) {
+       //      return GetVoteResult(db, seq)
+       // }
+
+       // cache := newCache(fillBlockHeaderFn, fillBlockTxsFn, fillBlockHashesFn, fillMainChainHashFn, fillVoteResultFn)
+       return &mockStore{
+               db: db,
+               // cache: nil,
+       }
+}
+
+func (s *mockStore) BlockExist(*bc.Hash) bool                                     { return false }
+func (s *mockStore) GetBlock(*bc.Hash) (*types.Block, error)                      { return nil, nil }
+func (s *mockStore) GetBlockHeader(*bc.Hash) (*types.BlockHeader, error)          { return nil, nil }
+func (s *mockStore) GetStoreStatus() *protocol.BlockStoreState                    { return nil }
+func (s *mockStore) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
+func (s *mockStore) GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error     { return nil }
+func (s *mockStore) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error)                 { return nil, nil }
+func (s *mockStore) GetVoteResult(uint64) (*state.VoteResult, error)              { return nil, nil }
+func (s *mockStore) GetMainChainHash(uint64) (*bc.Hash, error)                    { return nil, nil }
+func (s *mockStore) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error)            { return nil, nil }
+func (s *mockStore) SaveBlock(*types.Block, *bc.TransactionStatus) error          { return nil }
+func (s *mockStore) SaveBlockHeader(*types.BlockHeader) error                     { return nil }
+func (s *mockStore) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.VoteResult) error {
+       return nil
+}