The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/consensus/quorum/one-node-one-vote.go

131 lines
3.4 KiB

package quorum
import (
"encoding/json"
"math/big"
"github.com/ethereum/go-ethereum/common"
bls_cosi "github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard"
)
type uniformVoteWeight struct {
DependencyInjectionWriter
DependencyInjectionReader
SignatureReader
}
// Policy ..
func (v *uniformVoteWeight) Policy() Policy {
return SuperMajorityVote
}
// IsQuorumAchieved ..
func (v *uniformVoteWeight) IsQuorumAchieved(p Phase) bool {
r := v.SignersCount(p) >= v.TwoThirdsSignersCount()
utils.Logger().Info().Str("phase", p.String()).
Int64("signers-count", v.SignersCount(p)).
Int64("threshold", v.TwoThirdsSignersCount()).
Int64("participants", v.ParticipantsCount()).
Msg("Quorum details")
return r
}
// IsQuorumAchivedByMask ..
func (v *uniformVoteWeight) IsQuorumAchievedByMask(mask *bls_cosi.Mask) bool {
threshold := v.TwoThirdsSignersCount()
currentTotalPower := utils.CountOneBits(mask.Bitmap)
if currentTotalPower < threshold {
utils.Logger().Warn().
Msgf("[IsQuorumAchievedByMask] Not enough voting power: need %+v, have %+v", threshold, currentTotalPower)
return false
}
utils.Logger().Debug().
Msgf("[IsQuorumAchievedByMask] have enough voting power: need %+v, have %+v",
threshold, currentTotalPower)
return true
}
// QuorumThreshold ..
func (v *uniformVoteWeight) QuorumThreshold() numeric.Dec {
return numeric.NewDec(v.TwoThirdsSignersCount())
}
// RewardThreshold ..
func (v *uniformVoteWeight) IsRewardThresholdAchieved() bool {
return v.SignersCount(Commit) >= (v.ParticipantsCount() * 9 / 10)
}
func (v *uniformVoteWeight) SetVoters(shard.SlotList) (*TallyResult, error) {
// NO-OP do not add anything here
return nil, nil
}
// Award ..
func (v *uniformVoteWeight) Award(
// Here hook is the callback which gets the amount the earner is due in just reward
// up to the hook to do side-effects like write the statedb
Pie *big.Int,
earners shard.SlotList,
hook func(earner common.Address, due *big.Int),
) *big.Int {
payout := big.NewInt(0)
last := big.NewInt(0)
count := big.NewInt(int64(len(earners)))
for i, account := range earners {
cur := big.NewInt(0)
cur.Mul(Pie, big.NewInt(int64(i+1))).Div(cur, count)
diff := big.NewInt(0).Sub(cur, last)
hook(common.Address(account.EcdsaAddress), diff)
payout = big.NewInt(0).Add(payout, diff)
last = cur
}
return payout
}
func (v *uniformVoteWeight) String() string {
s, _ := json.Marshal(v)
return string(s)
}
func (v *uniformVoteWeight) MarshalJSON() ([]byte, error) {
type t struct {
Policy string `json:"policy"`
Count int `json:"count"`
Participants []string `json:"committee-members"`
}
members := v.DumpParticipants()
[staking][validation][protocol] (#2396) * [staking][validation][protocol] Limit max bls keys * [staking-era] Fold banned and active into single field * [slash][effective] Remove LRU cache for slash, change .Active to enumeration * [slash] Remove leftover wrong usage of Logger * [slash][offchain] Only Decode if len > 0 * [offchain] cosmetic * [slash] Remove some logs in proposal * [webhook] Move webhook with call for when cannot commit block * [shard] Finally make finding subcommittee by shardID an explicit error * [node] Whitespace, prefer literal * [webhook] Report bad block to webhook * [slash] Expand verify, remove bad log usage, explicit error handle * [slash] Check on key size * [slash] Explicit upper bound of pending slashes * [slash] Use right epoch snapshot, fail to verify if epoch wrong on beaconchain * [multibls] Make max count allowed be 1/3 of external slots * [quorum] Remove bad API of ShardIDProvider, factor out committee key as method of committee * [verify] Begin factor out of common verification approach * [project] Further remove RawJSON log, use proper epoch for snapshot * [slash] Implement verification * [slash] Implement BLS key verification of ballots * [rpc] Keep validator information as meaningful as possible * [staking] Never can stop being banned * [slash] Comments and default Unknown case of eligibility * [slash] Be explicit on what input values allowed when want to change EPOSStatus * [consensus] Remove unneeded TODO * [verify] Add proper error message * [rpc] Give back to caller their wrong chain id * [chain] Add extra map dump of delegation sizing for downstream analysis * [engine] Less code, more methods * [offchain] More leniency in handling slash bytes and delete from pending * [validator] Remove errors on bad input for edit
5 years ago
return json.Marshal(t{v.Policy().String(), len(members), members})
}
func (v *uniformVoteWeight) AmIMemberOfCommitee() bool {
pubKeyFunc := v.MyPublicKey()
if pubKeyFunc == nil {
return false
}
identity, _ := pubKeyFunc()
everyone := v.DumpParticipants()
for _, key := range identity.PublicKey {
myVoterID := key.SerializeToHexStr()
for i := range everyone {
if everyone[i] == myVoterID {
return true
}
}
}
return false
}
func (v *uniformVoteWeight) ResetPrepareAndCommitVotes() {
v.reset([]Phase{Prepare, Commit})
}
func (v *uniformVoteWeight) ResetViewChangeVotes() {
v.reset([]Phase{ViewChange})
}