Skip to content

rus-alex/opera-sfc

 
 

Repository files navigation

Special Fee Contract

The SFC (Special Fee Contract) maintains a list of validation stakers and delegators. It distributes the rewards, based on information written into contract by Lachesis node.

The essential:

  • SFC contract only maintains the list of delegators and stakers, and doesn’t calculate reward weights or validators groups. The reward weights and validators group are written into SFC contract storage by consensus (i.e. golang code) for performance reasons. SFC calculates the rewards, depending on epoch snapshots written by consensus.
  • Calculation of reward is O(1) for each epoch. Staker/delegator may specify a range of epochs he claims reward for, but not lower than prev reward epoch + 1 and not higher than last sealed epoch snapshot.
  • Each delegation is a separate object. Delegator claims rewards for each delegation independently of other delegations and stakers. Staker has a counter of delegated tokens to it.
  • Staker/delegator cannot withdraw his stake any time. He must deactivate the stake (prepare to withdraw) in advance of N days.
  • If validator is confirmed to be a cheater, then only a flag is written into SFC storage by consensus. SFC checks this flag and doesn't withdraw stake/delegation during withdraw operation if flag is set.
  • Validation staker is allowed to increase his stake, but not decrease (only allowed to withdraw the whole stake).

Compile

  1. Install solc 0.5.12
  2. solc -o $PWD/build --optimize --optimize-runs=2000 --abi --bin-runtime --allow-paths $PWD/contracts --overwrite $PWD/contracts/sfc/Staker.sol # compilation. don't forget --bin-runtime flag, if contract is pre-deployed!
  3. cat build/TestStakers.bin-runtime # paste this into GetTestContractBinV1 in go-lachesis repo
  4. cat build/TestStakers.abi # use this ABI to call contract methods

Test

  1. Install nodejs 10.5.0
  2. npm install -g truffle@v5.1.4 # install truffle v5.1.4
  3. npm update
  4. npm test

If everything is allright, it should output something along this:

> fantom-sfc@1.0.0 test fantom-sfc
> truffle test

Using network 'test'.


Compiling your contracts...
===========================
> Compiling ./contracts/ownership/Ownable.sol
> Compiling ./contracts/sfc/Migrations.sol
> Compiling ./contracts/sfc/SafeMath.sol
> Compiling ./contracts/sfc/Staker.sol
> Compiling ./contracts/sfc/StakerConstants.sol
> Compiling ./contracts/test/LegacyStaker.sol
> Compiling ./contracts/test/UnitTestStakers.sol
> Compiling ./contracts/upgradeability/Address.sol
> Compiling ./contracts/upgradeability/BaseUpgradeabilityProxy.sol
> Compiling ./contracts/upgradeability/Proxy.sol
> Compiling ./contracts/upgradeability/UpgradeabilityProxy.sol
> Compiling ./contracts/version/Version.sol



  Contract: SFC
    Delegation migration tests
	gas used for sfc deploying: 4913921
      ✓ should auto migrate legacy deposition to new model (1931ms)
      ✓ should manually migrate legacy deposition to new model (371ms)
      ✓ should not call calcDelegationRewards while delegation is in the legacy model (373ms)

  Contract: SFC
    Locking stake tests
      ✓ should start "locked stake" feature (441ms)
      ✓ should calc ValidatorEpochReward correctly after locked up started (940ms)
      ✓ should lock stake (1824ms)
      ✓ should lock stake with right duration (497ms)
      ✓ should not call prepareToWithdrawStake, until locked time is passed (376ms)
      ✓ should not call prepareToWithdrawStakePartial, until locked time is passed (389ms)
      ✓ should lock delegation (1906ms)
      ✓ should lock delegation with right duration (719ms)
      ✓ should subtract penalty if prepareToWithdrawDelegation will call earlier than locked time is passed (987ms)
      ✓ should adjust penalty if penalty is bigger than delegated stake (609ms)
      ✓ should subtract penalty if prepareToWithdrawDelegationPartial is called earlier than locked time is passed (1022ms)
      ✓ should claim lockup rewards (2843ms)
      ✓ should claim compound rewards (670ms)
      ✓ should claim compound rewards epoch-by-epoch (508ms)

  Contract: SFC
    Methods tests
      ✓ checking Staker parameters (122ms)
      ✓ checking createStake function (435ms)
      ✓ checking increaseStake function (222ms)
      ✓ checking createDelegation function (391ms)
      ✓ checking createDelegation function to several stakers (482ms)
      ✓ checking calcRawValidatorEpochReward function (316ms)
      ✓ checking epoch snapshot logic (130ms)
      ✓ checking calcValidatorEpochReward function (405ms)
      ✓ checking calcDelegationEpochReward function (356ms)
      ✓ checking claimDelegationRewards function (555ms)
      ✓ checking claimValidatorRewards function (340ms)
      ✓ checking prepareToWithdrawStake function (179ms)
      ✓ checking withdrawStake function (771ms)
      ✓ checking prepareToWithdrawDelegation function (294ms)
      ✓ checking withdrawDelegation function (1299ms)


  32 passing (24s)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 81.6%
  • Solidity 18.4%