Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
abukosek committed Sep 14, 2023
1 parent cd4f98d commit f313cbd
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go/consensus/cometbft/api/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func convertValidators(d *genesis.Document) ([]cmttypes.GenesisValidator, error)
// have an account in the ledger at all.
stake = &quantity.Quantity{}
}
power, err = scheduler.VotingPowerFromStake(stake)
power, err = scheduler.VotingPowerFromStake(stake, d.Scheduler.Parameters.VotingPowerDistribution)
if err != nil {
return nil, fmt.Errorf("cometbft: computing voting power for entity %s with account %s and stake %v: %w",
openedNode.EntityID,
Expand Down
4 changes: 2 additions & 2 deletions go/consensus/cometbft/apps/scheduler/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (app *schedulerApplication) InitChain(ctx *abciAPI.Context, req types.Reque
err,
)
}
expectedPower, err = scheduler.VotingPowerFromStake(&account.Escrow.Active.Balance)
expectedPower, err = scheduler.VotingPowerFromStake(&account.Escrow.Active.Balance, doc.Scheduler.Parameters.VotingPowerDistribution)
if err != nil {
ctx.Logger().Error("computing voting power from stake failed",
"err", err,
Expand Down Expand Up @@ -162,7 +162,7 @@ func (app *schedulerApplication) InitChain(ctx *abciAPI.Context, req types.Reque
}

if !doc.Scheduler.Parameters.DebugBypassStake {
supplyPower, err := scheduler.VotingPowerFromStake(&doc.Staking.TotalSupply)
supplyPower, err := scheduler.VotingPowerFromStake(&doc.Staking.TotalSupply, doc.Scheduler.Parameters.VotingPowerDistribution)
if err != nil {
return fmt.Errorf("init chain: total supply would break voting power computation: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go/consensus/cometbft/apps/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ electLoop:
if err != nil {
return nil, fmt.Errorf("failed to fetch escrow balance for account %s: %w", entAddr, err)
}
power, err = scheduler.VotingPowerFromStake(stake)
power, err = scheduler.VotingPowerFromStake(stake, params.VotingPowerDistribution)
if err != nil {
return nil, fmt.Errorf("computing voting power for account %s with balance %v: %w",
entAddr, stake, err,
Expand Down
2 changes: 1 addition & 1 deletion go/genesis/api/sanity_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (d *Document) SanityCheck() error {
if err := d.KeyManager.SanityCheck(); err != nil {
return err
}
if err := d.Scheduler.SanityCheck(&d.Staking.TotalSupply); err != nil {
if err := d.Scheduler.SanityCheck(&d.Staking.TotalSupply, d.Scheduler.Parameters.VotingPowerDistribution); err != nil {
return err
}
return d.Governance.SanityCheck(epoch, &d.Staking.GovernanceDeposits)
Expand Down
33 changes: 31 additions & 2 deletions go/scheduler/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,25 @@ func (c *Committee) EncodedMembersHash() hash.Hash {
// BaseUnitsPerVotingPower is the ratio of base units staked to validator power.
var BaseUnitsPerVotingPower quantity.Quantity

// VotingPowerDistribution is the voting power distribution type.
type VotingPowerDistribution uint8

const (
// VotingPowerDistributionLinear is the distribution where power is
// linearly proportional to the stake.
VotingPowerDistributionLinear = 0
// VotingPowerDistributionSqrt is the distribution where power is
// proportional to the square root of the stake.
VotingPowerDistributionSqrt = 1
)

// VotingPowerFromStake computes that by dividing by BaseUnitsPerVotingPower.
//
// NOTE: It's not that we're implementation-hiding the conversion though.
// It's just that otherwise if we accidentally skip the `IsInt64`, it would
// still appear to work, and that would be a bad thing to have in a routine
// that's written multiple times.
func VotingPowerFromStake(t *quantity.Quantity) (int64, error) {
func VotingPowerFromStake(t *quantity.Quantity, distribution VotingPowerDistribution) (int64, error) {
powerQ := t.Clone()
if err := powerQ.Quo(&BaseUnitsPerVotingPower); err != nil {
return 0, fmt.Errorf("quo %v / %v: %w", t, &BaseUnitsPerVotingPower, err)
Expand All @@ -226,7 +238,15 @@ func VotingPowerFromStake(t *quantity.Quantity) (int64, error) {
if !powerBI.IsInt64() {
return 0, fmt.Errorf("%v is too many base units to convert to power", powerQ)
}
return powerBI.Int64(), nil

switch distribution {
case VotingPowerDistributionLinear:
return powerBI.Int64(), nil
case VotingPowerDistributionSqrt:
return powerBI.Sqrt(powerBI).Int64(), nil
default:
panic("invalid voting power distribution")
}
}

// Validator is a consensus validator.
Expand Down Expand Up @@ -313,6 +333,9 @@ type ConsensusParameters struct {
// DebugAllowWeakAlpha allows VRF based elections based on proofs
// generated by an alpha value considered weak.
DebugAllowWeakAlpha bool `json:"debug_allow_weak_alpha,omitempty"`

// VotingPowerDistribution is the voting power distribution.
VotingPowerDistribution VotingPowerDistribution `json:"voting_power_distribution,omitempty"`
}

// ConsensusParameterChanges are allowed scheduler consensus parameter changes.
Expand All @@ -322,6 +345,9 @@ type ConsensusParameterChanges struct {

// MaxValidators is the new maximum number of validators.
MaxValidators *int `json:"max_validators"`

// VotingPowerDistribution is the new voting power distribution.
VotingPowerDistribution *VotingPowerDistribution `json:"voting_power_distribution,omitempty"`
}

// Apply applies changes to the given consensus parameters.
Expand All @@ -332,6 +358,9 @@ func (c *ConsensusParameterChanges) Apply(params *ConsensusParameters) error {
if c.MaxValidators != nil {
params.MaxValidators = *c.MaxValidators
}
if c.VotingPowerDistribution != nil {
params.VotingPowerDistribution = *c.VotingPowerDistribution
}
return nil
}

Expand Down
7 changes: 4 additions & 3 deletions go/scheduler/api/sanity_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
)

// SanityCheck does basic sanity checking on the genesis state.
func (g *Genesis) SanityCheck(stakingTotalSupply *quantity.Quantity) error {
func (g *Genesis) SanityCheck(stakingTotalSupply *quantity.Quantity, votingPowerDistribution VotingPowerDistribution) error {
if err := g.Parameters.SanityCheck(); err != nil {
return fmt.Errorf("scheduler: sanity check failed: %w", err)
}

if !g.Parameters.DebugBypassStake {
supplyPower, err := VotingPowerFromStake(stakingTotalSupply)
supplyPower, err := VotingPowerFromStake(stakingTotalSupply, votingPowerDistribution)
if err != nil {
return fmt.Errorf("scheduler: sanity check failed: total supply would break voting power computation: %w", err)
}
Expand All @@ -42,7 +42,8 @@ func (p *ConsensusParameters) SanityCheck() error {
// SanityCheck performs a sanity check on the consensus parameter changes.
func (c *ConsensusParameterChanges) SanityCheck() error {
if c.MinValidators == nil &&
c.MaxValidators == nil {
c.MaxValidators == nil &&
c.VotingPowerDistribution == nil {
return fmt.Errorf("consensus parameter changes should not be empty")
}
return nil
Expand Down

0 comments on commit f313cbd

Please sign in to comment.