Skip to content

Commit afdc797

Browse files
committed
cmd, eth, les, light: move checkpoint config to config file
1 parent 40e7199 commit afdc797

File tree

19 files changed

+109
-228
lines changed

19 files changed

+109
-228
lines changed

cmd/puppeth/wizard_genesis.go

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,10 @@ import (
2727
"net/http"
2828
"os"
2929
"path/filepath"
30-
"strings"
3130
"time"
3231

33-
"github.com/ethereum/go-ethereum/accounts/abi"
3432
"github.com/ethereum/go-ethereum/common"
35-
"github.com/ethereum/go-ethereum/common/math"
36-
"github.com/ethereum/go-ethereum/contracts/registrar/contract"
3733
"github.com/ethereum/go-ethereum/core"
38-
"github.com/ethereum/go-ethereum/core/rawdb"
39-
"github.com/ethereum/go-ethereum/core/state"
40-
"github.com/ethereum/go-ethereum/core/vm/runtime"
4134
"github.com/ethereum/go-ethereum/log"
4235
"github.com/ethereum/go-ethereum/params"
4336
)
@@ -140,65 +133,6 @@ func (w *wizard) makeGenesis() {
140133
fmt.Println("Specify your chain/network ID if you want an explicit one (default = random)")
141134
genesis.Config.ChainID = new(big.Int).SetUint64(uint64(w.readDefaultInt(rand.Intn(65536))))
142135

143-
// Query the user for checkpoint contract config
144-
fmt.Println()
145-
fmt.Println("Should a checkpoint contract be deployed (y/n)? (default = no)")
146-
if w.readDefaultYesNo(false) {
147-
// Read the addresses of the trusted signers
148-
fmt.Println("Which accounts should be trusted signers? (mandatory at least one)")
149-
var (
150-
signers []common.Address
151-
threshold uint64
152-
)
153-
// Get trusted signer addresses
154-
for {
155-
if address := w.readAddress(); address != nil {
156-
signers = append(signers, *address)
157-
continue
158-
}
159-
if len(signers) == 0 {
160-
continue
161-
}
162-
break
163-
}
164-
// Read the checkpoint signature threshold
165-
for {
166-
fmt.Printf("What is the minimal approval threshold (maximum %d)?\n", len(signers))
167-
threshold = uint64(w.readInt())
168-
if threshold <= 0 || threshold > uint64(len(signers)) {
169-
log.Error(fmt.Sprintf("Invalid approval threshold, please enter in range [1, %d]\n", len(signers)))
170-
continue
171-
}
172-
break
173-
}
174-
parsed, err := abi.JSON(strings.NewReader(contract.ContractABI))
175-
if err != nil {
176-
log.Crit("Parse contract ABI failed", "err", err)
177-
}
178-
input, err := parsed.Pack("", signers, big.NewInt(params.CheckpointFrequency), big.NewInt(params.CheckpointProcessConfirmations), new(big.Int).SetUint64(threshold))
179-
if err != nil {
180-
log.Crit("Pack contract constructor arguments failed", "err", err)
181-
}
182-
config := &runtime.Config{GasLimit: math.MaxInt64}
183-
config.State, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()))
184-
code, address, _, err := runtime.Create(append(common.FromHex(contract.ContractBin), input...), config)
185-
if err != nil {
186-
log.Crit("Execute contract constructor failed", "err", err)
187-
}
188-
config.State.Commit(true)
189-
genesis.Alloc[address] = core.GenesisAccount{Code: code, Storage: make(map[common.Hash]common.Hash), Balance: big.NewInt(1)}
190-
if err = config.State.ForEachStorage(address, func(key, value common.Hash) bool {
191-
genesis.Alloc[address].Storage[key] = value
192-
return true
193-
}); err != nil {
194-
log.Crit("Failed to iterate contract storage", "err", err)
195-
}
196-
genesis.Config.CheckpointConfig = &params.CheckpointContractConfig{
197-
Address: address,
198-
Signers: signers,
199-
Threshold: threshold,
200-
}
201-
}
202136
// All done, store the genesis and flush to disk
203137
log.Info("Configured new genesis block")
204138

