From 7da2789535632fcd7508f9f887137dbe6e36d0ab Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 21 Mar 2018 18:52:17 +0100 Subject: [PATCH] Rational -> Rat --- docs/spec/governance/governance.md | 4 +- docs/spec/governance/state.md | 4 +- docs/spec/staking/old/spec.md | 10 +-- types/rational.go | 105 +++++++++-------------------- x/stake/keeper.go | 4 +- 5 files changed, 43 insertions(+), 84 deletions(-) diff --git a/docs/spec/governance/governance.md b/docs/spec/governance/governance.md index 2e40e92dd..9fe9635bd 100644 --- a/docs/spec/governance/governance.md +++ b/docs/spec/governance/governance.md @@ -147,8 +147,8 @@ type Procedure struct { MinDeposit int64 // Minimum deposit for a proposal to enter voting period. OptionSet []string // Options available to voters. {Yes, No, NoWithVeto, Abstain} ProposalTypes []string // Types available to submitters. {PlainTextProposal, SoftwareUpgradeProposal} - Threshold rational.Rational // Minimum propotion of Yes votes for proposal to pass. Initial value: 0.5 - Veto rational.Rational // Minimum value of Veto votes to Total votes ratio for proposal to be vetoed. Initial value: 1/3 + Threshold rational.Rat // Minimum propotion of Yes votes for proposal to pass. Initial value: 0.5 + Veto rational.Rat // Minimum value of Veto votes to Total votes ratio for proposal to be vetoed. Initial value: 1/3 MaxDepositPeriod int64 // Maximum period for Atom holders to deposit on a proposal. Initial value: 2 months GovernancePenalty int64 // Penalty if validator does not vote diff --git a/docs/spec/governance/state.md b/docs/spec/governance/state.md index b6f0d3a61..5457590d0 100644 --- a/docs/spec/governance/state.md +++ b/docs/spec/governance/state.md @@ -15,8 +15,8 @@ type Procedure struct { MinDeposit int64 // Minimum deposit for a proposal to enter voting period. OptionSet []string // Options available to voters. {Yes, No, NoWithVeto, Abstain} ProposalTypes []string // Types available to submitters. {PlainTextProposal, SoftwareUpgradeProposal} - Threshold rational.Rational // Minimum propotion of Yes votes for proposal to pass. Initial value: 0.5 - Veto rational.Rational // Minimum value of Veto votes to Total votes ratio for proposal to be vetoed. Initial value: 1/3 + Threshold rational.Rat // Minimum propotion of Yes votes for proposal to pass. Initial value: 0.5 + Veto rational.Rat // Minimum value of Veto votes to Total votes ratio for proposal to be vetoed. Initial value: 1/3 MaxDepositPeriod int64 // Maximum period for Atom holders to deposit on a proposal. Initial value: 2 months GovernancePenalty int64 // Penalty if validator does not vote diff --git a/docs/spec/staking/old/spec.md b/docs/spec/staking/old/spec.md index dac69aa06..bbec3a94f 100644 --- a/docs/spec/staking/old/spec.md +++ b/docs/spec/staking/old/spec.md @@ -43,11 +43,11 @@ type Params struct { HoldBonded Address // account where all bonded coins are held HoldUnbonded Address // account where all delegated but unbonded coins are held - InflationRateChange rational.Rational // maximum annual change in inflation rate - InflationMax rational.Rational // maximum inflation rate - InflationMin rational.Rational // minimum inflation rate - GoalBonded rational.Rational // Goal of percent bonded atoms - ReserveTax rational.Rational // Tax collected on all fees + InflationRateChange rational.Rat // maximum annual change in inflation rate + InflationMax rational.Rat // maximum inflation rate + InflationMin rational.Rat // minimum inflation rate + GoalBonded rational.Rat // Goal of percent bonded atoms + ReserveTax rational.Rat // Tax collected on all fees MaxValidators uint16 // maximum number of validators BondDenom string // bondable coin denomination diff --git a/types/rational.go b/types/rational.go index 3fe1a3174..b28cba4af 100644 --- a/types/rational.go +++ b/types/rational.go @@ -23,31 +23,28 @@ type Rat struct { *big.Rat `json:"rat"` } -type Rational = Rat - -// RationalInterface - big Rat with additional functionality +// RatInterface - big Rat with additional functionality // NOTE: we only have one implementation of this interface // and don't use it anywhere, but it might come in handy -// if we want to provide Rational types that include +// if we want to provide Rat types that include // the units of the value in the type system. -type RationalInterface interface { - GetRat() *big.Rat - Num() int64 - Denom() int64 - GT(Rational) bool - LT(Rational) bool - Equal(Rational) bool - IsZero() bool - Inv() Rational - Mul(Rational) Rational - Quo(Rational) Rational - Add(Rational) Rational - Sub(Rational) Rational - Round(int64) Rational - Evaluate() int64 -} - -var _ Rational = Rat{} // enforce at compile time +//type RatInterface interface { +//GetRat() *big.Rat +//Num() int64 +//Denom() int64 +//GT(Rat) bool +//LT(Rat) bool +//Equal(Rat) bool +//IsZero() bool +//Inv() Rat +//Mul(Rat) Rat +//Quo(Rat) Rat +//Add(Rat) Rat +//Sub(Rat) Rat +//Round(int64) Rat +//Evaluate() int64 +//} +//var _ Rat = Rat{} // enforce at compile time // nolint - common values var ( @@ -111,18 +108,18 @@ func NewRatFromDecimal(decimalStr string) (f Rat, err Error) { } //nolint -func (r Rat) GetRat() *big.Rat { return r.Rat } // GetRat - get big.Rat -func (r Rat) Num() int64 { return r.Rat.Num().Int64() } // Num - return the numerator -func (r Rat) Denom() int64 { return r.Rat.Denom().Int64() } // Denom - return the denominator -func (r Rat) IsZero() bool { return r.Num() == 0 } // IsZero - Is the Rat equal to zero -func (r Rat) Equal(r2 Rational) bool { return r.Rat.Cmp(r2.GetRat()) == 0 } // Equal - rationals are equal -func (r Rat) GT(r2 Rational) bool { return r.Rat.Cmp(r2.GetRat()) == 1 } // GT - greater than -func (r Rat) LT(r2 Rational) bool { return r.Rat.Cmp(r2.GetRat()) == -1 } // LT - less than -func (r Rat) Inv() Rational { return Rat{new(big.Rat).Inv(r.Rat)} } // Inv - inverse -func (r Rat) Mul(r2 Rational) Rational { return Rat{new(big.Rat).Mul(r.Rat, r2.GetRat())} } // Mul - multiplication -func (r Rat) Quo(r2 Rational) Rational { return Rat{new(big.Rat).Quo(r.Rat, r2.GetRat())} } // Quo - quotient -func (r Rat) Add(r2 Rational) Rational { return Rat{new(big.Rat).Add(r.Rat, r2.GetRat())} } // Add - addition -func (r Rat) Sub(r2 Rational) Rational { return Rat{new(big.Rat).Sub(r.Rat, r2.GetRat())} } // Sub - subtraction +func (r Rat) GetRat() *big.Rat { return r.Rat } // GetRat - get big.Rat +func (r Rat) Num() int64 { return r.Rat.Num().Int64() } // Num - return the numerator +func (r Rat) Denom() int64 { return r.Rat.Denom().Int64() } // Denom - return the denominator +func (r Rat) IsZero() bool { return r.Num() == 0 } // IsZero - Is the Rat equal to zero +func (r Rat) Equal(r2 Rat) bool { return r.Rat.Cmp(r2.GetRat()) == 0 } // Equal - rationals are equal +func (r Rat) GT(r2 Rat) bool { return r.Rat.Cmp(r2.GetRat()) == 1 } // GT - greater than +func (r Rat) LT(r2 Rat) bool { return r.Rat.Cmp(r2.GetRat()) == -1 } // LT - less than +func (r Rat) Inv() Rat { return Rat{new(big.Rat).Inv(r.Rat)} } // Inv - inverse +func (r Rat) Mul(r2 Rat) Rat { return Rat{new(big.Rat).Mul(r.Rat, r2.GetRat())} } // Mul - multiplication +func (r Rat) Quo(r2 Rat) Rat { return Rat{new(big.Rat).Quo(r.Rat, r2.GetRat())} } // Quo - quotient +func (r Rat) Add(r2 Rat) Rat { return Rat{new(big.Rat).Add(r.Rat, r2.GetRat())} } // Add - addition +func (r Rat) Sub(r2 Rat) Rat { return Rat{new(big.Rat).Sub(r.Rat, r2.GetRat())} } // Sub - subtraction var zero = big.NewInt(0) var one = big.NewInt(1) @@ -167,51 +164,13 @@ func (r Rat) Evaluate() int64 { } // Round - round Rat with the provided precisionFactor -func (r Rat) Round(precisionFactor int64) Rational { +func (r Rat) Round(precisionFactor int64) Rat { rTen := Rat{new(big.Rat).Mul(r.Rat, big.NewRat(precisionFactor, 1))} return Rat{big.NewRat(rTen.Evaluate(), precisionFactor)} } //___________________________________________________________________________________ -//var ratCdc = RegisterWire(wire.NewCodec()) -//// add rational codec elements to provided codec -//func RegisterWire(cdc *wire.Codec) *wire.Codec { -//cdc.RegisterInterface((*Rational)(nil), nil) -//cdc.RegisterConcrete(Rat{}, "rat", nil) -//return cdc -//} - -//TODO there has got to be a better way using native MarshalText and UnmarshalText - -// RatMarshal - Marshable Rat Struct -//type RatMarshal struct { -//Numerator int64 `json:"numerator"` -//Denominator int64 `json:"denominator"` -//} - -//// MarshalJSON - custom implementation of JSON Marshal -//func (r Rat) MarshalJSON() ([]byte, error) { -//return ratCdc.MarshalJSON(RatMarshal{r.Num(), r.Denom()}) -//} - -//// UnmarshalJSON - custom implementation of JSON Unmarshal -//func (r *Rat) UnmarshalJSON(data []byte) (err error) { -//defer func() { -//if rcv := recover(); rcv != nil { -//err = fmt.Errorf("Panic during UnmarshalJSON: %v", rcv) -//} -//}() - -//ratMar := new(RatMarshal) -//if err := ratCdc.UnmarshalJSON(data, ratMar); err != nil { -//return err -//} -//r.Rat = big.NewRat(ratMar.Numerator, ratMar.Denominator) - -//return nil -//} - var ratCdc JSONCodec // TODO wire.Codec // Hack to just use json.Marshal for everything until diff --git a/x/stake/keeper.go b/x/stake/keeper.go index a54d006bc..a664edb14 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -28,7 +28,7 @@ func GetCandidateKey(addr sdk.Address) []byte { } // GetValidatorKey - get the key for the validator used in the power-store -func GetValidatorKey(addr sdk.Address, power sdk.Rational, cdc *wire.Codec) []byte { +func GetValidatorKey(addr sdk.Address, power sdk.Rat, cdc *wire.Codec) []byte { b, _ := cdc.MarshalJSON(power) // TODO need to handle error here? return append(ValidatorKeyPrefix, append(b, addr.Bytes()...)...) // TODO does this need prefix if its in its own store } @@ -122,7 +122,7 @@ func (k Keeper) removeCandidate(ctx sdk.Context, candidateAddr sdk.Address) { //___________________________________________________________________________ -//func loadValidator(store sdk.KVStore, address sdk.Address, votingPower sdk.Rational) *Validator { +//func loadValidator(store sdk.KVStore, address sdk.Address, votingPower sdk.Rat) *Validator { //b := store.Get(GetValidatorKey(address, votingPower)) //if b == nil { //return nil