diff --git a/action/protocol/execution/protocol_test.go b/action/protocol/execution/protocol_test.go index a9fa5d005d..db31cc527c 100644 --- a/action/protocol/execution/protocol_test.go +++ b/action/protocol/execution/protocol_test.go @@ -110,7 +110,7 @@ type ( ) var ( - fixedTime = time.Unix(genesis.TestDefault().Timestamp, 0) + fixedTime = genesis.GenesisTimestamp(genesis.TestDefault().Timestamp) ) func (eb *ExpectedBalance) Balance() *big.Int { @@ -470,7 +470,7 @@ func (sct *SmartContractTest) prepareBlockchain( indexer, err := blockindex.NewIndexer(db.NewMemKVStore(), cfg.Genesis.Hash()) r.NoError(err) // create BlockDAO - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) r.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf, indexer}, cfg.DB.MaxCacheSize) r.NotNil(dao) @@ -697,7 +697,6 @@ func TestProtocol_Handle(t *testing.T) { cfg.Chain.ChainDBPath = testDBPath cfg.Chain.IndexDBPath = testIndexPath cfg.Chain.EnableAsyncIndexWrite = false - cfg.Genesis.EnableGravityChainVoting = false cfg.ActPool.MinGasPriceStr = "0" cfg.Genesis = genesis.TestDefault() cfg.Genesis.InitBalanceMap[identityset.Address(27).String()] = unit.ConvertIotxToRau(1000000000).String() @@ -722,7 +721,7 @@ func TestProtocol_Handle(t *testing.T) { require.NoError(err) // create BlockDAO cfg.DB.DbPath = cfg.Chain.ChainDBPath - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf, indexer}, cfg.DB.MaxCacheSize) require.NotNil(dao) diff --git a/action/protocol/execution/testdata/rolldice.json b/action/protocol/execution/testdata/rolldice.json index e6d8a508c1..9cbc7fc205 100644 --- a/action/protocol/execution/testdata/rolldice.json +++ b/action/protocol/execution/testdata/rolldice.json @@ -7,7 +7,7 @@ "rawBalance": "0" }, { "account": "io1zf69lmyzkkzlywwqzzggst45qupvx2cyye62fp", - "rawBalance": "12000000" + "rawBalance": "20000000" }], "deployments":[{ "rawByteCode": "608060405234801561001057600080fd5b506102f5806100206000396000f3006080604052600436106100615763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632885ad2c8114610066578063797d9fbd14610070578063cd5e3c5d14610091578063d0e30db0146100b8575b600080fd5b61006e6100c0565b005b61006e73ffffffffffffffffffffffffffffffffffffffff600435166100cb565b34801561009d57600080fd5b506100a6610159565b60408051918252519081900360200190f35b61006e610229565b6100c9336100cb565b565b60006100d5610159565b6040805182815290519192507fbae72e55df73720e0f671f4d20a331df0c0dc31092fda6c573f35ff7f37f283e919081900360200190a160405173ffffffffffffffffffffffffffffffffffffffff8316906305f5e100830280156108fc02916000818181858888f19350505050158015610154573d6000803e3d6000fd5b505050565b604080514460208083019190915260001943014082840152825180830384018152606090920192839052815160009360059361021a9360029391929182918401908083835b602083106101bd5780518252601f19909201916020918201910161019e565b51815160209384036101000a600019018019909216911617905260405191909301945091925050808303816000865af11580156101fe573d6000803e3d6000fd5b5050506040513d602081101561021357600080fd5b5051610261565b81151561022357fe5b06905090565b60408051348152905133917fe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c919081900360200190a2565b600080805b60208110156102c25780600101602060ff160360080260020a848260208110151561028d57fe5b7f010000000000000000000000000000000000000000000000000000000000000091901a810204029190910190600101610266565b50929150505600a165627a7a72305820a426929891673b0a04d7163b60113d28e7d0f48ea667680ba48126c182b872c10029", @@ -38,10 +38,10 @@ "rawGasPrice": "0", "expectedBalances": [{ "account": "io1rc2d2de7rtuucalsqv4d9ng0h297t63w7wvlph", - "rawBalance": "100000000" + "rawBalance": "0" }, { "account": "", - "rawBalance": "400000000" + "rawBalance": "500000000" }], "expectedLogs": [{}], "comment": "roll dice" @@ -51,13 +51,13 @@ "rawAmount": "0", "rawGasLimit": 1200000, "rawGasPrice": "10", - "rawExpectedGasConsumed": 27495, + "rawExpectedGasConsumed": 20795, "expectedBalances": [{ "account": "", - "rawBalance": "300000000" + "rawBalance": "500000000" }, { "account": "io1zf69lmyzkkzlywwqzzggst45qupvx2cyye62fp", - "rawBalance": "111725050" + "rawBalance": "19792050" }], "expectedLogs": [{}], "comment": "roll dice to self" diff --git a/action/protocol/generic_validator_test.go b/action/protocol/generic_validator_test.go index 669745d81b..8f89aef22d 100644 --- a/action/protocol/generic_validator_test.go +++ b/action/protocol/generic_validator_test.go @@ -11,7 +11,6 @@ import ( "math/big" "strings" "testing" - "time" "github.com/golang/mock/gomock" "github.com/iotexproject/iotex-address/address" @@ -53,7 +52,7 @@ func TestActionProtoAndGenericValidator(t *testing.T) { Tip: TipInfo{ Height: 0, Hash: g.Hash(), - Timestamp: time.Unix(g.Timestamp, 0), + Timestamp: genesis.GenesisTimestamp(g.Timestamp), }, }, ) diff --git a/api/grpcserver.go b/api/grpcserver.go index 3856a08fbd..b9a929c016 100644 --- a/api/grpcserver.go +++ b/api/grpcserver.go @@ -282,14 +282,14 @@ func (svr *gRPCHandler) GetBlockMetas(ctx context.Context, in *iotexapi.GetBlock return nil, status.Error(codes.NotFound, err.Error()) } for _, blkStore := range blkStores { - ret = append(ret, generateBlockMeta(blkStore)) + ret = append(ret, svr.generateBlockMeta(blkStore)) } case in.GetByHash() != nil: blk, err := svr.coreService.BlockByHash(in.GetByHash().BlkHash) if err != nil { return nil, status.Error(codes.NotFound, err.Error()) } - ret = []*iotextypes.BlockMeta{generateBlockMeta(blk)} + ret = []*iotextypes.BlockMeta{svr.generateBlockMeta(blk)} default: return nil, status.Error(codes.NotFound, "invalid GetBlockMetasRequest type") } @@ -723,7 +723,7 @@ func (svr *gRPCHandler) TraceTransactionStructLogs(ctx context.Context, in *iote } // generateBlockMeta generates BlockMeta from block -func generateBlockMeta(blkStore *apitypes.BlockWithReceipts) *iotextypes.BlockMeta { +func (svr *gRPCHandler) generateBlockMeta(blkStore *apitypes.BlockWithReceipts) *iotextypes.BlockMeta { blk := blkStore.Block header := blk.Header height := header.Height() @@ -736,7 +736,8 @@ func generateBlockMeta(blkStore *apitypes.BlockWithReceipts) *iotextypes.BlockMe producerAddress = header.ProducerAddress() h = header.HashBlock() } else { - h = block.GenesisHash() + g := svr.coreService.Genesis() + h = g.Hash() } txRoot := header.TxRoot() receiptRoot := header.ReceiptRoot() diff --git a/api/grpcserver_integrity_test.go b/api/grpcserver_integrity_test.go index e650f96286..e320171774 100644 --- a/api/grpcserver_integrity_test.go +++ b/api/grpcserver_integrity_test.go @@ -1123,8 +1123,6 @@ func TestGrpcServer_GetBlockMetasIntegrity(t *testing.T) { require := require.New(t) cfg := newConfig() cfg.api.GRPCPort = testutil.RandomPort() - genesis.SetGenesisTimestamp(cfg.genesis.Timestamp) - block.LoadGenesisHash(&cfg.genesis) svr, _, _, _, _, _, bfIndexFile, err := createServerV2(cfg, false) require.NoError(err) grpcHandler := newGRPCHandler(svr.core) @@ -1153,7 +1151,7 @@ func TestGrpcServer_GetBlockMetasIntegrity(t *testing.T) { require.Equal(test.gasUsed, meta.GasUsed) if test.start == 0 { // genesis block - h := block.GenesisHash() + h := cfg.genesis.Hash() require.Equal(meta.Hash, hex.EncodeToString(h[:])) } var prevBlkPb *iotextypes.BlockMeta @@ -2205,7 +2203,7 @@ func TestGrpcServer_GetRawBlocksIntegrity(t *testing.T) { header := blkInfos[0].Block.Header.Core require.EqualValues(version.ProtocolVersion, header.Version) require.Zero(header.Height) - ts := timestamppb.New(time.Unix(genesis.Timestamp(), 0)) + ts := timestamppb.New(time.Unix(cfg.genesis.Timestamp, 0)) require.Equal(ts, header.Timestamp) require.Equal(0, bytes.Compare(hash.ZeroHash256[:], header.PrevBlockHash)) require.Equal(0, bytes.Compare(hash.ZeroHash256[:], header.TxRoot)) diff --git a/api/grpcserver_test.go b/api/grpcserver_test.go index 554907cc31..8edcffc2ba 100644 --- a/api/grpcserver_test.go +++ b/api/grpcserver_test.go @@ -22,6 +22,7 @@ import ( "github.com/iotexproject/iotex-core/v2/action" apitypes "github.com/iotexproject/iotex-core/v2/api/types" "github.com/iotexproject/iotex-core/v2/blockchain/block" + "github.com/iotexproject/iotex-core/v2/blockchain/genesis" "github.com/iotexproject/iotex-core/v2/pkg/version" "github.com/iotexproject/iotex-core/v2/test/identityset" mock_apitypes "github.com/iotexproject/iotex-core/v2/test/mock/mock_apiresponder" @@ -280,6 +281,7 @@ func TestGrpcServer_GetBlockMetas(t *testing.T) { }) t.Run("GetBlockMetasByIndexSuccess", func(t *testing.T) { core.EXPECT().BlockByHeightRange(gomock.Any(), gomock.Any()).Return(rets, nil) + core.EXPECT().Genesis().Return(genesis.Default).Times(2) res, err := grpcSvr.GetBlockMetas(context.Background(), reqIndex) require.NoError(err) require.Equal(res.Total, uint64(1)) diff --git a/api/serverV2_integrity_test.go b/api/serverV2_integrity_test.go index 3dbdcfe080..957cdbd17c 100644 --- a/api/serverV2_integrity_test.go +++ b/api/serverV2_integrity_test.go @@ -312,7 +312,7 @@ func setupChain(cfg testConfig) (blockchain.Blockchain, blockdao.BlockDAO, block return nil, nil, nil, nil, nil, nil, nil, "", errors.New("failed to create bloomfilter indexer") } // create BlockDAO - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.genesis) if err != nil { return nil, nil, nil, nil, nil, nil, nil, "", errors.Wrap(err, "failed to create dao in memory") } diff --git a/api/web3server_marshal.go b/api/web3server_marshal.go index f5cd5f5855..5224c870f1 100644 --- a/api/web3server_marshal.go +++ b/api/web3server_marshal.go @@ -47,6 +47,7 @@ type ( } getBlockResult struct { + blkHash hash.Hash256 blk *block.Block transactions []interface{} } @@ -172,7 +173,7 @@ func (obj *getBlockResult) MarshalJSON() ([]byte, error) { producerAddress = obj.blk.Header.ProducerAddress() blkHash = obj.blk.Header.HashBlock() } else { - blkHash = block.GenesisHash() + blkHash = obj.blkHash } producerAddr, err := ioAddrToEthAddr(producerAddress) if err != nil { diff --git a/api/web3server_utils.go b/api/web3server_utils.go index 7869e7b5fd..80e8068163 100644 --- a/api/web3server_utils.go +++ b/api/web3server_utils.go @@ -107,7 +107,15 @@ func (svr *web3Handler) getBlockWithTransactions(blk *block.Block, receipts []*a transactions = append(transactions, "0x"+hex.EncodeToString(actHash[:])) } } + var bh hash.Hash256 + if blk.Height() != 0 { + bh = blk.HashBlock() + } else { + g := svr.coreService.Genesis() + bh = g.Hash() + } return &getBlockResult{ + blkHash: bh, blk: blk, transactions: transactions, }, nil diff --git a/blockchain/block/genesis_block.go b/blockchain/block/genesis_block.go index 7eaa70a37a..adc05b532e 100644 --- a/blockchain/block/genesis_block.go +++ b/blockchain/block/genesis_block.go @@ -6,27 +6,20 @@ package block import ( - "sync" "time" "github.com/iotexproject/go-pkgs/hash" - "github.com/iotexproject/iotex-core/v2/blockchain/genesis" "github.com/iotexproject/iotex-core/v2/pkg/version" ) -var ( - _loadGenesisHash sync.Once - _genesisHash hash.Hash256 -) - // GenesisBlock returns the genesis block -func GenesisBlock() *Block { +func GenesisBlock(timestamp time.Time) *Block { return &Block{ Header: Header{ version: version.ProtocolVersion, height: 0, - timestamp: time.Unix(genesis.Timestamp(), 0), + timestamp: timestamp, prevBlockHash: hash.ZeroHash256, txRoot: hash.ZeroHash256, deltaStateDigest: hash.ZeroHash256, @@ -34,15 +27,3 @@ func GenesisBlock() *Block { }, } } - -// GenesisHash returns the genesis block's hash -func GenesisHash() hash.Hash256 { - return _genesisHash -} - -// LoadGenesisHash is done once to compute and save the genesis'es hash -func LoadGenesisHash(g *genesis.Genesis) { - _loadGenesisHash.Do(func() { - _genesisHash = g.Hash() - }) -} diff --git a/blockchain/block/genesis_block_test.go b/blockchain/block/genesis_block_test.go index d663241e5c..b027b48e99 100644 --- a/blockchain/block/genesis_block_test.go +++ b/blockchain/block/genesis_block_test.go @@ -20,8 +20,7 @@ func TestGenesisBlock(t *testing.T) { r := require.New(t) g := genesis.Default - genesis.SetGenesisTimestamp(g.Timestamp) - blk := GenesisBlock() + blk := GenesisBlock(genesis.GenesisTimestamp(g.Timestamp)) r.EqualValues(version.ProtocolVersion, blk.Version()) r.Zero(blk.Height()) r.Equal(g.Timestamp, blk.Timestamp().Unix()) @@ -30,8 +29,6 @@ func TestGenesisBlock(t *testing.T) { r.Equal(hash.ZeroHash256, blk.DeltaStateDigest()) r.Equal(hash.ZeroHash256, blk.ReceiptRoot()) - r.Equal(hash.ZeroHash256, GenesisHash()) - LoadGenesisHash(&g) - h := GenesisHash() + h := g.Hash() r.Equal("b337983730981c2d50f114eed5da9dd20b83c8c5e130beefdb3001dc858cfe8b", hex.EncodeToString(h[:])) } diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index efc32b84b9..a22d71fb0b 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -483,7 +483,7 @@ func (bc *blockchain) tipInfo(tipHeight uint64) (*protocol.TipInfo, error) { return &protocol.TipInfo{ Height: 0, Hash: bc.genesis.Hash(), - Timestamp: time.Unix(bc.genesis.Timestamp, 0), + Timestamp: genesis.GenesisTimestamp(bc.genesis.Timestamp), }, nil } header, err := bc.dao.HeaderByHeight(tipHeight) diff --git a/blockchain/blockdao/blob_store_test.go b/blockchain/blockdao/blob_store_test.go index 594dc13931..b4a354448c 100644 --- a/blockchain/blockdao/blob_store_test.go +++ b/blockchain/blockdao/blob_store_test.go @@ -15,6 +15,7 @@ import ( "github.com/stretchr/testify/require" "github.com/iotexproject/iotex-core/v2/blockchain/block" + "github.com/iotexproject/iotex-core/v2/blockchain/genesis" "github.com/iotexproject/iotex-core/v2/db" "github.com/iotexproject/iotex-core/v2/db/batch" "github.com/iotexproject/iotex-core/v2/pkg/compress" @@ -99,7 +100,7 @@ func TestBlobStore(t *testing.T) { testPath1, err := testutil.PathOfTempFile("test-blob-store") r.NoError(err) cfg.DbPath = testPath1 - fd, err := createFileDAO(false, false, compress.Snappy, cfg) + fd, err := createFileDAO(false, false, compress.Snappy, genesis.Default, cfg) r.NoError(err) r.NotNil(fd) dao := NewBlockDAOWithIndexersAndCache(fd, nil, 10, WithBlobStore(bs)) diff --git a/blockchain/blockdao/blockdao.go b/blockchain/blockdao/blockdao.go index c19c12b976..325bec341f 100644 --- a/blockchain/blockdao/blockdao.go +++ b/blockchain/blockdao/blockdao.go @@ -168,11 +168,10 @@ func (dao *blockDAO) Stop(ctx context.Context) error { } func (dao *blockDAO) GetBlockHash(height uint64) (hash.Hash256, error) { - if height == 0 { - return block.GenesisHash(), nil - } - if header := dao.headerFromCache(height); header != nil { - return header.HashBlock(), nil + if height != 0 { + if header := dao.headerFromCache(height); header != nil { + return header.HashBlock(), nil + } } timer := dao.timerFactory.NewTimer("get_block_hash") defer timer.End() diff --git a/blockchain/blockdao/blockdao_test.go b/blockchain/blockdao/blockdao_test.go index 029c3cecae..ac232b5f57 100644 --- a/blockchain/blockdao/blockdao_test.go +++ b/blockchain/blockdao/blockdao_test.go @@ -759,9 +759,10 @@ func TestBlockDAO(t *testing.T) { }, } + g := genesis.TestDefault() testBlockDao := func(dao BlockStore, t *testing.T) { ctx := protocol.WithBlockchainCtx( - genesis.WithGenesisContext(context.Background(), genesis.TestDefault()), + genesis.WithGenesisContext(context.Background(), g), protocol.BlockchainCtx{ ChainID: 1, }) @@ -874,7 +875,7 @@ func TestBlockDAO(t *testing.T) { testDeleteDao := func(dao filedao.FileDAO, t *testing.T) { ctx := protocol.WithBlockchainCtx( - genesis.WithGenesisContext(context.Background(), genesis.TestDefault()), + genesis.WithGenesisContext(context.Background(), g), protocol.BlockchainCtx{ ChainID: 1, }) @@ -919,7 +920,7 @@ func TestBlockDAO(t *testing.T) { if tipHeight == 0 { h, err := dao.GetBlockHash(0) require.NoError(err) - require.Equal(block.GenesisHash(), h) + require.Equal(g.Hash(), h) continue } tipBlk := blks[tipHeight-1] @@ -1005,12 +1006,9 @@ func TestBlockDAO(t *testing.T) { cfg := db.DefaultConfig cfg.DbPath = testPath - g := genesis.TestDefault() - genesis.SetGenesisTimestamp(g.Timestamp) - block.LoadGenesisHash(&g) for _, v := range daoList { testutil.CleanupPath(testPath) - dao, err := createFileDAO(v.inMemory, v.legacy, v.compressBlock, cfg) + dao, err := createFileDAO(v.inMemory, v.legacy, v.compressBlock, g, cfg) require.NoError(err) require.NotNil(dao) t.Run("test store blocks", func(t *testing.T) { @@ -1020,7 +1018,7 @@ func TestBlockDAO(t *testing.T) { for _, v := range daoList { testutil.CleanupPath(testPath) - dao, err := createFileDAO(v.inMemory, v.legacy, v.compressBlock, cfg) + dao, err := createFileDAO(v.inMemory, v.legacy, v.compressBlock, g, cfg) require.NoError(err) require.NotNil(dao) t.Run("test delete blocks", func(t *testing.T) { @@ -1029,17 +1027,17 @@ func TestBlockDAO(t *testing.T) { } } -func createFileDAO(inMemory, legacy bool, compressBlock string, cfg db.Config) (filedao.FileDAO, error) { +func createFileDAO(inMemory, legacy bool, compressBlock string, g genesis.Genesis, cfg db.Config) (filedao.FileDAO, error) { if inMemory { - return filedao.NewFileDAOInMemForTest() + return filedao.NewFileDAOInMemForTest(g) } deser := block.NewDeserializer(4689) if legacy { - return filedao.CreateFileDAO(true, cfg, deser) + return filedao.CreateFileDAO(true, g, cfg, deser) } cfg.Compressor = compressBlock - return filedao.NewFileDAO(cfg, deser) + return filedao.NewFileDAO(g, cfg, deser) } func BenchmarkBlockCache(b *testing.B) { @@ -1061,7 +1059,7 @@ func BenchmarkBlockCache(b *testing.B) { cfg.DbPath = testPath cfg.MaxCacheSize = cacheSize deser := block.NewDeserializer(4689) - fileDAO, err := filedao.NewFileDAO(cfg, deser) + fileDAO, err := filedao.NewFileDAO(genesis.Default, cfg, deser) require.NoError(b, err) blkDao := NewBlockDAOWithIndexersAndCache(fileDAO, []BlockIndexer{}, cfg.MaxCacheSize) require.NoError(b, blkDao.Start(context.Background())) diff --git a/blockchain/blockdao/blockindexer.go b/blockchain/blockdao/blockindexer.go index 7b3d40c3a1..f6d452eec2 100644 --- a/blockchain/blockdao/blockindexer.go +++ b/blockchain/blockdao/blockindexer.go @@ -7,7 +7,6 @@ package blockdao import ( "context" - "time" "github.com/pkg/errors" "go.uber.org/zap" @@ -113,7 +112,7 @@ func (bic *BlockIndexerChecker) CheckIndexer(ctx context.Context, indexer BlockI bcCtx.Tip.ExcessBlobGas = tipBlk.ExcessBlobGas() } else { bcCtx.Tip.Hash = g.Hash() - bcCtx.Tip.Timestamp = time.Unix(g.Timestamp, 0) + bcCtx.Tip.Timestamp = genesis.GenesisTimestamp(g.Timestamp) } for { if err = indexer.PutBlock(protocol.WithFeatureCtx(protocol.WithBlockCtx( diff --git a/blockchain/blockdao/grpcblockdao.go b/blockchain/blockdao/grpcblockdao.go index 1209c8041d..bf9a0ddaaa 100644 --- a/blockchain/blockdao/grpcblockdao.go +++ b/blockchain/blockdao/grpcblockdao.go @@ -11,6 +11,7 @@ import ( "encoding/hex" "fmt" "sync/atomic" + "time" "github.com/iotexproject/go-pkgs/hash" "github.com/iotexproject/iotex-proto/golang/iotextypes" @@ -23,9 +24,11 @@ import ( "github.com/iotexproject/iotex-core/v2/action" "github.com/iotexproject/iotex-core/v2/blockchain/block" "github.com/iotexproject/iotex-core/v2/blockchain/blockdao/blockdaopb" + "github.com/iotexproject/iotex-core/v2/blockchain/genesis" ) type GrpcBlockDAO struct { + genesisTimestamp time.Time url string insecure bool conn *grpc.ClientConn @@ -43,14 +46,16 @@ var ( ) func NewGrpcBlockDAO( + g genesis.Genesis, url string, insecure bool, deserializer *block.Deserializer, ) *GrpcBlockDAO { return &GrpcBlockDAO{ - url: url, - insecure: insecure, - deserializer: deserializer, + genesisTimestamp: genesis.GenesisTimestamp(g.Timestamp), + url: url, + insecure: insecure, + deserializer: deserializer, } } @@ -136,7 +141,7 @@ func (gbd *GrpcBlockDAO) GetBlock(h hash.Hash256) (*block.Block, error) { func (gbd *GrpcBlockDAO) GetBlockByHeight(height uint64) (*block.Block, error) { if height == 0 { - return block.GenesisBlock(), nil + return block.GenesisBlock(gbd.genesisTimestamp), nil } response, err := gbd.client.GetBlockByHeight(context.Background(), &blockdaopb.BlockHeightRequest{ Height: height, diff --git a/blockchain/filedao/filedao.go b/blockchain/filedao/filedao.go index 831375d646..23803adf5f 100644 --- a/blockchain/filedao/filedao.go +++ b/blockchain/filedao/filedao.go @@ -18,6 +18,7 @@ import ( "github.com/iotexproject/iotex-core/v2/action" "github.com/iotexproject/iotex-core/v2/blockchain/block" + "github.com/iotexproject/iotex-core/v2/blockchain/genesis" "github.com/iotexproject/iotex-core/v2/db" "github.com/iotexproject/iotex-core/v2/pkg/log" ) @@ -71,6 +72,7 @@ type ( // fileDAO implements FileDAO fileDAO struct { + genesis genesis.Genesis lock sync.Mutex topIndex uint64 splitHeight uint64 @@ -83,14 +85,14 @@ type ( ) // NewFileDAO creates an instance of FileDAO -func NewFileDAO(cfg db.Config, deser *block.Deserializer) (FileDAO, error) { +func NewFileDAO(g genesis.Genesis, cfg db.Config, deser *block.Deserializer) (FileDAO, error) { header, err := readFileHeader(cfg.DbPath, FileAll) if err != nil { if err != ErrFileNotExist { return nil, err } // start new chain db using v2 format - if err := createNewV2File(1, cfg, deser); err != nil { + if err := createNewV2File(g, 1, cfg, deser); err != nil { return nil, err } header = &FileHeader{Version: FileV2} @@ -102,18 +104,18 @@ func NewFileDAO(cfg db.Config, deser *block.Deserializer) (FileDAO, error) { return nil, ErrFileInvalid case FileLegacyMaster: // master file is legacy format - return CreateFileDAO(true, cfg, deser) + return CreateFileDAO(true, g, cfg, deser) case FileV2: // master file is v2 format - return CreateFileDAO(false, cfg, deser) + return CreateFileDAO(false, g, cfg, deser) default: panic(fmt.Errorf("corrupted file version: %s", header.Version)) } } // NewFileDAOInMemForTest creates an in-memory FileDAO for testing -func NewFileDAOInMemForTest() (FileDAO, error) { - return newTestInMemFd() +func NewFileDAOInMemForTest(g genesis.Genesis) (FileDAO, error) { + return newTestInMemFd(g) } func (fd *fileDAO) Start(ctx context.Context) error { @@ -333,7 +335,7 @@ func (fd *fileDAO) addNewV2File(height uint64) error { // create a new v2 file cfg := fd.cfg cfg.DbPath = kthAuxFileName(cfg.DbPath, fd.topIndex+1) - v2, err := newFileDAOv2(height, cfg, fd.blockDeserializer) + v2, err := newFileDAOv2(fd.genesis, height, cfg, fd.blockDeserializer) if err != nil { return err } @@ -367,12 +369,12 @@ func (fd *fileDAO) DeleteTipBlock() error { } // CreateFileDAO creates FileDAO according to master file -func CreateFileDAO(legacy bool, cfg db.Config, deser *block.Deserializer) (FileDAO, error) { - fd := fileDAO{splitHeight: 1, cfg: cfg, blockDeserializer: deser} +func CreateFileDAO(legacy bool, g genesis.Genesis, cfg db.Config, deser *block.Deserializer) (FileDAO, error) { + fd := fileDAO{genesis: g, splitHeight: 1, cfg: cfg, blockDeserializer: deser} fds := []*fileDAOv2{} v2Top, v2Files := checkAuxFiles(cfg.DbPath, FileV2) if legacy { - legacyFd, err := newFileDAOLegacy(cfg, deser) + legacyFd, err := newFileDAOLegacy(g, cfg, deser) if err != nil { return nil, err } @@ -385,14 +387,14 @@ func CreateFileDAO(legacy bool, cfg db.Config, deser *block.Deserializer) (FileD } } else { // v2 master file - fds = append(fds, openFileDAOv2(cfg, deser)) + fds = append(fds, openFileDAOv2(g, cfg, deser)) } // populate v2 files into v2 manager if len(v2Files) > 0 { for _, name := range v2Files { cfg.DbPath = name - fds = append(fds, openFileDAOv2(cfg, deser)) + fds = append(fds, openFileDAOv2(g, cfg, deser)) } // v2 file's top index overrides v1's top @@ -407,8 +409,8 @@ func CreateFileDAO(legacy bool, cfg db.Config, deser *block.Deserializer) (FileD } // createNewV2File creates a new v2 chain db file -func createNewV2File(start uint64, cfg db.Config, deser *block.Deserializer) error { - v2, err := newFileDAOv2(start, cfg, deser) +func createNewV2File(g genesis.Genesis, start uint64, cfg db.Config, deser *block.Deserializer) error { + v2, err := newFileDAOv2(g, start, cfg, deser) if err != nil { return err } diff --git a/blockchain/filedao/filedao_legacy.go b/blockchain/filedao/filedao_legacy.go index 23e022ed35..381422a772 100644 --- a/blockchain/filedao/filedao_legacy.go +++ b/blockchain/filedao/filedao_legacy.go @@ -10,6 +10,7 @@ import ( "os" "sync" "sync/atomic" + "time" "github.com/iotexproject/go-pkgs/cache" "github.com/iotexproject/go-pkgs/hash" @@ -20,6 +21,7 @@ import ( "github.com/iotexproject/iotex-core/v2/action" "github.com/iotexproject/iotex-core/v2/blockchain/block" + "github.com/iotexproject/iotex-core/v2/blockchain/genesis" "github.com/iotexproject/iotex-core/v2/db" "github.com/iotexproject/iotex-core/v2/db/batch" "github.com/iotexproject/iotex-core/v2/pkg/compress" @@ -47,26 +49,30 @@ var ( type ( // fileDAOLegacy handles chain db file before file split activation at v1.1.2 fileDAOLegacy struct { - compressBlock bool - lifecycle lifecycle.Lifecycle - cfg db.Config - mutex sync.RWMutex // for create new db file - topIndex atomic.Value - htf db.RangeIndex - kvStore db.KVStore - kvStores cache.LRUCache //store like map[index]db.KVStore,index from 1...N - deser *block.Deserializer + compressBlock bool + genesisHash hash.Hash256 + genesisTimestamp time.Time + lifecycle lifecycle.Lifecycle + cfg db.Config + mutex sync.RWMutex // for create new db file + topIndex atomic.Value + htf db.RangeIndex + kvStore db.KVStore + kvStores cache.LRUCache //store like map[index]db.KVStore,index from 1...N + deser *block.Deserializer } ) // newFileDAOLegacy creates a new legacy file -func newFileDAOLegacy(cfg db.Config, deser *block.Deserializer) (FileDAO, error) { +func newFileDAOLegacy(g genesis.Genesis, cfg db.Config, deser *block.Deserializer) (FileDAO, error) { return &fileDAOLegacy{ - compressBlock: cfg.CompressLegacy, - cfg: cfg, - kvStore: db.NewBoltDB(cfg), - kvStores: cache.NewThreadSafeLruCache(0), - deser: deser, + genesisHash: g.Hash(), + genesisTimestamp: genesis.GenesisTimestamp(g.Timestamp), + compressBlock: cfg.CompressLegacy, + cfg: cfg, + kvStore: db.NewBoltDB(cfg), + kvStores: cache.NewThreadSafeLruCache(0), + deser: deser, }, nil } @@ -127,7 +133,7 @@ func (fd *fileDAOLegacy) Height() (uint64, error) { func (fd *fileDAOLegacy) GetBlockHash(height uint64) (hash.Hash256, error) { if height == 0 { - return block.GenesisHash(), nil + return fd.genesisHash, nil } h := hash.ZeroHash256 value, err := fd.kvStore.Get(_blockHashHeightMappingNS, heightKey(height)) @@ -142,7 +148,7 @@ func (fd *fileDAOLegacy) GetBlockHash(height uint64) (hash.Hash256, error) { } func (fd *fileDAOLegacy) GetBlockHeight(h hash.Hash256) (uint64, error) { - if h == block.GenesisHash() { + if h == fd.genesisHash { return 0, nil } value, err := getValueMustBe8Bytes(fd.kvStore, _blockHashHeightMappingNS, hashKey(h)) @@ -153,8 +159,8 @@ func (fd *fileDAOLegacy) GetBlockHeight(h hash.Hash256) (uint64, error) { } func (fd *fileDAOLegacy) GetBlock(h hash.Hash256) (*block.Block, error) { - if h == block.GenesisHash() { - return block.GenesisBlock(), nil + if h == fd.genesisHash { + return block.GenesisBlock(fd.genesisTimestamp), nil } header, err := fd.Header(h) if err != nil { diff --git a/blockchain/filedao/filedao_legacy_test.go b/blockchain/filedao/filedao_legacy_test.go index 90cc0d889c..c3b440854d 100644 --- a/blockchain/filedao/filedao_legacy_test.go +++ b/blockchain/filedao/filedao_legacy_test.go @@ -25,12 +25,12 @@ func TestFileDAOLegacy_PutBlock(t *testing.T) { var ( normalHeaderSize, compressHeaderSize int ) - testFdInterface := func(cfg db.Config, t *testing.T) { + testFdInterface := func(g genesis.Genesis, cfg db.Config, t *testing.T) { r := require.New(t) testutil.CleanupPath(cfg.DbPath) deser := block.NewDeserializer(_defaultEVMNetworkID) - fdLegacy, err := newFileDAOLegacy(cfg, deser) + fdLegacy, err := newFileDAOLegacy(g, cfg, deser) r.NoError(err) fd, ok := fdLegacy.(*fileDAOLegacy) r.True(ok) @@ -56,13 +56,13 @@ func TestFileDAOLegacy_PutBlock(t *testing.T) { // verify API for genesis block h, err = fd.GetBlockHash(0) r.NoError(err) - r.Equal(block.GenesisHash(), h) + r.Equal(g.Hash(), h) height, err := fd.GetBlockHeight(h) r.NoError(err) r.Zero(height) blk, err = fd.GetBlockByHeight(0) r.NoError(err) - r.Equal(block.GenesisBlock(), blk) + r.Equal(block.GenesisBlock(genesis.GenesisTimestamp(g.Timestamp)), blk) } r := require.New(t) @@ -75,12 +75,10 @@ func TestFileDAOLegacy_PutBlock(t *testing.T) { cfg := db.DefaultConfig cfg.DbPath = testPath g := genesis.TestDefault() - genesis.SetGenesisTimestamp(g.Timestamp) - block.LoadGenesisHash(&g) for _, compress := range []bool{false, true} { cfg.CompressLegacy = compress t.Run("test fileDAOLegacy interface", func(t *testing.T) { - testFdInterface(cfg, t) + testFdInterface(g, cfg, t) }) } @@ -96,7 +94,7 @@ func TestFileDAOLegacy_DeleteTipBlock(t *testing.T) { cfg.CompressLegacy = true // enable compress deser := block.NewDeserializer(_defaultEVMNetworkID) - fd, err := newFileDAOLegacy(cfg, deser) + fd, err := newFileDAOLegacy(genesis.Default, cfg, deser) r.NoError(err) legacy := fd.(*fileDAOLegacy) @@ -133,7 +131,7 @@ func TestFileDAOLegacy_getBlockValue(t *testing.T) { cfg.DbPath = "./filedao_legacy.db" deser := block.NewDeserializer(_defaultEVMNetworkID) - fd, err := newFileDAOLegacy(cfg, deser) + fd, err := newFileDAOLegacy(genesis.Default, cfg, deser) r.NoError(err) legacy := fd.(*fileDAOLegacy) diff --git a/blockchain/filedao/filedao_test.go b/blockchain/filedao/filedao_test.go index 582759c45f..6a98ceb2b5 100644 --- a/blockchain/filedao/filedao_test.go +++ b/blockchain/filedao/filedao_test.go @@ -16,6 +16,7 @@ import ( "github.com/stretchr/testify/require" "github.com/iotexproject/iotex-core/v2/blockchain/block" + "github.com/iotexproject/iotex-core/v2/blockchain/genesis" "github.com/iotexproject/iotex-core/v2/db" "github.com/iotexproject/iotex-core/v2/pkg/compress" ) @@ -69,7 +70,7 @@ func TestReadFileHeader(t *testing.T) { // empty legacy file is invalid deser := block.NewDeserializer(_defaultEVMNetworkID) - legacy, err := newFileDAOLegacy(cfg, deser) + legacy, err := newFileDAOLegacy(genesis.Default, cfg, deser) r.NoError(err) ctx := context.Background() r.NoError(legacy.Start(ctx)) @@ -110,7 +111,7 @@ func TestReadFileHeader(t *testing.T) { } os.RemoveAll(cfg.DbPath) // test valid v2 master file - r.NoError(createNewV2File(1, cfg, deser)) + r.NoError(createNewV2File(genesis.Default, 1, cfg, deser)) defer os.RemoveAll(cfg.DbPath) test2 := []testCheckFile{ @@ -144,7 +145,7 @@ func TestNewFileDAOSplitV2(t *testing.T) { // test empty db file, this will create new v2 file deser := block.NewDeserializer(_defaultEVMNetworkID) - fd, err := NewFileDAO(cfg, deser) + fd, err := NewFileDAO(genesis.Default, cfg, deser) r.NoError(err) r.NotNil(fd) h, err := readFileHeader(cfg.DbPath, FileAll) @@ -197,7 +198,7 @@ func TestNewFileDAOSplitLegacy(t *testing.T) { cfg.SplitDBSizeMB = 20 deser := block.NewDeserializer(_defaultEVMNetworkID) - fd, err := newFileDAOLegacy(cfg, deser) + fd, err := newFileDAOLegacy(genesis.Default, cfg, deser) r.NoError(err) ctx := context.Background() r.NoError(fd.Start(ctx)) @@ -211,7 +212,7 @@ func TestNewFileDAOSplitLegacy(t *testing.T) { // set FileDAO to split at height 15, 30 and 40 cfg.V2BlocksToSplitDB = 15 - fd, err = NewFileDAO(cfg, deser) + fd, err = NewFileDAO(genesis.Default, cfg, deser) r.NoError(err) r.NoError(fd.Start(ctx)) fm := fd.(*fileDAO) @@ -267,7 +268,7 @@ func TestNewFileDAOSplitLegacy(t *testing.T) { r.Equal(files[2], file4) // open 4 db files and verify again - fd, err = NewFileDAO(cfg, deser) + fd, err = NewFileDAO(genesis.Default, cfg, deser) fm = fd.(*fileDAO) r.EqualValues(4, fm.topIndex) r.EqualValues(1, fm.splitHeight) @@ -320,7 +321,7 @@ 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, deser)) + r.NoError(createNewV2File(genesis.Default, 1, cfg, deser)) } defer func() { for i := 1; i <= 3; i++ { diff --git a/blockchain/filedao/filedao_v2.go b/blockchain/filedao/filedao_v2.go index 54c3e2e990..3003e1e500 100644 --- a/blockchain/filedao/filedao_v2.go +++ b/blockchain/filedao/filedao_v2.go @@ -8,6 +8,7 @@ package filedao import ( "context" "sync/atomic" + "time" "unsafe" "github.com/pkg/errors" @@ -18,6 +19,7 @@ import ( "github.com/iotexproject/iotex-core/v2/action" "github.com/iotexproject/iotex-core/v2/blockchain/block" + "github.com/iotexproject/iotex-core/v2/blockchain/genesis" "github.com/iotexproject/iotex-core/v2/db" "github.com/iotexproject/iotex-core/v2/db/batch" "github.com/iotexproject/iotex-core/v2/pkg/util/byteutil" @@ -37,28 +39,32 @@ 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 - blkStorePbCache 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 + genesisHash hash.Hash256 + genesisTimestamp time.Time + filename string + header *FileHeader + tip *FileTip + blkBuffer *stagingBuffer + blkStorePbCache 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 } ) // newFileDAOv2 creates a new v2 file -func newFileDAOv2(bottom uint64, cfg db.Config, deser *block.Deserializer) (*fileDAOv2, error) { +func newFileDAOv2(g genesis.Genesis, bottom uint64, cfg db.Config, deser *block.Deserializer) (*fileDAOv2, error) { if bottom == 0 { return nil, ErrNotSupported } fd := fileDAOv2{ - filename: cfg.DbPath, + genesisHash: g.Hash(), + genesisTimestamp: genesis.GenesisTimestamp(g.Timestamp), + filename: cfg.DbPath, header: &FileHeader{ Version: FileV2, Compressor: cfg.Compressor, @@ -77,13 +83,15 @@ func newFileDAOv2(bottom uint64, cfg db.Config, deser *block.Deserializer) (*fil } // openFileDAOv2 opens an existing v2 file -func openFileDAOv2(cfg db.Config, deser *block.Deserializer) *fileDAOv2 { +func openFileDAOv2(g genesis.Genesis, cfg db.Config, deser *block.Deserializer) *fileDAOv2 { return &fileDAOv2{ - filename: cfg.DbPath, - blkStorePbCache: cache.NewThreadSafeLruCache(16), - kvStore: db.NewBoltDB(cfg), - batch: batch.NewBatch(), - deser: deser, + genesisHash: g.Hash(), + genesisTimestamp: genesis.GenesisTimestamp(g.Timestamp), + filename: cfg.DbPath, + blkStorePbCache: cache.NewThreadSafeLruCache(16), + kvStore: db.NewBoltDB(cfg), + batch: batch.NewBatch(), + deser: deser, } } @@ -150,7 +158,7 @@ func (fd *fileDAOv2) ContainsHeight(height uint64) bool { func (fd *fileDAOv2) GetBlockHash(height uint64) (hash.Hash256, error) { if height == 0 { - return block.GenesisHash(), nil + return fd.genesisHash, nil } if !fd.ContainsHeight(height) { return hash.ZeroHash256, db.ErrNotExist @@ -163,7 +171,7 @@ func (fd *fileDAOv2) GetBlockHash(height uint64) (hash.Hash256, error) { } func (fd *fileDAOv2) GetBlockHeight(h hash.Hash256) (uint64, error) { - if h == block.GenesisHash() { + if h == fd.genesisHash { return 0, nil } value, err := getValueMustBe8Bytes(fd.kvStore, _blockHashHeightMappingNS, hashKey(h)) @@ -183,7 +191,7 @@ func (fd *fileDAOv2) GetBlock(h hash.Hash256) (*block.Block, error) { func (fd *fileDAOv2) GetBlockByHeight(height uint64) (*block.Block, error) { if height == 0 { - return block.GenesisBlock(), nil + return block.GenesisBlock(fd.genesisTimestamp), nil } blk, err := fd.getBlock(height) if err != nil { diff --git a/blockchain/filedao/filedao_v2_test.go b/blockchain/filedao/filedao_v2_test.go index b7c0f66031..a1557a8cfb 100644 --- a/blockchain/filedao/filedao_v2_test.go +++ b/blockchain/filedao/filedao_v2_test.go @@ -94,12 +94,12 @@ func TestNewFileDAOv2(t *testing.T) { r.Equal(16, cfg.BlockStoreBatchSize) cfg.DbPath = testPath deser := block.NewDeserializer(_defaultEVMNetworkID) - _, err = newFileDAOv2(0, cfg, deser) + _, err = newFileDAOv2(genesis.Default, 0, cfg, deser) r.Equal(ErrNotSupported, err) - inMemFd, err := newFileDAOv2InMem(1) + inMemFd, err := newFileDAOv2InMem(genesis.Default, 1) r.NoError(err) - fd, err := newFileDAOv2(2, cfg, deser) + fd, err := newFileDAOv2(genesis.Default, 2, cfg, deser) r.NoError(err) for _, v2Fd := range []*fileDAOv2{inMemFd, fd} { @@ -110,12 +110,12 @@ func TestNewFileDAOv2(t *testing.T) { } func TestNewFdInterface(t *testing.T) { - testFdInterface := func(cfg db.Config, start uint64, t *testing.T) { + testFdInterface := func(g genesis.Genesis, cfg db.Config, start uint64, t *testing.T) { r := require.New(t) testutil.CleanupPath(cfg.DbPath) deser := block.NewDeserializer(_defaultEVMNetworkID) - fd, err := newFileDAOv2(start, cfg, deser) + fd, err := newFileDAOv2(g, start, cfg, deser) r.NoError(err) ctx := context.Background() @@ -140,13 +140,13 @@ func TestNewFdInterface(t *testing.T) { // verify API for genesis block h, err = fd.GetBlockHash(0) r.NoError(err) - r.Equal(block.GenesisHash(), h) + r.Equal(g.Hash(), h) height, err = fd.GetBlockHeight(h) r.NoError(err) r.Zero(height) blk, err = fd.GetBlock(h) r.NoError(err) - r.Equal(block.GenesisBlock(), blk) + r.Equal(block.GenesisBlock(genesis.GenesisTimestamp(g.Timestamp)), blk) // commit _blockStoreBatchSize blocks for i := uint64(0); i < fd.header.BlockStoreSize; i++ { @@ -238,7 +238,7 @@ func TestNewFdInterface(t *testing.T) { h, err = fd.GetBlockHash(height) if height == 0 { r.NoError(err) - r.Equal(block.GenesisHash(), h) + r.Equal(g.Hash(), h) } else { r.Equal(db.ErrNotExist, err) r.Equal(hash.ZeroHash256, h) @@ -257,17 +257,15 @@ func TestNewFdInterface(t *testing.T) { cfg := db.DefaultConfig cfg.DbPath = testPath deser := block.NewDeserializer(_defaultEVMNetworkID) - _, err = newFileDAOv2(0, cfg, deser) - r.Equal(ErrNotSupported, err) g := genesis.TestDefault() - genesis.SetGenesisTimestamp(g.Timestamp) - block.LoadGenesisHash(&g) + _, err = newFileDAOv2(g, 0, cfg, deser) + r.Equal(ErrNotSupported, err) for _, compress := range []string{"", compress.Snappy} { for _, start := range []uint64{1, 5, _blockStoreBatchSize + 1, 4 * _blockStoreBatchSize} { cfg.Compressor = compress t.Run("test fileDAOv2 interface", func(t *testing.T) { - testFdInterface(cfg, start, t) + testFdInterface(g, cfg, start, t) }) } } @@ -279,7 +277,7 @@ func TestNewFdStart(t *testing.T) { deser := block.NewDeserializer(_defaultEVMNetworkID) for _, num := range []uint64{3, _blockStoreBatchSize - 1, _blockStoreBatchSize, 2*_blockStoreBatchSize - 1} { testutil.CleanupPath(cfg.DbPath) - fd, err := newFileDAOv2(start, cfg, deser) + fd, err := newFileDAOv2(genesis.Default, start, cfg, deser) r.NoError(err) ctx := context.Background() r.NoError(fd.Start(ctx)) @@ -292,7 +290,7 @@ func TestNewFdStart(t *testing.T) { r.NoError(fd.Stop(ctx)) // start from existing file - fd = openFileDAOv2(cfg, deser) + fd = openFileDAOv2(genesis.Default, cfg, deser) r.NoError(fd.Start(ctx)) height, err = fd.Bottom() r.NoError(err) @@ -356,7 +354,7 @@ func TestBlockWithSidecar(t *testing.T) { testutil.CleanupPath(cfg.DbPath) r.Equal(5, cfg.BlockStoreBatchSize) deser := block.NewDeserializer(_defaultEVMNetworkID) - fd, err := newFileDAOv2(start, cfg, deser) + fd, err := newFileDAOv2(genesis.Default, start, cfg, deser) r.NoError(err) ctx := context.Background() r.NoError(fd.Start(ctx)) diff --git a/blockchain/filedao/testing.go b/blockchain/filedao/testing.go index 89d4dee001..a4d868969e 100644 --- a/blockchain/filedao/testing.go +++ b/blockchain/filedao/testing.go @@ -10,6 +10,7 @@ import ( "encoding/hex" "math/big" "testing" + "time" "github.com/pkg/errors" "github.com/stretchr/testify/require" @@ -32,6 +33,8 @@ var _defaultEVMNetworkID uint32 = 4689 type ( // testInMemFd is an in-memory FileDAO testInMemFd struct { + genesisHash hash.Hash256 + genesisTimestamp time.Time *fileDAOv2 } @@ -42,21 +45,27 @@ type ( } ) -func newTestInMemFd() (*testInMemFd, error) { - v2, err := newFileDAOv2InMem(1) +func newTestInMemFd(g genesis.Genesis) (*testInMemFd, error) { + v2, err := newFileDAOv2InMem(g, 1) if err != nil { return nil, err } - return &testInMemFd{fileDAOv2: v2}, nil + return &testInMemFd{ + genesisHash: g.Hash(), + genesisTimestamp: genesis.GenesisTimestamp(g.Timestamp), + fileDAOv2: v2, + }, nil } // newFileDAOv2InMem creates a in-memory new v2 fileDAO -func newFileDAOv2InMem(bottom uint64) (*fileDAOv2, error) { +func newFileDAOv2InMem(g genesis.Genesis, bottom uint64) (*fileDAOv2, error) { if bottom == 0 { return nil, ErrNotSupported } fd := fileDAOv2{ + genesisHash: g.Hash(), + genesisTimestamp: genesis.GenesisTimestamp(g.Timestamp), header: &FileHeader{ Version: FileV2, Compressor: "", @@ -76,28 +85,28 @@ func newFileDAOv2InMem(bottom uint64) (*fileDAOv2, error) { func (fd *testInMemFd) GetBlockHash(height uint64) (hash.Hash256, error) { if height == 0 { - return block.GenesisHash(), nil + return fd.genesisHash, nil } return fd.fileDAOv2.GetBlockHash(height) } func (fd *testInMemFd) GetBlockHeight(h hash.Hash256) (uint64, error) { - if h == block.GenesisHash() { + if h == fd.genesisHash { return 0, nil } return fd.fileDAOv2.GetBlockHeight(h) } func (fd *testInMemFd) GetBlock(h hash.Hash256) (*block.Block, error) { - if h == block.GenesisHash() { - return block.GenesisBlock(), nil + if h == fd.genesisHash { + return block.GenesisBlock(fd.genesisTimestamp), nil } return fd.fileDAOv2.GetBlock(h) } func (fd *testInMemFd) GetBlockByHeight(height uint64) (*block.Block, error) { if height == 0 { - return block.GenesisBlock(), nil + return block.GenesisBlock(fd.genesisTimestamp), nil } return fd.fileDAOv2.GetBlockByHeight(height) } @@ -224,8 +233,6 @@ func testVerifyChainDB(t *testing.T, fd FileDAO, start, end uint64) { } func createTestingBlock(builder *block.TestingBuilder, height uint64, h hash.Hash256) *block.Block { - g := genesis.TestDefault() - block.LoadGenesisHash(&g) r := &action.Receipt{ Status: 1, BlockHeight: height, diff --git a/blockchain/genesis/genesis.go b/blockchain/genesis/genesis.go index c23733b2d3..3e7de4ea8e 100644 --- a/blockchain/genesis/genesis.go +++ b/blockchain/genesis/genesis.go @@ -10,7 +10,6 @@ import ( "math/big" "sort" "sync" - "sync/atomic" "time" "github.com/ethereum/go-ethereum/common" @@ -504,16 +503,9 @@ func New(genesisPath string) (Genesis, error) { return genesis, nil } -// SetGenesisTimestamp sets the genesis timestamp -func SetGenesisTimestamp(ts int64) { - _loadGenesisTs.Do(func() { - _genesisTs = ts - }) -} - -// Timestamp returns the genesis timestamp -func Timestamp() int64 { - return atomic.LoadInt64(&_genesisTs) +// GenesisTimestamp returns the genesis timestamp +func GenesisTimestamp(sec int64) time.Time { + return time.Unix(sec, 0) } // Hash is the hash of genesis config diff --git a/blockchain/integrity/benchmark_test.go b/blockchain/integrity/benchmark_test.go index d74e80cb89..5036957178 100644 --- a/blockchain/integrity/benchmark_test.go +++ b/blockchain/integrity/benchmark_test.go @@ -261,7 +261,7 @@ func newChainInDB() (blockchain.Blockchain, actpool.ActPool, error) { // create BlockDAO cfg.DB.DbPath = cfg.Chain.ChainDBPath deser := block.NewDeserializer(cfg.Chain.EVMNetworkID) - store, err := filedao.NewFileDAO(cfg.DB, deser) + store, err := filedao.NewFileDAO(cfg.Genesis, cfg.DB, deser) if err != nil { return nil, nil, err } diff --git a/blockchain/integrity/integrity_test.go b/blockchain/integrity/integrity_test.go index 34f36f52dd..467986519f 100644 --- a/blockchain/integrity/integrity_test.go +++ b/blockchain/integrity/integrity_test.go @@ -494,7 +494,7 @@ func TestCreateBlockchain(t *testing.T) { require.NoError(err) ap, err := actpool.NewActPool(cfg.Genesis, sf, cfg.ActPool) require.NoError(err) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, cfg.DB.MaxCacheSize) bc := blockchain.NewBlockchain( @@ -537,8 +537,6 @@ func TestGetBlockHash(t *testing.T) { cfg.Genesis.HawaiiBlockHeight = 4 cfg.Genesis.MidwayBlockHeight = 9 cfg.ActPool.MinGasPriceStr = "0" - genesis.SetGenesisTimestamp(cfg.Genesis.Timestamp) - block.LoadGenesisHash(&cfg.Genesis) // create chain registry := protocol.NewRegistry() acc := account.NewProtocol(rewarding.DepositGas) @@ -550,7 +548,7 @@ func TestGetBlockHash(t *testing.T) { require.NoError(err) ap, err := actpool.NewActPool(cfg.Genesis, sf, cfg.ActPool) require.NoError(err) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, cfg.DB.MaxCacheSize) bc := blockchain.NewBlockchain( @@ -715,7 +713,7 @@ func TestBlockchain_MintNewBlock(t *testing.T) { require.NoError(t, err) ap, err := actpool.NewActPool(cfg.Genesis, sf, cfg.ActPool) require.NoError(t, err) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(t, err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, cfg.DB.MaxCacheSize) bc := blockchain.NewBlockchain( @@ -785,7 +783,7 @@ func TestBlockchain_MintNewBlock_PopAccount(t *testing.T) { require.NoError(t, err) ap, err := actpool.NewActPool(cfg.Genesis, sf, cfg.ActPool) require.NoError(t, err) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(t, err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, cfg.DB.MaxCacheSize) bc := blockchain.NewBlockchain( @@ -908,13 +906,13 @@ func createChain(cfg config.Config, inMem bool) (blockchain.Blockchain, factory. var store blockdao.BlockStore // create BlockDAO if inMem { - store, err = filedao.NewFileDAOInMemForTest() + store, err = filedao.NewFileDAOInMemForTest(cfg.Genesis) if err != nil { return nil, nil, nil, nil, err } } else { cfg.DB.DbPath = cfg.Chain.ChainDBPath - store, err = filedao.NewFileDAO(cfg.DB, block.NewDeserializer(cfg.Chain.EVMNetworkID)) + store, err = filedao.NewFileDAO(cfg.Genesis, cfg.DB, block.NewDeserializer(cfg.Chain.EVMNetworkID)) } if err != nil { return nil, nil, nil, nil, err @@ -1472,7 +1470,7 @@ func TestConstantinople(t *testing.T) { require.NoError(err) // create BlockDAO cfg.DB.DbPath = cfg.Chain.ChainDBPath - store, err := filedao.NewFileDAO(cfg.DB, block.NewDeserializer(cfg.Chain.EVMNetworkID)) + store, err := filedao.NewFileDAO(cfg.Genesis, cfg.DB, block.NewDeserializer(cfg.Chain.EVMNetworkID)) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf, indexer}, cfg.DB.MaxCacheSize) require.NotNil(dao) @@ -1726,7 +1724,7 @@ func TestLoadBlockchainfromDB(t *testing.T) { cfg.Genesis.InitBalanceMap[identityset.Address(27).String()] = unit.ConvertIotxToRau(10000000000).String() // create BlockDAO cfg.DB.DbPath = cfg.Chain.ChainDBPath - store, err := filedao.NewFileDAO(cfg.DB, block.NewDeserializer(cfg.Chain.EVMNetworkID)) + store, err := filedao.NewFileDAO(cfg.Genesis, cfg.DB, block.NewDeserializer(cfg.Chain.EVMNetworkID)) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, indexers, cfg.DB.MaxCacheSize) require.NotNil(dao) @@ -1943,8 +1941,6 @@ func TestLoadBlockchainfromDB(t *testing.T) { cfg.Chain.ChainDBPath = testDBPath cfg.Chain.IndexDBPath = testIndexPath cfg.ActPool.MinGasPriceStr = "0" - genesis.SetGenesisTimestamp(cfg.Genesis.Timestamp) - block.LoadGenesisHash(&cfg.Genesis) t.Run("load blockchain from DB w/o explorer", func(t *testing.T) { testValidateBlockchain(cfg, t) @@ -2037,7 +2033,7 @@ func TestBlockchainInitialCandidate(t *testing.T) { require.NoError(accountProtocol.Register(registry)) dbcfg := cfg.DB dbcfg.DbPath = cfg.Chain.ChainDBPath - store, err := filedao.NewFileDAO(dbcfg, block.NewDeserializer(cfg.Chain.EVMNetworkID)) + store, err := filedao.NewFileDAO(cfg.Genesis, dbcfg, block.NewDeserializer(cfg.Chain.EVMNetworkID)) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, dbcfg.MaxCacheSize) bc := blockchain.NewBlockchain( @@ -2084,7 +2080,7 @@ func TestBlockchain_AccountState(t *testing.T) { require.NoError(err) ap, err := actpool.NewActPool(cfg.Genesis, sf, cfg.ActPool) require.NoError(err) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, cfg.DB.MaxCacheSize) bc := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao, factory.NewMinter(sf, ap)) @@ -2116,7 +2112,7 @@ func TestNewAccountAction(t *testing.T) { require.NoError(err) ap, err := actpool.NewActPool(cfg.Genesis, sf, cfg.ActPool) require.NoError(err) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, cfg.DB.MaxCacheSize) bc := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao, factory.NewMinter(sf, ap)) @@ -2161,7 +2157,7 @@ func TestNewAccountAction(t *testing.T) { factoryCfg = factory.GenerateConfig(cfg.Chain, cfg.Genesis) sf1, err := factory.NewFactory(factoryCfg, db.NewMemKVStore(), factory.RegistryOption(registry)) require.NoError(err) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao1 := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf1}, cfg.DB.MaxCacheSize) bc1 := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao1, factory.NewMinter(sf1, ap)) @@ -2228,7 +2224,7 @@ func TestBlocks(t *testing.T) { require.NoError(err) dbcfg := cfg.DB dbcfg.DbPath = cfg.Chain.ChainDBPath - store, err := filedao.NewFileDAO(dbcfg, block.NewDeserializer(cfg.Chain.EVMNetworkID)) + store, err := filedao.NewFileDAO(cfg.Genesis, dbcfg, block.NewDeserializer(cfg.Chain.EVMNetworkID)) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, dbcfg.MaxCacheSize) @@ -2303,7 +2299,7 @@ func TestActions(t *testing.T) { require.NoError(err) dbcfg := cfg.DB dbcfg.DbPath = cfg.Chain.ChainDBPath - store, err := filedao.NewFileDAO(dbcfg, block.NewDeserializer(cfg.Chain.EVMNetworkID)) + store, err := filedao.NewFileDAO(cfg.Genesis, dbcfg, block.NewDeserializer(cfg.Chain.EVMNetworkID)) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, dbcfg.MaxCacheSize) // Create a blockchain from scratch @@ -2368,7 +2364,7 @@ func TestBlockchain_AddRemoveSubscriber(t *testing.T) { req.NoError(err) ap, err := actpool.NewActPool(cfg.Genesis, sf, cfg.ActPool) req.NoError(err) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) req.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, cfg.DB.MaxCacheSize) bc := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao, factory.NewMinter(sf, ap)) @@ -2598,7 +2594,7 @@ func newChain(t *testing.T, stateTX bool) (blockchain.Blockchain, factory.Factor cfg.Genesis.InitBalanceMap[identityset.Address(27).String()] = unit.ConvertIotxToRau(10000000000).String() // create BlockDAO cfg.DB.DbPath = cfg.Chain.ChainDBPath - store, err := filedao.NewFileDAO(cfg.DB, block.NewDeserializer(cfg.Chain.EVMNetworkID)) + store, err := filedao.NewFileDAO(cfg.Genesis, cfg.DB, block.NewDeserializer(cfg.Chain.EVMNetworkID)) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, indexers, cfg.DB.MaxCacheSize) require.NotNil(dao) diff --git a/blockindex/indexbuilder_test.go b/blockindex/indexbuilder_test.go index 89284641cd..d12128e816 100644 --- a/blockindex/indexbuilder_test.go +++ b/blockindex/indexbuilder_test.go @@ -61,8 +61,8 @@ func TestIndexBuilder(t *testing.T) { }, } + g := genesis.TestDefault() testIndexer := func(dao blockdao.BlockStore, indexer Indexer, t *testing.T) { - g := genesis.TestDefault() ctx := protocol.WithBlockchainCtx( genesis.WithGenesisContext(context.Background(), g), protocol.BlockchainCtx{ @@ -163,9 +163,9 @@ func TestIndexBuilder(t *testing.T) { }() cfg := db.DefaultConfig cfg.DbPath = testPath - filestore, err := filedao.NewFileDAO(cfg, block.NewDeserializer(blockchain.DefaultConfig.EVMNetworkID)) + filestore, err := filedao.NewFileDAO(g, cfg, block.NewDeserializer(blockchain.DefaultConfig.EVMNetworkID)) require.NoError(err) - memstore, err := filedao.NewFileDAOInMemForTest() + memstore, err := filedao.NewFileDAOInMemForTest(g) require.NoError(err) for _, v := range []struct { dao blockdao.BlockStore diff --git a/blocksync/blocksync_test.go b/blocksync/blocksync_test.go index 26808a22b9..49002f70d7 100644 --- a/blocksync/blocksync_test.go +++ b/blocksync/blocksync_test.go @@ -201,7 +201,7 @@ func TestBlockSyncerProcessBlockTipHeight(t *testing.T) { require.NotNil(ap) require.NoError(err) ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, 16) chain := blockchain.NewBlockchain( @@ -267,7 +267,7 @@ func TestBlockSyncerProcessBlockOutOfOrder(t *testing.T) { require.NotNil(ap1) require.NoError(err) ap1.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, 16) chain1 := blockchain.NewBlockchain( @@ -294,7 +294,7 @@ func TestBlockSyncerProcessBlockOutOfOrder(t *testing.T) { require.NotNil(ap2) require.NoError(err) ap2.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf2, accountutil.AccountState)) - store2, err := filedao.NewFileDAOInMemForTest() + store2, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao2 := blockdao.NewBlockDAOWithIndexersAndCache(store2, []blockdao.BlockIndexer{sf2}, 16) chain2 := blockchain.NewBlockchain( @@ -369,7 +369,7 @@ func TestBlockSyncerProcessBlock(t *testing.T) { require.NotNil(ap1) require.NoError(err) ap1.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, 16) chain1 := blockchain.NewBlockchain( @@ -395,7 +395,7 @@ func TestBlockSyncerProcessBlock(t *testing.T) { require.NotNil(ap2) require.NoError(err) ap2.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf2, accountutil.AccountState)) - store2, err := filedao.NewFileDAOInMemForTest() + store2, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao2 := blockdao.NewBlockDAOWithIndexersAndCache(store2, []blockdao.BlockIndexer{sf2}, 16) chain2 := blockchain.NewBlockchain( @@ -463,7 +463,7 @@ func TestBlockSyncerSync(t *testing.T) { require.NotNil(ap) require.NoError(err) ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, 16) chain := blockchain.NewBlockchain( diff --git a/blocksync/buffer_test.go b/blocksync/buffer_test.go index 0d05f1d695..1ab5eb800f 100644 --- a/blocksync/buffer_test.go +++ b/blocksync/buffer_test.go @@ -48,7 +48,7 @@ func TestBlockBufferFlush(t *testing.T) { require.NotNil(ap) require.NoError(err) ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, 16) chain := blockchain.NewBlockchain( @@ -142,7 +142,7 @@ func TestBlockBufferGetBlocksIntervalsToSync(t *testing.T) { ap, err := actpool.NewActPool(cfg.Genesis, sf, cfg.ActPool) require.NotNil(ap) require.NoError(err) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, 16) chain := blockchain.NewBlockchain( diff --git a/chainservice/builder.go b/chainservice/builder.go index 65a0d531a7..3dbe3c9b8c 100644 --- a/chainservice/builder.go +++ b/chainservice/builder.go @@ -324,7 +324,7 @@ func (builder *Builder) buildBlockDAO(forTest bool) error { opts []blockdao.Option ) if forTest { - store, err = filedao.NewFileDAOInMemForTest() + store, err = filedao.NewFileDAOInMemForTest(builder.cfg.Genesis) } else { path := builder.cfg.Chain.ChainDBPath uri, err := url.Parse(path) @@ -333,11 +333,11 @@ func (builder *Builder) buildBlockDAO(forTest bool) error { } switch uri.Scheme { case "grpc": - store = blockdao.NewGrpcBlockDAO(uri.Host, uri.Query().Get("insecure") == "true", block.NewDeserializer(builder.cfg.Chain.EVMNetworkID)) + store = blockdao.NewGrpcBlockDAO(builder.cfg.Genesis, uri.Host, uri.Query().Get("insecure") == "true", block.NewDeserializer(builder.cfg.Chain.EVMNetworkID)) case "file", "": dbConfig := cfg.DB dbConfig.DbPath = uri.Path - store, err = filedao.NewFileDAO(dbConfig, block.NewDeserializer(builder.cfg.Chain.EVMNetworkID)) + store, err = filedao.NewFileDAO(builder.cfg.Genesis, dbConfig, block.NewDeserializer(builder.cfg.Chain.EVMNetworkID)) default: return errors.Errorf("unsupported blockdao scheme %s", uri.Scheme) } diff --git a/config/config_test.go b/config/config_test.go index 485da09d00..98ff884cfe 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -16,8 +16,6 @@ import ( "github.com/iotexproject/go-pkgs/crypto" "github.com/pkg/errors" "github.com/stretchr/testify/require" - - "github.com/iotexproject/iotex-core/v2/blockchain/genesis" ) const ( @@ -108,13 +106,6 @@ func TestStrs_String(t *testing.T) { require.Nil(t, ss.Set(str)) } -func TestNewDefaultConfig(t *testing.T) { - cfg, err := New([]string{}, []string{}) - require.NoError(t, err) - genesis.SetGenesisTimestamp(cfg.Genesis.Timestamp) - require.Equal(t, cfg.Genesis.Timestamp, genesis.Timestamp()) -} - func TestNewConfigWithoutValidation(t *testing.T) { cfg, err := New([]string{}, []string{}, DoNotValidate) require.NoError(t, err) diff --git a/consensus/scheme/rolldpos/rolldpos.go b/consensus/scheme/rolldpos/rolldpos.go index 5ca1cdf3fe..2cb5d019f9 100644 --- a/consensus/scheme/rolldpos/rolldpos.go +++ b/consensus/scheme/rolldpos/rolldpos.go @@ -95,7 +95,7 @@ func NewChainManager(bc blockchain.Blockchain) ChainManager { // BlockProposeTime return propose time by height func (cm *chainManager) BlockProposeTime(height uint64) (time.Time, error) { if height == 0 { - return time.Unix(cm.bc.Genesis().Timestamp, 0), nil + return genesis.GenesisTimestamp(cm.bc.Genesis().Timestamp), nil } header, err := cm.bc.BlockHeaderByHeight(height) if err != nil { diff --git a/consensus/scheme/rolldpos/rolldpos_test.go b/consensus/scheme/rolldpos/rolldpos_test.go index d93db94f62..10ed82db4f 100644 --- a/consensus/scheme/rolldpos/rolldpos_test.go +++ b/consensus/scheme/rolldpos/rolldpos_test.go @@ -461,7 +461,7 @@ func TestRollDPoSConsensus(t *testing.T) { require.NoError(t, acc.Register(registry)) rp := rolldpos.NewProtocol(g.NumCandidateDelegates, g.NumDelegates, g.NumSubEpochs) require.NoError(t, rp.Register(registry)) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(g) require.NoError(t, err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, db.DefaultConfig.MaxCacheSize) chain := blockchain.NewBlockchain( diff --git a/consensus/scheme/rolldpos/roundcalculator_test.go b/consensus/scheme/rolldpos/roundcalculator_test.go index 99cc8726da..7d8fe71103 100644 --- a/consensus/scheme/rolldpos/roundcalculator_test.go +++ b/consensus/scheme/rolldpos/roundcalculator_test.go @@ -186,7 +186,7 @@ func makeChain(t *testing.T) (blockchain.Blockchain, factory.Factory, actpool.Ac require.NoError(err) dbcfg := db.DefaultConfig dbcfg.DbPath = cfg.ChainDBPath - store, err := filedao.NewFileDAO(dbcfg, block.NewDeserializer(cfg.EVMNetworkID)) + store, err := filedao.NewFileDAO(g, dbcfg, block.NewDeserializer(cfg.EVMNetworkID)) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, dbcfg.MaxCacheSize) chain := blockchain.NewBlockchain( diff --git a/e2etest/bigint_test.go b/e2etest/bigint_test.go index 9148a7ff4f..10ad40f6fa 100644 --- a/e2etest/bigint_test.go +++ b/e2etest/bigint_test.go @@ -99,7 +99,7 @@ func prepareBlockchain(ctx context.Context, _executor string, r *require.Asserti ap, err := actpool.NewActPool(cfg.Genesis, sf, cfg.ActPool) r.NoError(err) ap.AddActionEnvelopeValidators(genericValidator) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) r.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, cfg.DB.MaxCacheSize) bc := blockchain.NewBlockchain( diff --git a/e2etest/contract_staking_test.go b/e2etest/contract_staking_test.go index 86bc4552e0..dfe82895d2 100644 --- a/e2etest/contract_staking_test.go +++ b/e2etest/contract_staking_test.go @@ -1972,7 +1972,7 @@ func prepareContractStakingBlockchain(ctx context.Context, cfg config.Config, r }) r.NoError(err) // create BlockDAO - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) r.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache( store, diff --git a/e2etest/local_test.go b/e2etest/local_test.go index 7ffa74d865..8ad20cd0ff 100644 --- a/e2etest/local_test.go +++ b/e2etest/local_test.go @@ -160,7 +160,7 @@ func TestLocalCommit(t *testing.T) { require.NoError(err) dbcfg := cfg.DB dbcfg.DbPath = cfg.Chain.ChainDBPath - store, err := filedao.NewFileDAO(dbcfg, block.NewDeserializer(cfg.Chain.EVMNetworkID)) + store, err := filedao.NewFileDAO(cfg.Genesis, dbcfg, block.NewDeserializer(cfg.Chain.EVMNetworkID)) require.NoError(err) dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf2}, dbcfg.MaxCacheSize) chain := blockchain.NewBlockchain( @@ -489,7 +489,7 @@ func TestStartExistingBlockchain(t *testing.T) { // Recover to height 3 from empty state DB cfg.DB.DbPath = cfg.Chain.ChainDBPath deser := block.NewDeserializer(cfg.Chain.EVMNetworkID) - dao, err := filedao.NewFileDAO(cfg.DB, deser) + dao, err := filedao.NewFileDAO(cfg.Genesis, cfg.DB, deser) require.NoError(err) require.NoError(dao.Start(protocol.WithBlockchainCtx( genesis.WithGenesisContext(ctx, cfg.Genesis), @@ -520,7 +520,7 @@ func TestStartExistingBlockchain(t *testing.T) { // Recover to height 2 from an existing state DB with Height 3 require.NoError(svr.Stop(ctx)) cfg.DB.DbPath = cfg.Chain.ChainDBPath - dao, err = filedao.NewFileDAO(cfg.DB, deser) + dao, err = filedao.NewFileDAO(cfg.Genesis, cfg.DB, deser) require.NoError(err) require.NoError(dao.Start(protocol.WithBlockchainCtx( genesis.WithGenesisContext(ctx, cfg.Genesis), diff --git a/e2etest/local_transfer_test.go b/e2etest/local_transfer_test.go index db1f466ad0..3e5a35d997 100644 --- a/e2etest/local_transfer_test.go +++ b/e2etest/local_transfer_test.go @@ -672,7 +672,7 @@ func TestEnforceChainID(t *testing.T) { require.NoError(err) ap, err := actpool.NewActPool(cfg.Genesis, sf, cfg.ActPool) require.NoError(err) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(err) blkMemDao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, cfg.DB.MaxCacheSize) bc := blockchain.NewBlockchain( diff --git a/e2etest/native_staking_test.go b/e2etest/native_staking_test.go index af65c5a23f..62cfdec79e 100644 --- a/e2etest/native_staking_test.go +++ b/e2etest/native_staking_test.go @@ -113,7 +113,7 @@ func TestNativeStaking(t *testing.T) { cand3Addr := identityset.Address(4) cand3PriKey := identityset.PrivateKey(4) - fixedTime := time.Unix(cfg.Genesis.Timestamp, 0) + fixedTime := genesis.GenesisTimestamp(cfg.Genesis.Timestamp) addOneTx := func(tx *action.SealedEnvelope, err error) (*action.SealedEnvelope, *action.Receipt, error) { if err != nil { return tx, nil, err diff --git a/e2etest/staking_test.go b/e2etest/staking_test.go index 11e218c76c..29b58f4711 100644 --- a/e2etest/staking_test.go +++ b/e2etest/staking_test.go @@ -67,7 +67,7 @@ func TestStakingContract(t *testing.T) { // deploy staking contract data, _ := hex.DecodeString("60806040526000805460a060020a60ff021916905534801561002057600080fd5b5060405160408061309d8339810180604052810190808051906020019092919080519060200190929190505050336000806101000a815481600160a060020a030219169083600160a060020a031602179055508160028190555080600381905550610140604051908101604052806000600160a060020a031916815260200160018152602001600081526020014281526020016001151581526020016000815260200133600160a060020a031681526020014281526020016000815260200160008152506004600080815260200190815260200160002060008201518160000160006101000a8154816001606060020a030219169083740100000000000000000000000000000000000000009004021790555060208201518160010155604082015181600201556060820151816003015560808201518160040160006101000a81548160ff02191690831515021790555060a0820151816005015560c08201518160060160006101000a815481600160a060020a030219169083600160a060020a0316021790555060e08201518160070155610100820151816008015561012082015181600901559050506006600033600160a060020a0316600160a060020a031681526020019081526020016000206000908060018154018082558091505090600182039060005260206000200160009091929091909150555060016005819055505050612e69806102346000396000f30060806040526004361061018a5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663030ba25d811461018f57806307c35fc0146101b55780631b0690ed146101f257806324953eaa14610207578063286dd3f5146102705780632f54bf6e146102915780633f4ba83a146102b2578063423ce1ae146102c75780635c975abb146102eb5780635fec5c641461030057806363809953146103155780636c0a5ebd1461032a5780636e7b3017146103e557806376f70003146103fc5780637b24a5fd146104115780637b9417c81461043f5780637d56493714610460578063817b1cd2146104915780638456cb59146104a65780638da5cb5b146104bb57806394a9c0f9146104ec5780639b19251a14610562578063c698d49514610583578063c8fd6ed014610598578063ccfafd5c146105bc578063d09daa9914610638578063d3e41fd2146106a9578063df43a94e146106db578063e2ec6ec3146108ef578063f188768414610944578063f2fde38b14610959575b600080fd5b34801561019b57600080fd5b506101b360048035906024803590810191013561097a565b005b6101e060048035600160a060020a031916906024803591604435151591606435908101910135610d09565b60408051918252519081900360200190f35b3480156101fe57600080fd5b506101e06111e5565b34801561021357600080fd5b506040805160206004803580820135838102808601850190965280855261025c953695939460249493850192918291850190849080828437509497506111eb9650505050505050565b604080519115158252519081900360200190f35b34801561027c57600080fd5b5061025c600160a060020a0360043516611247565b34801561029d57600080fd5b5061025c600160a060020a03600435166112dc565b3480156102be57600080fd5b506101b36112f0565b3480156102d357600080fd5b506101e0600160a060020a0360043516602435611363565b3480156102f757600080fd5b5061025c611393565b34801561030c57600080fd5b506101e06113a3565b34801561032157600080fd5b506101e06113a8565b34801561033657600080fd5b506103456004356024356113af565b604051808481526020018060200180602001838103835285818151815260200191508051906020019060200280838360005b8381101561038f578181015183820152602001610377565b50505050905001838103825284818151815260200191508051906020019060200280838360005b838110156103ce5781810151838201526020016103b6565b505050509050019550505050505060405180910390f35b6101b360048035906024803590810191013561145e565b34801561040857600080fd5b506101e061169f565b34801561041d57600080fd5b506101b3600480359060248035916044351515916064359081019101356116a5565b34801561044b57600080fd5b5061025c600160a060020a0360043516611a4c565b34801561046c57600080fd5b506101b3600480359060248035600160a060020a031691604435918201910135611ae5565b34801561049d57600080fd5b506101e0611c34565b3480156104b257600080fd5b506101b3611c3a565b3480156104c757600080fd5b506104d0611cb2565b60408051600160a060020a039092168252519081900360200190f35b3480156104f857600080fd5b50610507600435602435611cc1565b6040518083815260200180602001828103825283818151815260200191508051906020019060200280838360005b8381101561054d578181015183820152602001610535565b50505050905001935050505060405180910390f35b34801561056e57600080fd5b5061025c600160a060020a0360043516611cda565b34801561058f57600080fd5b506101e0611cef565b3480156105a457600080fd5b506101b3600480359060248035908101910135611cf4565b3480156105c857600080fd5b506105d46004356120e8565b60408051600160a060020a0319909b168b5260208b0199909952898901979097526060890195909552921515608088015260a0870191909152600160a060020a031660c086015260e085015261010084015261012083015251908190036101400190f35b34801561064457600080fd5b50610659600160a060020a036004351661214e565b60408051602080825283518183015283519192839290830191858101910280838360005b8381101561069557818101518382015260200161067d565b505050509050019250505060405180910390f35b3480156106b557600080fd5b506101b3600480359060248035600160a060020a031916916044359182019101356121ba565b3480156106e757600080fd5b506106f660043560243561233f565b604051808981526020018060200180602001806020018060200180602001806020018060200188810388528f818151815260200191508051906020019060200280838360005b8381101561075457818101518382015260200161073c565b5050505090500188810387528e818151815260200191508051906020019060200280838360005b8381101561079357818101518382015260200161077b565b5050505090500188810386528d818151815260200191508051906020019060200280838360005b838110156107d25781810151838201526020016107ba565b5050505090500188810385528c818151815260200191508051906020019060200280838360005b838110156108115781810151838201526020016107f9565b5050505090500188810384528b818151815260200191508051906020019060200280838360005b83811015610850578181015183820152602001610838565b5050505090500188810383528a818151815260200191508051906020019060200280838360005b8381101561088f578181015183820152602001610877565b50505050905001888103825289818151815260200191508051906020019060200280838360005b838110156108ce5781810151838201526020016108b6565b505050509050019f5050505050505050505050505050505060405180910390f35b3480156108fb57600080fd5b506040805160206004803580820135838102808601850190965280855261025c9536959394602494938501929182918501908490808284375094975061262a9650505050505050565b34801561095057600080fd5b506101e0612680565b34801561096557600080fd5b506101b3600160a060020a0360043516612686565b60008054819081908190819060a060020a900460ff161561099a57600080fd5b873315156109e0576040805160e560020a62461bcd02815260206004820152601d6024820152600080516020612dfe833981519152604482015290519081900360640190fd5b600081815260046020526040902060060154600160a060020a03163314610a3f576040805160e560020a62461bcd0281526020600482015260186024820152600080516020612e1e833981519152604482015290519081900360640190fd5b60008981526004602052604081206005015411610acc576040805160e560020a62461bcd02815260206004820152602560248201527f506c6561736520756e7374616b65206669727374206265666f7265207769746860448201527f647261772e000000000000000000000000000000000000000000000000000000606482015290519081900360840190fd5b6000898152600460205260409020600501544290610af3906203f48063ffffffff6126bc16565b1115610b6f576040805160e560020a62461bcd02815260206004820152603f60248201527f5374616b65686f6c646572206e6565647320746f207761697420666f7220332060448201527f64617973206265666f7265207769746864726177696e6720746f6b656e732e00606482015290519081900360840190fd5b6000898152600460205260408082206008808201546009808401548287528587209091018190558086529385209091018190558c845260018201805483546006909401549590915590995091975090955060a060020a029350600160a060020a03169150610bdc896126d2565b600089815260046020819052604080832080546bffffffffffffffffffffffff19168155600181018490556002810184905560038101849055918201805460ff1916905560058201839055600682018054600160a060020a03191690556007820183905560088201839055600990910182905551600160a060020a0384169186156108fc02918791818181858888f19350505050158015610c81573d6000803e3d6000fd5b507ff99c0736fafe9102d41ec0b56c187b26a6e35ae50415dcbecedf73112d0ec7638984868b8b6040518086815260200185600160a060020a031916600160a060020a031916815260200184815260200180602001828103825284848281815260200192508082843760405192018290039850909650505050505050a1505050505050505050565b6000805460a060020a900460ff1615610d2157600080fd5b8460008110158015610d35575061041a8111155b1515610db1576040805160e560020a62461bcd02815260206004820152602860248201527f546865207374616b65206475726174696f6e20697320746f6f20736d616c6c2060448201527f6f72206c61726765000000000000000000000000000000000000000000000000606482015290519081900360840190fd5b6007810615610e30576040805160e560020a62461bcd02815260206004820152602a60248201527f546865207374616b65206475726174696f6e2073686f756c64206265206d756c60448201527f7469706c65206f66203700000000000000000000000000000000000000000000606482015290519081900360840190fd5b600254341015610e8a576040805160e560020a62461bcd02815260206004820152601560248201527f616d6f756e742073686f756c64203e3d203130302e0000000000000000000000604482015290519081900360640190fd5b6003543360009081526006602052604090205410610f18576040805160e560020a62461bcd02815260206004820152602560248201527f4f6e6520616464726573732063616e2068617665207570206c696d697465642060448201527f7079676773000000000000000000000000000000000000000000000000000000606482015290519081900360840190fd5b6101406040519081016040528088600160a060020a031916815260200134815260200187815260200142815260200186151581526020016000815260200133600160a060020a0316815260200142815260200160046000808152602001908152602001600020600801548152602001600081525060046000600554815260200190815260200160002060008201518160000160006101000a8154816bffffffffffffffffffffffff021916908360a060020a9004021790555060208201518160010155604082015181600201556060820151816003015560808201518160040160006101000a81548160ff02191690831515021790555060a0820151816005015560c08201518160060160006101000a815481600160a060020a030219169083600160a060020a0316021790555060e082015181600701556101008201518160080155610120820151816009015590505060055460046000600460008081526020019081526020016000206008015481526020019081526020016000206009018190555060055460046000808152602001908152602001600020600801819055506006600033600160a060020a0316600160a060020a0316815260200190815260200160002060055490806001815401808255809150509060018203906000526020600020016000909192909190915055506005600081548092919060010191905055507fd7812fae7f8126d2df0f5449a2cc0744d2e9d3fc8c161de6193bc4df6c68d365600160055403883489428a338b8b604051808a815260200189600160a060020a031916600160a060020a03191681526020018881526020018781526020018681526020018515151515815260200184600160a060020a0316600160a060020a0316815260200180602001828103825284848281815260200192508082843760405192018290039c50909a5050505050505050505050a150506005546000190195945050505050565b60035481565b6000806111f7336112dc565b151561120257600080fd5b5060005b82518110156112415761122f838281518110151561122057fe5b90602001906020020151611247565b1561123957600191505b600101611206565b50919050565b6000611252336112dc565b151561125d57600080fd5b600160a060020a03821660009081526001602052604090205460ff16156112d757600160a060020a038216600081815260016020908152604091829020805460ff19169055815192835290517ff1abf01a1043b7c244d128e8595cf0c1d10743b022b03a02dffd8ca3bf729f5a9281900390910190a15060015b919050565b600054600160a060020a0391821691161490565b6112f9336112dc565b151561130457600080fd5b60005460a060020a900460ff16151561131c57600080fd5b6000805474ff0000000000000000000000000000000000000000191681556040517f7805862f689e2f13df9f062ff482ad3ad112aca9e0847911ed832e158c525b339190a1565b60066020528160005260406000208181548110151561137e57fe5b90600052602060002001600091509150505481565b60005460a060020a900460ff1681565b600081565b6201518081565b600060608060006113c086866128a0565b9094509250600084111561145657836040519080825280602002602001820160405280156113f8578160200160208202803883390190505b509150600090505b838110156114565760046000848381518110151561141a57fe5b90602001906020020151815260200190815260200160002060070154828281518110151561144457fe5b60209081029091010152600101611400565b509250925092565b60005460a060020a900460ff161561147557600080fd5b600083116114f3576040805160e560020a62461bcd02815260206004820152602860248201527f7079676720302063616e6e6f7420626520756e7374616b656420616e6420776960448201527f7468647261776e2e000000000000000000000000000000000000000000000000606482015290519081900360840190fd5b6000341161154b576040805160e560020a62461bcd02815260206004820152601160248201527f76616c75652063616e6e6f742062652030000000000000000000000000000000604482015290519081900360640190fd5b600083815260046020526040902060060154600160a060020a031615156115bc576040805160e560020a62461bcd02815260206004820152601360248201527f7079676720646f6573206e6f7420657869737400000000000000000000000000604482015290519081900360640190fd5b600083815260046020526040902060010154151561164a576040805160e560020a62461bcd02815260206004820152602160248201527f63616e6e6f742073746f726520746f20612077697468647261776e207079676760448201527f2e00000000000000000000000000000000000000000000000000000000000000606482015290519081900360840190fd5b6000838152600460209081526040918290206001018054340190558151601f840182900482028101820190925282825261169a918591859085908190840183828082843750612b57945050505050565b505050565b61041a81565b60005460a060020a900460ff16156116bc57600080fd5b84331515611702576040805160e560020a62461bcd02815260206004820152601d6024820152600080516020612dfe833981519152604482015290519081900360640190fd5b600081815260046020526040902060060154600160a060020a03163314611761576040805160e560020a62461bcd0281526020600482015260186024820152600080516020612e1e833981519152604482015290519081900360640190fd5b8460008110158015611775575061041a8111155b15156117f1576040805160e560020a62461bcd02815260206004820152602860248201527f546865207374616b65206475726174696f6e20697320746f6f20736d616c6c2060448201527f6f72206c61726765000000000000000000000000000000000000000000000000606482015290519081900360840190fd5b6007810615611870576040805160e560020a62461bcd02815260206004820152602a60248201527f546865207374616b65206475726174696f6e2073686f756c64206265206d756c60448201527f7469706c65206f66203700000000000000000000000000000000000000000000606482015290519081900360840190fd5b6000878152600460205260409020600281015460039091015461189d91620151800263ffffffff6126bc16565b6118b24262015180890263ffffffff6126bc16565b101561192d576040805160e560020a62461bcd028152602060048201526024808201527f63757272656e74207374616b65206475726174696f6e206e6f742066696e697360448201527f6865642e00000000000000000000000000000000000000000000000000000000606482015290519081900360840190fd5b6000878152600460208190526040909120015460ff16156119d7576000878152600460205260409020600201548610156119d7576040805160e560020a62461bcd02815260206004820152602160248201527f63616e6e6f742072656475636520746865207374616b65206475726174696f6e60448201527f2e00000000000000000000000000000000000000000000000000000000000000606482015290519081900360840190fd5b6000878152600460208181526040808420600281018b9055426003820155928301805460ff19168a15151790556005909201929092558051601f8601839004830281018301909152848152611a4391899190879087908190840183828082843750612b57945050505050565b50505050505050565b6000611a57336112dc565b1515611a6257600080fd5b600160a060020a03821660009081526001602052604090205460ff1615156112d757600160a060020a038216600081815260016020818152604092839020805460ff1916909217909155815192835290517fd1bba68c128cc3f427e5831b3c6f99f480b6efa6b9e80c757768f6124158cc3f9281900390910190a1506001919050565b60005460a060020a900460ff1615611afc57600080fd5b83331515611b42576040805160e560020a62461bcd02815260206004820152601d6024820152600080516020612dfe833981519152604482015290519081900360640190fd5b600081815260046020526040902060060154600160a060020a03163314611ba1576040805160e560020a62461bcd0281526020600482015260186024820152600080516020612e1e833981519152604482015290519081900360640190fd5b611baa856126d2565b600085815260046020908152604080832060069081018054600160a060020a031916600160a060020a038a16908117909155845282528083208054600181018255908452928290209092018790558151601f8501829004820281018201909252838252611c2d918791869086908190840183828082843750612b57945050505050565b5050505050565b30315b90565b611c43336112dc565b1515611c4e57600080fd5b60005460a060020a900460ff1615611c6557600080fd5b6000805474ff0000000000000000000000000000000000000000191660a060020a1781556040517f6985a02210a168e66602d3235cb6db0e70f92b3ba4d376a33c0f3d9434bff6259190a1565b600054600160a060020a031681565b60006060611ccf84846128a0565b915091509250929050565b60016020526000908152604090205460ff1681565b600381565b60005460a060020a900460ff1615611d0b57600080fd5b82331515611d51576040805160e560020a62461bcd02815260206004820152601d6024820152600080516020612dfe833981519152604482015290519081900360640190fd5b600081815260046020526040902060060154600160a060020a03163314611db0576040805160e560020a62461bcd0281526020600482015260186024820152600080516020612e1e833981519152604482015290519081900360640190fd5b60008411611e2e576040805160e560020a62461bcd02815260206004820152602860248201527f7079676720302063616e6e6f7420626520756e7374616b656420616e6420776960448201527f7468647261776e2e000000000000000000000000000000000000000000000000606482015290519081900360840190fd5b6000848152600460208190526040909120015460ff1615611ee5576040805160e560020a62461bcd02815260206004820152604860248201527f43616e6e6f7420756e7374616b652077697468206e6f6e446563617920666c6160448201527f672e204e65656420746f2064697361626c65206e6f6e2d6465636179206d6f6460648201527f652066697273742e000000000000000000000000000000000000000000000000608482015290519081900360a40190fd5b600084815260046020526040902060028101546003909101544291611f159190620151800263ffffffff6126bc16565b1115611fb8576040805160e560020a62461bcd028152602060048201526044602482018190527f5374616b696e672074696d6520646f6573206e6f742065787069726520796574908201527f2e20506c65617365207761697420756e74696c207374616b696e67206578706960648201527f7265732e00000000000000000000000000000000000000000000000000000000608482015290519081900360a40190fd5b60008481526004602052604090206005015415612045576040805160e560020a62461bcd02815260206004820152602b60248201527f556e7374616b656420616c72656164792e204e6f206e65656420746f20756e7360448201527f74616b6520616761696e2e000000000000000000000000000000000000000000606482015290519081900360840190fd5b60008481526004602090815260409182902042600582015580546001909101548351888152600160a060020a031960a060020a909302928316938101939093529282018390526080606083018181529083018690527f9954bdedc474e937b39bbb080fc136e2edf1cef61f0906d36203267f4930762e93889390918891889160a0820184848082843760405192018290039850909650505050505050a150505050565b6004602081905260009182526040909120805460018201546002830154600384015494840154600585015460068601546007870154600888015460099098015460a060020a909702989597949660ff909416949293600160a060020a039092169290918a565b600160a060020a0381166000908152600660209081526040918290208054835181840281018401909452808452606093928301828280156121ae57602002820191906000526020600020905b81548152602001906001019080831161219a575b50505050509050919050565b60005460a060020a900460ff16156121d157600080fd5b83331515612217576040805160e560020a62461bcd02815260206004820152601d6024820152600080516020612dfe833981519152604482015290519081900360640190fd5b600081815260046020526040902060060154600160a060020a03163314612276576040805160e560020a62461bcd0281526020600482015260186024820152600080516020612e1e833981519152604482015290519081900360640190fd5b600085815260046020526040902060050154156122dd576040805160e560020a62461bcd02815260206004820152601f60248201527f63616e6e6f74207265766f746520647572696e6720756e7374616b696e672e00604482015290519081900360640190fd5b60008581526004602090815260409182902080546bffffffffffffffffffffffff191660a060020a88041790558151601f8501829004820281018201909252838252611c2d918791869086908190840183828082843750612b57945050505050565b600060608060608060608060606000612356612d57565b6123608c8c6128a0565b909a50985060008a111561261b5789604051908082528060200260200182016040528015612398578160200160208202803883390190505b509750896040519080825280602002602001820160405280156123c5578160200160208202803883390190505b509650896040519080825280602002602001820160405280156123f2578160200160208202803883390190505b5095508960405190808252806020026020018201604052801561241f578160200160208202803883390190505b5094508960405190808252806020026020018201604052801561244c578160200160208202803883390190505b50935089604051908082528060200260200182016040528015612479578160200160208202803883390190505b509250600091505b8982101561261b57600460008a8481518110151561249b57fe5b6020908102919091018101518252818101929092526040908101600020815161014081018352815460a060020a02600160a060020a0319168152600182015493810193909352600281015491830191909152600381015460608301819052600482015460ff1615156080840152600582015460a08401526006820154600160a060020a031660c0840152600782015460e0840152600882015461010084015260099091015461012083015289519192509089908490811061255857fe5b602090810290910101526040810151875188908490811061257557fe5b602090810290910101526080810151865190159087908490811061259557fe5b911515602092830290910182015281015185518690849081106125b457fe5b60209081029091010152805184518590849081106125ce57fe5b600160a060020a031990921660209283029091019091015260c081015183518490849081106125f957fe5b600160a060020a03909216602092830290910190910152600190910190612481565b50509295985092959890939650565b600080612636336112dc565b151561264157600080fd5b5060005b82518110156112415761266e838281518110151561265f57fe5b90602001906020020151611a4c565b1561267857600191505b600101612645565b60025481565b61268f336112dc565b151561269a57600080fd5b60008054600160a060020a031916600160a060020a0392909216919091179055565b6000828201838110156126cb57fe5b9392505050565b6000818152600460209081526040808320600690810154600160a060020a031680855292528220549091908110612779576040805160e560020a62461bcd02815260206004820152602c60248201527f45787065637420746865206f776e657220686173206174206c65617374206f6e60448201527f65207079676720696e6465780000000000000000000000000000000000000000606482015290519081900360840190fd5b5060005b600160a060020a0382166000908152600660205260409020548110156127e257600160a060020a03821660009081526006602052604090208054849190839081106127c457fe5b906000526020600020015414156127da576127e2565b60010161277d565b600160a060020a0382166000908152600660205260409020546000190181101561287157600160a060020a038216600090815260066020526040902080546001830190811061282d57fe5b6000918252602080832090910154600160a060020a03851683526006909152604090912080548390811061285d57fe5b6000918252602090912001556001016127e2565b600160a060020a038216600090815260066020526040902080549061289a906000198301612dc0565b50505050565b600060606128ac612d57565b600080851180156128be575061138885105b15156128c957600080fd5b600086815260046020818152604092839020835161014081018552815460a060020a02600160a060020a0319168152600182015481840152600282015481860152600382015460608201529281015460ff1615156080840152600581015460a08401526006810154600160a060020a031660c0840152600781015460e0840152600881015461010084015260090154610120830152825188815288820281019091019092529250858015612987578160200160208202803883390190505b509250600090505b84811015612b4d575b60008261012001511180156129c25750610120820151600090815260046020526040812060050154115b15612a715761012091820151600090815260046020818152604092839020835161014081018552815460a060020a02600160a060020a0319168152600182015492810192909252600281015493820193909352600383015460608201529082015460ff1615156080820152600582015460a08201526006820154600160a060020a031660c0820152600782015460e0820152600882015461010082015260099091015492810192909252612998565b6101208201511515612a8257612b4d565b8161012001518382815181101515612a9657fe5b6020908102919091018101919091526101209283015160009081526004808352604091829020825161014081018452815460a060020a02600160a060020a031916815260018083015495820195909552600282015493810193909352600381015460608401529081015460ff1615156080830152600581015460a08301526006810154600160a060020a031660c0830152600781015460e0830152600881015461010083015260090154938101939093520161298f565b9250509250929050565b612b5f612d57565b6004600084815260200190815260200160002061014060405190810160405290816000820160009054906101000a900460a060020a02600160a060020a031916600160a060020a03191681526020016001820154815260200160028201548152602001600382015481526020016004820160009054906101000a900460ff16151515158152602001600582015481526020016006820160009054906101000a9004600160a060020a0316600160a060020a0316600160a060020a03168152602001600782015481526020016008820154815260200160098201548152505090507f0b074423c8a0f26c131cd7c88b19ef6adf084b812c97bdd1fb9dcf339ee9a38783826000015183602001518460400151856060015186608001518760c00151896040518089815260200188600160a060020a031916600160a060020a03191681526020018781526020018681526020018581526020018415151515815260200183600160a060020a0316600160a060020a0316815260200180602001828103825283818151815260200191508051906020019080838360005b83811015612d11578181015183820152602001612cf9565b50505050905090810190601f168015612d3e5780820380516001836020036101000a031916815260200191505b50995050505050505050505060405180910390a1505050565b610140604051908101604052806000600160a060020a0319168152602001600081526020016000815260200160008152602001600015158152602001600081526020016000600160a060020a031681526020016000815260200160008152602001600081525090565b81548183558181111561169a5760008381526020902061169a918101908301611c3791905b80821115612df95760008155600101612de5565b5090560073656e6465722073686f756c646e27742062652061646472657373203000000073656e646572206973206e6f7420746865206f776e65722e0000000000000000a165627a7a723058209fa15a477d62e4abdbbaf44343516ca6d6aad780e231ea577e62219d257b0db400290000000000000000000000000000000000000000000000056bc75e2d631000000000000000000000000000000000000000000000000000000000000000000064") - fixedTime := time.Unix(cfg.Genesis.Timestamp, 0) + fixedTime := genesis.GenesisTimestamp(cfg.Genesis.Timestamp) ex, err := action.SignedExecution(action.EmptyAddress, admin, 1, big.NewInt(0), 10000000, big.NewInt(testutil.TestGasPriceInt64), data) require.NoError(err) diff --git a/gasstation/gasstattion_test.go b/gasstation/gasstattion_test.go index 9780c190de..fda6f8e3cf 100644 --- a/gasstation/gasstattion_test.go +++ b/gasstation/gasstattion_test.go @@ -86,7 +86,7 @@ func TestSuggestGasPriceForUserAction(t *testing.T) { require.NoError(t, err) ap, err := actpool.NewActPool(cfg.Genesis, sf, cfg.ActPool) require.NoError(t, err) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(t, err) blkMemDao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, 16) bc := blockchain.NewBlockchain( @@ -157,7 +157,7 @@ func TestSuggestGasPriceForSystemAction(t *testing.T) { require.NoError(t, err) ap, err := actpool.NewActPool(cfg.Genesis, sf, cfg.ActPool) require.NoError(t, err) - store, err := filedao.NewFileDAOInMemForTest() + store, err := filedao.NewFileDAOInMemForTest(cfg.Genesis) require.NoError(t, err) blkMemDao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, 16) bc := blockchain.NewBlockchain( diff --git a/server/main.go b/server/main.go index 4246f0021d..fcc2679be4 100644 --- a/server/main.go +++ b/server/main.go @@ -20,11 +20,9 @@ import ( "strings" "syscall" - "github.com/iotexproject/go-pkgs/hash" "go.uber.org/automaxprocs/maxprocs" "go.uber.org/zap" - "github.com/iotexproject/iotex-core/v2/blockchain/block" "github.com/iotexproject/iotex-core/v2/blockchain/genesis" "github.com/iotexproject/iotex-core/v2/config" "github.com/iotexproject/iotex-core/v2/db/trie/mptrie" @@ -86,16 +84,6 @@ func main() { if err != nil { glog.Fatalln("Failed to new genesis config.", zap.Error(err)) } - // set genesis timestamp - genesis.SetGenesisTimestamp(genesisCfg.Timestamp) - if genesis.Timestamp() == 0 { - glog.Fatalln("Genesis timestamp is not set, call genesis.New() first") - } - // load genesis block's hash - block.LoadGenesisHash(&genesisCfg) - if block.GenesisHash() == hash.ZeroHash256 { - glog.Fatalln("Genesis hash is not set, call block.LoadGenesisHash() first") - } cfg, err := config.New([]string{_overwritePath, _secretPath}, _plugins) if err != nil { @@ -122,7 +110,7 @@ func main() { log.S().Infof("Config in use: %+v", cfgToLog) log.S().Infof("EVM Network ID: %d, Chain ID: %d", cfg.Chain.EVMNetworkID, cfg.Chain.ID) log.S().Infof("Genesis timestamp: %d", genesisCfg.Timestamp) - log.S().Infof("Genesis hash: %x", block.GenesisHash()) + log.S().Infof("Genesis hash: %x", cfg.Genesis.Hash()) // liveness start probeSvr := probe.New(cfg.System.HTTPStatsPort) diff --git a/tools/iomigrater/cmds/check-height.go b/tools/iomigrater/cmds/check-height.go index fe0a82dc5e..47363ca174 100644 --- a/tools/iomigrater/cmds/check-height.go +++ b/tools/iomigrater/cmds/check-height.go @@ -55,7 +55,7 @@ func checkDbFileHeight(filePath string) (uint64, error) { } cfg.DB.DbPath = filePath - store, err := filedao.NewFileDAO(cfg.DB, block.NewDeserializer(cfg.Chain.EVMNetworkID)) + store, err := filedao.NewFileDAO(cfg.Genesis, cfg.DB, block.NewDeserializer(cfg.Chain.EVMNetworkID)) if err != nil { return uint64(0), err } diff --git a/tools/iomigrater/cmds/migrate-db.go b/tools/iomigrater/cmds/migrate-db.go index 2508aa1373..b41f676090 100644 --- a/tools/iomigrater/cmds/migrate-db.go +++ b/tools/iomigrater/cmds/migrate-db.go @@ -116,13 +116,13 @@ func migrateDbFile() (err error) { cfg.DB.DbPath = oldFile deser := block.NewDeserializer(cfg.Chain.EVMNetworkID) - oldDAO, err := filedao.NewFileDAO(cfg.DB, deser) + oldDAO, err := filedao.NewFileDAO(cfg.Genesis, cfg.DB, deser) if err != nil { return errors.Wrapf(err, "failed to create dao from %s", oldFile) } cfg.DB.DbPath = newFile - newDAO, err := filedao.NewFileDAO(cfg.DB, deser) + newDAO, err := filedao.NewFileDAO(cfg.Genesis, cfg.DB, deser) if err != nil { return errors.Wrapf(err, "failed to create dao from %s", newFile) }