@@ -296,77 +230,6 @@ func (w *wizard) manageGenesis() {
296230
fmt.Printf("Which block should Petersburg come into effect? (default = %v)\n", w.conf.Genesis.Config.PetersburgBlock)
297231
w.conf.Genesis.Config.PetersburgBlock = w.readDefaultBigInt(w.conf.Genesis.Config.PetersburgBlock)
298232

299-
// The registrar contract might have been deployed
300-
fmt.Println()
301-
fmt.Printf("Should a checkpoint contract be active (y/n)? (default = %v)\n", w.conf.Genesis.Config.CheckpointConfig != nil)
302-
if !w.readDefaultYesNo(w.conf.Genesis.Config.CheckpointConfig != nil) {
303-
w.conf.Genesis.Config.CheckpointConfig = nil
304-
} else {
305-
// Make sure we have a checkpoint config to fill out
306-
checkpoint := w.conf.Genesis.Config.CheckpointConfig
307-
if checkpoint == nil {
308-
checkpoint = new(params.CheckpointContractConfig)
309-
}
310-
// Read the Ethereum address of the deployed contract
311-
fmt.Println()
312-
if checkpoint.Address == (common.Address{}) {
313-
fmt.Printf("Which address does the checkpoint contract reside at?\n")
314-
for checkpoint.Address == (common.Address{}) {
315-
if address := w.readAddress(); address != nil {
316-
checkpoint.Address = *address
317-
}
318-
}
319-
} else {
320-
fmt.Printf("Which address does the checkpoint contract reside at? (default = %s)\n", checkpoint.Address.Hex())
321-
checkpoint.Address = w.readDefaultAddress(checkpoint.Address)
322-
}
323-
// Read the addresses of the trusted signers
324-
if len(checkpoint.Signers) > 0 {
325-
signers := make([]string, len(checkpoint.Signers))
326-
for i, signer := range checkpoint.Signers {
327-
signers[i] = signer.Hex()
328-
}
329-
fmt.Println()
330-
fmt.Printf("Keep existing list of authorized signers %s? (default = yes)\n", strings.Join(signers, ","))
331-
if !w.readDefaultYesNo(true) {
332-
checkpoint.Signers = nil
333-
}
334-
}
335-
if len(checkpoint.Signers) == 0 {
336-
fmt.Println()
337-
fmt.Println("Which accounts should be trusted signers? (mandatory at least one)")
338-
for {
339-
if address := w.readAddress(); address != nil {
340-
checkpoint.Signers = append(checkpoint.Signers, *address)
341-
continue
342-
}
343-
if len(checkpoint.Signers) == 0 {
344-
continue
345-
}
346-
break
347-
}
348-
}
349-
// Read the checkpoint signature threshold
350-
fmt.Println()
351-
if checkpoint.Threshold == 0 {
352-
fmt.Printf("What is the minimal approval threshold (maximum %d)?\n", len(checkpoint.Signers))
353-
} else {
354-
fmt.Printf("What is the minimal approval threshold (maximum %d)? (default = %d)\n", len(checkpoint.Signers), checkpoint.Threshold)
355-
}
356-
for {
357-
if checkpoint.Threshold == 0 {
358-
checkpoint.Threshold = uint64(w.readInt())
359-
} else {
360-
checkpoint.Threshold = uint64(w.readDefaultInt(int(checkpoint.Threshold)))
361-
}
362-
if checkpoint.Threshold <= 0 || checkpoint.Threshold > uint64(len(checkpoint.Signers)) {
363-
log.Error(fmt.Sprintf("Invalid approval threshold, please enter in range [1, %d]\n", len(checkpoint.Signers)))
364-
continue
365-
}
366-
break
367-
}
368-
w.conf.Genesis.Config.CheckpointConfig = checkpoint
369-
}
370233
out, _ := json.MarshalIndent(w.conf.Genesis.Config, "", " ")
371234
fmt.Printf("Chain configuration updated:\n\n%s\n", out)
372235

cmd/utils/flags.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,16 +1451,20 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
14511451
cfg.NetworkId = 3
14521452
}
14531453
cfg.Genesis = core.DefaultTestnetGenesisBlock()
1454+
cfg.Checkpoint = params.TestnetTrustedCheckpoint
14541455
case ctx.GlobalBool(RinkebyFlag.Name):
14551456
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
14561457
cfg.NetworkId = 4
14571458
}
14581459
cfg.Genesis = core.DefaultRinkebyGenesisBlock()
1460+
cfg.Checkpoint = params.RinkebyTrustedCheckpoint
1461+
cfg.CheckpointConfig = params.RinkebyCheckpointConfig // Enable checkpoint contract for rinkeby testnet.
14591462
case ctx.GlobalBool(GoerliFlag.Name):
14601463
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
14611464
cfg.NetworkId = 5
14621465
}
14631466
cfg.Genesis = core.DefaultGoerliGenesisBlock()
1467+
cfg.Checkpoint = params.GoerliTrustedCheckpoint
14641468
case ctx.GlobalBool(DeveloperFlag.Name):
14651469
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
14661470
cfg.NetworkId = 1337

