cosmos-sdk/x/stake/types.go

155 lines
5.5 KiB
Go
Raw Normal View History

2018-01-25 19:31:07 -08:00
package stake
import (
2018-02-20 07:55:53 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
abci "github.com/tendermint/abci/types"
2018-01-25 19:31:07 -08:00
crypto "github.com/tendermint/go-crypto"
)
// Params defines the high level settings for staking
2018-01-25 19:31:07 -08:00
type Params struct {
2018-03-16 12:47:17 -07:00
InflationRateChange sdk.Rat `json:"inflation_rate_change"` // maximum annual change in inflation rate
InflationMax sdk.Rat `json:"inflation_max"` // maximum inflation rate
InflationMin sdk.Rat `json:"inflation_min"` // minimum inflation rate
GoalBonded sdk.Rat `json:"goal_bonded"` // Goal of percent bonded atoms
2018-01-25 19:31:07 -08:00
2018-03-20 06:56:07 -07:00
MaxValidators uint16 `json:"max_validators"` // maximum number of validators
BondDenom string `json:"bond_denom"` // bondable coin denomination
2018-01-25 19:31:07 -08:00
}
//_________________________________________________________________________
2018-03-22 09:00:45 -07:00
// Pool - dynamic parameters of the current state
type Pool struct {
2018-03-16 12:47:17 -07:00
TotalSupply int64 `json:"total_supply"` // total supply of all tokens
BondedShares sdk.Rat `json:"bonded_shares"` // sum of all shares distributed for the Bonded Pool
UnbondedShares sdk.Rat `json:"unbonded_shares"` // sum of all shares distributed for the Unbonded Pool
BondedPool int64 `json:"bonded_pool"` // reserve of bonded tokens
UnbondedPool int64 `json:"unbonded_pool"` // reserve of unbonded tokens held with candidates
InflationLastTime int64 `json:"inflation_last_time"` // block which the last inflation was processed // TODO make time
Inflation sdk.Rat `json:"inflation"` // current annual inflation rate
2018-01-25 19:31:07 -08:00
}
// GenesisState - all staking state that must be provided at genesis
type GenesisState struct {
Pool Pool `json:"pool"`
Params Params `json:"params"`
}
//_______________________________________________________________________________________________________
2018-01-25 19:31:07 -08:00
// CandidateStatus - status of a validator-candidate
type CandidateStatus byte
const (
// nolint
Bonded CandidateStatus = 0x00
Unbonded CandidateStatus = 0x01
Revoked CandidateStatus = 0x02
)
// Candidate defines the total amount of bond shares and their exchange rate to
// coins. Accumulation of interest is modelled as an in increase in the
// exchange rate, and slashing as a decrease. When coins are delegated to this
// candidate, the candidate is credited with a DelegatorBond whose number of
// bond shares is based on the amount of coins delegated divided by the current
// exchange rate. Voting power can be calculated as total bonds multiplied by
// exchange rate.
type Candidate struct {
Status CandidateStatus `json:"status"` // Bonded status
Address sdk.Address `json:"owner"` // Sender of BondTx - UnbondTx returns here
PubKey crypto.PubKey `json:"pub_key"` // Pubkey of candidate
Assets sdk.Rat `json:"assets"` // total shares of a global hold pools
Liabilities sdk.Rat `json:"liabilities"` // total shares issued to a candidate's delegators
Description Description `json:"description"` // Description terms for the candidate
2018-01-25 19:31:07 -08:00
}
// NewCandidate - initialize a new candidate
2018-03-20 14:21:18 -07:00
func NewCandidate(address sdk.Address, pubKey crypto.PubKey, description Description) Candidate {
return Candidate{
2018-01-25 19:31:07 -08:00
Status: Unbonded,
Address: address,
2018-03-20 06:56:07 -07:00
PubKey: pubKey,
2018-02-23 05:13:55 -08:00
Assets: sdk.ZeroRat,
Liabilities: sdk.ZeroRat,
2018-01-25 19:31:07 -08:00
Description: description,
}
}
2018-03-26 07:48:15 -07:00
// Description - description fields for a candidate
type Description struct {
Moniker string `json:"moniker"`
Identity string `json:"identity"`
Website string `json:"website"`
Details string `json:"details"`
}
func NewDescription(moniker, identity, website, details string) Description {
return Description{
Moniker: moniker,
Identity: identity,
Website: website,
Details: details,
}
}
// get the exchange rate of global pool shares over delegator shares
2018-03-20 14:21:18 -07:00
func (c Candidate) delegatorShareExRate() sdk.Rat {
if c.Liabilities.IsZero() {
2018-02-23 05:13:55 -08:00
return sdk.OneRat
}
return c.Assets.Quo(c.Liabilities)
}
// Validator returns a copy of the Candidate as a Validator.
// Should only be called when the Candidate qualifies as a validator.
2018-03-20 14:21:18 -07:00
func (c Candidate) validator() Validator {
return Validator{
2018-04-03 20:41:08 -07:00
Address: c.Address,
PubKey: c.PubKey,
VotingPower: c.Assets,
}
}
2018-03-20 06:56:07 -07:00
//XXX updateDescription function
//XXX enforce limit to number of description characters
//______________________________________________________________________
// Validator is one of the top Candidates
type Validator struct {
Address sdk.Address `json:"address"`
PubKey sdk.PubKey `json:"PubKey"`
VotingPower sdk.Rat `json:"voting_power"`
}
// ABCIValidator - Get the validator from a bond value
func (v Validator) ABCIValidator() abci.Validator {
pkBytes, err := wire.MarshalBinary(v.PubKey)
if err != nil {
panic(err)
}
return abci.Validator{
PubKey: pkBytes,
Power: v.VotingPower.Evaluate(),
}
}
//_________________________________________________________________________
// Candidates - list of Candidates
2018-03-20 14:21:18 -07:00
type Candidates []Candidate
//_________________________________________________________________________
// DelegatorBond represents the bond with tokens held by an account. It is
// owned by one delegator, and is associated with the voting power of one
// pubKey.
2018-03-20 06:56:07 -07:00
// TODO better way of managing space
2018-01-25 19:31:07 -08:00
type DelegatorBond struct {
2018-03-20 14:21:18 -07:00
DelegatorAddr sdk.Address `json:"delegatoraddr"`
2018-03-20 06:56:07 -07:00
CandidateAddr sdk.Address `json:"candidate_addr"`
Shares sdk.Rat `json:"shares"`
}