Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove config.EVMNetworkID() #3460

Merged
merged 30 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions blockchain/block/block_deserializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,35 @@ func (bd *Deserializer) DeserializeBody(buf []byte) (*Body, error) {
}
return &b, nil
}

// FromBlockStoreProto converts protobuf to block store
func (bd *Deserializer) FromBlockStoreProto(pb *iotextypes.BlockStore) (*Store, error) {
in := &Store{}
blk, err := bd.FromBlockProto(pb.Block)
if err != nil {
return nil, err
}
// verify merkle root can match after deserialize
if err := blk.VerifyTxRoot(); err != nil {
return nil, err
}

in.Block = blk
in.Receipts = []*action.Receipt{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line not needed, in.Receipts is nil and will be allocated in the append() below

for _, receiptPb := range pb.Receipts {
receipt := &action.Receipt{}
receipt.ConvertFromReceiptPb(receiptPb)
in.Receipts = append(in.Receipts, receipt)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove empty line

return in, nil
}

// DeserializeBlockStore de-serializes a block store
func (bd *Deserializer) DeserializeBlockStore(buf []byte) (*Store, error) {
pb := iotextypes.BlockStore{}
if err := proto.Unmarshal(buf, &pb); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal block store")
}
return bd.FromBlockStoreProto(&pb)
}
18 changes: 18 additions & 0 deletions blockchain/block/block_deserializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,21 @@ func TestBlockDeserializer(t *testing.T) {
r.Equal(_pbBlock.Body.Actions[0].Core.ChainID, blk.Actions[0].ChainID())
r.Equal(_pbBlock.Body.Actions[1].Core.ChainID, blk.Actions[1].ChainID())
}

func TestBlockStoreDeserializer(t *testing.T) {
require := require.New(t)
store, err := makeStore()
require.NoError(err)

storeProto := store.ToProto()

require.NotNil(storeProto)

bd := Deserializer{}
store1, err := bd.FromBlockStoreProto(storeProto)
require.NoError(err)

require.Equal(store1.Block.height, store.Block.height)
require.Equal(store1.Block.Header.prevBlockHash, store.Block.Header.prevBlockHash)
require.Equal(store1.Block.Header.blockSig, store.Block.Header.blockSig)
}
1 change: 0 additions & 1 deletion blockchain/block/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func (in *Store) ToProto() *iotextypes.BlockStore {

// FromProto converts from proto message
func (in *Store) FromProto(pb *iotextypes.BlockStore) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are this function and the deserialize function still in use? if not, we can delete them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still need to make it FromProto(pb *iotextypes.BlockStore, evmid uint32) so we can remove the config.EVMNetworkID() in line 45

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or like @CoderZhi commented, remove it (and fix test)

// TODO: pass the correct EVM network ID at the time of newFileDAOv2()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

blk, err := (&Deserializer{}).SetEvmNetworkID(config.EVMNetworkID()).FromBlockProto(pb.Block)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func BoltDBDaoOption(indexers ...blockdao.BlockIndexer) Option {
}
cfg.DB.DbPath = cfg.Chain.ChainDBPath // TODO: remove this after moving TrieDBPath from cfg.Chain to cfg.DB
cfg.DB.CompressLegacy = cfg.Chain.CompressBlock
bc.dao = blockdao.NewBlockDAO(indexers, cfg.DB)
bc.dao = blockdao.NewBlockDAO(indexers, cfg.Chain.EVMNetworkID, cfg.DB)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems EVMNetworkID is only for deserializer, therefore, we may just pass deserializer into dao in next PR.

return nil
}
}
Expand Down
9 changes: 7 additions & 2 deletions blockchain/blockdao/blockdao.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ type (
)

