@ -5,6 +5,7 @@ import (
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard"
"github.com/harmony-one/harmony/shard"
"github.com/pkg/errors"
)
)
var (
var (
@ -12,6 +13,8 @@ var (
HarmonysShare = numeric . MustNewDecFromStr ( "0.68" )
HarmonysShare = numeric . MustNewDecFromStr ( "0.68" )
// StakersShare ..
// StakersShare ..
StakersShare = numeric . MustNewDecFromStr ( "0.32" )
StakersShare = numeric . MustNewDecFromStr ( "0.32" )
// ErrVotingPowerNotEqualOne ..
ErrVotingPowerNotEqualOne = errors . New ( "voting power not equal to one" )
)
)
type stakedVoter struct {
type stakedVoter struct {
@ -32,7 +35,7 @@ type Roster struct {
}
}
// Compute creates a new roster based off the shard.SlotList
// Compute creates a new roster based off the shard.SlotList
func Compute ( staked shard . SlotList ) * Roster {
func Compute ( staked shard . SlotList ) ( * Roster , error ) {
roster := NewRoster ( )
roster := NewRoster ( )
for i := range staked {
for i := range staked {
if staked [ i ] . TotalStake == nil {
if staked [ i ] . TotalStake == nil {
@ -82,7 +85,7 @@ func Compute(staked shard.SlotList) *Roster {
// NOTE Enforce voting power sums to one, give diff (expect tiny amt) to last staked voter
// NOTE Enforce voting power sums to one, give diff (expect tiny amt) to last staked voter
if diff := numeric . OneDec ( ) . Sub (
if diff := numeric . OneDec ( ) . Sub (
ourPercentage . Add ( theirPercentage ) ,
ourPercentage . Add ( theirPercentage ) ,
) ; diff . GT ( numeric . ZeroDec ( ) ) && lastStakedVoter != nil {
) ; lastStakedVoter != nil {
lastStakedVoter . EffectivePercent = lastStakedVoter . EffectivePercent . Add ( diff )
lastStakedVoter . EffectivePercent = lastStakedVoter . EffectivePercent . Add ( diff )
theirPercentage = theirPercentage . Add ( diff )
theirPercentage = theirPercentage . Add ( diff )
utils . Logger ( ) . Info ( ) .
utils . Logger ( ) . Info ( ) .
@ -91,10 +94,19 @@ func Compute(staked shard.SlotList) *Roster {
Msg ( "sum of voting power of hmy & staked slots not equal to 1, gave diff to staked slot" )
Msg ( "sum of voting power of hmy & staked slots not equal to 1, gave diff to staked slot" )
}
}
if lastStakedVoter != nil &&
! ourPercentage . Add ( theirPercentage ) . Equal ( numeric . OneDec ( ) ) {
utils . Logger ( ) . Error ( ) .
Str ( "theirs" , theirPercentage . String ( ) ) .
Str ( "ours" , ourPercentage . String ( ) ) .
Msg ( "Total voting power not equal 100 percent" )
return nil , ErrVotingPowerNotEqualOne
}
roster . OurVotingPowerTotalPercentage = ourPercentage
roster . OurVotingPowerTotalPercentage = ourPercentage
roster . TheirVotingPowerTotalPercentage = theirPercentage
roster . TheirVotingPowerTotalPercentage = theirPercentage
return roster
return roster , nil
}
}