Rational -> Rat

This commit is contained in:
rigelrozanski 2018-03-21 18:52:17 +01:00
parent 13835f084f
commit 7da2789535
5 changed files with 43 additions and 84 deletions

View File

@ -147,8 +147,8 @@ type Procedure struct {
MinDeposit int64 // Minimum deposit for a proposal to enter voting period. MinDeposit int64 // Minimum deposit for a proposal to enter voting period.
OptionSet []string // Options available to voters. {Yes, No, NoWithVeto, Abstain} OptionSet []string // Options available to voters. {Yes, No, NoWithVeto, Abstain}
ProposalTypes []string // Types available to submitters. {PlainTextProposal, SoftwareUpgradeProposal} ProposalTypes []string // Types available to submitters. {PlainTextProposal, SoftwareUpgradeProposal}
Threshold rational.Rational // Minimum propotion of Yes votes for proposal to pass. Initial value: 0.5 Threshold rational.Rat // 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 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 MaxDepositPeriod int64 // Maximum period for Atom holders to deposit on a proposal. Initial value: 2 months
GovernancePenalty int64 // Penalty if validator does not vote GovernancePenalty int64 // Penalty if validator does not vote

View File

@ -15,8 +15,8 @@ type Procedure struct {
MinDeposit int64 // Minimum deposit for a proposal to enter voting period. MinDeposit int64 // Minimum deposit for a proposal to enter voting period.
OptionSet []string // Options available to voters. {Yes, No, NoWithVeto, Abstain} OptionSet []string // Options available to voters. {Yes, No, NoWithVeto, Abstain}
ProposalTypes []string // Types available to submitters. {PlainTextProposal, SoftwareUpgradeProposal} ProposalTypes []string // Types available to submitters. {PlainTextProposal, SoftwareUpgradeProposal}
Threshold rational.Rational // Minimum propotion of Yes votes for proposal to pass. Initial value: 0.5 Threshold rational.Rat // 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 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 MaxDepositPeriod int64 // Maximum period for Atom holders to deposit on a proposal. Initial value: 2 months
GovernancePenalty int64 // Penalty if validator does not vote GovernancePenalty int64 // Penalty if validator does not vote

View File

@ -43,11 +43,11 @@ type Params struct {
HoldBonded Address // account where all bonded coins are held HoldBonded Address // account where all bonded coins are held
HoldUnbonded Address // account where all delegated but unbonded coins are held HoldUnbonded Address // account where all delegated but unbonded coins are held
InflationRateChange rational.Rational // maximum annual change in inflation rate InflationRateChange rational.Rat // maximum annual change in inflation rate
InflationMax rational.Rational // maximum inflation rate InflationMax rational.Rat // maximum inflation rate
InflationMin rational.Rational // minimum inflation rate InflationMin rational.Rat // minimum inflation rate
GoalBonded rational.Rational // Goal of percent bonded atoms GoalBonded rational.Rat // Goal of percent bonded atoms
ReserveTax rational.Rational // Tax collected on all fees ReserveTax rational.Rat // Tax collected on all fees
MaxValidators uint16 // maximum number of validators MaxValidators uint16 // maximum number of validators
BondDenom string // bondable coin denomination BondDenom string // bondable coin denomination

View File

@ -23,31 +23,28 @@ type Rat struct {
*big.Rat `json:"rat"` *big.Rat `json:"rat"`
} }
type Rational = Rat // RatInterface - big Rat with additional functionality
// RationalInterface - big Rat with additional functionality
// NOTE: we only have one implementation of this interface // NOTE: we only have one implementation of this interface
// and don't use it anywhere, but it might come in handy // 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. // the units of the value in the type system.
type RationalInterface interface { //type RatInterface interface {
GetRat() *big.Rat //GetRat() *big.Rat
Num() int64 //Num() int64
Denom() int64 //Denom() int64
GT(Rational) bool //GT(Rat) bool
LT(Rational) bool //LT(Rat) bool
Equal(Rational) bool //Equal(Rat) bool
IsZero() bool //IsZero() bool
Inv() Rational //Inv() Rat
Mul(Rational) Rational //Mul(Rat) Rat
Quo(Rational) Rational //Quo(Rat) Rat
Add(Rational) Rational //Add(Rat) Rat
Sub(Rational) Rational //Sub(Rat) Rat
Round(int64) Rational //Round(int64) Rat
Evaluate() int64 //Evaluate() int64
} //}
//var _ Rat = Rat{} // enforce at compile time
var _ Rational = Rat{} // enforce at compile time
// nolint - common values // nolint - common values
var ( var (
@ -115,14 +112,14 @@ func (r Rat) GetRat() *big.Rat { return r.Rat }
func (r Rat) Num() int64 { return r.Rat.Num().Int64() } // Num - return the numerator 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) 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) 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) Equal(r2 Rat) 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) GT(r2 Rat) 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) LT(r2 Rat) 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) Inv() Rat { 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) Mul(r2 Rat) Rat { 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) Quo(r2 Rat) Rat { 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) Add(r2 Rat) Rat { 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) Sub(r2 Rat) Rat { return Rat{new(big.Rat).Sub(r.Rat, r2.GetRat())} } // Sub - subtraction
var zero = big.NewInt(0) var zero = big.NewInt(0)
var one = big.NewInt(1) var one = big.NewInt(1)
@ -167,51 +164,13 @@ func (r Rat) Evaluate() int64 {
} }
// Round - round Rat with the provided precisionFactor // 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))} rTen := Rat{new(big.Rat).Mul(r.Rat, big.NewRat(precisionFactor, 1))}
return Rat{big.NewRat(rTen.Evaluate(), precisionFactor)} 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 var ratCdc JSONCodec // TODO wire.Codec
// Hack to just use json.Marshal for everything until // Hack to just use json.Marshal for everything until

View File

@ -28,7 +28,7 @@ func GetCandidateKey(addr sdk.Address) []byte {
} }
// GetValidatorKey - get the key for the validator used in the power-store // 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? 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 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)) //b := store.Get(GetValidatorKey(address, votingPower))
//if b == nil { //if b == nil {
//return nil //return nil