OSDN Git Service

fix test
authormars <mars@bytom.io>
Sat, 15 Jun 2019 02:37:14 +0000 (10:37 +0800)
committermars <mars@bytom.io>
Sat, 15 Jun 2019 02:37:14 +0000 (10:37 +0800)
database/cache_test.go
database/store.go
database/store_test.go
protocol/txpool_test.go

index 673ab7d..f49ce59 100644 (file)
@@ -21,14 +21,14 @@ func TestBlockCache(t *testing.T) {
                blocks[block.Hash()] = block
        }
 
-       cache := newBlockCache(func(hash *bc.Hash) (*types.Block, error) {
+       cache := newBlockCache(func(hash *bc.Hash, height uint64) (*types.Block, error) {
                return blocks[*hash], nil
        })
 
        for i := 0; i < maxCachedBlocks+10; i++ {
                block := newBlock(uint64(i))
                hash := block.Hash()
-               cache.lookup(&hash)
+               cache.lookup(&hash, block.Height)
        }
 
        for i := 0; i < 10; i++ {
index 9ca7777..be84e75 100644 (file)
@@ -76,14 +76,35 @@ func calcVoteResultKey(seq uint64) []byte {
 
 // GetBlock return the block by given hash and height
 func GetBlock(db dbm.DB, hash *bc.Hash, height uint64) (*types.Block, error) {
-       bytez := db.Get(calcBlockKey(hash))
-       if bytez == nil {
+       /*
+               bytez := db.Get(calcBlockKey(hash))
+               if bytez == nil {
+                       return nil, nil
+               }
+       */
+
+       block := &types.Block{}
+       //err := block.UnmarshalText(bytez)
+
+       binaryBlockHeader := db.Get(calcBlockHeaderKey(height, hash))
+       if binaryBlockHeader == nil {
                return nil, nil
        }
 
-       block := &types.Block{}
-       err := block.UnmarshalText(bytez)
-       return block, err
+       binaryBlockTxs := db.Get(calcBlockTransactionsKey(hash))
+       if binaryBlockTxs == nil {
+               return nil, nil
+       }
+
+       if err := block.BlockHeader.UnmarshalText(binaryBlockHeader); err != nil {
+               return nil, err
+       }
+
+       if err := block.UnmarshalTextForTransactions(binaryBlockTxs); err != nil {
+               return nil, err
+       }
+
+       return block, nil
 }
 
 // NewStore creates and returns a new Store object.
index 68f62c3..ea009eb 100644 (file)
@@ -221,7 +221,7 @@ func TestSaveBlock(t *testing.T) {
        }
 
        blockHash := block.Hash()
-       gotBlock, err := store.GetBlock(&blockHash)
+       gotBlock, err := store.GetBlock(&blockHash, block.Height)
        if err != nil {
                t.Fatal(err)
        }
index a42133f..338d57f 100644 (file)
@@ -113,6 +113,7 @@ type mockStore struct{}
 
 func (s *mockStore) BlockExist(hash *bc.Hash, height uint64) bool                 { return false }
 func (s *mockStore) GetBlock(*bc.Hash, uint64) (*types.Block, error)              { return nil, nil }
+func (s *mockStore) GetBlockHeader(*bc.Hash, uint64) (*types.BlockHeader, error)  { return nil, nil }
 func (s *mockStore) GetStoreStatus() *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 }
@@ -656,6 +657,7 @@ type mockStore1 struct{}
 
 func (s *mockStore1) BlockExist(hash *bc.Hash, height uint64) bool                 { return false }
 func (s *mockStore1) GetBlock(*bc.Hash, uint64) (*types.Block, error)              { return nil, nil }
+func (s *mockStore1) GetBlockHeader(*bc.Hash, uint64) (*types.BlockHeader, error)  { return nil, nil }
 func (s *mockStore1) GetStoreStatus() *BlockStoreState                             { return nil }
 func (s *mockStore1) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
 func (s *mockStore1) GetTransactionsUtxo(utxoView *state.UtxoViewpoint, tx []*bc.Tx) error {