From 7b01f6a2884ac03fe42eb1801f7e4dba35127205 Mon Sep 17 00:00:00 2001 From: Eduard S Date: Fri, 12 Feb 2021 17:49:47 +0100 Subject: [PATCH] Add discard cli to discard synced blocks The command is useful in case the synchronizer reaches an invalid state and you want to roll back a few blocks and try again (maybe with some fixes in the code). --- cli/node/README.md | 11 +++++++- cli/node/cfg.buidler.toml | 7 +++-- cli/node/main.go | 56 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/cli/node/README.md b/cli/node/README.md index 9f2a27e..073ac82 100644 --- a/cli/node/README.md +++ b/cli/node/README.md @@ -117,7 +117,16 @@ Generate a new BabyJubJub key pair: ./node --mode coord --cfg cfg.buidler.toml genbjj ``` -Wipe the entier SQL database (this will destroy all synchronized and pool data): +Wipe the entier SQL database (this will destroy all synchronized and pool +data): ``` ./node --mode coord --cfg cfg.buidler.toml wipesql ``` + +Discard all synchronized blocks and associated state up to a given block +number. This command is useful in case the synchronizer reaches an invalid +state and you want to roll back a few blocks and try again (maybe with some +fixes in the code). +``` +./node --mode coord --cfg cfg.buidler.toml discard --block 8061330 +``` diff --git a/cli/node/cfg.buidler.toml b/cli/node/cfg.buidler.toml index dd63f9d..f80efa8 100644 --- a/cli/node/cfg.buidler.toml +++ b/cli/node/cfg.buidler.toml @@ -53,6 +53,8 @@ SendBatchBlocksMarginCheck = 1 ProofServerPollInterval = "1s" ForgeRetryInterval = "500ms" SyncRetryInterval = "1s" +ForgeDelay = "10s" +ForgeNoTxsDelay = "0s" [Coordinator.FeeAccount] Address = "0x56232B1c5B10038125Bc7345664B4AFD745bcF8E" @@ -83,16 +85,13 @@ MaxTx = 512 NLevels = 32 [Coordinator.EthClient] -ReceiptTimeout = "60s" -ReceiptLoopInterval = "500ms" CheckLoopInterval = "500ms" Attempts = 4 AttemptsDelay = "500ms" TxResendTimeout = "2m" NoReuseNonce = false -CallGasLimit = 300000 -GasPriceDiv = 100 MaxGasPrice = "5000000000" +GasPriceIncPerc = 10 [Coordinator.EthClient.Keystore] Path = "/tmp/iden3-test/hermez/ethkeystore" diff --git a/cli/node/main.go b/cli/node/main.go index 3c21331..132b5ab 100644 --- a/cli/node/main.go +++ b/cli/node/main.go @@ -11,6 +11,8 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/hermeznetwork/hermez-node/config" dbUtils "github.com/hermeznetwork/hermez-node/db" + "github.com/hermeznetwork/hermez-node/db/historydb" + "github.com/hermeznetwork/hermez-node/db/l2db" "github.com/hermeznetwork/hermez-node/log" "github.com/hermeznetwork/hermez-node/node" "github.com/hermeznetwork/tracerr" @@ -23,6 +25,7 @@ const ( flagMode = "mode" flagSK = "privatekey" flagYes = "yes" + flagBlock = "block" modeSync = "sync" modeCoord = "coord" ) @@ -139,6 +142,47 @@ func cmdRun(c *cli.Context) error { return nil } +func cmdDiscard(c *cli.Context) error { + _cfg, err := parseCli(c) + if err != nil { + return tracerr.Wrap(fmt.Errorf("error parsing flags and config: %w", err)) + } + cfg := _cfg.node + blockNum := c.Int64(flagBlock) + log.Infof("Discarding all blocks up to block %v...", blockNum) + + db, err := dbUtils.InitSQLDB( + cfg.PostgreSQL.Port, + cfg.PostgreSQL.Host, + cfg.PostgreSQL.User, + cfg.PostgreSQL.Password, + cfg.PostgreSQL.Name, + ) + if err != nil { + return tracerr.Wrap(fmt.Errorf("dbUtils.InitSQLDB: %w", err)) + } + historyDB := historydb.NewHistoryDB(db, nil) + if err := historyDB.Reorg(blockNum); err != nil { + return tracerr.Wrap(fmt.Errorf("historyDB.Reorg: %w", err)) + } + batchNum, err := historyDB.GetLastBatchNum() + if err != nil { + return tracerr.Wrap(fmt.Errorf("historyDB.GetLastBatchNum: %w", err)) + } + l2DB := l2db.NewL2DB( + db, + cfg.Coordinator.L2DB.SafetyPeriod, + cfg.Coordinator.L2DB.MaxTxs, + cfg.Coordinator.L2DB.TTL.Duration, + nil, + ) + if err := l2DB.Reorg(batchNum); err != nil { + return tracerr.Wrap(fmt.Errorf("l2DB.Reorg: %w", err)) + } + + return nil +} + // Config is the configuration of the hermez node execution type Config struct { mode node.Mode @@ -239,6 +283,18 @@ func main() { Usage: "Run the hermez-node in the indicated mode", Action: cmdRun, }, + { + Name: "discard", + Aliases: []string{}, + Usage: "Discard blocks up to a specified block number", + Action: cmdDiscard, + Flags: []cli.Flag{ + &cli.Int64Flag{ + Name: flagBlock, + Usage: "last block number to keep", + Required: false, + }}, + }, } err := app.Run(os.Args)