dpos

package
v0.0.0-...-7b0d77e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2024 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RegisterValidatorGas        uint64 = 80000
	SetCommissionGas            uint64 = 20000
	DelegateGas                 uint64 = 40000
	UndelegateGas               uint64 = 60000
	ConfirmUndelegateGas        uint64 = 20000
	CancelUndelegateGas         uint64 = 60000
	ReDelegateGas               uint64 = 80000
	ClaimRewardsGas             uint64 = 40000
	ClaimCommissionRewardsGas   uint64 = 20000
	SetValidatorInfoGas         uint64 = 20000
	DposGetMethodsGas           uint64 = 5000
	DposBatchGetMethodsGas      uint64 = 5000
	DefaultDposMethodGas        uint64 = 20000
	TransferIntoDPoSContractGas uint64 = 1000
)

Gas constants - gas is determined based on storage writes. Each 32Bytes == 20k gas

View Source
const (
	// Max num of characters in url
	MaxEndpointLength = 50

	// Max num of characters in description
	MaxDescriptionLength = 100

	// Maximal commission  [%] * 100 so 1% is 100 & 100% is 10000
	MaxCommission = uint64(10000)

	// Length of vrf public key
	VrfKeyLength = 32

	// Maximum number of validators per batch that delegator get claim rewards from
	ClaimAllRewardsMaxCount = 10

	// Maximum number of validators per batch returned by getValidators call
	GetValidatorsMaxCount = 20

	// Maximum number of delegations per batch returned by getDelegations call
	GetDelegationsMaxCount = 20

	// Maximum number of undelegations per batch returned by getUndelegations call
	GetUndelegationsMaxCount = 20
)

Variables

View Source
var (
	ErrInsufficientBalance          = util.ErrorString("Insufficient balance")
	ErrNonExistentValidator         = util.ErrorString("Validator does not exist")
	ErrNonExistentDelegation        = util.ErrorString("Delegation does not exist")
	ErrExistentDelegation           = util.ErrorString("Delegation already exist")
	ErrExistentUndelegation         = util.ErrorString("Undelegation already exist")
	ErrNonExistentUndelegation      = util.ErrorString("Undelegation does not exist")
	ErrLockedUndelegation           = util.ErrorString("Undelegation is not yet ready to be withdrawn")
	ErrExistentValidator            = util.ErrorString("Validator already exist")
	ErrSameValidator                = util.ErrorString("From and to validators are the same")
	ErrInvalidRedelegation          = util.ErrorString("Redelegation has to be more than 0")
	ErrBrokenState                  = util.ErrorString("Fatal error state is broken")
	ErrValidatorsMaxStakeExceeded   = util.ErrorString("Validator's max stake exceeded")
	ErrInsufficientDelegation       = util.ErrorString("Insufficient delegation")
	ErrCallIsNotToplevel            = util.ErrorString("only top-level calls are allowed")
	ErrWrongProof                   = util.ErrorString("Wrong proof, validator address could not be recovered")
	ErrWrongOwnerAcc                = util.ErrorString("This account is not owner of specified validator")
	ErrWrongVrfKey                  = util.ErrorString("Wrong vrf key specified in validator arguments")
	ErrForbiddenCommissionChange    = util.ErrorString("Forbidden commission change")
	ErrCommissionOverflow           = util.ErrorString("Commission is bigger than maximum value")
	ErrMaxEndpointLengthExceeded    = util.ErrorString("Max endpoint length exceeded")
	ErrMaxDescriptionLengthExceeded = util.ErrorString("Max description length exceeded")
)

Contract methods error return values

View Source
var TransferIntoDPoSContractMethod []byte = common.Hex2Bytes("44df8e70")
View Source
var YieldFractionDecimalPrecision = uint256.NewInt(1e+6)

Yield is calculated with 6 decimal precision

Functions

func BlockToBytes

func BlockToBytes(number types.BlockNum) []byte

Returns block number as bytes

func ContractAddress

func ContractAddress() *common.Address

func Get

func Get[T Field](v *Validators, validator_address *common.Address) (ret *T)

func Max

func Max(x, y uint64) uint64

func Modify

func Modify[T Field](v *Validators, address *common.Address, data *T)

func Save

