diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index f0d9ce64c3..8d94539ab9 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1468,34 +1468,16 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { cfg.NetworkId = 3 } cfg.Genesis = core.DefaultTestnetGenesisBlock() - // Only override default checkpoint so that user can customize checkpoint - // by themselves. - if cfg.Checkpoint == eth.DefaultConfig.Checkpoint { - cfg.Checkpoint = params.TestnetTrustedCheckpoint - } case ctx.GlobalBool(RinkebyFlag.Name): if !ctx.GlobalIsSet(NetworkIdFlag.Name) { cfg.NetworkId = 4 } cfg.Genesis = core.DefaultRinkebyGenesisBlock() - // Only override default checkpoint so that user can customize checkpoint - // by themselves. - if cfg.Checkpoint == eth.DefaultConfig.Checkpoint { - cfg.Checkpoint = params.RinkebyTrustedCheckpoint - } - if cfg.CheckpointConfig == nil { - cfg.CheckpointConfig = params.RinkebyCheckpointConfig // Enable checkpoint contract for rinkeby testnet. - } case ctx.GlobalBool(GoerliFlag.Name): if !ctx.GlobalIsSet(NetworkIdFlag.Name) { cfg.NetworkId = 5 } cfg.Genesis = core.DefaultGoerliGenesisBlock() - // Only override default checkpoint so that user can customize checkpoint - // by themselves. - if cfg.Checkpoint == eth.DefaultConfig.Checkpoint { - cfg.Checkpoint = params.GoerliTrustedCheckpoint - } case ctx.GlobalBool(DeveloperFlag.Name): if !ctx.GlobalIsSet(NetworkIdFlag.Name) { cfg.NetworkId = 1337 diff --git a/eth/backend.go b/eth/backend.go index 8a4e1fc4e3..93e9501750 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -202,6 +202,10 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { // Permit the downloader to use the trie cache allowance during fast sync cacheLimit := cacheConfig.TrieCleanLimit + cacheConfig.TrieDirtyLimit + checkpoint := config.Checkpoint + if checkpoint == nil { + checkpoint, _ = params.TrustedCheckpoints[genesisHash] + } if eth.protocolManager, err = NewProtocolManager(chainConfig, config.Checkpoint, config.SyncMode, config.NetworkId, eth.eventMux, eth.txPool, eth.engine, eth.blockchain, chainDb, cacheLimit, config.Whitelist); err != nil { return nil, err } diff --git a/eth/config.go b/eth/config.go index 144638c9f2..eb61f1d5dc 100644 --- a/eth/config.go +++ b/eth/config.go @@ -60,7 +60,6 @@ var DefaultConfig = Config{ Blocks: 20, Percentile: 60, }, - Checkpoint: params.MainnetTrustedCheckpoint, } func init() { @@ -154,6 +153,6 @@ type Config struct { // Checkpoint is a hardcoded checkpoint which can be nil. Checkpoint *params.TrustedCheckpoint - // CheckpointConfig is a set of checkpoint contract configs. - CheckpointConfig *params.CheckpointContractConfig + // CheckpointOracle is the configuration for checkpoint oracle. + CheckpointOracle *params.CheckpointOracleConfig } diff --git a/eth/gen_config.go b/eth/gen_config.go index 2d1dd2483c..77b28d025b 100644 --- a/eth/gen_config.go +++ b/eth/gen_config.go @@ -48,7 +48,7 @@ func (c Config) MarshalTOML() (interface{}, error) { ConstantinopleOverride *big.Int RPCGasCap *big.Int `toml:",omitempty"` Checkpoint *params.TrustedCheckpoint - CheckpointConfig *params.CheckpointContractConfig + CheckpointOracle *params.CheckpointOracleConfig } var enc Config enc.Genesis = c.Genesis @@ -81,7 +81,7 @@ func (c Config) MarshalTOML() (interface{}, error) { enc.ConstantinopleOverride = c.ConstantinopleOverride enc.RPCGasCap = c.RPCGasCap enc.Checkpoint = c.Checkpoint - enc.CheckpointConfig = c.CheckpointConfig + enc.CheckpointOracle = c.CheckpointOracle return &enc, nil } @@ -118,7 +118,7 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { ConstantinopleOverride *big.Int RPCGasCap *big.Int `toml:",omitempty"` Checkpoint *params.TrustedCheckpoint - CheckpointConfig *params.CheckpointContractConfig + CheckpointOracle *params.CheckpointOracleConfig } var dec Config if err := unmarshal(&dec); err != nil { @@ -214,8 +214,8 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.Checkpoint != nil { c.Checkpoint = dec.Checkpoint } - if dec.CheckpointConfig != nil { - c.CheckpointConfig = dec.CheckpointConfig + if dec.CheckpointOracle != nil { + c.CheckpointOracle = dec.CheckpointOracle } return nil } diff --git a/les/backend.go b/les/backend.go index 6867ff1181..934e39c3a7 100644 --- a/les/backend.go +++ b/les/backend.go @@ -124,9 +124,13 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) { leth.bloomTrieIndexer = light.NewBloomTrieIndexer(chainDb, leth.odr, params.BloomBitsBlocksClient, params.BloomTrieFrequency) leth.odr.SetIndexers(leth.chtIndexer, leth.bloomTrieIndexer, leth.bloomIndexer) + checkpoint := config.Checkpoint + if checkpoint == nil { + checkpoint, _ = params.TrustedCheckpoints[genesisHash] + } // Note: NewLightChain adds the trusted checkpoint so it needs an ODR with // indexers already set but not started yet - if leth.blockchain, err = light.NewLightChain(leth.odr, leth.chainConfig, leth.engine, config.Checkpoint); err != nil { + if leth.blockchain, err = light.NewLightChain(leth.odr, leth.chainConfig, leth.engine, checkpoint); err != nil { return nil, err } // Note: AddChildIndexer starts the update process for the child @@ -150,8 +154,12 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) { } leth.ApiBackend.gpo = gasprice.NewOracle(leth.ApiBackend, gpoParams) - registrar := newCheckpointRegistrar(config.CheckpointConfig, leth.getLocalCheckpoint) - if leth.protocolManager, err = NewProtocolManager(leth.chainConfig, config.Checkpoint, light.DefaultClientIndexerConfig, config.ULC, true, config.NetworkId, leth.eventMux, leth.peers, leth.blockchain, nil, chainDb, leth.odr, leth.serverPool, registrar, quitSync, &leth.wg, nil); err != nil { + oracle := config.CheckpointOracle + if oracle == nil { + oracle, _ = params.CheckpointOracles[genesisHash] + } + registrar := newCheckpointRegistrar(oracle, leth.getLocalCheckpoint) + if leth.protocolManager, err = NewProtocolManager(leth.chainConfig, checkpoint, light.DefaultClientIndexerConfig, config.ULC, true, config.NetworkId, leth.eventMux, leth.peers, leth.blockchain, nil, chainDb, leth.odr, leth.serverPool, registrar, quitSync, &leth.wg, nil); err != nil { return nil, err } if leth.protocolManager.isULCEnabled() { diff --git a/les/helper_test.go b/les/helper_test.go index 0332b41003..19a8270ee3 100644 --- a/les/helper_test.go +++ b/les/helper_test.go @@ -200,7 +200,7 @@ func newTestProtocolManager(lightSync bool, blocks int, odr *LesOdr, indexers [] if lightSync { indexConfig = light.TestClientIndexerConfig } - config := ¶ms.CheckpointContractConfig{ + config := ¶ms.CheckpointOracleConfig{ Address: crypto.CreateAddress(bankAddr, 0), Signers: []common.Address{signerAddr}, Threshold: 1, diff --git a/les/registrar.go b/les/registrar.go index 41934882a7..85d6befc77 100644 --- a/les/registrar.go +++ b/les/registrar.go @@ -33,7 +33,7 @@ import ( // generated and announced by the contract admins on-chain. The checkpoint is // verified by clients locally during the checkpoint syncing. type checkpointRegistrar struct { - config *params.CheckpointContractConfig + config *params.CheckpointOracleConfig contract *registrar.Registrar // Whether the contract backend is set. @@ -44,7 +44,7 @@ type checkpointRegistrar struct { } // newCheckpointRegistrar returns a checkpoint registrar handler. -func newCheckpointRegistrar(config *params.CheckpointContractConfig, getLocal func(uint64) params.TrustedCheckpoint) *checkpointRegistrar { +func newCheckpointRegistrar(config *params.CheckpointOracleConfig, getLocal func(uint64) params.TrustedCheckpoint) *checkpointRegistrar { if config == nil { log.Info("Checkpoint registrar is not enabled") return nil diff --git a/les/server.go b/les/server.go index 7b7dc40975..0dbf14281d 100644 --- a/les/server.go +++ b/les/server.go @@ -120,8 +120,13 @@ func NewLesServer(e *eth.Ethereum, config *eth.Config) (*LesServer, error) { srv.chtIndexer.Start(e.BlockChain()) - registrar := newCheckpointRegistrar(config.CheckpointConfig, srv.getLocalCheckpoint) - pm, err := NewProtocolManager(e.BlockChain().Config(), config.Checkpoint, light.DefaultServerIndexerConfig, config.ULC, false, config.NetworkId, e.EventMux(), newPeerSet(), e.BlockChain(), e.TxPool(), e.ChainDb(), nil, nil, registrar, quitSync, new(sync.WaitGroup), e.Synced) + oracle := config.CheckpointOracle + if oracle == nil { + oracle, _ = params.CheckpointOracles[e.BlockChain().Genesis().Hash()] + } + registrar := newCheckpointRegistrar(oracle, srv.getLocalCheckpoint) + // TODO(rjl493456442) Checkpoint is useless for les server, separate handler for client and server. + pm, err := NewProtocolManager(e.BlockChain().Config(), nil, light.DefaultServerIndexerConfig, config.ULC, false, config.NetworkId, e.EventMux(), newPeerSet(), e.BlockChain(), e.TxPool(), e.ChainDb(), nil, nil, registrar, quitSync, new(sync.WaitGroup), e.Synced) if err != nil { return nil, err } diff --git a/les/sync.go b/les/sync.go index 6c2ce5eefc..078080c15b 100644 --- a/les/sync.go +++ b/les/sync.go @@ -159,7 +159,7 @@ func (pm *ProtocolManager) synchronise(peer *peer) { log.Debug("Disable checkpoint syncing", "reason", "empty checkpoint") case latest.Number.Uint64() >= (checkpoint.SectionIndex+1)*pm.iConfig.ChtSize-1: mode = lightSync - log.Debug("Disable checkpoint syncing", "reason", "local chain beyonds the checkpoint") + log.Debug("Disable checkpoint syncing", "reason", "local chain beyond the checkpoint") case hardcoded: mode = legacyCheckpointSync log.Debug("Disable checkpoint syncing", "reason", "checkpoint is hardcoded") diff --git a/params/config.go b/params/config.go index 54f7234418..e19dee9141 100644 --- a/params/config.go +++ b/params/config.go @@ -33,6 +33,21 @@ var ( GoerliGenesisHash = common.HexToHash("0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a") ) +// TrustedCheckpoints associates each known checkpoint with the genesis hash of +// the chain it belongs to. +var TrustedCheckpoints = map[common.Hash]*TrustedCheckpoint{ + MainnetGenesisHash: MainnetTrustedCheckpoint, + TestnetGenesisHash: TestnetTrustedCheckpoint, + RinkebyGenesisHash: RinkebyTrustedCheckpoint, + GoerliGenesisHash: GoerliTrustedCheckpoint, +} + +// CheckpointOracles associates each known checkpoint oracles with the genesis hash of +// the chain it belongs to. +var CheckpointOracles = map[common.Hash]*CheckpointOracleConfig{ + RinkebyGenesisHash: RinkebyCheckpointOracleConfig, +} + var ( // MainnetChainConfig is the chain parameters to run a node on the main network. MainnetChainConfig = &ChainConfig{ @@ -109,8 +124,8 @@ var ( BloomRoot: common.HexToHash("0xa3048fe8b7e30f77f11bc755a88478363d7d3e71c2bdfe4e8ab9e269cd804ba2"), } - // RinkebyCheckpointConfig contains a set of checkpoint contract configs for the Rinkeby test network. - RinkebyCheckpointConfig = &CheckpointContractConfig{ + // RinkebyCheckpointOracleConfig contains a set of configs for the Rinkeby test network oracle. + RinkebyCheckpointOracleConfig = &CheckpointOracleConfig{ Address: common.HexToAddress("0x62652ed8e969ce7bd5e3dd13590efa1e569215f1"), Signers: []common.Address{ common.HexToAddress("0xd9c9cd5f6779558b6e0ed4e6acf6b1947e7fa1f3"), // Peter @@ -199,9 +214,9 @@ func (c *TrustedCheckpoint) Empty() bool { return c.SectionHead == (common.Hash{}) || c.CHTRoot == (common.Hash{}) || c.BloomRoot == (common.Hash{}) } -// CheckpointContractConfig represents a set of checkpoint contract config -// which used for light client checkpoint syncing. -type CheckpointContractConfig struct { +// CheckpointOracleConfig represents a set of checkpoint contract(which acts as an oracle) +// config which used for light client checkpoint syncing. +type CheckpointOracleConfig struct { Address common.Address `json:"address"` Signers []common.Address `json:"signers"` Threshold uint64 `json:"threshold"`