From ff987140dc30c9b9e5e3ff4107246a20d820d5fd Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Thu, 13 Feb 2025 14:33:36 +0800 Subject: [PATCH] eth: set networkID to chainId by default (#28250) --- eth/backend.go | 10 +++++++--- eth/ethconfig/config.go | 7 ++++--- eth/protocol_test.go | 2 +- les/backend.go | 9 +++++++-- les/server.go | 2 +- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index e55c81aa6d..6634a4ee78 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -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) diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index c3c78d0b0d..9ab2c8e66b 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -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 diff --git a/eth/protocol_test.go b/eth/protocol_test.go index 858e1da5aa..b398f90650 100644 --- a/eth/protocol_test.go +++ b/eth/protocol_test.go @@ -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}}, diff --git a/les/backend.go b/les/backend.go index 1e85638db7..0f250f5181 100644 --- a/les/backend.go +++ b/les/backend.go @@ -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} diff --git a/les/server.go b/les/server.go index 2a3c7bfb4c..e75c4b4a83 100644 --- a/les/server.go +++ b/les/server.go @@ -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 }