Skip to content

remove global genesis related code #4585

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions action/protocol/execution/testdata/rolldice.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"rawBalance": "0"
}, {
"account": "io1zf69lmyzkkzlywwqzzggst45qupvx2cyye62fp",
"rawBalance": "12000000"
"rawBalance": "20000000"
}],
"deployments":[{
"rawByteCode": "608060405234801561001057600080fd5b506102f5806100206000396000f3006080604052600436106100615763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632885ad2c8114610066578063797d9fbd14610070578063cd5e3c5d14610091578063d0e30db0146100b8575b600080fd5b61006e6100c0565b005b61006e73ffffffffffffffffffffffffffffffffffffffff600435166100cb565b34801561009d57600080fd5b506100a6610159565b60408051918252519081900360200190f35b61006e610229565b6100c9336100cb565b565b60006100d5610159565b6040805182815290519192507fbae72e55df73720e0f671f4d20a331df0c0dc31092fda6c573f35ff7f37f283e919081900360200190a160405173ffffffffffffffffffffffffffffffffffffffff8316906305f5e100830280156108fc02916000818181858888f19350505050158015610154573d6000803e3d6000fd5b505050565b604080514460208083019190915260001943014082840152825180830384018152606090920192839052815160009360059361021a9360029391929182918401908083835b602083106101bd5780518252601f19909201916020918201910161019e565b51815160209384036101000a600019018019909216911617905260405191909301945091925050808303816000865af11580156101fe573d6000803e3d6000fd5b5050506040513d602081101561021357600080fd5b5051610261565b81151561022357fe5b06905090565b60408051348152905133917fe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c919081900360200190a2565b600080805b60208110156102c25780600101602060ff160360080260020a848260208110151561028d57fe5b7f010000000000000000000000000000000000000000000000000000000000000091901a810204029190910190600101610266565b50929150505600a165627a7a72305820a426929891673b0a04d7163b60113d28e7d0f48ea667680ba48126c182b872c10029",
Expand Down Expand Up @@ -38,10 +38,10 @@
"rawGasPrice": "0",
"expectedBalances": [{
"account": "io1rc2d2de7rtuucalsqv4d9ng0h297t63w7wvlph",
"rawBalance": "100000000"
"rawBalance": "0"
}, {
"account": "",
"rawBalance": "400000000"
"rawBalance": "500000000"
}],
"expectedLogs": [{}],
"comment": "roll dice"
Expand All @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions action/protocol/generic_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"math/big"
"strings"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/iotexproject/iotex-address/address"
Expand Down Expand Up @@ -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),
},
},
)
Expand Down
9 changes: 5 additions & 4 deletions api/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
6 changes: 2 additions & 4 deletions api/grpcserver_integrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions api/grpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion api/serverV2_integrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
3 changes: 2 additions & 1 deletion api/web3server_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type (
}

getBlockResult struct {
blkHash hash.Hash256
blk *block.Block
transactions []interface{}
}
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions api/web3server_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 2 additions & 21 deletions blockchain/block/genesis_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,24 @@
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,
Copy link
Member

Choose a reason for hiding this comment

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

Does this solve the issue of block.Hash() not being equal to genesis.Hash() when the block height equals 0?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sadly, no...
with current implementation, block(0) shouldn't return a block, but throw an error.
we made a mistake here: block(0).hash() == genesis.Hash(). if we were able to redesign the blockchain, block(0).prevHash == genesis.Hash() and block(0) is the very first block instead of a virtual block.

prevBlockHash: hash.ZeroHash256,
txRoot: hash.ZeroHash256,
deltaStateDigest: hash.ZeroHash256,
receiptRoot: hash.ZeroHash256,
},
}
}

// 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()
})
}
7 changes: 2 additions & 5 deletions blockchain/block/genesis_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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[:]))
}
2 changes: 1 addition & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion blockchain/blockdao/blob_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
9 changes: 4 additions & 5 deletions blockchain/blockdao/blockdao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading