Staking manager

For the Ramestta’s Proof of Security based consensus, all the ⅔+1 proof verification and handling of staking, rewards are executed on the Polygon smart contract. The whole design follows this philosophy of doing less on the Mainnet contract. It does information verification and pushes all the computation-heavy operations to L2 (read about Heimdall).

Stakers are divided into validators, delegators, and watchers (for fraud reporting).

StakeManager is the main contract for handling validator related activities like checkPoint signature verification, reward distribution, and stake management. Since the contract is using NFT ID as a source of ownership, change of ownership and signer won’t affect anything in the system.

To ensure Proof of Security consensus, Ramestta executes all proof verification and staking operations on the Polygon smart contract, leaving the computation-heavy tasks to L2.

Stakeholders can take on the role of a validator, delegate, or watcher for reporting fraud. StakeManager is the primary contract for validator-related activities such as stake management, reward distribution, and signature verification. Only one role, either validator or delegator, can be assigned to a single Polygon address as a design choice.

Using NFT ID as the source of ownership ensures that changes in ownership and signer will not impact the system.

validatorThreshold: Shows the maximum number of validators accepted by the system, also called slots

AccountStateRoot

  • For various accounting done on heimdall for validators and delegator account root is submitted while submitting the checkpoint .

  • accRoot is used while claimRewards and unStakeClaim .

Stake/stakeFor

StakeManager.sol

function stake(
    uint256 amount,
    uint256 heimdallFee,
    bool acceptDelegation,
    bytes calldata signerPubkey
) public;

function stakeFor(
    address user,
    uint256 amount,
    uint256 heimdallFee,
    bool acceptDelegation,
    bytes memory signerPubkey
) public;
  • Allows anyone with amount (in RAMA tokens) greater minDeposit then if currentValidatorSetSize is less then validatorThreshold .

  • MUST transfer amount+heimdallFee , puts validator into auction period for an auctionInterval.(more on auction in auction section)

  • updateTimeLine updates special timeline data structure, which keeps track of active validators and active stake for given epoch/checkpoint count.

  • One unique NFT is minted on each new stake/stakeFor call, which can be transferred to anyone but can be owned 1:1 ethereum address.

  • acceptDelegation set true if validators want to accept delegation, ValidatorShare contract is deployed for the validator.

unstake:

  1. If a validator decides to unstake, they will be removed from the validator set in the next epoch. However, this removal is only applicable for the current checkpoint.

  2. To account for a validator's exit, their stake will be removed from the timeline data structure, and the count for their exit epoch will be updated.

  3. If a validator had a delegation, they should collect all rewards before their exit, and their delegation contract should be locked to prevent new delegations.

unstakeClaim:

function unstakeClaim(uint256 validatorId) public;
  • After unstaking validators are put into withdrawal period so that they can be slashed if any fraud found after unstaking for pas frauds.

  • Once WITHDRAWAL_DELAY period is served validator's can call this function and do settlement with stakeManager(get rewards if any, get staked tokens back, burn NFT etc)

Restake

function restake(uint256 validatorId, uint256 amount, bool stakeRewards) public;
  • Allows validators to increase their stake by putting new amount or rewards or both.

  • MUST update timeline(amount) for active stake.

withdrawRewards

function withdrawRewards(uint256 validatorId) public;
  • Allows validators to withdraw accumulated rewards, must consider getting rewards from delegation contract if validator accepts delegation.

updateSigner

function updateSigner(uint256 validatorId, bytes memory signerPubkey) public
  • Allows validators to update signer address(which is used to validate blocks on ramestta chain and checkpoint sigs on stakeManager)

topUpForFee

function topUpForFee(uint256 validatorId, uint256 heimdallFee) public;
  • Validators can top-up their balance for heimdall fee by invoking this method.

claimFee

function claimFee(
        uint256 validatorId,
        uint256 accumSlashedAmount,
        uint256 accumFeeAmount,
        uint256 index,
        bytes memory proof
    ) public;
  • Used to withdraw fee from heimdall.

  • accountStateRoot is updated on each checkpoint, so that validators can provide proof of inclusion in this root for account on heimdall and withdraw fee.

  • Note that accountStateRoot is re-written to prevent exits on multiple checkpoints(for old root and save accounting on stakeManager)

  • accumSlashedAmount is unused atm, will be used for slashing on heimdall if needed.

StakingNFT

  • Standard erc721 with few restrictions like one token per user and minted in sequential manner.

Validator Replacement

  • In order to replace poor performing validator there is periodic auction for each validator slot.

  • For individual validators there is auction window where wanna be validators can bid their amount and start an auction using startAuction function.

  • Once the auctionInterval is over last bidder needs to close the auction in order to confirm and become validator. For which she needs to call confirmAuctionBid which accepts and behave similar to new stake function for upcoming validator and unStake for old validator.

  • Current validator can bid for herself and try to keep that place.

  • Whole mech dynamically balances the stake value and overall security according to market conditions and use of ramestta chain.

startAuction

function startAuction(
    uint256 validatorId, /**  auction for validator */
    uint256 amount /**  amount greater then old validator's stake */
    ) external;
  • In order to start a bid or bid higher on already running auction this function is used.

  • Auction period runs in cycles like (auctionPeriod--dynasty)--(auctionPeriod--dynasty)--(auctionPeriod--dynasty) so it MUST check for correct auction period.

  • perceivedStakeFactor is used to calculate exact factor*old stake (note currently it is by default 1 WIP for picking the function).

  • MUST check for auction from last auction period if any still going on (one can choose to not call confirmAuction in order to get her capital out in next auction).

  • Normally continuous english auction is going on in a auctionPeriod .

confirmAuctionBid

function confirmAuctionBid(
        uint256 validatorId,
        uint256 heimdallFee, /** for new validator */
        bool acceptDelegation,
        bytes calldata signerPubkey
    ) external
  • MUST check that this is not an auctionPeriod.

  • If last bidder is owner of validatorId behaviour should be similar to restake.

  • In second case unStake validatorId and add new user as validator from next checkpoint, for the new user behaviour should be similar to stake/stakeFor.

checkSignatures

function checkSignatures(
        uint256 blockInterval,
        bytes32 voteHash,
        bytes32 stateRoot,
        bytes memory sigs
    ) public;
  • Writes are meant only for RootChain contract when submitting checkpoints

  • voteHash on which all validators sign (BFT ⅔+1 agreement)

  • This function validates only unique sigs and checks for ⅔+1 power has signed on checkpoint root (inclusion in voteHash verification in RootChain contract for all data) currentValidatorSetTotalStake provides current active stake.

  • Rewards are distributed proportional to validator's stake.

isValidator

  • Checks if given validator is active validator for current epoch.

Timeline data structure

struct State {
    int256 amount;
    int256 stakerCount;
}

mapping(uint256 => State) public validatorState;

StakingInfo:

Source: StakingInfo.sol

Centralised logging contract for both validator and delegation events, Includes few read only functions.

ValidatorShareFactory:

A factory contract to deploy ValidatorShare contract for each validator who opt-in for delegation. You can check out the source code of the ValidatorShareFactory.sol contract on GitHub.

Last updated