staking

package
v0.4.5-0...-e935ded Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2022 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

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 AddGenesisValidatorsIntoStakingInfo

func AddGenesisValidatorsIntoStakingInfo(ctx *mevmtypes.Context, genesisValidators []*types.Validator)

func AddVoters

func AddVoters(ctx *mevmtypes.Context, validator common.Address)

func CalcMedian

func CalcMedian(nums []uint64) uint64

func CalculateTarget

func CalculateTarget(ctx *mevmtypes.Context, voters []common.Address) uint64

func CreateInitVotes

func CreateInitVotes(ctx *mevmtypes.Context, xhedgeContractSeq uint64, activeValidators []*types.Validator)

func DeleteProposal

func DeleteProposal(ctx *mevmtypes.Context)

func DeleteProposalInfos

func DeleteProposalInfos(ctx *mevmtypes.Context, voters []common.Address)

func DeleteVote

func DeleteVote(ctx *mevmtypes.Context, validator common.Address)

func DeleteVoters

func DeleteVoters(ctx *mevmtypes.Context)

func DistributeFee

func DistributeFee(ctx *mevmtypes.Context, stakingAcc *mevmtypes.AccountInfo, info *types.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 GetAndClearPosVotes(ctx *mevmtypes.Context, xHedgeContractSeq uint64) map[[32]byte]int64

func GetPosVotes

func GetPosVotes(ctx *mevmtypes.Context, xhedgeContractSeq uint64) map[[32]byte]*big.Int

func GetVoters

func GetVoters(ctx *mevmtypes.Context) (voters []common.Address)

func LoadEpoch

func LoadEpoch(ctx *mevmtypes.Context, epochNum int64) (epoch types.Epoch, ok bool)

func LoadMinGasPrice

func LoadMinGasPrice(ctx *mevmtypes.Context, isLast bool) uint64

func LoadProposal

func LoadProposal(ctx *mevmtypes.Context) (target uint64, deadline uint64)

func LoadStakingAccAndInfo

func LoadStakingAccAndInfo(ctx *mevmtypes.Context) (stakingAcc *mevmtypes.AccountInfo, info types.StakingInfo)

func LoadStakingInfo

func LoadStakingInfo(ctx *mevmtypes.Context) (info types.StakingInfo)

func LoadVote

func LoadVote(ctx *mevmtypes.Context, validator common.Address) (target, votingPower uint64)

func PackCreateValidator

func PackCreateValidator(rewardTo gethcmn.Address, intro [32]byte, pubKey [32]byte) []byte

func PackDecreaseMinGasPrice

func PackDecreaseMinGasPrice() []byte

func PackEditValidator

func PackEditValidator(rewardTo gethcmn.Address, intro [32]byte) []byte

func PackExecuteProposal

func PackExecuteProposal() []byte

func PackGetVote

func PackGetVote(validator gethcmn.Address) []byte

func PackIncreaseMinGasPrice

func PackIncreaseMinGasPrice() []byte

func PackProposal

func PackProposal(target *big.Int) []byte

func PackRetire

func PackRetire() []byte

func PackSumVotingPower

func PackSumVotingPower(addrList []gethcmn.Address) []byte

func PackVote

func PackVote(target *big.Int) []byte

func SaveEpoch

func SaveEpoch(ctx *mevmtypes.Context, epoch *types.Epoch)

func SaveMinGasPrice

func SaveMinGasPrice(ctx *mevmtypes.Context, minGP uint64, isLast bool)

In this file, only SlotMinGasPrice is written. In refresh of app.go, SlotMinGasPrice is copied to SlotLastMinGasPrice

func SaveProposal

func SaveProposal(ctx *mevmtypes.Context, target, deadline uint64)

func SaveStakingInfo

func SaveStakingInfo(ctx *mevmtypes.Context, info types.StakingInfo)

func SaveVote

func SaveVote(ctx *mevmtypes.Context, validator common.Address, target, votingPower uint64)

func Slash

func Slash(ctx *mevmtypes.Context, info *types.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 *mevmtypes.Context, slashValidators [][20]byte,
	currProposer, lastProposer [20]byte, lastVoters [][]byte,
	blockReward *uint256.Int) (currValidators, newValidators []*types.Validator)

slashValidators and lastVoters are consensus addresses generated from validator consensus pubkey

func SwitchEpoch

func SwitchEpoch(ctx *mevmtypes.Context, epoch *types.Epoch, posVotes map[[32]byte]int64, logger log.Logger) []*types.Validator

switch to a new epoch

func UnpackSumVotingPowerReturnData

func UnpackSumVotingPowerReturnData(data []byte) (*big.Int, *big.Int)

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 *mevmtypes.Context, currBlock *mevmtypes.BlockInfo, tx *mevmtypes.TxToRun) (status int, logs []mevmtypes.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) 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

func (*StakingContractExecutor) Run

func (_ *StakingContractExecutor) Run(input []byte) ([]byte, error)

function sumVotingPower(address[] calldata addrList) external override returns (uint summedPower, uint totalPower)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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