diff options
-rw-r--r-- | dex/cache.go | 13 | ||||
-rw-r--r-- | dex/cache_test.go | 28 | ||||
-rw-r--r-- | dex/handler.go | 3 |
3 files changed, 40 insertions, 4 deletions
diff --git a/dex/cache.go b/dex/cache.go index 96ba2d765..c95038e1d 100644 --- a/dex/cache.go +++ b/dex/cache.go @@ -21,6 +21,7 @@ import ( "sync" coreCommon "github.com/dexon-foundation/dexon-consensus/common" + coreBlockdb "github.com/dexon-foundation/dexon-consensus/core/blockdb" coreTypes "github.com/dexon-foundation/dexon-consensus/core/types" ) @@ -47,14 +48,16 @@ type cache struct { blockCache map[coreCommon.Hash]*coreTypes.Block voteCache map[coreTypes.Position]map[voteKey]*coreTypes.Vote votePosition []coreTypes.Position + db coreBlockdb.BlockDatabase voteSize int size int } -func newCache(size int) *cache { +func newCache(size int, db coreBlockdb.BlockDatabase) *cache { return &cache{ blockCache: make(map[coreCommon.Hash]*coreTypes.Block), voteCache: make(map[coreTypes.Position]map[voteKey]*coreTypes.Vote), + db: db, size: size, } } @@ -111,6 +114,14 @@ func (c *cache) blocks(hashes coreCommon.Hashes) []*coreTypes.Block { for _, hash := range hashes { if block, exist := c.blockCache[hash]; exist { cacheBlocks = append(cacheBlocks, block) + } else { + block, err := c.db.Get(hash) + if err != nil { + continue + } + // Blocks request from the cache do not need the finalization info. + block.Finalization = coreTypes.FinalizationResult{} + cacheBlocks = append(cacheBlocks, &block) } } return cacheBlocks diff --git a/dex/cache_test.go b/dex/cache_test.go index ac5609c1a..8a0066c94 100644 --- a/dex/cache_test.go +++ b/dex/cache_test.go @@ -23,6 +23,7 @@ import ( "testing" coreCommon "github.com/dexon-foundation/dexon-consensus/common" + coreBlockdb "github.com/dexon-foundation/dexon-consensus/core/blockdb" coreTypes "github.com/dexon-foundation/dexon-consensus/core/types" ) @@ -41,7 +42,11 @@ func (v byHash) Swap(i int, j int) { } func TestCacheVote(t *testing.T) { - cache := newCache(3) + db, err := coreBlockdb.NewMemBackedBlockDB() + if err != nil { + panic(err) + } + cache := newCache(3, db) pos0 := coreTypes.Position{ Height: uint64(0), } @@ -126,7 +131,11 @@ func TestCacheVote(t *testing.T) { } func TestCacheBlock(t *testing.T) { - cache := newCache(3) + db, err := coreBlockdb.NewMemBackedBlockDB() + if err != nil { + panic(err) + } + cache := newCache(3, db) block1 := &coreTypes.Block{ Hash: coreCommon.NewRandomHash(), } @@ -178,4 +187,19 @@ func TestCacheBlock(t *testing.T) { if !hasNewBlock { t.Errorf("expect block %s in cache, have %v", block4, blocks) } + + block5 := &coreTypes.Block{ + Hash: coreCommon.NewRandomHash(), + } + if err := db.Put(*block5); err != nil { + panic(err) + } + blocks = cache.blocks(coreCommon.Hashes{block5.Hash}) + if len(blocks) != 1 { + t.Errorf("fail to get blocks: have %d, want 1", len(blocks)) + } else { + if !blocks[0].Hash.Equal(block5.Hash) { + t.Errorf("get wrong block: have %s, want %s", blocks[0], block5) + } + } } diff --git a/dex/handler.go b/dex/handler.go index 51167c9cb..620320f49 100644 --- a/dex/handler.go +++ b/dex/handler.go @@ -53,6 +53,7 @@ import ( "github.com/dexon-foundation/dexon/core" "github.com/dexon-foundation/dexon/core/types" "github.com/dexon-foundation/dexon/crypto" + "github.com/dexon-foundation/dexon/dex/blockdb" "github.com/dexon-foundation/dexon/dex/downloader" "github.com/dexon-foundation/dexon/dex/fetcher" "github.com/dexon-foundation/dexon/ethdb" @@ -157,7 +158,7 @@ func NewProtocolManager( nodeTable: tab, gov: gov, blockchain: blockchain, - cache: newCache(128), + cache: newCache(5120, blockdb.NewDatabase(chaindb)), chainconfig: config, newPeerCh: make(chan *peer), noMorePeers: make(chan struct{}), |