Skip to content

Commit

Permalink
Merge pull request #71 from BoostryJP/feature/#70
Browse files Browse the repository at this point in the history
geth: add new update transitions helper utility
  • Loading branch information
YoshihitoAso authored Mar 13, 2024
2 parents 3bbd4c3 + 9caf94f commit 18a948b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
71 changes: 71 additions & 0 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"os"
"reflect"
"runtime"
"strconv"
"sync/atomic"
Expand Down Expand Up @@ -73,6 +74,19 @@ It expects the genesis file as argument.`,
Category: "BLOCKCHAIN COMMANDS",
Description: `
The dumpgenesis command dumps the genesis block configuration in JSON format to stdout.`,
}
updateCommand = cli.Command{
Action: utils.MigrateFlags(updateTransitions),
Name: "update",
Usage: "Update genesis block with new transitions",
ArgsUsage: "<genesisPath>",
Flags: []cli.Flag{
utils.DataDirFlag,
},
Category: "BLOCKCHAIN COMMANDS",
Description: `
The update commands updates the chain data configuration in genesis block for new transition changes.
It expects the genesis file as argument.`,
}
importCommand = cli.Command{
Action: utils.MigrateFlags(importChain),
Expand Down Expand Up @@ -206,6 +220,63 @@ func getIsQuorum(file io.Reader) bool {
return altGenesis.Config.IsQuorum == nil || *altGenesis.Config.IsQuorum
}

// updateTransitions will update genesis block with the new transitions data
func updateTransitions(ctx *cli.Context) error {
// Open and initialise both full and light databases
stack, _ := makeConfigNode(ctx)
defer stack.Close()

// Make sure we have a valid genesis JSON
genesisPath := ctx.Args().First()
if len(genesisPath) == 0 {
utils.Fatalf("Must supply path to genesis JSON file")
}
file, err := os.Open(genesisPath)
if err != nil {
utils.Fatalf("Failed to read genesis file: %v", err)
}
defer file.Close()

genesis := new(core.Genesis)
if err := json.NewDecoder(file).Decode(genesis); err != nil {
utils.Fatalf("invalid genesis file: %v", err)
}

// Transitions updates only applies to Quorum
file.Seek(0, 0)
genesis.Config.IsQuorum = getIsQuorum(file)
if genesis.Config.IsQuorum {
err = genesis.Config.CheckTransitionsData()
if err != nil {
utils.Fatalf("Transitions data invalid: %v", err)
}
} else {
return fmt.Errorf("update transitions only apply to quorum configuration")
}

// Update transitions and recommit to db
for _, name := range []string{"chaindata", "lightchaindata"} {
chaindb, err := stack.OpenDatabase(name, 0, 0, "", false)
if err != nil {
utils.Fatalf("Failed to open database: %v", err)
}
stored := rawdb.ReadCanonicalHash(chaindb, 0)
storedcfg := rawdb.ReadChainConfig(chaindb, stored)
if storedcfg == nil {
return fmt.Errorf("found genesis block without chain config")
}
// Check that new transitions have changed before updating them
if !reflect.DeepEqual(storedcfg.Transitions, genesis.Config.Transitions) {
log.Info("Change found in transitions, proceeding to update chain config")
storedcfg.Transitions = genesis.Config.Transitions
rawdb.WriteChainConfig(chaindb, stored, storedcfg)
} else {
log.Info("No change in transitions, no update required to chain config")
}
}
return nil
}

// initGenesis will initialise the given JSON format genesis file and writes it as
// the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
func initGenesis(ctx *cli.Context) error {
Expand Down
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ func init() {
app.Commands = []cli.Command{
// See chaincmd.go:
initCommand,
updateCommand,
mpsdbUpgradeCommand,
importCommand,
exportCommand,
Expand Down

0 comments on commit 18a948b

Please sign in to comment.