func Save[T Field](v *Validators, address *common.Address, data *T)

Types

type API

type API struct {
	// contains filtered or unexported fields
}

func (*API) GetConfigByBlockNum

func (api *API) GetConfigByBlockNum(blk_n uint64) chain_config.ChainConfig

func (*API) Init

func (api *API) Init(cfg chain_config.ChainConfig) *API

func (*API) InitAndRegisterAllContracts

func (api *API) InitAndRegisterAllContracts(storage contract_storage.Storage, blk_n types.BlockNum, storage_factory func(types.BlockNum) contract_storage.StorageReader, evm *vm.EVM, registry func(*common.Address, vm.PrecompiledContract))

func (*API) NewContract

func (api *API) NewContract(storage contract_storage.Storage, reader Reader, evm *vm.EVM) *Contract

func (*API) NewDelayedReader

func (api *API) NewDelayedReader(blk_n types.BlockNum, storage_factory func(types.BlockNum) contract_storage.StorageReader) (ret Reader)

func (*API) NewReader

func (api *API) NewReader(blk_n types.BlockNum, storage_factory func(types.BlockNum) contract_storage.StorageReader) (ret Reader)

func (*API) NewSlashingContract

func (api *API) NewSlashingContract(storage contract_storage.Storage, reader slashing.Reader, evm *vm.EVM) *slashing.Contract

func (*API) NewSlashingReader

func (api *API) NewSlashingReader(blk_n types.BlockNum, storage_factory func(types.BlockNum) contract_storage.StorageReader) (ret slashing.Reader)

func (*API) UpdateConfig

func (api *API) UpdateConfig(blk_n types.BlockNum, cfg chain_config.ChainConfig)

type Contract

type Contract struct {

	// ABI of the contract
	Abi abi.ABI
	// contains filtered or unexported fields
}

Main contract class

func (*Contract) ApplyGenesis

func (self *Contract) ApplyGenesis(get_account func(*common.Address) vm.StateAccount) error

Fills contract based on genesis values

func (*Contract) CommitCall

func (self *Contract) CommitCall(readStorage Reader)

Should be called on each block commit - updates delayedStorage

func (*Contract) DistributeRewards

func (self *Contract) DistributeRewards(rewardsStats *rewards_stats.RewardsStats) *uint256.Int

func (*Contract) EndBlockCall

func (self *Contract) EndBlockCall(block_num uint64)

Should be called from EndBlock on each block

func (*Contract) GetOldClaimAllRewardsABI

func (self *Contract) GetOldClaimAllRewardsABI(input []byte, blockNum types.BlockNum) *abi.Method

GetOldClaimAllRewardsABI returns the *old* ABI method for claiming all rewards in the DPOS contract. It should be there, so we don't have a different result during the syncing. And it is hardcoded because we don't need it in the actual interface. If the block number is part of the Aspen hardfork, it returns nil. If the input matches the specified hex value, it returns the ABI method for claiming all rewards.

func (*Contract) Init

func (self *Contract) Init(cfg chain_config.ChainConfig, storage storage.Storage, readStorage Reader, evm *vm.EVM) *Contract

Initialize contract class

func (*Contract) IsFicusHardfork

func (self *Contract) IsFicusHardfork(block types.BlockNum) bool

func (*Contract) IsTransferIntoDPoSContract

func (self *Contract) IsTransferIntoDPoSContract(input []byte, blockNum types.BlockNum) bool

func (*Contract) Register

func (self *Contract) Register(registry func(*common.Address, vm.PrecompiledContract))

Register this precompiled contract

func (*Contract) RequiredGas

func (self *Contract) RequiredGas(ctx vm.CallFrame, evm *vm.EVM) uint64

Calculate required gas for call to this contract

func (*Contract) Run

func (self *Contract) Run(ctx vm.CallFrame, evm *vm.EVM) ([]byte, error)

This is called on each call to contract It translates call and tries to execute them

func (*Contract) UpdateConfig

func (self *Contract) UpdateConfig(cfg chain_config.ChainConfig)

Updates config - for HF

func (*Contract) UpdateStorage

func (self *Contract) UpdateStorage(readStorage Reader)

Updates delayed storage after each commited block

type Delegation

