From 19c6b8410e283f8741213b63a5c87d1fa9b30ea1 Mon Sep 17 00:00:00 2001 From: DinhLN Date: Thu, 17 Jan 2019 13:59:02 +0700 Subject: [PATCH] Add rollback flag command for rollback chain at block hash. --- cmd/tomo/config.go | 5 +++++ cmd/tomo/main.go | 1 + cmd/utils/flags.go | 6 ++++++ common/constants.go | 1 + contracts/test.go | 1 + contracts/validator/validator_test.go | 6 +++--- contracts/validatorReader.go | 2 +- eth/backend.go | 11 +++++++++++ params/config.go | 1 + 9 files changed, 30 insertions(+), 4 deletions(-) diff --git a/cmd/tomo/config.go b/cmd/tomo/config.go index b6ec2db452c5..59cc910972c9 100644 --- a/cmd/tomo/config.go +++ b/cmd/tomo/config.go @@ -157,6 +157,11 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, tomoConfig) { common.IsTestnet = true } + // Check rollback hash exist. + if rollbackHash := ctx.GlobalString(utils.RollbackFlag.Name); rollbackHash != "" { + common.RollbackHash = common.HexToHash(rollbackHash) + } + // read passwords from environment passwords := []string{} for _, env := range cfg.Account.Passwords { diff --git a/cmd/tomo/main.go b/cmd/tomo/main.go index 88ddafbd65e3..92aa03bee09d 100644 --- a/cmd/tomo/main.go +++ b/cmd/tomo/main.go @@ -123,6 +123,7 @@ var ( configFileFlag, utils.AnnounceTxsFlag, utils.StoreRewardFlag, + utils.RollbackFlag, } rpcFlags = []cli.Flag{ diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index d1bff61b6581..cdebc9a99f58 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -112,6 +112,12 @@ func NewApp(gitCommit, usage string) *cli.App { // are the same for all commands. var ( + // Tomo flags. + RollbackFlag = cli.StringFlag{ + Name: "rollback", + Usage: "Rollback chain at hash", + Value: "", + } // General settings AnnounceTxsFlag = cli.BoolFlag{ Name: "announce-txs", diff --git a/common/constants.go b/common/constants.go index 715ad4f0c62c..4f64c40ed6ae 100644 --- a/common/constants.go +++ b/common/constants.go @@ -23,3 +23,4 @@ const ( var TIP2019Block = big.NewInt(1050000) var IsTestnet bool = false var StoreRewardFolder string +var RollbackHash Hash diff --git a/contracts/test.go b/contracts/test.go index a71056455e06..dcf72285d9ef 100644 --- a/contracts/test.go +++ b/contracts/test.go @@ -1,4 +1,5 @@ package contracts + // //import ( // "fmt" diff --git a/contracts/validator/validator_test.go b/contracts/validator/validator_test.go index a1cbf6a38680..7d1517da24e7 100644 --- a/contracts/validator/validator_test.go +++ b/contracts/validator/validator_test.go @@ -17,11 +17,11 @@ package validator import ( "context" + "encoding/json" "math/big" + "math/rand" "testing" "time" - "math/rand" - "encoding/json" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" @@ -248,4 +248,4 @@ func GetCandidatesOwnerBySigner(validator *contractValidator.TomoValidator, sign } return owner -} \ No newline at end of file +} diff --git a/contracts/validatorReader.go b/contracts/validatorReader.go index 5311f2f8fe01..517103019d6a 100644 --- a/contracts/validatorReader.go +++ b/contracts/validatorReader.go @@ -10,7 +10,7 @@ import ( ) var ( - slotValidatorMapping = map[string]uint64{ + slotValidatorMapping = map[string]uint64{ "withdrawsState": 0, "validatorsState": 1, "voters": 2, diff --git a/eth/backend.go b/eth/backend.go index cdd9788b71d0..6069c1a5e67a 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -170,6 +170,17 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { } eth.txPool = core.NewTxPool(config.TxPool, eth.chainConfig, eth.blockchain) + if common.RollbackHash != common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000") { + curBlock := eth.blockchain.CurrentBlock() + prevBlock := eth.blockchain.GetBlockByHash(common.RollbackHash) + + if curBlock.NumberU64() > prevBlock.NumberU64() { + for ; curBlock != nil && curBlock.NumberU64() != prevBlock.NumberU64(); curBlock = eth.blockchain.GetBlock(curBlock.ParentHash(), curBlock.NumberU64()-1) { + eth.blockchain.Rollback([]common.Hash{curBlock.Hash()}) + } + } + } + if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.SyncMode, config.NetworkId, eth.eventMux, eth.txPool, eth.engine, eth.blockchain, chainDb); err != nil { return nil, err } diff --git a/params/config.go b/params/config.go index bffd3da55dc3..5a3c194d0a99 100644 --- a/params/config.go +++ b/params/config.go @@ -216,6 +216,7 @@ func (c *ChainConfig) IsConstantinople(num *big.Int) bool { func (c *ChainConfig) IsTIP2019(num *big.Int) bool { return isForked(common.TIP2019Block, num) } + // GasTable returns the gas table corresponding to the current phase (homestead or homestead reprice). // // The returned GasTable's fields shouldn't, under any circumstances, be changed.