Store compactBlocks
This commit is contained in:
@@ -3,7 +3,7 @@ package common
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
"github.com/adityapk00/lightwalletd/parser"
|
"github.com/adityapk00/lightwalletd/walletrpc"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ type BlockCache struct {
|
|||||||
FirstBlock int
|
FirstBlock int
|
||||||
LastBlock int
|
LastBlock int
|
||||||
|
|
||||||
m map[int]*parser.Block
|
m map[int]*walletrpc.CompactBlock
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(maxEntries int) *BlockCache {
|
func New(maxEntries int) *BlockCache {
|
||||||
@@ -21,12 +21,12 @@ func New(maxEntries int) *BlockCache {
|
|||||||
MaxEntries: maxEntries,
|
MaxEntries: maxEntries,
|
||||||
FirstBlock: -1,
|
FirstBlock: -1,
|
||||||
LastBlock: -1,
|
LastBlock: -1,
|
||||||
m: make(map[int]*parser.Block),
|
m: make(map[int]*walletrpc.CompactBlock),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *BlockCache) Add(height int, block *parser.Block) error {
|
func (c *BlockCache) Add(height int, block *walletrpc.CompactBlock) error {
|
||||||
println("Cache add", height)
|
//println("Cache add", height)
|
||||||
if c.FirstBlock == -1 && c.LastBlock == -1 {
|
if c.FirstBlock == -1 && c.LastBlock == -1 {
|
||||||
// If this is the first block, prep the data structure
|
// If this is the first block, prep the data structure
|
||||||
c.FirstBlock = height
|
c.FirstBlock = height
|
||||||
@@ -35,6 +35,7 @@ func (c *BlockCache) Add(height int, block *parser.Block) error {
|
|||||||
// Overwriting an existing entry. If so, then remove all
|
// Overwriting an existing entry. If so, then remove all
|
||||||
// subsequent blocks, since this might be a reorg
|
// subsequent blocks, since this might be a reorg
|
||||||
for i := height; i <= c.LastBlock; i++ {
|
for i := height; i <= c.LastBlock; i++ {
|
||||||
|
//println("Deleteing at height", i)
|
||||||
delete(c.m, i)
|
delete(c.m, i)
|
||||||
}
|
}
|
||||||
c.LastBlock = height - 1
|
c.LastBlock = height - 1
|
||||||
@@ -44,7 +45,7 @@ func (c *BlockCache) Add(height int, block *parser.Block) error {
|
|||||||
return errors.New("Blocks need to be added sequentially")
|
return errors.New("Blocks need to be added sequentially")
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.m[height-1] != nil && !bytes.Equal(block.GetPrevHash(), c.m[height-1].GetEncodableHash()) {
|
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")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,23 +56,26 @@ func (c *BlockCache) Add(height int, block *parser.Block) error {
|
|||||||
|
|
||||||
// If the cache is full, remove the oldest block
|
// If the cache is full, remove the oldest block
|
||||||
if c.LastBlock-c.FirstBlock+1 > c.MaxEntries {
|
if c.LastBlock-c.FirstBlock+1 > c.MaxEntries {
|
||||||
|
//println("Deleteing at height", c.FirstBlock)
|
||||||
delete(c.m, c.FirstBlock)
|
delete(c.m, c.FirstBlock)
|
||||||
c.FirstBlock = c.FirstBlock + 1
|
c.FirstBlock = c.FirstBlock + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//println("Cache size is ", len(c.m))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *BlockCache) Get(height int) *parser.Block {
|
func (c *BlockCache) Get(height int) *walletrpc.CompactBlock {
|
||||||
println("Cache get", height)
|
//println("Cache get", height)
|
||||||
if c.LastBlock == -1 || c.FirstBlock == -1 {
|
if c.LastBlock == -1 || c.FirstBlock == -1 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if height < c.FirstBlock || height > c.LastBlock {
|
if height < c.FirstBlock || height > c.LastBlock {
|
||||||
|
//println("Cache miss: index out of range")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Cache returned")
|
//println("Cache returned")
|
||||||
return c.m[height]
|
return c.m[height]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func GetSaplingInfo(rpcClient *rpcclient.Client) (int, string, error) {
|
|||||||
return int(saplingHeight), chainName, nil
|
return int(saplingHeight), chainName, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBlockFromRPC(rpcClient *rpcclient.Client, height int) (*parser.Block, error) {
|
func getBlockFromRPC(rpcClient *rpcclient.Client, height int) (*walletrpc.CompactBlock, error) {
|
||||||
params := make([]json.RawMessage, 2)
|
params := make([]json.RawMessage, 2)
|
||||||
params[0] = json.RawMessage("\"" + strconv.Itoa(height) + "\"")
|
params[0] = json.RawMessage("\"" + strconv.Itoa(height) + "\"")
|
||||||
params[1] = json.RawMessage("0")
|
params[1] = json.RawMessage("0")
|
||||||
@@ -85,10 +85,10 @@ func getBlockFromRPC(rpcClient *rpcclient.Client, height int) (*parser.Block, er
|
|||||||
return nil, errors.New("received overlong message")
|
return nil, errors.New("received overlong message")
|
||||||
}
|
}
|
||||||
|
|
||||||
return block, nil
|
return block.ToCompact(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetBlock(rpcClient *rpcclient.Client, cache *BlockCache, height int) (*parser.Block, error) {
|
func GetBlock(rpcClient *rpcclient.Client, cache *BlockCache, height int) (*walletrpc.CompactBlock, error) {
|
||||||
// First, check the cache to see if we have the block
|
// First, check the cache to see if we have the block
|
||||||
block := cache.Get(height)
|
block := cache.Get(height)
|
||||||
if block != nil {
|
if block != nil {
|
||||||
@@ -104,16 +104,16 @@ func GetBlock(rpcClient *rpcclient.Client, cache *BlockCache, height int) (*pars
|
|||||||
prevBlock := cache.Get(height - 1)
|
prevBlock := cache.Get(height - 1)
|
||||||
|
|
||||||
if prevBlock != nil {
|
if prevBlock != nil {
|
||||||
if !bytes.Equal(prevBlock.GetEncodableHash(), block.GetPrevHash()) {
|
if !bytes.Equal(prevBlock.Hash, block.PrevHash) {
|
||||||
// Reorg!
|
// Reorg!
|
||||||
reorgCount := 0
|
reorgCount := 0
|
||||||
cacheBlock := cache.Get(height - reorgCount)
|
cacheBlock := cache.Get(height - reorgCount)
|
||||||
|
|
||||||
rpcBlocks := []*parser.Block{}
|
rpcBlocks := []*walletrpc.CompactBlock{}
|
||||||
|
|
||||||
for ; reorgCount <= 100 &&
|
for ; reorgCount <= 100 &&
|
||||||
cacheBlock != nil &&
|
cacheBlock != nil &&
|
||||||
!bytes.Equal(block.GetPrevHash(), cacheBlock.GetEncodableHash()); reorgCount++ {
|
!bytes.Equal(block.PrevHash, cacheBlock.Hash); reorgCount++ {
|
||||||
|
|
||||||
block, err = getBlockFromRPC(rpcClient, height-reorgCount-1)
|
block, err = getBlockFromRPC(rpcClient, height-reorgCount-1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -133,7 +133,7 @@ func GetBlock(rpcClient *rpcclient.Client, cache *BlockCache, height int) (*pars
|
|||||||
// At this point, the block.prevHash == cache.hash
|
// At this point, the block.prevHash == cache.hash
|
||||||
// Store all blocks starting with 'block'
|
// Store all blocks starting with 'block'
|
||||||
for i := len(rpcBlocks) - 1; i >= 0; i-- {
|
for i := len(rpcBlocks) - 1; i >= 0; i-- {
|
||||||
cache.Add(rpcBlocks[i].GetHeight(), rpcBlocks[i])
|
cache.Add(int(rpcBlocks[i].Height), rpcBlocks[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -146,8 +146,6 @@ func GetBlock(rpcClient *rpcclient.Client, cache *BlockCache, height int) (*pars
|
|||||||
func GetBlockRange(rpcClient *rpcclient.Client, cache *BlockCache,
|
func GetBlockRange(rpcClient *rpcclient.Client, cache *BlockCache,
|
||||||
blockOut chan<- walletrpc.CompactBlock, errOut chan<- error, start, end int) {
|
blockOut chan<- walletrpc.CompactBlock, errOut chan<- error, start, end int) {
|
||||||
|
|
||||||
println("Getting block range")
|
|
||||||
|
|
||||||
// Go over [start, end] inclusive
|
// Go over [start, end] inclusive
|
||||||
for i := start; i <= end; i++ {
|
for i := start; i <= end; i++ {
|
||||||
block, err := GetBlock(rpcClient, cache, i)
|
block, err := GetBlock(rpcClient, cache, i)
|
||||||
@@ -156,7 +154,7 @@ func GetBlockRange(rpcClient *rpcclient.Client, cache *BlockCache,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
blockOut <- *block.ToCompact()
|
blockOut <- *block
|
||||||
}
|
}
|
||||||
|
|
||||||
errOut <- nil
|
errOut <- nil
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ func (s *SqlStreamer) GetBlock(ctx context.Context, id *walletrpc.BlockID) (*wal
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return cBlock.ToCompact(), err
|
return cBlock, err
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user