arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
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).
Eduard S eduard@iden3.io
Fri, 12 Feb 2021 17:49:47 +0100
commit

7b01f6a2884ac03fe42eb1801f7e4dba35127205

parent

f2e5800ebd17a58c1f27a63660353c9f46ded72f

3 files changed, 69 insertions(+), 5 deletions(-)

jump to
M cli/node/README.md → cli/node/README.md

@@ -117,7 +117,16 @@ ```

./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 +```
M cli/node/cfg.buidler.toml → 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"
M cli/node/main.go → cli/node/main.go

@@ -11,6 +11,8 @@ ethKeystore "github.com/ethereum/go-ethereum/accounts/keystore"

"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 @@ flagCfg = "cfg"

flagMode = "mode" flagSK = "privatekey" flagYes = "yes" + flagBlock = "block" modeSync = "sync" modeCoord = "coord" )

@@ -139,6 +142,47 @@

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

@@ -238,6 +282,18 @@ Name: "run",

Aliases: []string{}, 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, + }}, }, }