From 30ae355fd1a7c81f22b805080c033b7fa8215510 Mon Sep 17 00:00:00 2001 From: Tuna Date: Thu, 27 Sep 2018 10:36:40 +0700 Subject: [PATCH 1/2] adjust signed recently condition --- cmd/tomo/config.go | 4 ++-- consensus/posv/posv.go | 41 ++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/cmd/tomo/config.go b/cmd/tomo/config.go index f6c6c11542..56c95f8918 100644 --- a/cmd/tomo/config.go +++ b/cmd/tomo/config.go @@ -20,12 +20,12 @@ import ( "bufio" "errors" "fmt" + "gopkg.in/urfave/cli.v1" "io" "os" "reflect" - "unicode" "strings" - "gopkg.in/urfave/cli.v1" + "unicode" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/dashboard" diff --git a/consensus/posv/posv.go b/consensus/posv/posv.go index 950ad32059..30e3641aac 100644 --- a/consensus/posv/posv.go +++ b/consensus/posv/posv.go @@ -575,13 +575,16 @@ func (c *Posv) verifySeal(chain consensus.ChainReader, header *types.Header, par return errUnauthorized } } - for seen, recent := range snap.Recents { - if recent == signer { - // Signer is among recents, only fail if the current block doesn't shift it out - if limit := uint64(len(masternodes)/2 + 1); seen > number-limit { - // Only take into account the non-epoch blocks - if number%c.config.Epoch != 0 { - return errUnauthorized + if len(masternodes) > 1 { + for seen, recent := range snap.Recents { + if recent == signer { + // Signer is among recents, only fail if the current block doesn't shift it out + // There is only case that we don't allow signer to create two continuous blocks. + if limit := uint64(2); seen > number-limit { + // Only take into account the non-epoch blocks + if number%c.config.Epoch != 0 { + return errUnauthorized + } } } } @@ -755,16 +758,20 @@ func (c *Posv) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan } } // If we're amongst the recent signers, wait for the next block - for seen, recent := range snap.Recents { - if recent == signer { - // Signer is among recents, only wait if the current block doesn't shift it out - if limit := uint64(len(masternodes)/2 + 1); number < limit || seen > number-limit { - // Only take into account the non-epoch blocks - if number%c.config.Epoch != 0 { - log.Info("Debugging", "len(masternodes)", len(masternodes), "number", number, "limit", limit, "seen", seen, "recent", recent.String(), "snap.Recents", snap.Recents) - log.Info("Signed recently, must wait for others") - <-stop - return nil, nil + // only check recent signers if there are more than one signer. + if len(masternodes) > 1 { + for seen, recent := range snap.Recents { + if recent == signer { + // Signer is among recents, only wait if the current block doesn't shift it out + // There is only case that we don't allow signer to create two continuous blocks. + if limit := uint64(2); number < limit || seen > number-limit { + // Only take into account the non-epoch blocks + if number%c.config.Epoch != 0 { + log.Info("Debugging", "len(masternodes)", len(masternodes), "number", number, "limit", limit, "seen", seen, "recent", recent.String(), "snap.Recents", snap.Recents) + log.Info("Signed recently, must wait for others") + <-stop + return nil, nil + } } } } From 205878c40b1d1ec4dd277eb5e8d81eebf846c45a Mon Sep 17 00:00:00 2001 From: Tuna Date: Thu, 27 Sep 2018 14:36:58 +0700 Subject: [PATCH 2/2] fix golint --- cmd/tomo/config.go | 2 +- cmd/tomo/main.go | 57 ++++++++++++++++++++++------------------------ 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/cmd/tomo/config.go b/cmd/tomo/config.go index 56c95f8918..6fc12571e7 100644 --- a/cmd/tomo/config.go +++ b/cmd/tomo/config.go @@ -151,7 +151,7 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, tomoConfig) { ctx.Set(utils.NATFlag.Name, cfg.NAT) } - // read passwords from enviroment + // read passwords from environment passwords := []string{} for _, env := range cfg.Account.Passwords { if trimmed := strings.TrimSpace(env); trimmed != "" { diff --git a/cmd/tomo/main.go b/cmd/tomo/main.go index e624fad7b8..ecf4df5725 100644 --- a/cmd/tomo/main.go +++ b/cmd/tomo/main.go @@ -321,40 +321,37 @@ func startNode(ctx *cli.Context, stack *node.Node, cfg tomoConfig) { log.Info("Enabled staking node!!!") } defer close(core.CheckpointCh) - for { - select { - case <-core.CheckpointCh: - log.Info("Checkpoint!!! It's time to reconcile node's state...") - ok, err := ethereum.ValidateStaker() - if err != nil { - utils.Fatalf("Can't verify masternode permission: %v", err) + for range core.CheckpointCh { + log.Info("Checkpoint!!! It's time to reconcile node's state...") + ok, err := ethereum.ValidateStaker() + if err != nil { + utils.Fatalf("Can't verify masternode permission: %v", err) + } + if !ok { + if started { + log.Info("Only masternode can propose and verify blocks. Cancelling staking on this node...") + ethereum.StopStaking() + started = false + log.Info("Cancelled mining mode!!!") } - if !ok { - if started { - log.Info("Only masternode can propose and verify blocks. Cancelling staking on this node...") - ethereum.StopStaking() - started = false - log.Info("Cancelled mining mode!!!") + } else if !started { + log.Info("Masternode found. Enabling staking mode...") + // Use a reduced number of threads if requested + if threads := ctx.GlobalInt(utils.StakerThreadsFlag.Name); threads > 0 { + type threaded interface { + SetThreads(threads int) } - } else if !started { - log.Info("Masternode found. Enabling staking mode...") - // Use a reduced number of threads if requested - if threads := ctx.GlobalInt(utils.StakerThreadsFlag.Name); threads > 0 { - type threaded interface { - SetThreads(threads int) - } - if th, ok := ethereum.Engine().(threaded); ok { - th.SetThreads(threads) - } + if th, ok := ethereum.Engine().(threaded); ok { + th.SetThreads(threads) } - // Set the gas price to the limits from the CLI and start mining - ethereum.TxPool().SetGasPrice(cfg.Eth.GasPrice) - if err := ethereum.StartStaking(true); err != nil { - utils.Fatalf("Failed to start staking: %v", err) - } - started = true - log.Info("Enabled staking node!!!") } + // Set the gas price to the limits from the CLI and start mining + ethereum.TxPool().SetGasPrice(cfg.Eth.GasPrice) + if err := ethereum.StartStaking(true); err != nil { + utils.Fatalf("Failed to start staking: %v", err) + } + started = true + log.Info("Enabled staking node!!!") } } }()