Store serialized bytes in cache
This commit is contained in:
@@ -23,7 +23,7 @@ import (
|
|||||||
var log *logrus.Entry
|
var log *logrus.Entry
|
||||||
var logger = logrus.New()
|
var logger = logrus.New()
|
||||||
|
|
||||||
var cacheSize = 10000
|
var cacheSize = 30000
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
logger.SetFormatter(&logrus.TextFormatter{
|
logger.SetFormatter(&logrus.TextFormatter{
|
||||||
|
|||||||
@@ -5,16 +5,22 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/adityapk00/lightwalletd/walletrpc"
|
"github.com/adityapk00/lightwalletd/walletrpc"
|
||||||
|
"github.com/golang/protobuf/proto"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type BlockCacheEntry struct {
|
||||||
|
data []byte
|
||||||
|
hash []byte
|
||||||
|
}
|
||||||
|
|
||||||
type BlockCache struct {
|
type BlockCache struct {
|
||||||
MaxEntries int
|
MaxEntries int
|
||||||
|
|
||||||
FirstBlock int
|
FirstBlock int
|
||||||
LastBlock int
|
LastBlock int
|
||||||
|
|
||||||
m map[int]*walletrpc.CompactBlock
|
m map[int]*BlockCacheEntry
|
||||||
|
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
}
|
}
|
||||||
@@ -24,7 +30,7 @@ func NewBlockCache(maxEntries int) *BlockCache {
|
|||||||
MaxEntries: maxEntries,
|
MaxEntries: maxEntries,
|
||||||
FirstBlock: -1,
|
FirstBlock: -1,
|
||||||
LastBlock: -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
|
// 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 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")
|
return errors.New("Prev hash of the block didn't match")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the entry and update the counters
|
// 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
|
c.LastBlock = height
|
||||||
|
|
||||||
@@ -86,7 +101,14 @@ func (c *BlockCache) Get(height int) *walletrpc.CompactBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//println("Cache returned")
|
//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 {
|
func (c *BlockCache) GetLatestBlock() int {
|
||||||
|
|||||||
Reference in New Issue
Block a user