eth: set networkID to chainId by default (#28250)

This commit is contained in:
Daniel Liu 2025-02-13 14:33:36 +08:00
parent 4656718f0c
commit ff987140dc
5 changed files with 20 additions and 10 deletions

View file

@ -137,6 +137,10 @@ func New(ctx *node.ServiceContext, config *ethconfig.Config, XDCXServ *XDCx.XDCX
}
log.Info(strings.Repeat("-", 153))
networkID := config.NetworkId
if networkID == 0 {
networkID = chainConfig.ChainId.Uint64()
}
eth := &Ethereum{
config: config,
chainDb: chainDb,
@ -145,7 +149,7 @@ func New(ctx *node.ServiceContext, config *ethconfig.Config, XDCXServ *XDCx.XDCX
accountManager: ctx.AccountManager,
engine: CreateConsensusEngine(ctx, &config.Ethash, chainConfig, chainDb),
shutdownChan: make(chan bool),
networkId: config.NetworkId,
networkId: networkID,
gasPrice: config.GasPrice,
etherbase: config.Etherbase,
bloomRequests: make(chan chan *bloombits.Retrieval),
@ -164,7 +168,7 @@ func New(ctx *node.ServiceContext, config *ethconfig.Config, XDCXServ *XDCx.XDCX
if bcVersion != nil {
dbVer = fmt.Sprintf("%d", *bcVersion)
}
log.Info("Initialising Ethereum protocol", "versions", ProtocolVersions, "network", config.NetworkId, "dbversion", dbVer)
log.Info("Initialising Ethereum protocol", "versions", ProtocolVersions, "network", networkID, "dbversion", dbVer)
if !config.SkipBcVersionCheck {
if bcVersion != nil && *bcVersion > core.BlockChainVersion {
@ -232,7 +236,7 @@ func New(ctx *node.ServiceContext, config *ethconfig.Config, XDCXServ *XDCx.XDCX
}
}
if eth.protocolManager, err = NewProtocolManagerEx(eth.chainConfig, config.SyncMode, config.NetworkId, eth.eventMux, eth.txPool, eth.orderPool, eth.lendingPool, eth.engine, eth.blockchain, chainDb); err != nil {
if eth.protocolManager, err = NewProtocolManagerEx(eth.chainConfig, config.SyncMode, networkID, eth.eventMux, eth.txPool, eth.orderPool, eth.lendingPool, eth.engine, eth.blockchain, chainDb); err != nil {
return nil, err
}
eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine, ctx.GetConfig().AnnounceTxs)

View file

@ -65,7 +65,7 @@ var Defaults = Config{
DatasetsInMem: 1,
DatasetsOnDisk: 2,
},
NetworkId: 88,
NetworkId: 0, // enable auto configuration of networkID == chainID
LightPeers: 100,
DatabaseCache: 768,
TrieCache: 256,
@ -108,8 +108,9 @@ type Config struct {
// If nil, the Ethereum main net block is used.
Genesis *core.Genesis `toml:",omitempty"`
// Protocol options
NetworkId uint64 // Network ID to use for selecting peers to connect to
// Network ID separates blockchains on the peer-to-peer networking level. When left
// zero, the chain ID is used as network ID.
NetworkId uint64
SyncMode downloader.SyncMode
NoPruning bool

View file

@ -65,7 +65,7 @@ func testStatusMsgErrors(t *testing.T, protocol int) {
},
{
code: StatusMsg, data: statusData{uint32(protocol), 999, td, head.Hash(), genesis.Hash()},
wantError: errResp(ErrNetworkIdMismatch, "999 (!= 88)"),
wantError: errResp(ErrNetworkIdMismatch, "999 (!= 0)"),
},
{
code: StatusMsg, data: statusData{uint32(protocol), ethconfig.Defaults.NetworkId, td, head.Hash(), common.Hash{3}},

View file

@ -91,6 +91,7 @@ func New(ctx *node.ServiceContext, config *ethconfig.Config) (*LightEthereum, er
if _, isCompat := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !isCompat {
return nil, genesisErr
}
log.Info(strings.Repeat("-", 153))
for _, line := range strings.Split(chainConfig.Description(), "\n") {
log.Info(line)
@ -100,6 +101,10 @@ func New(ctx *node.ServiceContext, config *ethconfig.Config) (*LightEthereum, er
peers := newPeerSet()
quitSync := make(chan struct{})
networkID := config.NetworkId
if networkID == 0 {
networkID = chainConfig.ChainId.Uint64()
}
leth := &LightEthereum{
config: config,
chainConfig: chainConfig,
@ -110,7 +115,7 @@ func New(ctx *node.ServiceContext, config *ethconfig.Config) (*LightEthereum, er
accountManager: ctx.AccountManager,
engine: eth.CreateConsensusEngine(ctx, &config.Ethash, chainConfig, chainDb),
shutdownChan: make(chan bool),
networkId: config.NetworkId,
networkId: networkID,
bloomRequests: make(chan chan *bloombits.Retrieval),
bloomIndexer: eth.NewBloomIndexer(chainDb, light.BloomTrieFrequency),
chtIndexer: light.NewChtIndexer(chainDb, true),
@ -133,7 +138,7 @@ func New(ctx *node.ServiceContext, config *ethconfig.Config) (*LightEthereum, er
}
leth.txPool = light.NewTxPool(leth.chainConfig, leth.blockchain, leth.relay)
if leth.protocolManager, err = NewProtocolManager(leth.chainConfig, true, ClientProtocolVersions, config.NetworkId, leth.eventMux, leth.engine, leth.peers, leth.blockchain, nil, chainDb, leth.odr, leth.relay, quitSync, &leth.wg); err != nil {
if leth.protocolManager, err = NewProtocolManager(leth.chainConfig, true, ClientProtocolVersions, networkID, leth.eventMux, leth.engine, leth.peers, leth.blockchain, nil, chainDb, leth.odr, leth.relay, quitSync, &leth.wg); err != nil {
return nil, err
}
leth.ApiBackend = &LesApiBackend{leth, nil}

View file

@ -53,7 +53,7 @@ type LesServer struct {
func NewLesServer(eth *eth.Ethereum, config *ethconfig.Config) (*LesServer, error) {
quitSync := make(chan struct{})
pm, err := NewProtocolManager(eth.BlockChain().Config(), false, ServerProtocolVersions, config.NetworkId, eth.EventMux(), eth.Engine(), newPeerSet(), eth.BlockChain(), eth.TxPool(), eth.ChainDb(), nil, nil, quitSync, new(sync.WaitGroup))
pm, err := NewProtocolManager(eth.BlockChain().Config(), false, ServerProtocolVersions, eth.NetVersion(), eth.EventMux(), eth.Engine(), newPeerSet(), eth.BlockChain(), eth.TxPool(), eth.ChainDb(), nil, nil, quitSync, new(sync.WaitGroup))
if err != nil {
return nil, err
}