eth/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
202202

203203
// Permit the downloader to use the trie cache allowance during fast sync
204204
cacheLimit := cacheConfig.TrieCleanLimit + cacheConfig.TrieDirtyLimit
205-
if eth.protocolManager, err = NewProtocolManager(chainConfig, config.SyncMode, config.NetworkId, eth.eventMux, eth.txPool, eth.engine, eth.blockchain, chainDb, cacheLimit, config.Whitelist); err != nil {
205+
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 {
206206
return nil, err
207207
}
208208
eth.miner = miner.New(eth, &config.Miner, chainConfig, eth.EventMux(), eth.engine, eth.isLocalBlock)

eth/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ var DefaultConfig = Config{
6060
Blocks: 20,
6161
Percentile: 60,
6262
},
63+
Checkpoint: params.MainnetTrustedCheckpoint,
6364
}
6465

6566
func init() {
@@ -149,4 +150,10 @@ type Config struct {
149150

150151
// RPCGasCap is the global gas cap for eth-call variants.
151152
RPCGasCap *big.Int `toml:",omitempty"`
153+
154+
// Checkpoint is a hardcoded checkpoint which can be nil.
155+
Checkpoint *params.TrustedCheckpoint
156+
157+
// CheckpointConfig is a set of checkpoint contract configs.
158+
CheckpointConfig *params.CheckpointContractConfig
152159
}

eth/gen_config.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eth/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ type ProtocolManager struct {
106106

107107
// NewProtocolManager returns a new Ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
108108
// with the Ethereum network.
109-
func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, networkID uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb ethdb.Database, cacheLimit int, whitelist map[uint64]common.Hash) (*ProtocolManager, error) {
109+
func NewProtocolManager(config *params.ChainConfig, checkpoint *params.TrustedCheckpoint, mode downloader.SyncMode, networkID uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb ethdb.Database, cacheLimit int, whitelist map[uint64]common.Hash) (*ProtocolManager, error) {
110110
// Create the protocol manager with the base fields
111111
manager := &ProtocolManager{
112112
networkID: networkID,
@@ -126,7 +126,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
126126
manager.fastSync = uint32(1)
127127
}
128128
// If we have trusted checkpoints, enforce them on the chain
129-
if checkpoint, ok := params.TrustedCheckpoints[blockchain.Genesis().Hash()]; ok {
129+
if checkpoint != nil {
130130
manager.checkpointNumber = (checkpoint.SectionIndex+1)*params.CHTFrequency - 1
131131
manager.checkpointHash = checkpoint.SectionHead
132132
}

eth/handler_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -504,31 +504,30 @@ func testCheckpointChallenge(t *testing.T, syncmode downloader.SyncMode, checkpo
504504

505505
// Initialize a chain and generate a fake CHT if checkpointing is enabled
506506
var (
507-
db = rawdb.NewMemoryDatabase()
508-
config = new(params.ChainConfig)
509-
genesis = (&core.Genesis{Config: config}).MustCommit(db)
507+
db = rawdb.NewMemoryDatabase()
508+
config = new(params.ChainConfig)
510509
)
510+
(&core.Genesis{Config: config}).MustCommit(db) // Commit genesis block
511511
// If checkpointing is enabled, create and inject a fake CHT and the corresponding
512512
// chllenge response.
513513
var response *types.Header
514+
var cht *params.TrustedCheckpoint
514515
if checkpoint {
515516
index := uint64(rand.Intn(500))
516517
number := (index+1)*params.CHTFrequency - 1
517518
response = &types.Header{Number: big.NewInt(int64(number)), Extra: []byte("valid")}
518519

519-
cht := &params.TrustedCheckpoint{
520+
cht = &params.TrustedCheckpoint{
520521
SectionIndex: index,
521522
SectionHead: response.Hash(),
522523
}
523-
params.TrustedCheckpoints[genesis.Hash()] = cht
524-
defer delete(params.TrustedCheckpoints, genesis.Hash())
525524
}
526525
// Create a checkpoint aware protocol manager
527526
blockchain, err := core.NewBlockChain(db, nil, config, ethash.NewFaker(), vm.Config{}, nil)
528527
if err != nil {
529528
t.Fatalf("failed to create new blockchain: %v", err)
530529
}
531-
pm, err := NewProtocolManager(config, syncmode, DefaultConfig.NetworkId, new(event.TypeMux), new(testTxPool), ethash.NewFaker(), blockchain, db, 1, nil)
530+
pm, err := NewProtocolManager(config, cht, syncmode, DefaultConfig.NetworkId, new(event.TypeMux), new(testTxPool), ethash.NewFaker(), blockchain, db, 1, nil)
532531
if err != nil {
533532
t.Fatalf("failed to start test protocol manager: %v", err)
534533
}
@@ -615,7 +614,7 @@ func testBroadcastBlock(t *testing.T, totalPeers, broadcastExpected int) {
615614
if err != nil {
616615
t.Fatalf("failed to create new blockchain: %v", err)
617616
}
618-
pm, err := NewProtocolManager(config, downloader.FullSync, DefaultConfig.NetworkId, evmux, new(testTxPool), pow, blockchain, db, 1, nil)
617+
pm, err := NewProtocolManager(config, nil, downloader.FullSync, DefaultConfig.NetworkId, evmux, new(testTxPool), pow, blockchain, db, 1, nil)
619618
if err != nil {
620619
t.Fatalf("failed to start test protocol manager: %v", err)
621620
}

eth/helper_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func newTestProtocolManager(mode downloader.SyncMode, blocks int, generator func
6666
if _, err := blockchain.InsertChain(chain); err != nil {
6767
panic(err)
6868
}
69-
pm, err := NewProtocolManager(gspec.Config, mode, DefaultConfig.NetworkId, evmux, &testTxPool{added: newtx}, engine, blockchain, db, 1, nil)
69+
pm, err := NewProtocolManager(gspec.Config, nil, mode, DefaultConfig.NetworkId, evmux, &testTxPool{added: newtx}, engine, blockchain, db, 1, nil)
7070
if err != nil {
7171
return nil, nil, err
7272
}

les/backend.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
126126

127127
// Note: NewLightChain adds the trusted checkpoint so it needs an ODR with
128128
// indexers already set but not started yet
129-
if leth.blockchain, err = light.NewLightChain(leth.odr, leth.chainConfig, leth.engine); err != nil {
129+
if leth.blockchain, err = light.NewLightChain(leth.odr, leth.chainConfig, leth.engine, config.Checkpoint); err != nil {
130130
return nil, err
131131
}
132132
// Note: AddChildIndexer starts the update process for the child
@@ -150,8 +150,8 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
150150
}
151151
leth.ApiBackend.gpo = gasprice.NewOracle(leth.ApiBackend, gpoParams)
152152

153-
registrar := newCheckpointRegistrar(chainConfig.CheckpointConfig, leth.getLocalCheckpoint)
154-
if leth.protocolManager, err = NewProtocolManager(leth.chainConfig, 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 {
153+
registrar := newCheckpointRegistrar(config.CheckpointConfig, leth.getLocalCheckpoint)
154+
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 {
155155
return nil, err
156156
}
157157
if leth.protocolManager.isULCEnabled() {

0 commit comments

Comments
 (0)