Merge pull request #170 from ngtuna/sort-ms

adding sort to masternode list
This commit is contained in:
Tuna 2018-09-10 12:51:42 +07:00 committed by GitHub
commit b2cb528045
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View file

@ -51,7 +51,7 @@ const (
type Masternode struct { type Masternode struct {
Address common.Address Address common.Address
Stake string Stake uint64
} }
// Posv proof-of-stake-voting protocol constants. // Posv proof-of-stake-voting protocol constants.

View file

@ -27,6 +27,7 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
"sort"
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -63,6 +64,7 @@ const (
maxTimeFutureBlocks = 30 maxTimeFutureBlocks = 30
badBlockLimit = 10 badBlockLimit = 10
triesInMemory = 128 triesInMemory = 128
masterNodeLimit = 150
// BlockChainVersion ensures that an incompatible database forces a resync from scratch. // BlockChainVersion ensures that an incompatible database forces a resync from scratch.
BlockChainVersion = 3 BlockChainVersion = 3
@ -1631,7 +1633,7 @@ func (bc *BlockChain) UpdateM1() error {
} }
//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.Uint64()})
} }
} }
log.Info("Ordered list of masternode candidates") log.Info("Ordered list of masternode candidates")
@ -1641,9 +1643,16 @@ func (bc *BlockChain) UpdateM1() error {
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")
} else { } else {
sort.Slice(ms, func(i, j int) bool {
return ms[i].Stake >= ms[j].Stake
})
// update masternodes // update masternodes
log.Info("Updating new set of masternodes") log.Info("Updating new set of masternodes")
if len(ms) > masterNodeLimit {
err = engine.UpdateMasternodes(bc, bc.CurrentHeader(), ms[:masterNodeLimit])
} else {
err = engine.UpdateMasternodes(bc, bc.CurrentHeader(), ms) err = engine.UpdateMasternodes(bc, bc.CurrentHeader(), ms)
}
if err != nil { if err != nil {
return err return err
} }