From c688de510ee68c450fb7ecb245db8ca50cd55e95 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Tue, 1 Oct 2019 11:05:43 -0700 Subject: [PATCH] Store serialized bytes in cache --- cmd/server/main.go | 2 +- common/cache.go | 32 +++++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 4ae0410..954af85 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -23,7 +23,7 @@ import ( var log *logrus.Entry var logger = logrus.New() -var cacheSize = 10000 +var cacheSize = 30000 func init() { logger.SetFormatter(&logrus.TextFormatter{ diff --git a/common/cache.go b/common/cache.go index 00825bb..a314bed 100644 --- a/common/cache.go +++ b/common/cache.go @@ -5,16 +5,22 @@ import ( "sync" "github.com/adityapk00/lightwalletd/walletrpc" + "github.com/golang/protobuf/proto" "github.com/pkg/errors" ) +type BlockCacheEntry struct { + data []byte + hash []byte +} + type BlockCache struct { MaxEntries int FirstBlock int LastBlock int - m map[int]*walletrpc.CompactBlock + m map[int]*BlockCacheEntry mutex sync.RWMutex } @@ -24,7 +30,7 @@ func NewBlockCache(maxEntries int) *BlockCache { MaxEntries: maxEntries, FirstBlock: -1, LastBlock: -1, - m: make(map[int]*walletrpc.CompactBlock), + m: make(map[int]*BlockCacheEntry), } } @@ -51,12 +57,21 @@ func (c *BlockCache) Add(height int, block *walletrpc.CompactBlock) error { // Don't allow out-of-order blocks. This is more of a sanity check than anything // If there is a reorg, then the ingestor needs to handle it. - if c.m[height-1] != nil && !bytes.Equal(block.PrevHash, c.m[height-1].Hash) { + if c.m[height-1] != nil && !bytes.Equal(block.PrevHash, c.m[height-1].hash) { return errors.New("Prev hash of the block didn't match") } // Add the entry and update the counters - c.m[height] = block + data, err := proto.Marshal(block) + if err != nil { + println("Error marshalling block!") + return nil + } + + c.m[height] = &BlockCacheEntry{ + data: data, + hash: block.GetHash(), + } c.LastBlock = height @@ -86,7 +101,14 @@ func (c *BlockCache) Get(height int) *walletrpc.CompactBlock { } //println("Cache returned") - return c.m[height] + serialized := &walletrpc.CompactBlock{} + err := proto.Unmarshal(c.m[height].data, serialized) + if err != nil { + println("Error unmarshalling compact block") + return nil + } + + return serialized } func (c *BlockCache) GetLatestBlock() int {