Skip to content

Commit

Permalink
allow to select genesis sections for export
Browse files Browse the repository at this point in the history
  • Loading branch information
uprendis committed Aug 19, 2022
1 parent 01d857a commit fed5cb7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 30 deletions.
11 changes: 9 additions & 2 deletions cmd/opera/launcher/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import (
var (
EvmExportMode = cli.StringFlag{
Name: "export.evm.mode",
Usage: `EVM export mode ("full" or "ext-mpt" or "mpt" or "none")`,
Usage: `EVM export mode ("full" or "ext-mpt" or "mpt")`,
Value: "mpt",
}
EvmExportExclude = cli.StringFlag{
Name: "export.evm.exclude",
Usage: `DB of EVM keys to exclude from genesis`,
}
GenesisExportSections = cli.StringFlag{
Name: "export.sections",
Usage: `Genesis sections to export separated by comma (e.g. "brs-1" or "ers" or "evm-2")`,
Value: "brs,ers,evm",
}
importCommand = cli.Command{
Name: "import",
Usage: "Import a blockchain file",
Expand Down Expand Up @@ -80,11 +85,13 @@ be gzipped
{
Name: "genesis",
Usage: "Export current state into a genesis file",
ArgsUsage: "<filename or dry-run> [<epochFrom> <epochTo>] [--export.evm.mode=none]",
ArgsUsage: "<filename or dry-run> [<epochFrom> <epochTo>] [--export.evm.mode=MODE --export.evm.exclude=DB_PATH --export.sections=A,B,C]",
Action: utils.MigrateFlags(exportGenesis),
Flags: []cli.Flag{
DataDirFlag,
EvmExportMode,
EvmExportExclude,
GenesisExportSections,
},
Description: `
opera export genesis
Expand Down
81 changes: 53 additions & 28 deletions cmd/opera/launcher/genesiscmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path"
"strconv"
"strings"

"github.com/Fantom-foundation/lachesis-base/common/bigendian"
"github.com/Fantom-foundation/lachesis-base/hash"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/syndtr/goleveldb/leveldb/opt"
"gopkg.in/urfave/cli.v1"

"github.com/Fantom-foundation/go-opera/gossip"
"github.com/Fantom-foundation/go-opera/gossip/evmstore"
"github.com/Fantom-foundation/go-opera/inter/ibr"
"github.com/Fantom-foundation/go-opera/inter/ier"
Expand Down Expand Up @@ -192,6 +194,14 @@ func (w *unitWriter) Write(b []byte) (n int, err error) {
return
}

func getEpochBlock(epoch idx.Epoch, store *gossip.Store) idx.Block {
bs, _ := store.GetHistoryBlockEpochState(epoch)
if bs == nil {
return 0
}
return bs.LastBlock.Idx
}

func exportGenesis(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
utils.Fatalf("This command requires an argument.")
Expand All @@ -214,8 +224,8 @@ func exportGenesis(ctx *cli.Context) error {
to = idx.Epoch(n)
}
mode := ctx.String(EvmExportMode.Name)
if mode != "full" && mode != "ext-mpt" && mode != "mpt" && mode != "none" {
return errors.New("--export.evm.mode must be one of {full, ext-mpt, mpt, none}")
if mode != "full" && mode != "ext-mpt" && mode != "mpt" {
return errors.New("--export.evm.mode must be one of {full, ext-mpt, mpt}")
}

var excludeEvmDB kvdb.Store
Expand All @@ -227,6 +237,24 @@ func exportGenesis(ctx *cli.Context) error {
excludeEvmDB = db
}

sectionsStr := ctx.String(GenesisExportSections.Name)
sections := map[string]string{}
for _, str := range strings.Split(sectionsStr, ",") {
before := len(sections)
if strings.HasPrefix(str, "brs") {
sections["brs"] = str
} else if strings.HasPrefix(str, "ers") {
sections["ers"] = str
} else if strings.HasPrefix(str, "evm") {
sections["evm"] = str
} else {
return fmt.Errorf("unknown section '%s': has to start with either 'brs' or 'ers' or 'evm'", str)
}
if len(sections) == before {
return fmt.Errorf("duplicate section: '%s'", str)
}
}

cfg := makeAllConfigs(ctx)
tmpPath := path.Join(cfg.Node.DataDir, "tmp")
_ = os.RemoveAll(tmpPath)
Expand Down Expand Up @@ -268,12 +296,10 @@ func exportGenesis(ctx *cli.Context) error {
if to > gdb.GetEpoch() {
to = gdb.GetEpoch()
}
toBlock := idx.Block(0)
fromBlock := idx.Block(0)
{
if len(sections["ers"]) > 0 {
log.Info("Exporting epochs", "from", from, "to", to)
writer := newUnitWriter(plain)
err := writer.Start(header, genesisstore.EpochsSection, tmpPath)
err := writer.Start(header, sections["ers"], tmpPath)
if err != nil {
return err
}
Expand All @@ -291,28 +317,29 @@ func exportGenesis(ctx *cli.Context) error {
if err != nil {
return err
}
if i == from {
fromBlock = er.BlockState.LastBlock.Idx
}
if i == to {
toBlock = er.BlockState.LastBlock.Idx
}
}
epochsHash, err = writer.Flush()
if err != nil {
return err
}
log.Info("Exported epochs", "hash", epochsHash.String())
log.Info("Exported epochs")
fmt.Printf("- Epochs hash: %v \n", epochsHash.String())
}

if fromBlock < 1 {
// avoid underflow
fromBlock = 1
}
{
if len(sections["brs"]) > 0 {
toBlock := getEpochBlock(to, gdb)
fromBlock := getEpochBlock(from, gdb)
if sections["brs"] != "brs" {
// to continue prev section, include blocks of prev epochs too, excluding first blocks of prev epoch (which is last block if prev section)
fromBlock = getEpochBlock(from-1, gdb) + 1
}
if fromBlock < 1 {
// avoid underflow
fromBlock = 1
}
log.Info("Exporting blocks", "from", fromBlock, "to", toBlock)
writer := newUnitWriter(plain)
err := writer.Start(header, genesisstore.BlocksSection, tmpPath)
err := writer.Start(header, sections["brs"], tmpPath)
if err != nil {
return err
}
Expand All @@ -338,13 +365,14 @@ func exportGenesis(ctx *cli.Context) error {
if err != nil {
return err
}
log.Info("Exported blocks", "hash", blocksHash.String())
log.Info("Exported blocks")
fmt.Printf("- Blocks hash: %v \n", blocksHash.String())
}

if mode != "none" {
log.Info("Exporting EVM data", "from", fromBlock, "to", toBlock)
if len(sections["evm"]) > 0 {
log.Info("Exporting EVM data")
writer := newUnitWriter(plain)
err := writer.Start(header, genesisstore.EvmSection, tmpPath)
err := writer.Start(header, sections["evm"], tmpPath)
if err != nil {
return err
}
Expand All @@ -368,12 +396,9 @@ func exportGenesis(ctx *cli.Context) error {
if err != nil {
return err
}
log.Info("Exported EVM data", "hash", evmHash.String())
log.Info("Exported EVM data")
fmt.Printf("- EVM hash: %v \n", evmHash.String())
}

fmt.Printf("- Epochs hash: %v \n", epochsHash.String())
fmt.Printf("- Blocks hash: %v \n", blocksHash.String())
fmt.Printf("- EVM hash: %v \n", evmHash.String())

return nil
}

0 comments on commit fed5cb7

Please sign in to comment.