update return error when update M1

This commit is contained in:
Nguyen Ba Tam 2018-08-27 14:45:26 +07:00
parent 9882b08034
commit 9bac2dd5e3
4 changed files with 18 additions and 15 deletions

View file

@ -349,7 +349,10 @@ func startNode(ctx *cli.Context, stack *node.Node) {
log.Info("Enabled staking node!!!") log.Info("Enabled staking node!!!")
} }
case <-core.M1Ch: case <-core.M1Ch:
ethereum.BlockChain().UpdateM1() err := ethereum.BlockChain().UpdateM1()
if(err !=nil){
log.Error("Error when update M1",err)
}
} }
} }
}() }()

View file

@ -698,7 +698,7 @@ func (bc *BlockChain) procFutureBlocks() {
type WriteStatus byte type WriteStatus byte
const ( const (
NonStatTy WriteStatus = iota NonStatTy WriteStatus = iota
CanonStatTy CanonStatTy
SideStatTy SideStatTy
) )
@ -1594,9 +1594,9 @@ func (bc *BlockChain) GetClient() (*ethclient.Client, error) {
return bc.Client, nil return bc.Client, nil
} }
func (bc *BlockChain) UpdateM1() { func (bc *BlockChain) UpdateM1() error {
if bc.Config().Posv == nil { if bc.Config().Posv == nil {
return return errors.New("Posv not found in config")
} }
engine := bc.Engine().(*posv.Posv) engine := bc.Engine().(*posv.Posv)
log.Info("It's time to update new set of masternodes for the next epoch...") log.Info("It's time to update new set of masternodes for the next epoch...")
@ -1608,29 +1608,25 @@ func (bc *BlockChain) UpdateM1() {
addr := common.HexToAddress(common.MasternodeVotingSMC) addr := common.HexToAddress(common.MasternodeVotingSMC)
validator, err := contractValidator.NewTomoValidator(addr, client) validator, err := contractValidator.NewTomoValidator(addr, client)
if err != nil { if err != nil {
log.Crit("Fail to get validator smc: %v", err) return err
} }
opts := new(bind.CallOpts) opts := new(bind.CallOpts)
candidates, err := validator.GetCandidates(opts) candidates, err := validator.GetCandidates(opts)
if err != nil { if err != nil {
log.Crit("Can't get list of masternode candidates: %v", err) return err
} }
var ms []posv.Masternode var ms []posv.Masternode
for _, candidate := range candidates { for _, candidate := range candidates {
v, err := validator.GetCandidateCap(opts, candidate) v, err := validator.GetCandidateCap(opts, candidate)
if err != nil { if err != nil {
log.Warn("Can't get cap of a masternode candidate. Will ignore him", "address", candidate, "error", err) return err
} }
//TODO: smart contract shouldn't return "0x0000000000000000000000000000000000000000" //TODO: smart contract shouldn't return "0x0000000000000000000000000000000000000000"
if candidate.String() != "0x0000000000000000000000000000000000000000" { if candidate.String() != "0x0000000000000000000000000000000000000000" {
ms = append(ms, posv.Masternode{Address: candidate, Stake: v.String()}) ms = append(ms, posv.Masternode{Address: candidate, Stake: v.String()})
} }
} }
//// order by cap
//sort.Slice(ms, func(i, j int) bool {
// return ms[i].Stake > ms[j].Stake
//})
log.Info("Ordered list of masternode candidates") log.Info("Ordered list of masternode candidates")
for _, m := range ms { for _, m := range ms {
fmt.Printf("address: %s, stake: %s\n", m.Address.String(), m.Stake) fmt.Printf("address: %s, stake: %s\n", m.Address.String(), m.Stake)
@ -1642,8 +1638,9 @@ func (bc *BlockChain) UpdateM1() {
log.Info("Updating new set of masternodes") log.Info("Updating new set of masternodes")
err = engine.UpdateMasternodes(bc, bc.CurrentHeader(), ms) err = engine.UpdateMasternodes(bc, bc.CurrentHeader(), ms)
if err != nil { if err != nil {
log.Crit("Can't update masternodes: %v", err) return err
} }
log.Info("Masternodes are ready for the next epoch") log.Info("Masternodes are ready for the next epoch")
} }
return nil
} }

View file

@ -173,7 +173,7 @@ type LightChain interface {
// BlockChain encapsulates functions required to sync a (full or fast) blockchain. // BlockChain encapsulates functions required to sync a (full or fast) blockchain.
type BlockChain interface { type BlockChain interface {
Config() *params.ChainConfig Config() *params.ChainConfig
UpdateM1() UpdateM1() error
LightChain LightChain
// HasBlock verifies a block's presence in the local chain. // HasBlock verifies a block's presence in the local chain.
@ -1349,7 +1349,10 @@ func (d *Downloader) processFullSyncContent() error {
} }
// prepare set of masternodes for the next epoch // prepare set of masternodes for the next epoch
if (inserts[len(inserts)-1].Header.Number.Uint64() % epoch) == (epoch - gap) { if (inserts[len(inserts)-1].Header.Number.Uint64() % epoch) == (epoch - gap) {
d.blockchain.UpdateM1() err := d.blockchain.UpdateM1()
if (err != nil) {
log.Error("Error when update M1", err)
}
} }
} }
start = end + 1 start = end + 1

View file

@ -458,7 +458,7 @@ func (dl *downloadTester) dropPeer(id string) {
// Config retrieves the blockchain's chain configuration. // Config retrieves the blockchain's chain configuration.
func (dl *downloadTester) Config() *params.ChainConfig { return params.TestChainConfig } func (dl *downloadTester) Config() *params.ChainConfig { return params.TestChainConfig }
func (dl *downloadTester) UpdateM1() {} func (dl *downloadTester) UpdateM1() error { return nil }
type downloadTesterPeer struct { type downloadTesterPeer struct {
dl *downloadTester dl *downloadTester