From 4962300b5e9195dc8e131048d9c283f070b04b07 Mon Sep 17 00:00:00 2001 From: parmarrushabh Date: Fri, 2 Nov 2018 14:27:17 +0530 Subject: [PATCH] adding sort to masternode list --- core/blockchain.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index ac6e279a12..bf661863ab 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -26,6 +26,7 @@ import ( "sync" "sync/atomic" "time" + "sort" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" @@ -62,6 +63,7 @@ const ( maxTimeFutureBlocks = 30 badBlockLimit = 10 triesInMemory = 128 + masterNodeLimit = 150 // BlockChainVersion ensures that an incompatible database forces a resync from scratch. BlockChainVersion = 3 @@ -1627,7 +1629,7 @@ func (bc *BlockChain) UpdateM1() error { } //TODO: smart contract shouldn't return "0x0000000000000000000000000000000000000000" if candidate.String() != "0x0000000000000000000000000000000000000000" { - ms = append(ms, XDPoS.Masternode{Address: candidate, Stake: v.String()}) + ms = append(ms, XDPoS.Masternode{Address: candidate, Stake: v.Uint64()}) } } log.Info("Ordered list of masternode candidates") @@ -1637,9 +1639,16 @@ func (bc *BlockChain) UpdateM1() error { if len(ms) == 0 { log.Info("No masternode candidates found. Keep the current masternodes set for the next epoch") } else { + sort.Slice(ms, func(i, j int) bool { + return ms[i].Stake >= ms[j].Stake + }) // update masternodes log.Info("Updating new set of masternodes") - err = engine.UpdateMasternodes(bc, bc.CurrentHeader(), ms) + if len(ms) > masterNodeLimit { + err = engine.UpdateMasternodes(bc, bc.CurrentHeader(), ms[:masterNodeLimit]) + } else { + err = engine.UpdateMasternodes(bc, bc.CurrentHeader(), ms) + } if err != nil { return err }