Skip to content

Commit

Permalink
refactor getBlock/Receipts
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored and Liuhaai committed Apr 5, 2024
1 parent 1bee8d5 commit 8fcd84e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 58 deletions.
49 changes: 26 additions & 23 deletions blockchain/filedao/filedao_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@ var (
type (
// fileDAOv2 handles chain db file after file split activation at v1.1.2
fileDAOv2 struct {
filename string
header *FileHeader
tip *FileTip
blkBuffer *stagingBuffer
blkCache cache.LRUCache
receiptCache cache.LRUCache
kvStore db.KVStore
batch batch.KVStoreBatch
hashStore db.CountingIndex // store block hash
blkStore db.CountingIndex // store raw blocks
sysStore db.CountingIndex // store transaction log
deser *block.Deserializer
filename string
header *FileHeader
tip *FileTip
blkBuffer *stagingBuffer
blkStorePbCache cache.LRUCache
blkCache cache.LRUCache
receiptCache cache.LRUCache
kvStore db.KVStore
batch batch.KVStoreBatch
hashStore db.CountingIndex // store block hash
blkStore db.CountingIndex // store raw blocks
sysStore db.CountingIndex // store transaction log
deser *block.Deserializer
}
)

Expand All @@ -69,24 +70,26 @@ func newFileDAOv2(bottom uint64, cfg db.Config, deser *block.Deserializer) (*fil
tip: &FileTip{
Height: bottom - 1,
},
blkCache: cache.NewThreadSafeLruCache(64),
receiptCache: cache.NewThreadSafeLruCache(64),
kvStore: db.NewBoltDB(cfg),
batch: batch.NewBatch(),
deser: deser,
blkStorePbCache: cache.NewThreadSafeLruCache(16),
blkCache: cache.NewThreadSafeLruCache(256),
receiptCache: cache.NewThreadSafeLruCache(256),
kvStore: db.NewBoltDB(cfg),
batch: batch.NewBatch(),
deser: deser,
}
return &fd, nil
}

// openFileDAOv2 opens an existing v2 file
func openFileDAOv2(cfg db.Config, deser *block.Deserializer) *fileDAOv2 {
return &fileDAOv2{
filename: cfg.DbPath,
blkCache: cache.NewThreadSafeLruCache(64),
receiptCache: cache.NewThreadSafeLruCache(64),
kvStore: db.NewBoltDB(cfg),
batch: batch.NewBatch(),
deser: deser,
filename: cfg.DbPath,
blkStorePbCache: cache.NewThreadSafeLruCache(16),
blkCache: cache.NewThreadSafeLruCache(256),
receiptCache: cache.NewThreadSafeLruCache(256),
kvStore: db.NewBoltDB(cfg),
batch: batch.NewBatch(),
deser: deser,
}
}

Expand Down
62 changes: 31 additions & 31 deletions blockchain/filedao/filedao_v2_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package filedao
import (
"github.com/pkg/errors"

"github.com/iotexproject/iotex-proto/golang/iotextypes"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/db"
Expand Down Expand Up @@ -175,7 +177,6 @@ func (fd *fileDAOv2) getBlock(height uint64) (*block.Block, error) {
if !fd.ContainsHeight(height) {
return nil, db.ErrNotExist
}

// check whether block in staging buffer or not
storeKey := blockStoreKey(height, fd.header)
if storeKey >= fd.blkStore.Size() {
Expand All @@ -185,30 +186,16 @@ func (fd *fileDAOv2) getBlock(height uint64) (*block.Block, error) {
}
return blkStore.Block, nil
}

// check whether block in read cache or not
if value, ok := fd.blkCache.Get(height); ok {
blk := value.(*block.Block)
return blk, nil
}

value, err := fd.blkStore.Get(storeKey)
if err != nil {
return nil, err
return value.(*block.Block), nil

Check warning on line 191 in blockchain/filedao/filedao_v2_util.go

View check run for this annotation

Codecov / codecov/patch

blockchain/filedao/filedao_v2_util.go#L191

Added line #L191 was not covered by tests
}
value, err = decompBytes(value, fd.header.Compressor)
// read from storage DB
blockStore, err := fd.getBlockStore(height)
if err != nil {
return nil, err
}
pbStores, err := block.DeserializeBlockStoresPb(value)
if err != nil {
return nil, err
}
if len(pbStores.BlockStores) != int(fd.header.BlockStoreSize) {
return nil, ErrDataCorruption
}

blk, err := fd.deser.BlockFromBlockStoreProto(pbStores.BlockStores[stagingKey(height, fd.header)])
blk, err := fd.deser.BlockFromBlockStoreProto(blockStore)
if err != nil {
return nil, err
}
Expand All @@ -221,7 +208,6 @@ func (fd *fileDAOv2) getReceipt(height uint64) ([]*action.Receipt, error) {
if !fd.ContainsHeight(height) {
return nil, db.ErrNotExist
}

// check whether block in staging buffer or not
storeKey := blockStoreKey(height, fd.header)
if storeKey >= fd.blkStore.Size() {
Expand All @@ -231,13 +217,32 @@ func (fd *fileDAOv2) getReceipt(height uint64) ([]*action.Receipt, error) {
}
return blkStore.Receipts, nil
}

// check whether block in read cache or not
// check whether receipts in read cache or not
if value, ok := fd.receiptCache.Get(height); ok {
receipts := value.([]*action.Receipt)
return receipts, nil
return value.([]*action.Receipt), nil

Check warning on line 222 in blockchain/filedao/filedao_v2_util.go

View check run for this annotation

Codecov / codecov/patch

blockchain/filedao/filedao_v2_util.go#L222

Added line #L222 was not covered by tests
}
// read from storage DB
blockStore, err := fd.getBlockStore(height)
if err != nil {
return nil, err
}
receipts, err := fd.deser.ReceiptsFromBlockStoreProto(blockStore)
if err != nil {
return nil, err
}
// add to read cache
fd.receiptCache.Add(height, receipts)
return receipts, nil
}

func (fd *fileDAOv2) getBlockStore(height uint64) (*iotextypes.BlockStore, error) {
// check whether blockStore in read cache or not
storeKey := blockStoreKey(height, fd.header)
if value, ok := fd.blkStorePbCache.Get(storeKey); ok {
pbInfos := value.(*iotextypes.BlockStores)
return pbInfos.BlockStores[stagingKey(height, fd.header)], nil
}
// read from storage DB
value, err := fd.blkStore.Get(storeKey)
if err != nil {
return nil, err
Expand All @@ -253,12 +258,7 @@ func (fd *fileDAOv2) getReceipt(height uint64) ([]*action.Receipt, error) {
if len(pbStores.BlockStores) != int(fd.header.BlockStoreSize) {
return nil, ErrDataCorruption
}

receipts, err := fd.deser.ReceiptsFromBlockStoreProto(pbStores.BlockStores[stagingKey(height, fd.header)])
if err != nil {
return nil, err
}
// add to read cache
fd.receiptCache.Add(height, receipts)
return receipts, nil
fd.blkStorePbCache.Add(storeKey, pbStores)
return pbStores.BlockStores[stagingKey(height, fd.header)], nil
}
10 changes: 6 additions & 4 deletions blockchain/filedao/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ func newFileDAOv2InMem(bottom uint64) (*fileDAOv2, error) {
tip: &FileTip{
Height: bottom - 1,
},
blkCache: cache.NewThreadSafeLruCache(16),
kvStore: db.NewMemKVStore(),
batch: batch.NewBatch(),
deser: block.NewDeserializer(_defaultEVMNetworkID),
blkStorePbCache: cache.NewThreadSafeLruCache(16),
blkCache: cache.NewThreadSafeLruCache(256),
receiptCache: cache.NewThreadSafeLruCache(256),
kvStore: db.NewMemKVStore(),
batch: batch.NewBatch(),
deser: block.NewDeserializer(_defaultEVMNetworkID),
}
return &fd, nil
}
Expand Down

0 comments on commit 8fcd84e

Please sign in to comment.