diff --git a/cmd/tomo/main.go b/cmd/tomo/main.go index 7d3b328416..e624fad7b8 100644 --- a/cmd/tomo/main.go +++ b/cmd/tomo/main.go @@ -321,7 +321,6 @@ func startNode(ctx *cli.Context, stack *node.Node, cfg tomoConfig) { log.Info("Enabled staking node!!!") } defer close(core.CheckpointCh) - defer close(core.M1Ch) for { select { case <-core.CheckpointCh: @@ -356,11 +355,6 @@ func startNode(ctx *cli.Context, stack *node.Node, cfg tomoConfig) { started = true log.Info("Enabled staking node!!!") } - case <-core.M1Ch: - err := ethereum.BlockChain().UpdateM1() - if err != nil { - log.Error("Error when update M1", err) - } } } }() diff --git a/core/blockchain.go b/core/blockchain.go index ff234c4b5b..c468e42695 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -23,6 +23,7 @@ import ( "io" "math/big" mrand "math/rand" + "os" "sync" "sync/atomic" "time" @@ -52,7 +53,6 @@ import ( var ( blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil) CheckpointCh = make(chan int) - M1Ch = make(chan int) ErrNoGenesis = errors.New("Genesis not found in chain") ) @@ -1199,7 +1199,15 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*ty } // prepare set of masternodes for the next epoch if (chain[i].NumberU64() % bc.chainConfig.Posv.Epoch) == (bc.chainConfig.Posv.Epoch - bc.chainConfig.Posv.Gap) { - M1Ch <- 1 + err := bc.UpdateM1() + if err != nil { + if err == ErrNotPoSV { + log.Error("Stopping node", "err", err) + os.Exit(1) + } else { + log.Error("Error when update masternodes set. Keep the current masternodes set for the next epoch.", "err", err) + } + } } } } @@ -1596,14 +1604,14 @@ func (bc *BlockChain) GetClient() (*ethclient.Client, error) { func (bc *BlockChain) UpdateM1() error { if bc.Config().Posv == nil { - return errors.New("Posv not found in config") + return ErrNotPoSV } engine := bc.Engine().(*posv.Posv) log.Info("It's time to update new set of masternodes for the next epoch...") // get masternodes information from smart contract - client, err := ethclient.Dial(bc.IPCEndpoint) + client, err := bc.GetClient() if err != nil { - log.Crit("Fail to connect IPC: %v", err) + return err } addr := common.HexToAddress(common.MasternodeVotingSMC) validator, err := contractValidator.NewTomoValidator(addr, client) @@ -1615,7 +1623,6 @@ func (bc *BlockChain) UpdateM1() error { if err != nil { return err } - var ms []posv.Masternode for _, candidate := range candidates { v, err := validator.GetCandidateCap(opts, candidate) @@ -1629,7 +1636,7 @@ func (bc *BlockChain) UpdateM1() error { } log.Info("Ordered list of masternode candidates") for _, m := range ms { - fmt.Printf("address: %s, stake: %s\n", m.Address.String(), m.Stake) + log.Info("", "address", m.Address.String(), "stake", m.Stake) } if len(ms) == 0 { log.Info("No masternode candidates found. Keep the current masternodes set for the next epoch") diff --git a/core/error.go b/core/error.go index 410eca1e1e..d55c7840e6 100644 --- a/core/error.go +++ b/core/error.go @@ -32,4 +32,6 @@ var ( // ErrNonceTooHigh is returned if the nonce of a transaction is higher than the // next one expected based on the local chain. ErrNonceTooHigh = errors.New("nonce too high") + + ErrNotPoSV = errors.New("Posv not found in config") ) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 183c36e716..6b67cf3080 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -173,7 +173,6 @@ type LightChain interface { // BlockChain encapsulates functions required to sync a (full or fast) blockchain. type BlockChain interface { Config() *params.ChainConfig - UpdateM1() error LightChain // HasBlock verifies a block's presence in the local chain. @@ -1347,13 +1346,6 @@ func (d *Downloader) processFullSyncContent() error { if err := d.importBlockResults(inserts); err != nil { return err } - // prepare set of masternodes for the next epoch - if (inserts[len(inserts)-1].Header.Number.Uint64() % epoch) == (epoch - gap) { - err := d.blockchain.UpdateM1() - if err != nil { - log.Error("Error when update M1", err) - } - } } start = end + 1 end = end + int(epoch) diff --git a/eth/downloader/downloader_test.go b/eth/downloader/downloader_test.go index d3f422de64..7ac2e14c3a 100644 --- a/eth/downloader/downloader_test.go +++ b/eth/downloader/downloader_test.go @@ -458,7 +458,6 @@ func (dl *downloadTester) dropPeer(id string) { // Config retrieves the blockchain's chain configuration. func (dl *downloadTester) Config() *params.ChainConfig { return params.TestChainConfig } -func (dl *downloadTester) UpdateM1() error { return nil } type downloadTesterPeer struct { dl *downloadTester diff --git a/miner/worker.go b/miner/worker.go index ed3e37098e..8c6beb8c65 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -20,6 +20,7 @@ import ( "bytes" "fmt" "math/big" + "os" "sync" "sync/atomic" "time" @@ -593,7 +594,15 @@ func (self *worker) commitNewWork() { } // prepare set of masternodes for the next epoch if (work.Block.NumberU64() % work.config.Posv.Epoch) == (work.config.Posv.Epoch - work.config.Posv.Gap) { - core.M1Ch <- 1 + err := self.chain.UpdateM1() + if err != nil { + if err == core.ErrNotPoSV { + log.Error("Stopping node", "err", err) + os.Exit(1) + } else { + log.Error("Error when update masternodes set. Keep the current masternodes set for the next epoch.", "err", err) + } + } } } self.push(work)