// NewBlockDAO instantiates a block DAO
func NewBlockDAO(indexers []BlockIndexer, cfg db.Config) BlockDAO {
blkStore, err := filedao.NewFileDAO(cfg)
func NewBlockDAO(indexers []BlockIndexer, evmNetworkID uint32, cfg db.Config) BlockDAO {
filedaoConfig, err := filedao.CreateConfig(cfg, filedao.EVMNetworkIDOption(evmNetworkID))
if err != nil {
log.L().Fatal(err.Error(), zap.Any("cfg", cfg))
return nil
}
blkStore, err := filedao.NewFileDAO(filedaoConfig)
if err != nil {
log.L().Fatal(err.Error(), zap.Any("cfg", cfg))
return nil
Expand Down
7 changes: 4 additions & 3 deletions blockchain/blockdao/blockdao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,16 +442,17 @@ func createTestBlockDAO(inMemory, legacy bool, compressBlock string, cfg db.Conf
return NewBlockDAOInMemForTest(nil), nil
}

newCfg, _ := filedao.CreateConfig(cfg)
if legacy {
fileDAO, err := filedao.CreateFileDAO(true, cfg)
fileDAO, err := filedao.CreateFileDAO(true, newCfg)
if err != nil {
return nil, err
}
return createBlockDAO(fileDAO, nil, cfg), nil
}

cfg.Compressor = compressBlock
return NewBlockDAO(nil, cfg), nil
return NewBlockDAO(nil, 0, cfg), nil
}

func BenchmarkBlockCache(b *testing.B) {
Expand All @@ -472,7 +473,7 @@ func BenchmarkBlockCache(b *testing.B) {
cfg.DbPath = indexPath
cfg.DbPath = testPath
cfg.MaxCacheSize = cacheSize
blkDao := NewBlockDAO([]BlockIndexer{}, cfg)
blkDao := NewBlockDAO([]BlockIndexer{}, 0, cfg)
require.NoError(b, blkDao.Start(context.Background()))
defer func() {
require.NoError(b, blkDao.Stop(context.Background()))
Expand Down
39 changes: 35 additions & 4 deletions blockchain/filedao/filedao.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,46 @@ type (
lock sync.Mutex
topIndex uint64
splitHeight uint64
cfg db.Config
cfg Config
currFd BaseFileDAO
legacyFd FileDAO
v2Fd *FileV2Manager // a collection of v2 db files
deser *block.Deserializer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deser -> blockDeserializer

}

// Config represents the configuration of File DAO
Config struct {
db.Config
evmNetworkID uint32
}
dustinxie marked this conversation as resolved.
Show resolved Hide resolved

// ConfigOption applies the configuration of File DAO
ConfigOption func(*Config) error
)

// EVMNetworkIDOption sets the EVM network ID
func EVMNetworkIDOption(evmNetworkID uint32) ConfigOption {
return func(opt *Config) error {
opt.evmNetworkID = evmNetworkID
return nil
}
}

// CreateConfig creates a config for FileDAO
func CreateConfig(cfg db.Config, options ...ConfigOption) (Config, error) {
cf := Config{
Config: cfg,
}
for _, opt := range options {
if err := opt(&cf); err != nil {
return cf, err
}
}
return cf, nil
}

// NewFileDAO creates an instance of FileDAO
func NewFileDAO(cfg db.Config) (FileDAO, error) {
func NewFileDAO(cfg Config) (FileDAO, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar to blockdao, filedao's config may not be essential.

header, err := checkMasterChainDBFile(cfg.DbPath)
if err == ErrFileInvalid || err == ErrFileCantAccess {
return nil, err
Expand Down Expand Up @@ -365,7 +396,7 @@ func (fd *fileDAO) DeleteTipBlock() error {
}

// CreateFileDAO creates FileDAO according to master file
func CreateFileDAO(legacy bool, cfg db.Config) (FileDAO, error) {
func CreateFileDAO(legacy bool, cfg Config) (FileDAO, error) {
fd := fileDAO{splitHeight: 1, cfg: cfg}
fds := []*fileDAOv2{}
v2Top, v2Files := checkAuxFiles(cfg.DbPath, FileV2)
Expand Down Expand Up @@ -405,7 +436,7 @@ func CreateFileDAO(legacy bool, cfg db.Config) (FileDAO, error) {
}

// createNewV2File creates a new v2 chain db file
func createNewV2File(start uint64, cfg db.Config) error {
func createNewV2File(start uint64, cfg Config) error {
v2, err := newFileDAOv2(start, cfg)
if err != nil {
return err
Expand Down
12 changes: 5 additions & 7 deletions blockchain/filedao/filedao_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/db/batch"
"github.com/iotexproject/iotex-core/pkg/compress"
Expand Down Expand Up @@ -51,7 +50,7 @@ type (
fileDAOLegacy struct {
compressBlock bool
lifecycle lifecycle.Lifecycle
cfg db.Config
cfg Config
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

mutex sync.RWMutex // for create new db file
topIndex atomic.Value
htf db.RangeIndex
Expand All @@ -61,11 +60,11 @@ type (
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar to filedao_v2, add deser *Deserializer here


// newFileDAOLegacy creates a new legacy file
func newFileDAOLegacy(cfg db.Config) (FileDAO, error) {
func newFileDAOLegacy(cfg Config) (FileDAO, error) {
return &fileDAOLegacy{
compressBlock: cfg.CompressLegacy,
cfg: cfg,
kvStore: db.NewBoltDB(cfg),
kvStore: db.NewBoltDB(cfg.Config),
kvStores: cache.NewThreadSafeLruCache(0),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and initialize deser here

}, nil
}
Expand Down Expand Up @@ -263,8 +262,7 @@ func (fd *fileDAOLegacy) body(h hash.Hash256) (*block.Body, error) {
// block body could be empty
return &block.Body{}, nil
}
// TODO: pass the correct EVM network ID at the time of newFileDAOLegacy()
return (&block.Deserializer{}).SetEvmNetworkID(config.EVMNetworkID()).DeserializeBody(value)
return (&block.Deserializer{}).SetEvmNetworkID(fd.cfg.evmNetworkID).DeserializeBody(value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so here just fd.deser.DeserializeBody()

}

func (fd *fileDAOLegacy) footer(h hash.Hash256) (*block.Footer, error) {
Expand Down Expand Up @@ -603,7 +601,7 @@ func (fd *fileDAOLegacy) openDB(idx uint64) (kvStore db.KVStore, index uint64, e
newFile = true
}

kvStore = db.NewBoltDB(cfg)
kvStore = db.NewBoltDB(cfg.Config)
fd.kvStores.Add(idx, kvStore)
err = kvStore.Start(context.Background())
if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions blockchain/filedao/filedao_legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestFileDAOLegacy_PutBlock(t *testing.T) {
var (
normalHeaderSize, compressHeaderSize int
)
testFdInterface := func(cfg db.Config, t *testing.T) {
testFdInterface := func(cfg Config, t *testing.T) {
r := require.New(t)

testutil.CleanupPath(cfg.DbPath)
Expand Down Expand Up @@ -79,8 +79,9 @@ func TestFileDAOLegacy_PutBlock(t *testing.T) {
block.LoadGenesisHash(&config.Default.Genesis)
for _, compress := range []bool{false, true} {
cfg.CompressLegacy = compress
newCfg, _ := CreateConfig(cfg)
t.Run("test fileDAOLegacy interface", func(t *testing.T) {
testFdInterface(cfg, t)
testFdInterface(newCfg, t)
})
}

Expand All @@ -95,7 +96,8 @@ func TestFileDAOLegacy_DeleteTipBlock(t *testing.T) {
cfg.DbPath = "./filedao_legacy.db"
cfg.CompressLegacy = true // enable compress

fd, err := newFileDAOLegacy(cfg)
newCfg, _ := CreateConfig(cfg)
fd, err := newFileDAOLegacy(newCfg)
r.NoError(err)
legacy := fd.(*fileDAOLegacy)

Expand Down Expand Up @@ -131,7 +133,8 @@ func TestFileDAOLegacy_getBlockValue(t *testing.T) {
cfg := db.DefaultConfig
cfg.DbPath = "./filedao_legacy.db"

fd, err := newFileDAOLegacy(cfg)
newCfg, _ := CreateConfig(cfg)
fd, err := newFileDAOLegacy(newCfg)
r.NoError(err)
legacy := fd.(*fileDAOLegacy)

Expand Down
38 changes: 30 additions & 8 deletions blockchain/filedao/filedao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ func TestReadFileHeader(t *testing.T) {
_, err = readFileHeader(cfg.DbPath, FileAll)
r.Equal(ErrFileNotExist, err)

newCfg, _ := CreateConfig(cfg)
// empty legacy file is invalid
legacy, err := newFileDAOLegacy(cfg)
legacy, err := newFileDAOLegacy(newCfg)
r.NoError(err)
ctx := context.Background()
r.NoError(legacy.Start(ctx))
Expand Down Expand Up @@ -109,9 +110,9 @@ func TestReadFileHeader(t *testing.T) {
}
}
os.RemoveAll(cfg.DbPath)

newCfg, _ = CreateConfig(cfg)
// test valid v2 master file
r.NoError(createNewV2File(1, cfg))
r.NoError(createNewV2File(1, newCfg))
defer os.RemoveAll(cfg.DbPath)

test2 := []testCheckFile{
Expand Down Expand Up @@ -143,8 +144,9 @@ func TestNewFileDAOSplitV2(t *testing.T) {
_, err := checkMasterChainDBFile(cfg.DbPath)
r.Equal(ErrFileNotExist, err)

newCfg, _ := CreateConfig(cfg)
// test empty db file, this will create new v2 file
fd, err := NewFileDAO(cfg)
fd, err := NewFileDAO(newCfg)
r.NoError(err)
r.NotNil(fd)
h, err := readFileHeader(cfg.DbPath, FileAll)
Expand Down Expand Up @@ -195,7 +197,9 @@ func TestNewFileDAOSplitLegacy(t *testing.T) {

cfg.SplitDBHeight = 5
cfg.SplitDBSizeMB = 20
fd, err := newFileDAOLegacy(cfg)

newCfg, _ := CreateConfig(cfg)
fd, err := newFileDAOLegacy(newCfg)
r.NoError(err)
ctx := context.Background()
r.NoError(fd.Start(ctx))
Expand All @@ -208,7 +212,9 @@ func TestNewFileDAOSplitLegacy(t *testing.T) {

// set FileDAO to split at height 15, 30 and 40
cfg.V2BlocksToSplitDB = 15
fd, err = NewFileDAO(cfg)

newCfg, _ = CreateConfig(cfg)
fd, err = NewFileDAO(newCfg)
r.NoError(err)
r.NoError(fd.Start(ctx))
fm := fd.(*fileDAO)
Expand Down Expand Up @@ -263,8 +269,9 @@ func TestNewFileDAOSplitLegacy(t *testing.T) {
r.Equal(files[1], file3)
r.Equal(files[2], file4)

newCfg, _ = CreateConfig(cfg)
// open 4 db files and verify again
fd, err = NewFileDAO(cfg)
fd, err = NewFileDAO(newCfg)
fm = fd.(*fileDAO)
r.EqualValues(4, fm.topIndex)
r.EqualValues(1, fm.splitHeight)
Expand Down Expand Up @@ -316,7 +323,8 @@ func TestCheckFiles(t *testing.T) {
// create 3 v2 files
for i := 1; i <= 3; i++ {
cfg.DbPath = kthAuxFileName("./filedao_v2.db", uint64(i))
r.NoError(createNewV2File(1, cfg))
newCfg, _ := CreateConfig(cfg)
r.NoError(createNewV2File(1, newCfg))
}
defer func() {
for i := 1; i <= 3; i++ {
Expand All @@ -330,3 +338,17 @@ func TestCheckFiles(t *testing.T) {
r.Equal(files[i-1], kthAuxFileName("./filedao_v2.db", uint64(i)))
}
}

func TestCreateConfig(t *testing.T) {
r := require.New(t)
cfg := db.DefaultConfig
newCfg, err := CreateConfig(cfg)
r.NoError(err)
r.Equal("", newCfg.DbPath)
r.Equal(uint32(0), newCfg.evmNetworkID)
cfg.DbPath = "./filedao_v2.db"
newCfg, err = CreateConfig(cfg, EVMNetworkIDOption(1))
r.NoError(err)
r.Equal(cfg.DbPath, newCfg.DbPath)
r.Equal(uint32(1), newCfg.evmNetworkID)
}
Loading