Skip to content

Commit

Permalink
Network type.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frozen authored and ONECasey committed Mar 27, 2023
1 parent 15885f4 commit 5e1878a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
3 changes: 1 addition & 2 deletions cmd/harmony/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,9 +700,8 @@ func setupConsensusAndNode(hc harmonyconfig.HarmonyConfig, nodeConfig *nodeconfi
chainDBFactory = &shardchain.LDBFactory{RootDir: nodeConfig.DBDir}
}

chainConfig := nodeConfig.GetNetworkType().ChainConfig()
collection := shardchain.NewCollection(
&hc, chainDBFactory, &core.GenesisInitializer{NetworkType: nodeConfig.GetNetworkType()}, &chainConfig,
&hc, chainDBFactory, &core.GenesisInitializer{}, nodeConfig.GetNetworkType(),
)
for shardID, archival := range nodeConfig.ArchiveModes() {
if archival {
Expand Down
9 changes: 4 additions & 5 deletions core/genesis_initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import (

// GenesisInitializer is a shardchain.DBInitializer adapter.
type GenesisInitializer struct {
NetworkType nodeconfig.NetworkType
}

// InitChainDB sets up a new genesis block in the database for the given shard.
func (gi *GenesisInitializer) InitChainDB(db ethdb.Database, shardID uint32) error {
func (gi *GenesisInitializer) InitChainDB(db ethdb.Database, networkType nodeconfig.NetworkType, shardID uint32) error {
shardState, _ := committee.WithStakingEnabled.Compute(
big.NewInt(GenesisEpoch), nil,
)
Expand All @@ -34,14 +33,14 @@ func (gi *GenesisInitializer) InitChainDB(db ethdb.Database, shardID uint32) err
}
shardState = &shard.State{Shards: []shard.Committee{*subComm}}
}
gi.setupGenesisBlock(db, shardID, shardState)
gi.setupGenesisBlock(db, shardID, shardState, networkType)
return nil
}

// SetupGenesisBlock sets up a genesis blockchain.
func (gi *GenesisInitializer) setupGenesisBlock(db ethdb.Database, shardID uint32, myShardState *shard.State) {
func (gi *GenesisInitializer) setupGenesisBlock(db ethdb.Database, shardID uint32, myShardState *shard.State, networkType nodeconfig.NetworkType) {
utils.Logger().Info().Interface("shardID", shardID).Msg("setting up a brand new chain database")
gspec := NewGenesisSpec(gi.NetworkType, shardID)
gspec := NewGenesisSpec(networkType, shardID)
gspec.ShardStateHash = myShardState.Hash()
gspec.ShardState = *myShardState.DeepCopy()
// Store genesis block into db.
Expand Down
7 changes: 5 additions & 2 deletions internal/shardchain/dbinit.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package shardchain

import "github.com/ethereum/go-ethereum/ethdb"
import (
"github.com/ethereum/go-ethereum/ethdb"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
)

// DBInitializer initializes a newly created chain database.
type DBInitializer interface {
InitChainDB(db ethdb.Database, shardID uint32) error
InitChainDB(db ethdb.Database, networkType nodeconfig.NetworkType, shardID uint32) error
}
12 changes: 6 additions & 6 deletions internal/shardchain/shardchains.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/harmony-one/harmony/core/state"
harmonyconfig "github.com/harmony-one/harmony/internal/configs/harmony"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/shardchain/tikv_manage"

"github.com/harmony-one/harmony/shard"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/rawdb"
"github.com/harmony-one/harmony/core/vm"
"github.com/harmony-one/harmony/internal/params"
"github.com/harmony-one/harmony/internal/utils"
"github.com/pkg/errors"
)
Expand All @@ -42,7 +42,7 @@ type CollectionImpl struct {
mtx sync.Mutex
pool map[uint32]core.BlockChain
disableCache map[uint32]bool
chainConfig *params.ChainConfig
networkType nodeconfig.NetworkType
harmonyconfig *harmonyconfig.HarmonyConfig
}

Expand All @@ -55,15 +55,15 @@ type CollectionImpl struct {
func NewCollection(
harmonyconfig *harmonyconfig.HarmonyConfig,
dbFactory DBFactory, dbInit DBInitializer,
chainConfig *params.ChainConfig,
network nodeconfig.NetworkType,
) *CollectionImpl {
return &CollectionImpl{
harmonyconfig: harmonyconfig,
dbFactory: dbFactory,
dbInit: dbInit,
pool: make(map[uint32]core.BlockChain),
disableCache: make(map[uint32]bool),
chainConfig: chainConfig,
networkType: network,
}
}

Expand Down Expand Up @@ -92,7 +92,7 @@ func (sc *CollectionImpl) ShardChain(shardID uint32, options ...core.Options) (c
utils.Logger().Info().
Uint32("shardID", shardID).
Msg("initializing a new chain database")
if err := sc.dbInit.InitChainDB(db, shardID); err != nil {
if err := sc.dbInit.InitChainDB(db, sc.networkType, shardID); err != nil {
return nil, errors.Wrapf(err, "cannot initialize a new chain database")
}
}
Expand All @@ -113,7 +113,7 @@ func (sc *CollectionImpl) ShardChain(shardID uint32, options ...core.Options) (c
}
}

chainConfig := *sc.chainConfig
chainConfig := sc.networkType.ChainConfig()

if shardID == shard.BeaconChainShardID {
// For beacon chain inside a shard chain, need to reset the eth chainID to shard 0's eth chainID in the config
Expand Down

0 comments on commit 5e1878a

Please sign in to comment.