Skip to content

Commit

Permalink
[blockdao] add BlobStore interface (#4372)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Sep 21, 2024
1 parent 2a6eb14 commit 8d90cef
Show file tree
Hide file tree
Showing 12 changed files with 617 additions and 23 deletions.
10 changes: 10 additions & 0 deletions blockchain/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,13 @@ func (b *Block) ActionByHash(h hash.Hash256) (*action.SealedEnvelope, uint32, er
}
return nil, 0, errors.Errorf("block does not have action %x", h)
}

// HasBlob returns whether the block contains blobs
func (b *Block) HasBlob() bool {
for _, act := range b.Actions {
if act.BlobTxSidecar() != nil {
return true
}
}
return false
}
25 changes: 25 additions & 0 deletions blockchain/blockdao/blob_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.

package blockdao

import (
"context"

"github.com/ethereum/go-ethereum/core/types"
"github.com/iotexproject/go-pkgs/hash"

"github.com/iotexproject/iotex-core/blockchain/block"
)

type (
BlobStore interface {
Start(context.Context) error
Stop(context.Context) error
GetBlob(hash.Hash256) (*types.BlobTxSidecar, string, error)
GetBlobsByHeight(uint64) ([]*types.BlobTxSidecar, []string, error)
PutBlock(*block.Block) error
}
)
52 changes: 47 additions & 5 deletions blockchain/blockdao/blockdao.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 IoTeX Foundation
// Copyright (c) 2024 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
Expand All @@ -9,6 +9,7 @@ import (
"context"
"sync/atomic"

"github.com/ethereum/go-ethereum/core/types"
"github.com/iotexproject/go-pkgs/cache"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
Expand Down Expand Up @@ -37,8 +38,14 @@ var (
type (
// BlockDAO represents the block data access object
BlockDAO interface {
Start(ctx context.Context) error
Stop(ctx context.Context) error
BlockStore
GetBlob(hash.Hash256) (*types.BlobTxSidecar, string, error)
GetBlobsByHeight(uint64) ([]*types.BlobTxSidecar, []string, error)
}

BlockStore interface {
Start(context.Context) error
Stop(context.Context) error
Height() (uint64, error)
GetBlockHash(uint64) (hash.Hash256, error)
GetBlockHeight(hash.Hash256) (uint64, error)
Expand All @@ -54,7 +61,8 @@ type (
}

blockDAO struct {
blockStore BlockDAO
blockStore BlockStore
blobStore BlobStore
indexers []BlockIndexer
timerFactory *prometheustimer.TimerFactory
lifecycle lifecycle.Lifecycle
Expand All @@ -66,9 +74,17 @@ type (
}
)

type Option func(*blockDAO)

func WithBlobStore(bs BlobStore) Option {
return func(dao *blockDAO) {
dao.blobStore = bs
}
}

// NewBlockDAOWithIndexersAndCache returns a BlockDAO with indexers which will consume blocks appended, and
// caches which will speed up reading
func NewBlockDAOWithIndexersAndCache(blkStore BlockDAO, indexers []BlockIndexer, cacheSize int) BlockDAO {
func NewBlockDAOWithIndexersAndCache(blkStore BlockStore, indexers []BlockIndexer, cacheSize int, opts ...Option) BlockDAO {
if blkStore == nil {
return nil
}
Expand All @@ -77,8 +93,14 @@ func NewBlockDAOWithIndexersAndCache(blkStore BlockDAO, indexers []BlockIndexer,
blockStore: blkStore,
indexers: indexers,
}
for _, opt := range opts {
opt(blockDAO)
}

blockDAO.lifecycle.Add(blkStore)
if blockDAO.blobStore != nil {
blockDAO.lifecycle.Add(blockDAO.blobStore)
}
for _, indexer := range indexers {
blockDAO.lifecycle.Add(indexer)
}
Expand Down Expand Up @@ -261,6 +283,12 @@ func (dao *blockDAO) PutBlock(ctx context.Context, blk *block.Block) error {
timer.End()
return err
}
if dao.blobStore != nil && blk.HasBlob() {
if err := dao.blobStore.PutBlock(blk); err != nil {
timer.End()
return err
}
}
atomic.StoreUint64(&dao.tipHeight, blk.Height())
header := blk.Header
lruCachePut(dao.headerCache, blk.Height(), &header)
Expand All @@ -278,6 +306,20 @@ func (dao *blockDAO) PutBlock(ctx context.Context, blk *block.Block) error {
return nil
}

func (dao *blockDAO) GetBlob(h hash.Hash256) (*types.BlobTxSidecar, string, error) {
if dao.blobStore == nil {
return nil, "", errors.New("blob store is not available")
}
return dao.blobStore.GetBlob(h)
}

func (dao *blockDAO) GetBlobsByHeight(height uint64) ([]*types.BlobTxSidecar, []string, error) {
if dao.blobStore == nil {
return nil, nil, errors.New("blob store is not available")
}
return dao.blobStore.GetBlobsByHeight(height)
}

func lruCacheGet(c cache.LRUCache, key interface{}) (interface{}, bool) {
if c != nil {
return c.Get(key)
Expand Down
2 changes: 1 addition & 1 deletion blockchain/blockdao/blockdao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ func TestBlockDAO(t *testing.T) {
},
}

testBlockDao := func(dao BlockDAO, t *testing.T) {
testBlockDao := func(dao BlockStore, t *testing.T) {
ctx := protocol.WithBlockchainCtx(
genesis.WithGenesisContext(context.Background(), genesis.Default),
protocol.BlockchainCtx{
Expand Down
2 changes: 1 addition & 1 deletion blockchain/integrity/integrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ func createChain(cfg config.Config, inMem bool) (blockchain.Blockchain, factory.
if err != nil {
return nil, nil, nil, nil, err
}
var store blockdao.BlockDAO
var store blockdao.BlockStore
// create BlockDAO
if inMem {
store, err = filedao.NewFileDAOInMemForTest()
Expand Down
4 changes: 2 additions & 2 deletions blockindex/indexbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ const (
// IndexBuilder defines the index builder
type IndexBuilder struct {
timerFactory *prometheustimer.TimerFactory
dao blockdao.BlockDAO
dao blockdao.BlockStore
indexer Indexer
genesis genesis.Genesis
}

// NewIndexBuilder instantiates an index builder
func NewIndexBuilder(chainID uint32, g genesis.Genesis, dao blockdao.BlockDAO, indexer Indexer) (*IndexBuilder, error) {
func NewIndexBuilder(chainID uint32, g genesis.Genesis, dao blockdao.BlockStore, indexer Indexer) (*IndexBuilder, error) {
timerFactory, err := prometheustimer.New(
"iotex_indexer_batch_time",
"Indexer batch time",
Expand Down
4 changes: 2 additions & 2 deletions blockindex/indexbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestIndexBuilder(t *testing.T) {
},
}

testIndexer := func(dao blockdao.BlockDAO, indexer Indexer, t *testing.T) {
testIndexer := func(dao blockdao.BlockStore, indexer Indexer, t *testing.T) {
ctx := protocol.WithBlockchainCtx(
genesis.WithGenesisContext(context.Background(), genesis.Default),
protocol.BlockchainCtx{
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestIndexBuilder(t *testing.T) {
memstore, err := filedao.NewFileDAOInMemForTest()
require.NoError(err)
for _, v := range []struct {
dao blockdao.BlockDAO
dao blockdao.BlockStore
inMem bool
}{
{
Expand Down
2 changes: 1 addition & 1 deletion chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (builder *Builder) buildBlockDAO(forTest bool) error {
}
var (
err error
store blockdao.BlockDAO
store blockdao.BlockStore
)
if forTest {
store, err = filedao.NewFileDAOInMemForTest()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/iotexproject/iotex-address v0.2.8
github.com/iotexproject/iotex-antenna-go/v2 v2.5.1
github.com/iotexproject/iotex-election v0.3.5-0.20210611041425-20ddf674363d
github.com/iotexproject/iotex-proto v0.6.4-0.20240911042910-09456fe9bbbe
github.com/iotexproject/iotex-proto v0.6.4-0.20240921015631-53754f857c48
github.com/ipfs/go-ipfs-api v0.7.0
github.com/libp2p/go-libp2p v0.32.2
github.com/mackerelio/go-osstat v0.2.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1184,8 +1184,8 @@ github.com/iotexproject/iotex-antenna-go/v2 v2.5.1/go.mod h1:8pDZcM45M0gY6jm3PoM
github.com/iotexproject/iotex-election v0.3.5-0.20210611041425-20ddf674363d h1:/j1xCAC9YiG/8UKqYvycS/v3ddVsb1G7AMyLXOjeYI0=
github.com/iotexproject/iotex-election v0.3.5-0.20210611041425-20ddf674363d/go.mod h1:GRWevxtqQ4gPMrd7Qxhr29/7aTgvjiTp+rFI9KMMZEo=
github.com/iotexproject/iotex-proto v0.5.0/go.mod h1:Xg6REkv+nTZN+OC22xXIQuqKdTWWHwOAJEXCoMpDwtI=
github.com/iotexproject/iotex-proto v0.6.4-0.20240911042910-09456fe9bbbe h1:BMfTZiNdDbnozZBiyJ9mkQmrg48lO6ojPBCPwoisv9Y=
github.com/iotexproject/iotex-proto v0.6.4-0.20240911042910-09456fe9bbbe/go.mod h1:Hxct427jhlu08PHQ44Jwh//hZVhXKq+XO/Mdqwc6dNc=
github.com/iotexproject/iotex-proto v0.6.4-0.20240921015631-53754f857c48 h1:TwlS6DHX+feaUezPjkKvBWsd9KOvbNvkxVTjKsfuvfU=
github.com/iotexproject/iotex-proto v0.6.4-0.20240921015631-53754f857c48/go.mod h1:Hxct427jhlu08PHQ44Jwh//hZVhXKq+XO/Mdqwc6dNc=
github.com/ipfs/bbloom v0.0.1/go.mod h1:oqo8CVWsJFMOZqTglBG4wydCE4IQA/G2/SEofB0rjUI=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/boxo v0.8.1/go.mod h1:xJ2hVb4La5WyD7GvKYE0lq2g1rmQZoCD2K4WNrV6aZI=
Expand Down
Loading

0 comments on commit 8d90cef

Please sign in to comment.