convert update M1 method from async to sync

This commit is contained in:
AnilChinchawale 2018-11-02 12:14:43 +05:30
parent e0ced295e1
commit 7e7bc37c20
7 changed files with 27 additions and 25 deletions

View file

@ -321,7 +321,6 @@ func startNode(ctx *cli.Context, stack *node.Node, cfg XDCConfig) {
log.Info("Enabled staking node!!!") log.Info("Enabled staking node!!!")
} }
defer close(core.CheckpointCh) defer close(core.CheckpointCh)
defer close(core.M1Ch)
for { for {
select { select {
case <-core.CheckpointCh: case <-core.CheckpointCh:
@ -356,11 +355,7 @@ func startNode(ctx *cli.Context, stack *node.Node, cfg XDCConfig) {
started = true started = true
log.Info("Enabled staking node!!!") log.Info("Enabled staking node!!!")
} }
case <-core.M1Ch:
err := ethereum.BlockChain().UpdateM1()
if err != nil {
log.Error("Error when update M1", err)
}
} }
} }
}() }()

Binary file not shown.

View file

@ -52,7 +52,6 @@ import (
var ( var (
blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil) blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil)
CheckpointCh = make(chan int) CheckpointCh = make(chan int)
M1Ch = make(chan int)
ErrNoGenesis = errors.New("Genesis not found in chain") ErrNoGenesis = errors.New("Genesis not found in chain")
) )
@ -1192,14 +1191,21 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*ty
stats.processed++ stats.processed++
stats.usedGas += usedGas stats.usedGas += usedGas
stats.report(chain, i, bc.stateCache.TrieDB().Size()) stats.report(chain, i, bc.stateCache.TrieDB().Size())
if i == len(chain)-1 && bc.chainConfig.XDPoS != nil { if bc.chainConfig.XDPoS != nil {
// epoch block // epoch block
if (chain[i].NumberU64() % bc.chainConfig.XDPoS.Epoch) == 0 { if (chain[i].NumberU64() % bc.chainConfig.XDPoS.Epoch) == 0 {
CheckpointCh <- 1 CheckpointCh <- 1
} }
// prepare set of masternodes for the next epoch // prepare set of masternodes for the next epoch
if (chain[i].NumberU64() % bc.chainConfig.XDPoS.Epoch) == (bc.chainConfig.XDPoS.Epoch - bc.chainConfig.XDPoS.Gap) { if (chain[i].NumberU64() % bc.chainConfig.XDPoS.Epoch) == (bc.chainConfig.XDPoS.Epoch - bc.chainConfig.XDPoS.Gap) {
M1Ch <- 1 err := bc.UpdateM1()
if err != nil {
if err == ErrNotXDPoS {
log.Crit("Error when update M1 ", "err", err)
} else {
log.Error("Error when update M1 ", "err", err)
}
}
} }
} }
} }
@ -1594,14 +1600,14 @@ func (bc *BlockChain) GetClient() (*ethclient.Client, error) {
func (bc *BlockChain) UpdateM1() error { func (bc *BlockChain) UpdateM1() error {
if bc.Config().XDPoS == nil { if bc.Config().XDPoS == nil {
return errors.New("XDPoS not found in config") return ErrNotXDPoS
} }
engine := bc.Engine().(*XDPoS.XDPoS) engine := bc.Engine().(*XDPoS.XDPoS)
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...")
// get masternodes information from smart contract // get masternodes information from smart contract
client, err := ethclient.Dial(bc.IPCEndpoint) client, err := bc.GetClient()
if err != nil { if err != nil {
log.Crit("Fail to connect IPC: %v", err) return err
} }
addr := common.HexToAddress(common.MasternodeVotingSMC) addr := common.HexToAddress(common.MasternodeVotingSMC)
validator, err := contractValidator.NewXDCValidator(addr, client) validator, err := contractValidator.NewXDCValidator(addr, client)
@ -1613,7 +1619,6 @@ func (bc *BlockChain) UpdateM1() error {
if err != nil { if err != nil {
return err return err
} }
var ms []XDPoS.Masternode var ms []XDPoS.Masternode
for _, candidate := range candidates { for _, candidate := range candidates {
v, err := validator.GetCandidateCap(opts, candidate) v, err := validator.GetCandidateCap(opts, candidate)
@ -1627,7 +1632,7 @@ func (bc *BlockChain) UpdateM1() error {
} }
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) log.Info("", "address", m.Address.String(), "stake", m.Stake)
} }
if len(ms) == 0 { if len(ms) == 0 {
log.Info("No masternode candidates found. Keep the current masternodes set for the next epoch") log.Info("No masternode candidates found. Keep the current masternodes set for the next epoch")

View file

@ -32,4 +32,7 @@ var (
// ErrNonceTooHigh is returned if the nonce of a transaction is higher than the // ErrNonceTooHigh is returned if the nonce of a transaction is higher than the
// next one expected based on the local chain. // next one expected based on the local chain.
ErrNonceTooHigh = errors.New("nonce too high") ErrNonceTooHigh = errors.New("nonce too high")
ErrNotXDPoS = errors.New("XDPoS not found in config")
) )

View file

@ -173,7 +173,6 @@ 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() error
LightChain LightChain
// HasBlock verifies a block's presence in the local chain. // HasBlock verifies a block's presence in the local chain.
@ -1347,13 +1346,7 @@ func (d *Downloader) processFullSyncContent() error {
if err := d.importBlockResults(inserts); err != nil { if err := d.importBlockResults(inserts); err != nil {
return err 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 start = end + 1
end = end + int(epoch) end = end + int(epoch)

View file

@ -458,7 +458,6 @@ 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() error { return nil }
type downloadTesterPeer struct { type downloadTesterPeer struct {
dl *downloadTester dl *downloadTester

View file

@ -593,7 +593,14 @@ func (self *worker) commitNewWork() {
} }
// prepare set of masternodes for the next epoch // prepare set of masternodes for the next epoch
if (work.Block.NumberU64() % work.config.XDPoS.Epoch) == (work.config.XDPoS.Epoch - work.config.XDPoS.Gap) { if (work.Block.NumberU64() % work.config.XDPoS.Epoch) == (work.config.XDPoS.Epoch - work.config.XDPoS.Gap) {
core.M1Ch <- 1 err := self.chain.UpdateM1()
if err != nil {
if err == core.ErrNotXDPoS {
log.Crit("Error when update M1 ", "err", err)
} else {
log.Error("Error when update M1 ", "err", err)
}
}
} }
} }
self.push(work) self.push(work)