Documentation ¶
Index ¶
- Constants
- Variables
- func AddrKeyWithPrefix(addr address.Address, prefix byte) []byte
- func BucketIndexFromReceiptLog(log *iotextypes.Log) (uint64, bool)
- func ProtocolAddr() address.Address
- type BucketGet
- type BucketGetByIndex
- type BucketIndices
- type BucketPool
- func (bp *BucketPool) Commit(sr protocol.StateReader) error
- func (bp *BucketPool) Copy(enableSMStorage bool) *BucketPool
- func (bp *BucketPool) Count() uint64
- func (bp *BucketPool) CreditPool(sm protocol.StateManager, amount *big.Int) error
- func (bp *BucketPool) DebitPool(sm protocol.StateManager, amount *big.Int, newBucket bool) error
- func (bp *BucketPool) Sync(sm protocol.StateManager) error
- func (bp *BucketPool) Total() *big.Int
- type BucketSet
- type BuilderConfig
- type Candidate
- func (d *Candidate) AddSelfStake(amount *big.Int) error
- func (d *Candidate) AddVote(amount *big.Int) error
- func (d *Candidate) Clone() *Candidate
- func (d *Candidate) Collision(c *Candidate) error
- func (d *Candidate) Deserialize(buf []byte) error
- func (d *Candidate) Equal(c *Candidate) bool
- func (d *Candidate) Serialize() ([]byte, error)
- func (d *Candidate) SubSelfStake(amount *big.Int) error
- func (d *Candidate) SubVote(amount *big.Int) error
- func (d *Candidate) Validate() error
- type CandidateCenter
- func (m *CandidateCenter) All() CandidateList
- func (m CandidateCenter) Base() *CandidateCenter
- func (m *CandidateCenter) Commit() error
- func (m *CandidateCenter) ContainsName(name string) bool
- func (m *CandidateCenter) ContainsOperator(operator address.Address) bool
- func (m *CandidateCenter) ContainsOwner(owner address.Address) bool
- func (m *CandidateCenter) ContainsSelfStakingBucket(index uint64) bool
- func (m *CandidateCenter) Delta() CandidateList
- func (m *CandidateCenter) GetByName(name string) *Candidate
- func (m *CandidateCenter) GetByOwner(owner address.Address) *Candidate
- func (m *CandidateCenter) GetBySelfStakingIndex(index uint64) *Candidate
- func (m *CandidateCenter) LegacyCommit() error
- func (m *CandidateCenter) SetDelta(l CandidateList) error
- func (m *CandidateCenter) Size() int
- func (m *CandidateCenter) Sync(sm protocol.StateManager) error
- func (m *CandidateCenter) Upsert(d *Candidate) error
- type CandidateGet
- type CandidateList
- type CandidateSet
- type CandidateStateManager
- type CandidateStateReader
- type CandidatesBucketsIndexer
- func (cbi *CandidatesBucketsIndexer) GetBuckets(height uint64, offset, limit uint32) ([]byte, uint64, error)
- func (cbi *CandidatesBucketsIndexer) GetCandidates(height uint64, offset, limit uint32) ([]byte, uint64, error)
- func (cbi *CandidatesBucketsIndexer) PutBuckets(height uint64, buckets *iotextypes.VoteBucketList) error
- func (cbi *CandidatesBucketsIndexer) PutCandidates(height uint64, candidates *iotextypes.CandidateListV2) error
- func (cbi *CandidatesBucketsIndexer) Start(ctx context.Context) error
- func (cbi *CandidatesBucketsIndexer) Stop(ctx context.Context) error
- type Configuration
- type DepositGas
- type PatchStore
- type Protocol
- func (p *Protocol) ActiveCandidates(ctx context.Context, sr protocol.StateReader, height uint64) (state.CandidateList, error)
- func (p *Protocol) Commit(ctx context.Context, sm protocol.StateManager) error
- func (p *Protocol) CreateGenesisStates(ctx context.Context, sm protocol.StateManager) error
- func (p *Protocol) CreatePreStates(ctx context.Context, sm protocol.StateManager) error
- func (p *Protocol) ForceRegister(r *protocol.Registry) error
- func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error)
- func (p *Protocol) Name() string
- func (p *Protocol) PreCommit(ctx context.Context, sm protocol.StateManager) error
- func (p *Protocol) ReadState(ctx context.Context, sr protocol.StateReader, method []byte, args ...[]byte) ([]byte, uint64, error)
- func (p *Protocol) Register(r *protocol.Registry) error
- func (p *Protocol) Start(ctx context.Context, sr protocol.StateReader) (interface{}, error)
- func (p *Protocol) Validate(ctx context.Context, act action.Action, sr protocol.StateReader) error
- type ReadState
- type ReceiptError
- type RegistrationConsts
- type ViewData
- type VoteBucket
- type VoteReviser
Constants ¶
const ( // StakingCandidatesNamespace is a namespace to store candidates with epoch start height StakingCandidatesNamespace = "stakingCandidates" // StakingBucketsNamespace is a namespace to store vote buckets with epoch start height StakingBucketsNamespace = "stakingBuckets" // StakingMetaNamespace is a namespace to store metadata StakingMetaNamespace = "stakingMeta" )
const ( HandleCreateStake = "createStake" HandleUnstake = "unstake" HandleWithdrawStake = "withdrawStake" HandleChangeCandidate = "changeCandidate" HandleTransferStake = "transferStake" HandleDepositToStake = "depositToStake" HandleRestake = "restake" HandleCandidateRegister = "candidateRegister" HandleCandidateUpdate = "candidateUpdate" )
constants
const (
// CandsMapNS is the bucket name to store candidate map
CandsMapNS = "CandsMap"
)
Variables ¶
var ( ErrWithdrawnBucket = errors.New("the bucket is already withdrawn") TotalBucketKey = append([]byte{_const}, []byte("totalBucket")...) )
Errors
var ( ErrInvalidAmount = errors.New("invalid staking amount") ErrInvalidCanName = errors.New("invalid candidate name") ErrInvalidOwner = errors.New("invalid owner address") ErrInvalidOperator = errors.New("invalid operator address") ErrInvalidReward = errors.New("invalid reward address") ErrInvalidSelfStkIndex = errors.New("invalid self-staking bucket index") ErrMissingField = errors.New("missing data field") ErrTypeAssertion = errors.New("failed type assertion") )
Errors
var (
ErrNilParameters = errors.New("parameter is nil")
)
Errors and vars
Functions ¶
func AddrKeyWithPrefix ¶ added in v1.8.1
AddrKeyWithPrefix returns address key with prefix
func BucketIndexFromReceiptLog ¶ added in v1.0.0
func BucketIndexFromReceiptLog(log *iotextypes.Log) (uint64, bool)
BucketIndexFromReceiptLog extracts bucket index from log
func ProtocolAddr ¶ added in v1.7.1
ProtocolAddr returns the address generated from protocol id
Types ¶
type BucketGet ¶ added in v1.8.0
type BucketGet interface { BucketGetByIndex // contains filtered or unexported methods }
BucketGet related to obtaining bucket
type BucketGetByIndex ¶ added in v1.8.0
type BucketGetByIndex interface {
// contains filtered or unexported methods
}
BucketGetByIndex related to obtaining bucket by index
type BucketIndices ¶
type BucketIndices []uint64
BucketIndices defines the array of bucket index for a
func (*BucketIndices) Deserialize ¶
func (bis *BucketIndices) Deserialize(data []byte) error
Deserialize deserializes bytes into bucket indices
func (*BucketIndices) LoadProto ¶
func (bis *BucketIndices) LoadProto(bucketIndicesPb *stakingpb.BucketIndices) error
LoadProto converts protobuf to bucket indices
func (*BucketIndices) Proto ¶
func (bis *BucketIndices) Proto() *stakingpb.BucketIndices
Proto converts bucket indices to protobuf
func (*BucketIndices) Serialize ¶
func (bis *BucketIndices) Serialize() ([]byte, error)
Serialize serializes bucket indices into bytes
type BucketPool ¶ added in v1.1.0
type BucketPool struct {
// contains filtered or unexported fields
}
BucketPool implements the bucket pool
func (*BucketPool) Commit ¶ added in v1.1.0
func (bp *BucketPool) Commit(sr protocol.StateReader) error
Commit is called upon workingset commit
func (*BucketPool) Copy ¶ added in v1.1.0
func (bp *BucketPool) Copy(enableSMStorage bool) *BucketPool
Copy returns a copy of the bucket pool
func (*BucketPool) Count ¶ added in v1.1.0
func (bp *BucketPool) Count() uint64
Count returns the total number of buckets in bucket pool
func (*BucketPool) CreditPool ¶ added in v1.1.0
func (bp *BucketPool) CreditPool(sm protocol.StateManager, amount *big.Int) error
CreditPool subtracts staked amount out of the pool
func (*BucketPool) DebitPool ¶ added in v1.1.0
func (bp *BucketPool) DebitPool(sm protocol.StateManager, amount *big.Int, newBucket bool) error
DebitPool adds staked amount into the pool
func (*BucketPool) Sync ¶ added in v1.1.0
func (bp *BucketPool) Sync(sm protocol.StateManager) error
Sync syncs the data from state manager
func (*BucketPool) Total ¶ added in v1.1.0
func (bp *BucketPool) Total() *big.Int
Total returns the total amount staked in bucket pool
type BucketSet ¶ added in v1.8.0
type BucketSet interface {
// contains filtered or unexported methods
}
BucketSet related to setting bucket
type BuilderConfig ¶ added in v1.8.4
type BuilderConfig struct { Staking genesis.Staking PersistStakingPatchBlock uint64 StakingPatchDir string }
BuilderConfig returns the configuration of the builder
type Candidate ¶
type Candidate struct { Owner address.Address Operator address.Address Reward address.Address Name string Votes *big.Int SelfStakeBucketIdx uint64 SelfStake *big.Int }
Candidate represents the candidate
func (*Candidate) AddSelfStake ¶
AddSelfStake adds self stake
func (*Candidate) Deserialize ¶
Deserialize deserializes bytes to candidate
func (*Candidate) SubSelfStake ¶
SubSelfStake subtracts self stake
type CandidateCenter ¶
type CandidateCenter struct {
// contains filtered or unexported fields
}
CandidateCenter is a struct to manage the candidates
func NewCandidateCenter ¶
func NewCandidateCenter(all CandidateList) (*CandidateCenter, error)
NewCandidateCenter creates an instance of CandidateCenter
func (*CandidateCenter) All ¶
func (m *CandidateCenter) All() CandidateList
All returns all candidates in candidate center
func (CandidateCenter) Base ¶ added in v1.0.0
func (m CandidateCenter) Base() *CandidateCenter
Base returns the confirmed base state
func (*CandidateCenter) Commit ¶ added in v1.0.0
func (m *CandidateCenter) Commit() error
Commit writes the change into base
func (*CandidateCenter) ContainsName ¶
func (m *CandidateCenter) ContainsName(name string) bool
ContainsName returns true if the map contains the candidate by name
func (*CandidateCenter) ContainsOperator ¶
func (m *CandidateCenter) ContainsOperator(operator address.Address) bool
ContainsOperator returns true if the map contains the candidate by operator
func (*CandidateCenter) ContainsOwner ¶
func (m *CandidateCenter) ContainsOwner(owner address.Address) bool
ContainsOwner returns true if the map contains the candidate by owner
func (*CandidateCenter) ContainsSelfStakingBucket ¶
func (m *CandidateCenter) ContainsSelfStakingBucket(index uint64) bool
ContainsSelfStakingBucket returns true if the map contains the self staking bucket index
func (*CandidateCenter) Delta ¶ added in v1.0.0
func (m *CandidateCenter) Delta() CandidateList
Delta exports the pending changes
func (*CandidateCenter) GetByName ¶
func (m *CandidateCenter) GetByName(name string) *Candidate
GetByName returns the candidate by name
func (*CandidateCenter) GetByOwner ¶
func (m *CandidateCenter) GetByOwner(owner address.Address) *Candidate
GetByOwner returns the candidate by owner
func (*CandidateCenter) GetBySelfStakingIndex ¶
func (m *CandidateCenter) GetBySelfStakingIndex(index uint64) *Candidate
GetBySelfStakingIndex returns the candidate by self-staking index
func (*CandidateCenter) LegacyCommit ¶ added in v1.9.0
func (m *CandidateCenter) LegacyCommit() error
LegacyCommit writes the change into base with legacy logic
func (*CandidateCenter) SetDelta ¶ added in v1.0.0
func (m *CandidateCenter) SetDelta(l CandidateList) error
SetDelta sets the delta
func (*CandidateCenter) Size ¶
func (m *CandidateCenter) Size() int
Size returns number of candidates
func (*CandidateCenter) Sync ¶ added in v1.1.0
func (m *CandidateCenter) Sync(sm protocol.StateManager) error
Sync syncs the data from state manager
func (*CandidateCenter) Upsert ¶
func (m *CandidateCenter) Upsert(d *Candidate) error
Upsert adds a candidate into map, overwrites if already exist
type CandidateGet ¶ added in v1.8.0
type CandidateGet interface {
// contains filtered or unexported methods
}
CandidateGet related to obtaining Candidate
type CandidateList ¶
type CandidateList []*Candidate
CandidateList is a list of candidates which is sortable
func (*CandidateList) Deserialize ¶
func (l *CandidateList) Deserialize(buf []byte) error
Deserialize deserializes bytes to list of candidates
func (CandidateList) Len ¶
func (l CandidateList) Len() int
func (CandidateList) Less ¶
func (l CandidateList) Less(i, j int) bool
func (CandidateList) Serialize ¶ added in v1.0.0
func (l CandidateList) Serialize() ([]byte, error)
Serialize serializes candidate to bytes
func (CandidateList) Swap ¶
func (l CandidateList) Swap(i, j int)
type CandidateSet ¶ added in v1.8.0
type CandidateSet interface {
// contains filtered or unexported methods
}
CandidateSet related to setting candidates
type CandidateStateManager ¶ added in v1.0.0
type CandidateStateManager interface { BucketSet BucketGetByIndex CandidateSet // candidate and bucket pool related DirtyView() *ViewData ContainsName(string) bool ContainsOwner(address.Address) bool ContainsOperator(address.Address) bool ContainsSelfStakingBucket(uint64) bool GetByName(string) *Candidate GetByOwner(address.Address) *Candidate Upsert(*Candidate) error CreditBucketPool(*big.Int) error DebitBucketPool(*big.Int, bool) error Commit(context.Context) error SM() protocol.StateManager }
CandidateStateManager is candidate state manager on top of StateManager
func NewCandidateStateManager ¶ added in v1.0.0
func NewCandidateStateManager(sm protocol.StateManager, enableSMStorage bool) (CandidateStateManager, error)
NewCandidateStateManager returns a new CandidateStateManager instance
type CandidateStateReader ¶ added in v1.1.0
type CandidateStateReader interface { BucketGet CandidateGet ReadState Height() uint64 SR() protocol.StateReader BaseView() *ViewData NewBucketPool(enableSMStorage bool) (*BucketPool, error) GetCandidateByName(string) *Candidate GetCandidateByOwner(address.Address) *Candidate AllCandidates() CandidateList TotalStakedAmount() *big.Int ActiveBucketsCount() uint64 }
CandidateStateReader contains candidate center and bucket pool
func ConstructBaseView ¶ added in v1.1.0
func ConstructBaseView(sr protocol.StateReader) (CandidateStateReader, error)
ConstructBaseView returns a candidate state reader that reflects the base view it will be used read-only
type CandidatesBucketsIndexer ¶ added in v1.1.0
type CandidatesBucketsIndexer struct {
// contains filtered or unexported fields
}
CandidatesBucketsIndexer is an indexer to store candidates by given height
func NewStakingCandidatesBucketsIndexer ¶ added in v1.1.0
func NewStakingCandidatesBucketsIndexer(kv db.KVStoreForRangeIndex) (*CandidatesBucketsIndexer, error)
NewStakingCandidatesBucketsIndexer creates a new StakingCandidatesIndexer
func (*CandidatesBucketsIndexer) GetBuckets ¶ added in v1.1.0
func (cbi *CandidatesBucketsIndexer) GetBuckets(height uint64, offset, limit uint32) ([]byte, uint64, error)
GetBuckets gets vote buckets from indexer given epoch start height
func (*CandidatesBucketsIndexer) GetCandidates ¶ added in v1.1.0
func (cbi *CandidatesBucketsIndexer) GetCandidates(height uint64, offset, limit uint32) ([]byte, uint64, error)
GetCandidates gets candidates from indexer given epoch start height
func (*CandidatesBucketsIndexer) PutBuckets ¶ added in v1.1.0
func (cbi *CandidatesBucketsIndexer) PutBuckets(height uint64, buckets *iotextypes.VoteBucketList) error
PutBuckets puts vote buckets into indexer
func (*CandidatesBucketsIndexer) PutCandidates ¶ added in v1.1.0
func (cbi *CandidatesBucketsIndexer) PutCandidates(height uint64, candidates *iotextypes.CandidateListV2) error
PutCandidates puts candidates into indexer
type Configuration ¶
type Configuration struct { VoteWeightCalConsts genesis.VoteWeightCalConsts RegistrationConsts RegistrationConsts WithdrawWaitingPeriod time.Duration MinStakeAmount *big.Int BootstrapCandidates []genesis.BootstrapCandidate PersistStakingPatchBlock uint64 }
Configuration is the staking protocol configuration.
type DepositGas ¶
type DepositGas func(ctx context.Context, sm protocol.StateManager, amount *big.Int) (*action.TransactionLog, error)
DepositGas deposits gas to some pool
type PatchStore ¶ added in v1.8.4
type PatchStore struct {
// contains filtered or unexported fields
}
PatchStore is the patch store of staking protocol
func NewPatchStore ¶ added in v1.8.4
func NewPatchStore(dir string) *PatchStore
NewPatchStore creates a new staking patch store
func (*PatchStore) Read ¶ added in v1.8.4
func (store *PatchStore) Read(height uint64) (CandidateList, CandidateList, CandidateList, error)
Read reads CandidateList by name and CandidateList by operator of given height
type Protocol ¶
type Protocol struct {
// contains filtered or unexported fields
}
Protocol defines the protocol of handling staking
func FindProtocol ¶ added in v1.8.0
FindProtocol return a registered protocol from registry
func NewProtocol ¶
func NewProtocol( depositGas DepositGas, cfg *BuilderConfig, candBucketsIndexer *CandidatesBucketsIndexer, correctCandsHeight uint64, reviseHeights ...uint64, ) (*Protocol, error)
NewProtocol instantiates the protocol of staking
func (*Protocol) ActiveCandidates ¶
func (p *Protocol) ActiveCandidates(ctx context.Context, sr protocol.StateReader, height uint64) (state.CandidateList, error)
ActiveCandidates returns all active candidates in candidate center
func (*Protocol) CreateGenesisStates ¶
CreateGenesisStates is used to setup BootstrapCandidates from genesis config.
func (*Protocol) CreatePreStates ¶ added in v1.1.0
CreatePreStates updates state manager
func (*Protocol) ForceRegister ¶
ForceRegister registers the protocol with a unique ID and force replacing the previous protocol if it exists
func (*Protocol) Handle ¶
func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error)
Handle handles a staking message
func (*Protocol) ReadState ¶
func (p *Protocol) ReadState(ctx context.Context, sr protocol.StateReader, method []byte, args ...[]byte) ([]byte, uint64, error)
ReadState read the state on blockchain via protocol
type ReadState ¶ added in v1.8.0
type ReadState interface {
// contains filtered or unexported methods
}
ReadState related to read bucket and candidate by request
type ReceiptError ¶ added in v1.0.0
ReceiptError indicates a non-critical error with corresponding receipt status
type RegistrationConsts ¶
RegistrationConsts are the registration fee and min self stake
type ViewData ¶ added in v1.1.0
type ViewData struct {
// contains filtered or unexported fields
}
ViewData is the data that need to be stored in protocol's view
func CreateBaseView ¶ added in v1.1.0
CreateBaseView creates the base view from state reader
type VoteBucket ¶
type VoteBucket struct { Index uint64 Candidate address.Address Owner address.Address StakedAmount *big.Int StakedDuration time.Duration CreateTime time.Time StakeStartTime time.Time UnstakeStartTime time.Time AutoStake bool }
VoteBucket represents a vote
func NewVoteBucket ¶
func NewVoteBucket(cand, owner address.Address, amount *big.Int, duration uint32, ctime time.Time, autoStake bool) *VoteBucket
NewVoteBucket creates a new vote bucket
func (*VoteBucket) Deserialize ¶
func (vb *VoteBucket) Deserialize(buf []byte) error
Deserialize deserializes bytes into bucket
func (*VoteBucket) Serialize ¶
func (vb *VoteBucket) Serialize() ([]byte, error)
Serialize serializes bucket into bytes
type VoteReviser ¶ added in v1.1.0
type VoteReviser struct {
// contains filtered or unexported fields
}
VoteReviser is used to recalculate candidate votes.
func NewVoteReviser ¶ added in v1.1.0
func NewVoteReviser(c genesis.VoteWeightCalConsts, correctCandsHeight uint64, reviseHeights ...uint64) *VoteReviser
NewVoteReviser creates a VoteReviser.
func (*VoteReviser) NeedRevise ¶ added in v1.1.0
func (vr *VoteReviser) NeedRevise(height uint64) bool
NeedRevise returns true if height needs revise
func (*VoteReviser) Revise ¶ added in v1.1.0
func (vr *VoteReviser) Revise(csm CandidateStateManager, height uint64) error
Revise recalculate candidate votes on preset revising height.