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) {
}
}
-// 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)
+ }
+ }
-// }
+}
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"
)
}
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
+}