type Delegation struct {
	// Num of delegated tokens == delegator's stake
	Stake *big.Int

	// Block number related to rewards
	LastUpdated types.BlockNum
}

type Delegations

type Delegations struct {
	// contains filtered or unexported fields
}

Delegations type groups together all functionality related to creating/deleting/modifying/etc... delegations as such info is stored under multiple independent storage keys, it is important that caller does not need to think about all implementation details, but just calls functions on Delegations type

func (*Delegations) CreateDelegation

func (self *Delegations) CreateDelegation(delegator_address *common.Address, validator_address *common.Address, block types.BlockNum, stake *big.Int)

func (*Delegations) DelegationExists

func (self *Delegations) DelegationExists(delegator_address *common.Address, validator_address *common.Address) bool

Checks if delegation exists

func (*Delegations) GetAllDelegatorValidatorsAddresses

func (self *Delegations) GetAllDelegatorValidatorsAddresses(delegator_address *common.Address) []common.Address

func (*Delegations) GetDelegation

func (self *Delegations) GetDelegation(delegator_address *common.Address, validator_address *common.Address) (delegation *Delegation)

Gets delegation

func (*Delegations) GetDelegationsCount

func (self *Delegations) GetDelegationsCount(delegator_address *common.Address) uint32

Returns number of delegations for specified address

func (*Delegations) GetDelegatorValidatorsAddresses

func (self *Delegations) GetDelegatorValidatorsAddresses(delegator_address *common.Address, batch uint32, count uint32) ([]common.Address, bool)

func (*Delegations) Init

func (self *Delegations) Init(stor *contract_storage.StorageWrapper, prefix []byte)

func (*Delegations) ModifyDelegation

func (self *Delegations) ModifyDelegation(delegator_address *common.Address, validator_address *common.Address, delegation *Delegation)

func (*Delegations) RemoveDelegation

func (self *Delegations) RemoveDelegation(delegator_address *common.Address, validator_address *common.Address)

type DposConfigWithBlock

type DposConfigWithBlock struct {
	DposConfig chain_config.DPOSConfig
	Blk_n      types.BlockNum
}

type Field

type Field interface {
	ValidatorV1 | Validator | ValidatorInfo | ValidatorRewards
}

type GenesisTransfer

type GenesisTransfer = struct {
	Beneficiary common.Address
	Value       *big.Int
}

type Logs

type Logs struct {
	Events map[string]abi.Event
}

func (*Logs) Init

func (self *Logs) Init(events map[string]abi.Event) *Logs

func (*Logs) MakeCommissionRewardsClaimedLog

func (self *Logs) MakeCommissionRewardsClaimedLog(account, validator *common.Address, amount *big.Int) vm.LogRecord

event CommissionRewardsClaimed(address indexed account, address indexed validator);

func (*Logs) MakeCommissionSetLog

func (self *Logs) MakeCommissionSetLog(account *common.Address, amount uint16) vm.LogRecord

event CommissionSet(address indexed validator, uint16 commission);

func (*Logs) MakeDelegatedLog

func (self *Logs) MakeDelegatedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord

event Delegated(address indexed delegator, address indexed validator, uint256 amount);

func (*Logs) MakeRedelegatedLog

func (self *Logs) MakeRedelegatedLog(delegator, from, to *common.Address, amount *big.Int) vm.LogRecord

event Redelegated(address indexed delegator, address indexed from, address indexed to, uint256 amount);

func (*Logs) MakeRewardsClaimedLog

func (self *Logs) MakeRewardsClaimedLog(account, validator *common.Address, amount *big.Int) vm.LogRecord

event RewardsClaimed(address indexed account, address indexed validator);

func (*Logs) MakeUndelegateCanceledLog

func (self *Logs) MakeUndelegateCanceledLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord

event UndelegateCanceled(address indexed delegator, address indexed validator, uint256 amount);

func (*Logs) MakeUndelegateConfirmedLog

func (self *Logs) MakeUndelegateConfirmedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord

event UndelegateConfirmed(address indexed delegator, address indexed validator, uint256 amount);

func (*Logs) MakeUndelegatedLog

func (self *Logs) MakeUndelegatedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord

