Skip to content

Commit

Permalink
Merge pull request ethereum#170 from ngtuna/sort-ms
Browse files Browse the repository at this point in the history
adding sort to masternode list
  • Loading branch information
ngtuna authored Sep 10, 2018
2 parents 523a9ea + 8d6549d commit b2cb528
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion consensus/posv/posv.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const (

type Masternode struct {
Address common.Address
Stake string
Stake uint64
}

// Posv proof-of-stake-voting protocol constants.
Expand Down
13 changes: 11 additions & 2 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sync"
"sync/atomic"
"time"
"sort"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -63,6 +64,7 @@ const (
maxTimeFutureBlocks = 30
badBlockLimit = 10
triesInMemory = 128
masterNodeLimit = 150

// BlockChainVersion ensures that an incompatible database forces a resync from scratch.
BlockChainVersion = 3
Expand Down Expand Up @@ -1631,7 +1633,7 @@ func (bc *BlockChain) UpdateM1() error {
}
//TODO: smart contract shouldn't return "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")
Expand All @@ -1641,9 +1643,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
}
Expand Down

0 comments on commit b2cb528

Please sign in to comment.