Skip to content

Commit 4bc60e3

Browse files
authored
Merge pull request #15241 from karalabe/puppeth-fork-management
cmd/puppeth: support managing fork block in the chain config
2 parents eb9abbd + edba5e9 commit 4bc60e3

4 files changed

Lines changed: 77 additions & 11 deletions

File tree

‎cmd/puppeth/wizard.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,28 @@ func (w *wizard) readDefaultInt(def int) int {
161161
}
162162
}
163163

164+
// readDefaultBigInt reads a single line from stdin, trimming if from spaces,
165+
// enforcing it to parse into a big integer. If an empty line is entered, the
166+
// default value is returned.
167+
func (w *wizard) readDefaultBigInt(def *big.Int) *big.Int {
168+
for {
169+
fmt.Printf("> ")
170+
text, err := w.in.ReadString('\n')
171+
if err != nil {
172+
log.Crit("Failed to read user input", "err", err)
173+
}
174+
if text = strings.TrimSpace(text); text == "" {
175+
return def
176+
}
177+
val, ok := new(big.Int).SetString(text, 0)
178+
if !ok {
179+
log.Error("Invalid input, expected big integer")
180+
continue
181+
}
182+
return val
183+
}
184+
}
185+
164186
/*
165187
// readFloat reads a single line from stdin, trimming if from spaces, enforcing it
166188
// to parse into a float.

‎cmd/puppeth/wizard_genesis.go‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package main
1818

1919
import (
2020
"bytes"
21+
"encoding/json"
2122
"fmt"
23+
"io/ioutil"
2224
"math/big"
2325
"math/rand"
2426
"time"
@@ -135,3 +137,53 @@ func (w *wizard) makeGenesis() {
135137
// All done, store the genesis and flush to disk
136138
w.conf.genesis = genesis
137139
}
140+
141+
// manageGenesis permits the modification of chain configuration parameters in
142+
// a genesis config and the export of the entire genesis spec.
143+
func (w *wizard) manageGenesis() {
144+
// Figure out whether to modify or export the genesis
145+
fmt.Println()
146+
fmt.Println(" 1. Modify existing fork rules")
147+
fmt.Println(" 2. Export genesis configuration")
148+
149+
choice := w.read()
150+
switch {
151+
case choice == "1":
152+
// Fork rule updating requested, iterate over each fork
153+
fmt.Println()
154+
fmt.Printf("Which block should Homestead come into effect? (default = %v)\n", w.conf.genesis.Config.HomesteadBlock)
155+
w.conf.genesis.Config.HomesteadBlock = w.readDefaultBigInt(w.conf.genesis.Config.HomesteadBlock)
156+
157+
fmt.Println()
158+
fmt.Printf("Which block should EIP150 come into effect? (default = %v)\n", w.conf.genesis.Config.EIP150Block)
159+
w.conf.genesis.Config.EIP150Block = w.readDefaultBigInt(w.conf.genesis.Config.EIP150Block)
160+
161+
fmt.Println()
162+
fmt.Printf("Which block should EIP155 come into effect? (default = %v)\n", w.conf.genesis.Config.EIP155Block)
163+
w.conf.genesis.Config.EIP155Block = w.readDefaultBigInt(w.conf.genesis.Config.EIP155Block)
164+
165+
fmt.Println()
166+
fmt.Printf("Which block should EIP158 come into effect? (default = %v)\n", w.conf.genesis.Config.EIP158Block)
167+
w.conf.genesis.Config.EIP158Block = w.readDefaultBigInt(w.conf.genesis.Config.EIP158Block)
168+
169+
fmt.Println()
170+
fmt.Printf("Which block should Byzantium come into effect? (default = %v)\n", w.conf.genesis.Config.ByzantiumBlock)
171+
w.conf.genesis.Config.ByzantiumBlock = w.readDefaultBigInt(w.conf.genesis.Config.ByzantiumBlock)
172+
173+
out, _ := json.MarshalIndent(w.conf.genesis.Config, "", " ")
174+
fmt.Printf("Chain configuration updated:\n\n%s\n", out)
175+
176+
case choice == "2":
177+
// Save whatever genesis configuration we currently have
178+
fmt.Println()
179+
fmt.Printf("Which file to save the genesis into? (default = %s.json)\n", w.network)
180+
out, _ := json.MarshalIndent(w.conf.genesis, "", " ")
181+
if err := ioutil.WriteFile(w.readDefaultString(fmt.Sprintf("%s.json", w.network)), out, 0644); err != nil {
182+
log.Error("Failed to save genesis file", "err", err)
183+
}
184+
log.Info("Exported existing genesis block")
185+
186+
default:
187+
log.Error("That's not something I can do")
188+
}
189+
}

‎cmd/puppeth/wizard_intro.go‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (w *wizard) run() {
9898
if w.conf.genesis == nil {
9999
fmt.Println(" 2. Configure new genesis")
100100
} else {
101-
fmt.Println(" 2. Save existing genesis")
101+
fmt.Println(" 2. Manage existing genesis")
102102
}
103103
if len(w.servers) == 0 {
104104
fmt.Println(" 3. Track new remote server")
@@ -118,18 +118,10 @@ func (w *wizard) run() {
118118
w.networkStats(false)
119119

120120
case choice == "2":
121-
// If we don't have a genesis, make one
122121
if w.conf.genesis == nil {
123122
w.makeGenesis()
124123
} else {
125-
// Otherwise just save whatever we currently have
126-
fmt.Println()
127-
fmt.Printf("Which file to save the genesis into? (default = %s.json)\n", w.network)
128-
out, _ := json.MarshalIndent(w.conf.genesis, "", " ")
129-
if err := ioutil.WriteFile(w.readDefaultString(fmt.Sprintf("%s.json", w.network)), out, 0644); err != nil {
130-
log.Error("Failed to save genesis file", "err", err)
131-
}
132-
log.Info("Exported existing genesis block")
124+
w.manageGenesis()
133125
}
134126
case choice == "3":
135127
if len(w.servers) == 0 {

‎cmd/puppeth/wizard_netstats.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (w *wizard) networkStats(tips bool) {
129129
}
130130
}
131131
// If a genesis block was found, load it into our configs
132-
if protips.genesis != "" {
132+
if protips.genesis != "" && w.conf.genesis == nil {
133133
genesis := new(core.Genesis)
134134
if err := json.Unmarshal([]byte(protips.genesis), genesis); err != nil {
135135
log.Error("Failed to parse remote genesis", "err", err)

0 commit comments

Comments
 (0)