event Undelegated(address indexed delegator, address indexed validator, uint256 amount);

func (*Logs) MakeValidatorInfoSetLog

func (self *Logs) MakeValidatorInfoSetLog(account *common.Address) vm.LogRecord

event ValidatorInfoSet(address indexed validator);

func (*Logs) MakeValidatorRegisteredLog

func (self *Logs) MakeValidatorRegisteredLog(account *common.Address) vm.LogRecord

event ValidatorRegistered(address indexed validator);

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

func (Reader) GetEligibleVoteCount

func (r Reader) GetEligibleVoteCount(addr *common.Address) (ret uint64)

func (Reader) GetStakingBalance

func (r Reader) GetStakingBalance(addr *common.Address) (ret *big.Int)

func (Reader) GetTotalSupply

func (r Reader) GetTotalSupply() *big.Int

func (Reader) GetValidatorsTotalStakes

func (r Reader) GetValidatorsTotalStakes() (ret []ValidatorStake)

func (Reader) GetValidatorsVoteCounts

func (r Reader) GetValidatorsVoteCounts() (ret []ValidatorVoteCount)

func (Reader) GetVrfKey

func (r Reader) GetVrfKey(addr *common.Address) (ret []byte)

func (Reader) GetYield

func (r Reader) GetYield() uint64

func (*Reader) Init

func (r *Reader) Init(cfg *chain_config.ChainConfig, blk_n types.BlockNum, storage_factory func(types.BlockNum) storage.StorageReader) *Reader

func (*Reader) InitDelayedReader

func (r *Reader) InitDelayedReader(cfg *chain_config.ChainConfig, blk_n types.BlockNum, storage_factory func(types.BlockNum) storage.StorageReader) *Reader

func (Reader) IsEligible

func (r Reader) IsEligible(address *common.Address) bool

func (Reader) TotalAmountDelegated

func (r Reader) TotalAmountDelegated() (ret *big.Int)

func (Reader) TotalEligibleVoteCount

func (r Reader) TotalEligibleVoteCount() (ret uint64)

type State

type State struct {
	// represents number of rewards per 1 stake
	RewardsPer1Stake *big.Int
	// number of references
	Count uint32
}

State of the rewards distribution algorithm

type Undelegation

type Undelegation struct {
	// Amount of DLY that accound should be able to get
	Amount *big.Int

	// Block number when the withdrawal be ready
	Block types.BlockNum
}

type Undelegations

type Undelegations struct {
	// contains filtered or unexported fields
}

func (*Undelegations) CreateUndelegation

func (self *Undelegations) CreateUndelegation(delegator_address *common.Address, validator_address *common.Address, block types.BlockNum, amount *big.Int)

Creates undelegation object in storage

func (*Undelegations) GetDelegatorValidatorsAddresses

func (self *Undelegations) GetDelegatorValidatorsAddresses(delegator_address *common.Address, batch uint32, count uint32) ([]common.Address, bool)

Returns all addressess of validators, from which is delegator <delegator_address> currently undelegating

func (*Undelegations) GetUndelegation

func (self *Undelegations) GetUndelegation(delegator_address *common.Address, validator_address *common.Address) (undelegation *Undelegation)

Returns undelegation object from queue

func (*Undelegations) GetUndelegationsCount

func (self *Undelegations) GetUndelegationsCount(delegator_address *common.Address) uint32

Returns number of undelegations for specified address

func (*Undelegations) Init

func (self *Undelegations) Init(stor *contract_storage.StorageWrapper, prefix []byte)

func (*Undelegations) RemoveUndelegation

func (self *Undelegations) RemoveUndelegation(delegator_address *common.Address, validator_address *common.Address)

Removes undelegation object from storage

func (*Undelegations) UndelegationExists

func (self *Undelegations) UndelegationExists(delegator_address *common.Address, validator_address *common.Address) bool

Returns true if for given values there is undelegation in queue

type Validator

type Validator struct {
	*ValidatorV1

	// Number of ongoing/unclaimed undelegations from the validator
	UndelegationsCount uint16
}

type ValidatorInfo

type ValidatorInfo struct {
	// Validators description
	Description string

	// Validators website endpoint
	Endpoint string
}

type ValidatorRewards

