Documentation
¶
Index ¶
- Constants
- Variables
- func AddGenesisValidatorsIntoStakingInfo(ctx *types.Context, genesisValidators []*stake.Validator)
- func AddVoters(ctx *types.Context, validator common.Address)
- func CalcMedian(nums []uint64) uint64
- func CalculateTarget(ctx *types.Context, voters []common.Address) uint64
- func CreateInitVotes(ctx *mevmtypes.Context, xhedgeContractSeq uint64, ...)
- func DeleteProposal(ctx *types.Context)
- func DeleteProposalInfos(ctx *types.Context, voters []common.Address)
- func DeleteVote(ctx *types.Context, validator common.Address)
- func DeleteVoters(ctx *types.Context)
- func DistributeFee(ctx *types.Context, stakingAcc *types.AccountInfo, info *stake.StakingInfo, ...)
- func GetAndClearPosVotes(ctx *mevmtypes.Context, xHedgeContractSeq uint64) map[[32]byte]int64
- func GetPosVotes(ctx *mevmtypes.Context, xhedgeContractSeq uint64) map[[32]byte]*big.Int
- func GetVoters(ctx *types.Context) (voters []common.Address)
- func LoadEpoch(ctx *types.Context, epochNum int64) (epoch stake.Epoch, ok bool)
- func LoadMinGasPrice(ctx *types.Context, isLast bool) uint64
- func LoadProposal(ctx *types.Context) (target uint64, deadline uint64)
- func LoadStakingAccAndInfo(ctx *types.Context) (stakingAcc *types.AccountInfo, info stake.StakingInfo)
- func LoadStakingInfo(ctx *types.Context) (info stake.StakingInfo)
- func LoadVote(ctx *types.Context, validator common.Address) (target, votingPower uint64)
- func PackCreateValidator(rewardTo gethcmn.Address, intro [32]byte, pubKey [32]byte) []byte
- func PackDecreaseMinGasPrice() []byte
- func PackEditValidator(rewardTo gethcmn.Address, intro [32]byte) []byte
- func PackExecuteProposal() []byte
- func PackGetVote(validator gethcmn.Address) []byte
- func PackIncreaseMinGasPrice() []byte
- func PackProposal(target *big.Int) []byte
- func PackRetire() []byte
- func PackSumVotingPower(addrList []gethcmn.Address) []byte
- func PackVote(target *big.Int) []byte
- func SaveEpoch(ctx *types.Context, epoch *stake.Epoch)
- func SaveMinGasPrice(ctx *types.Context, minGP uint64, isLast bool)
- func SaveProposal(ctx *types.Context, target, deadline uint64)
- func SaveStakingInfo(ctx *types.Context, info stake.StakingInfo)
- func SaveVote(ctx *types.Context, validator common.Address, target, votingPower uint64)
- func Slash(ctx *types.Context, info *stake.StakingInfo, pubkey [32]byte, ...) (totalSlashed *uint256.Int)
- func SlashAndReward(ctx *types.Context, slashValidators [][20]byte, ...) (currValidators, newValidators []*stake.Validator, currEpochNum int64)
- func SwitchEpoch(ctx *types.Context, epoch *stake.Epoch, posVotes map[[32]byte]int64, ...) []*stake.Validator
- func UnpackSumVotingPowerReturnData(data []byte) (*big.Int, *big.Int)
- type StakingContractExecutor
- func (s *StakingContractExecutor) Execute(ctx *types.Context, currBlock *types.BlockInfo, tx *types.TxToRun) (status int, logs []types.EvmLog, gasUsed uint64, outData []byte)
- func (_ *StakingContractExecutor) Init(ctx *types.Context)
- func (_ *StakingContractExecutor) IsSystemContract(addr common.Address) bool
- func (_ *StakingContractExecutor) RequiredGas(input []byte) uint64
- func (_ *StakingContractExecutor) Run(input []byte) ([]byte, error)
Constants ¶
View Source
const ( SumVotingPowerGasPerByte uint64 = 25 SumVotingPowerBaseGas uint64 = 10000 StatusSuccess int = 0 // because EVMC_SUCCESS = 0, StatusFailed int = 1 // because EVMC_FAILURE = 1, StakingContractSequence uint64 = math.MaxUint64 - 2 /*uint64(-3)*/ Uint64_1e18 uint64 = 1000_000_000_000_000_000 )
Variables ¶
View Source
var ( //contract address, 10000 StakingContractAddress = [20]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x27, 0x10} /*------selector------*/ /*interface Staking { // following methods can only be called by EOA //0x24d1ed5d function createValidator(address rewardTo, bytes32 introduction, bytes32 pubkey) external; //0x9dc159b6 function editValidator(address rewardTo, bytes32 introduction) external; //0xa4874d77 function retire() external; //0xf2016e8e function increaseMinGasPrice() external; //0x696e6ad2 function decreaseMinGasPrice() external; //30326c17 function proposal(uint target) external; //0121b93f function vote(uint target) external; //373058b8 function executeProposal() external; //8d337b81 function getVote(address validator) external view returns (uint) // sumVotingPower can only be called by other smart contracts //9ce06909 function sumVotingPower(address[] calldata addrList) external override returns (uint summedPower, uint totalPower) }*/ SelectorCreateValidator = [4]byte{0x24, 0xd1, 0xed, 0x5d} SelectorEditValidator = [4]byte{0x9d, 0xc1, 0x59, 0xb6} SelectorRetire = [4]byte{0xa4, 0x87, 0x4d, 0x77} SelectorIncreaseMinGasPrice = [4]byte{0xf2, 0x01, 0x6e, 0x8e} SelectorDecreaseMinGasPrice = [4]byte{0x69, 0x6e, 0x6a, 0xd2} SelectorProposal = [4]byte{0x30, 0x32, 0x6c, 0x17} SelectorVote = [4]byte{0x01, 0x21, 0xb9, 0x3f} SelectorExecuteProposal = [4]byte{0x37, 0x30, 0x58, 0xb8} SelectorGetVote = [4]byte{0x8d, 0x33, 0x7b, 0x81} SelectorSumVotingPower = [4]byte{0x9c, 0xe0, 0x69, 0x09} //slot SlotStakingInfo = strings.Repeat(string([]byte{0}), 32) SlotAllBurnt = strings.Repeat(string([]byte{0}), 31) + string([]byte{1}) SlotMinGasPrice = strings.Repeat(string([]byte{0}), 31) + string([]byte{2}) SlotLastMinGasPrice = strings.Repeat(string([]byte{0}), 31) + string([]byte{3}) SlotMinGasPriceProposalTarget = strings.Repeat(string([]byte{0}), 31) + string([]byte{4}) SlotVoters = strings.Repeat(string([]byte{0}), 31) + string([]byte{5}) // slot in hex SlotMinGasPriceHex = hex.EncodeToString([]byte(SlotMinGasPrice)) )
View Source
var ( /*------param------*/ //staking InitialStakingAmount = uint256.NewInt(0) MinimumStakingAmount = uint256.NewInt(0) SlashedStakingAmount = uint256.NewInt(0) GasOfValidatorOp uint64 = 400_000 GasOfMinGasPriceOp uint64 = 50_000 //minGasPrice DefaultMinGasPrice uint64 = param.DefaultMinGasPrice //10gwei MinGasPriceDeltaRateInBlock uint64 = 16 MinGasPriceDeltaRate uint64 = 5 //gas delta rate every proposal can change MinGasPriceUpperBound uint64 = 500_000_000_000 //500gwei MinGasPriceLowerBoundOld uint64 = 1_000_000_000 //1gwei MinGasPriceLowerBound uint64 = 10_000_000 //0.01gwei DefaultProposalDuration uint64 = 60 * 60 * 24 //24hour )
View Source
var ( /*------error info------*/ InvalidCallData = errors.New("invalid call data") InvalidSelector = errors.New("invalid selector") BalanceNotEnough = errors.New("balance is not enough") NoSuchValidator = errors.New("no such validator") ValidatorNotActive = errors.New("validator not active") OperatorNotValidator = errors.New("minGasPrice operator not validator or its rewardTo") MinGasPriceTooBig = errors.New("minGasPrice bigger than allowed highest value") MinGasPriceTooSmall = errors.New("minGasPrice smaller than allowed lowest value") InvalidArgument = errors.New("invalid argument") CreateValidatorCoinLtInitAmount = errors.New("validator's staking coin less than init amount") MinGasPriceExceedBlockChangeDelta = errors.New("the amount of variation in minGasPrice exceeds the allowable range") StillInProposal = errors.New("still in proposal") NotInProposal = errors.New("not in proposal") TargetExceedChangeDelta = errors.New("minGasPrice target exceeds the allowable range") ProposalHasFinished = errors.New("proposal has finished") ProposalNotFinished = errors.New("proposal not finished") )
View Source
var ( SlotValidatorsMap = strings.Repeat(string([]byte{0}), 31) + string([]byte{134}) SlotValidatorsArray = strings.Repeat(string([]byte{0}), 31) + string([]byte{135}) CoindayUnit = uint256.NewInt(0).Mul(uint256.NewInt(24*60*60), uint256.NewInt(Uint64_1e18)) )
View Source
var ABI = ethutils.MustParseABI(`
[
{
"inputs": [
{
"internalType": "address",
"name": "rewardTo",
"type": "address"
},
{
"internalType": "bytes32",
"name": "introduction",
"type": "bytes32"
},
{
"internalType": "bytes32",
"name": "pubkey",
"type": "bytes32"
}
],
"name": "createValidator",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "decreaseMinGasPrice",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "rewardTo",
"type": "address"
},
{
"internalType": "bytes32",
"name": "introduction",
"type": "bytes32"
}
],
"name": "editValidator",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "increaseMinGasPrice",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "retire",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address[]",
"name": "addrList",
"type": "address[]"
}
],
"name": "sumVotingPower",
"outputs": [
{
"internalType": "uint256",
"name": "summedPower",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "totalPower",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "executeProposal",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "validator",
"type": "address"
}
],
"name": "getVote",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "target",
"type": "uint256"
}
],
"name": "proposal",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "target",
"type": "uint256"
}
],
"name": "vote",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
`)
Functions ¶
func CalcMedian ¶
func CreateInitVotes ¶
func DeleteProposal ¶
func DeleteVoters ¶
func DistributeFee ¶
func DistributeFee(ctx *types.Context, stakingAcc *types.AccountInfo, info *stake.StakingInfo, collectedFee *uint256.Int, collector, proposer [32]byte, voters [][32]byte)
distribute the collected gas fee to validators who voted for current block, half fee burnt to blackHole Acc.
func GetAndClearPosVotes ¶
func GetPosVotes ¶
func LoadStakingAccAndInfo ¶
func LoadStakingAccAndInfo(ctx *types.Context) (stakingAcc *types.AccountInfo, info stake.StakingInfo)
func LoadStakingInfo ¶
func LoadStakingInfo(ctx *types.Context) (info stake.StakingInfo)
func PackCreateValidator ¶
func PackDecreaseMinGasPrice ¶
func PackDecreaseMinGasPrice() []byte
func PackExecuteProposal ¶
func PackExecuteProposal() []byte
func PackGetVote ¶
func PackIncreaseMinGasPrice ¶
func PackIncreaseMinGasPrice() []byte
func PackProposal ¶
func PackRetire ¶
func PackRetire() []byte
func PackSumVotingPower ¶
func SaveMinGasPrice ¶
In this file, only SlotMinGasPrice is written. In refresh of app.go, SlotMinGasPrice is copied to SlotLastMinGasPrice
func SaveProposal ¶
func SaveStakingInfo ¶
func SaveStakingInfo(ctx *types.Context, info stake.StakingInfo)
func Slash ¶
func Slash(ctx *types.Context, info *stake.StakingInfo, pubkey [32]byte, amount *uint256.Int) (totalSlashed *uint256.Int)
Slash 'amount' of coins from the validator with 'pubkey'. These coins are burnt and booked on BlackHole acc.
func SlashAndReward ¶
func SlashAndReward(ctx *types.Context, slashValidators [][20]byte, currProposer, lastProposer [20]byte, lastVoters [][]byte, blockReward *uint256.Int) (currValidators, newValidators []*stake.Validator, currEpochNum int64)
slashValidators and lastVoters are consensus addresses generated from validator consensus pubkey
Types ¶
type StakingContractExecutor ¶
type StakingContractExecutor struct {
// contains filtered or unexported fields
}
func NewStakingContractExecutor ¶
func NewStakingContractExecutor(logger log.Logger) *StakingContractExecutor
func (*StakingContractExecutor) Execute ¶
func (s *StakingContractExecutor) Execute(ctx *types.Context, currBlock *types.BlockInfo, tx *types.TxToRun) (status int, logs []types.EvmLog, gasUsed uint64, outData []byte)
Staking functions which can be invoked by EOA through smart contract calls The extra gas fee is distributed to the validators, not refunded
func (*StakingContractExecutor) Init ¶
func (_ *StakingContractExecutor) Init(ctx *types.Context)
func (*StakingContractExecutor) IsSystemContract ¶
func (_ *StakingContractExecutor) IsSystemContract(addr common.Address) bool
func (*StakingContractExecutor) RequiredGas ¶
func (_ *StakingContractExecutor) RequiredGas(input []byte) uint64
this functions is called when other contract calls sumVotingPower
Click to show internal directories.
Click to hide internal directories.