type ValidatorRewards struct {
	// Rewards accumulated
	RewardsPool *big.Int

	// Rewards accumulated
	CommissionRewardsPool *big.Int
}

func (*ValidatorRewards) Empty

func (self *ValidatorRewards) Empty() bool

type ValidatorStake

type ValidatorStake struct {
	Address    common.Address
	TotalStake *big.Int
}

type ValidatorV1

type ValidatorV1 struct {
	// TotalStake == sum of all delegated tokens to the validator
	TotalStake *big.Int

	// Commission
	Commission uint16

	// Block number related to commission
	LastCommissionChange types.BlockNum

	// Block number pointing to latest state
	LastUpdated types.BlockNum
}

Pre-hardfork validator struct without UndelegationsCount member

type ValidatorVoteCount

type ValidatorVoteCount struct {
	Address   common.Address
	VoteCount uint64
}

type Validators

type Validators struct {
	// contains filtered or unexported fields
}

Validators type groups together all functionality related to creating/deleting/modifying/etc... validators as such info is stored under multiple independent storage keys, it is important that caller does not need to think about all implementation details, but just calls functions on Validators type

func (*Validators) AddValidatorRewards

func (self *Validators) AddValidatorRewards(validator_address *common.Address, commission_reward, reward *big.Int)

func (*Validators) CheckValidatorOwner

func (self *Validators) CheckValidatorOwner(owner, validator *common.Address) bool

Checks if correct account is trying to access validator object

func (*Validators) CreateValidator

func (self *Validators) CreateValidator(extended_validator bool, owner_address *common.Address, validator_address *common.Address, vrf_key []byte, block types.BlockNum, commission uint16, description string, endpoint string) (validator *Validator)

func (*Validators) DeleteValidator

func (self *Validators) DeleteValidator(validator_address *common.Address)

func (*Validators) GetValidator

func (self *Validators) GetValidator(validator_address *common.Address) (validator *Validator)

func (*Validators) GetValidatorInfo

func (self *Validators) GetValidatorInfo(validator_address *common.Address) (validator_info *ValidatorInfo)

func (*Validators) GetValidatorOwner

func (self *Validators) GetValidatorOwner(validator *common.Address) (ret common.Address)

Checks if correct account is trying to access validator object

func (*Validators) GetValidatorRewards

func (self *Validators) GetValidatorRewards(validator_address *common.Address) (rewards *ValidatorRewards)

func (*Validators) GetValidatorsAddresses

func (self *Validators) GetValidatorsAddresses(batch uint32, count uint32) ([]common.Address, bool)

func (*Validators) GetValidatorsCount

func (self *Validators) GetValidatorsCount() uint32

func (*Validators) GetVrfKey

func (self *Validators) GetVrfKey(validator *common.Address) (ret []byte)

Returns public vrf key for validator

func (*Validators) Init

func (self *Validators) Init(stor *contract_storage.StorageWrapper, prefix []byte) *Validators

func (*Validators) ModifyValidator

func (self *Validators) ModifyValidator(extended_validator bool, validator_address *common.Address, validator *Validator)

func (*Validators) ModifyValidatorInfo

func (self *Validators) ModifyValidatorInfo(validator_address *common.Address, validator_info *ValidatorInfo)

func (*Validators) ModifyValidatorRewards

func (self *Validators) ModifyValidatorRewards(validator_address *common.Address, rewards *ValidatorRewards)

func (*Validators) ValidatorExists

func (self *Validators) ValidatorExists(validator_address *common.Address) bool

Checks is validator exists

type YieldCurve

type YieldCurve struct {
	// contains filtered or unexported fields
}

func (*YieldCurve) CalculateBlockReward

func (self *YieldCurve) CalculateBlockReward(current_total_delegation, current_total_dly_supply *uint256.Int) (block_reward *uint256.Int, yield *uint256.Int)

func (*YieldCurve) CalculateTotalSupply

func (self *YieldCurve) CalculateTotalSupply(minted_tokens *uint256.Int) *uint256.Int

Calculates total supply based on minted_toknes + genesis balances + total generated rewards until Aspen hardfork

func (*YieldCurve) Init

func (self *YieldCurve) Init(cfg chain_config.ChainConfig)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL