Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal Audit #22

Closed
wants to merge 193 commits into from
Closed
Show file tree
Hide file tree
Changes from 184 commits
Commits
Show all changes
193 commits
Select commit Hold shift + click to select a range
9528766
Init commit
nxqbao Aug 19, 2022
92ab45d
Add interface of ValidatorSet and Slash
nxqbao Aug 19, 2022
09db951
Add quick sort lib
nxqbao Aug 19, 2022
c794321
Add linter
nxqbao Aug 22, 2022
a741c84
Init commit staking contract
nxqbao Aug 22, 2022
bbffbfd
update
nxqbao Aug 23, 2022
3fd308f
Update
nxqbao Aug 23, 2022
b053aaf
Update deps & fix lint
ducthotran2010 Aug 23, 2022
5ea4ba0
Update staking contract & its interface
ducthotran2010 Aug 23, 2022
eec0f23
Update IStaking
nxqbao Aug 24, 2022
3b6d99c
Update ValidatorSet interface
nxqbao Aug 24, 2022
da3ca0e
Init commit ValidatorSet implementation
nxqbao Aug 24, 2022
55d1e0f
Update ValidatorSet contract
nxqbao Aug 25, 2022
a68f416
[Validator] Add isWorkingValidator function
nxqbao Aug 25, 2022
7929908
[Validator] Fix updateValidators function
nxqbao Aug 25, 2022
84c07d4
[Validator] Update _setValidator function
nxqbao Aug 25, 2022
88c0c64
[Validator] Update validator array
nxqbao Aug 25, 2022
c8f392b
[Validator] Add more view functions
nxqbao Aug 25, 2022
40ba714
[Validator] Fix updateValidators function
nxqbao Aug 25, 2022
e6d9c53
[Validator] Rename contract name
nxqbao Aug 25, 2022
8483d9a
[Staking] Add sorting validators
nxqbao Aug 30, 2022
7370541
Add hardhat-gas-reporter package
nxqbao Aug 30, 2022
b6e8e94
[Slash] Add slash contract
nxqbao Aug 30, 2022
023af0f
Fix constructor for Slash and Validators contract
nxqbao Aug 30, 2022
64f5b9e
[CoreStaking] add contract & test
ducthotran2010 Aug 30, 2022
6f797cd
Rename. Fix error messages.
nxqbao Aug 30, 2022
366eed9
[Validator] Refactor. Lint. Add mock.
nxqbao Aug 30, 2022
c539c2b
[Validator] Fix ValidatorCore. Add test.
nxqbao Aug 30, 2022
d5b9c12
Add hardhat-gas-reporter config
nxqbao Aug 30, 2022
840b3ad
chore: lint
nxqbao Aug 30, 2022
24e88eb
[Validator] Fix bug in core contract. Update test.
nxqbao Aug 30, 2022
d675321
chore: lint
nxqbao Aug 31, 2022
7ad79fe
[Misc] Add benchmark test for swapping on storage
nxqbao Aug 31, 2022
215c5d8
[Staking] Add deploy script
nxqbao Aug 31, 2022
90352a0
[CoreStaking] update contract & test
ducthotran2010 Aug 31, 2022
f7e5264
[CoreStaking] Finalize & rename to RewardCalculation, add test
ducthotran2010 Sep 3, 2022
344c2cc
[Staking] Finalize staking contract, rename some variable from other …
ducthotran2010 Sep 3, 2022
800eca7
[Staking] add test
ducthotran2010 Sep 3, 2022
79a3db9
[Staking] Fix proposeValidator function
nxqbao Sep 2, 2022
5ce6b66
[Staking] Fix staking bug. Refactor test.
nxqbao Sep 3, 2022
4ea4233
[hardhat] Update initial balance
nxqbao Sep 3, 2022
dc57fd1
[Validator] Refactor
nxqbao Sep 4, 2022
0e8ac5b
[Staking] Add renounce validator. Add test.
nxqbao Sep 4, 2022
e5aa5f8
[Staking] Resolve conflict
nxqbao Sep 4, 2022
1d93c3a
[Staking] Fix renouncing function
nxqbao Sep 4, 2022
ac3d47d
[Staking] Fix updateValidatorSet
nxqbao Sep 4, 2022
6fbed87
[Sorting] Fix quick sort implementation
nxqbao Sep 4, 2022
6b1f12f
Ignore remix config file
nxqbao Sep 4, 2022
72cb111
Refactor code. Fix missing imports.
nxqbao Sep 5, 2022
b44041b
[Staking] Optimize sorting
nxqbao Sep 5, 2022
833e41d
[Staking] Fix sorting
nxqbao Sep 5, 2022
5429333
[Staking] Update test
nxqbao Sep 5, 2022
e2a9c8f
[SlashIndicator] Update slash contract
ducthotran2010 Sep 5, 2022
5463ac8
[Staking] Rename & expose fn
ducthotran2010 Sep 5, 2022
dfea65d
[Lib] Add math lib
ducthotran2010 Sep 5, 2022
e6d0e8c
[RoninValidatorSet] add contract
ducthotran2010 Sep 5, 2022
0aba9c1
[RoninValidatorSet] update get validator function
ducthotran2010 Sep 5, 2022
06c599c
[Slash] Remove historical data. Implement resetCounter(s).
nxqbao Sep 5, 2022
55a56d7
[Staking] update test & fix contract bug
ducthotran2010 Sep 5, 2022
1f47ce1
[Staking] remove console.log & update test
ducthotran2010 Sep 5, 2022
ce376e6
[Staking] rename function
ducthotran2010 Sep 5, 2022
fb384e4
[SlashIndicator] update contract & its interface
ducthotran2010 Sep 6, 2022
b2c7d1a
[RoninValidatorSet] fix delegating reward
ducthotran2010 Sep 6, 2022
34359d6
[RoninValidatorSet] update wrap up epoch fn
ducthotran2010 Sep 6, 2022
ee4b910
[Sorting] Add mock. Add test.
nxqbao Sep 6, 2022
85c5b7b
[RoninValidatorSet] Fix wrapUpEpoch to update balance after settling
nxqbao Sep 6, 2022
55b4c5c
[StakingManager] Update error messages
nxqbao Sep 6, 2022
e89557c
Update test case
ducthotran2010 Sep 6, 2022
3781704
[RoninValidatorSet] Rename fn & apply proxy pattern
ducthotran2010 Sep 6, 2022
f64ebc3
Remove unused deploy & add dpostaking deployment script
ducthotran2010 Sep 6, 2022
116e597
Remove unused deploy
ducthotran2010 Sep 6, 2022
4259748
[SlashIndicator] update contract & its interface
ducthotran2010 Sep 6, 2022
04d50d5
Rename variable & lint fix
ducthotran2010 Sep 6, 2022
abfd5d2
[Staking] add test
ducthotran2010 Sep 6, 2022
13bfdc5
[Staking] use bulk fn & update test
ducthotran2010 Sep 6, 2022
039c49b
[RoninValidatorSet] Remove console log
ducthotran2010 Sep 6, 2022
3141bb1
[RoninValidatorSet] add unit test
ducthotran2010 Sep 6, 2022
d8a60ba
[SlashIndicator] Fix slash function. Add test.
nxqbao Sep 6, 2022
3977f91
[RoninValidatorSet] add tests
ducthotran2010 Sep 7, 2022
9d8ebd9
[SlashIndicator] Add gov function. Fix test. Fix typo.
nxqbao Sep 7, 2022
c97f812
Add deployment script
ducthotran2010 Sep 7, 2022
91f3fdb
Fix comments
ducthotran2010 Sep 7, 2022
94ecac4
Fix visibility
ducthotran2010 Sep 7, 2022
1dae65d
Update README.md
ducthotran2010 Sep 7, 2022
edac01b
Fix hardfork issue
ducthotran2010 Sep 7, 2022
9584a45
[README.md] Add ToC & update deployment
ducthotran2010 Sep 7, 2022
aa8e330
Update README.md
ducthotran2010 Sep 7, 2022
fa22015
Update requirements
ducthotran2010 Sep 8, 2022
ade3438
Remove unnecessary contracts & refine Validator contract
ducthotran2010 Sep 8, 2022
b388710
[SlashIndicator] Update test to work with new contract
nxqbao Sep 8, 2022
5a9004e
Rename governance admin
ducthotran2010 Sep 8, 2022
4617a75
Rename DPoStaking to Staking
ducthotran2010 Sep 8, 2022
a500d0a
[RoninValidatorSet] add events & update tests
ducthotran2010 Sep 8, 2022
80a0d18
Separate Staking interface, update comments for events
ducthotran2010 Sep 8, 2022
a0fca46
Merge pull request #1 from axieinfinity/dev
ducthotran2010 Sep 9, 2022
4a8fedc
Update devnet config (#2)
ducthotran2010 Sep 9, 2022
bdd4f6e
Update README.md (#3)
ducthotran2010 Sep 9, 2022
f4d5856
[Staking] Rename variable & remove unnecessary variable (#4)
ducthotran2010 Sep 13, 2022
d2fcd65
[Staking/Validator] Add bonus per block (#7)
ducthotran2010 Sep 13, 2022
2ad8aac
[Staking] Fix contract events and add test cases (#6)
ducthotran2010 Sep 13, 2022
da51fda
[StashIndicator] Add governance functions
nxqbao Sep 8, 2022
d71d4cf
[SlashIndicator] Add test and integration test
nxqbao Sep 9, 2022
3b6d501
chore: add TODO for contracts
nxqbao Sep 9, 2022
3006dd7
refactor: refactor test
nxqbao Sep 9, 2022
35e9d95
[RoninValidatorSet] Change expect `.have.same.members` to `.eql`
nxqbao Sep 9, 2022
c0141a7
[Staking] Fix deductStaking function
nxqbao Sep 9, 2022
1fbffba
[ValidatorSet] Temporarily fix unstake on slashing felony
nxqbao Sep 12, 2022
516ff7b
[Slash] Add integration test
nxqbao Sep 12, 2022
5488f24
[Test] Add configuration test
nxqbao Sep 12, 2022
af3aa27
[IntegrationTest] Fix slashing test
nxqbao Sep 12, 2022
2485e11
[IntegrationTest] Add more test for submit reward
nxqbao Sep 12, 2022
d51b244
[IntegrationTest] Rename files
nxqbao Sep 12, 2022
0f15d6e
[package] Add hardhat-chai-matchers, remove ethereum-waffle
nxqbao Sep 12, 2022
3caa3e1
[IntegrationTest] Fix submit reward test
nxqbao Sep 12, 2022
655410c
[IntegrationTest] Fix test description
nxqbao Sep 12, 2022
983a229
[IntegrationTest] Refactor test
nxqbao Sep 12, 2022
bd2749c
[Slash] Fix test with assert emitted events
nxqbao Sep 12, 2022
6896588
[IntegrationTest] Add WrapUpEpoch test
nxqbao Sep 12, 2022
2f1c33d
[IStaking] Fix typo
nxqbao Sep 13, 2022
244855a
[Slash] Emit events for set functions
nxqbao Sep 13, 2022
879515b
[IntegrationTest] Add more test for wrapUp function
nxqbao Sep 13, 2022
c9b46aa
[IntegrationTest] Remove unnecessary test
nxqbao Sep 13, 2022
d77ce8a
[RoninValidatorSet] Add setter for unchangeable variables
nxqbao Sep 13, 2022
cecf90c
All: resolve conflicts
nxqbao Sep 13, 2022
f632aa2
[Slash] Fix setter
nxqbao Sep 13, 2022
11a0b09
[Staking] Cleanup unstake function
nxqbao Sep 13, 2022
42c1ff4
[IntegrationTest] Add test on contract constructor parameters
nxqbao Sep 13, 2022
c017366
[ValidatorSet] Emit events in initializer
nxqbao Sep 13, 2022
8d13d9a
[ValidatorSet] Add maxValidatorNumber getter
nxqbao Sep 13, 2022
8eb5d78
[ValidatorSet] Fix initializer
nxqbao Sep 13, 2022
ac55a6e
chore: remove outdated TODO
nxqbao Sep 14, 2022
7851583
chore: fix typo
nxqbao Sep 14, 2022
a32d104
[Staking] Add setGovernanceAdmin function
nxqbao Sep 14, 2022
f35b5ee
[ValidatorSet] Add setGovernanceAdmin function
nxqbao Sep 14, 2022
2747d95
Add integration test. Fix minor bugs in contracts and other tests. (#5)
nxqbao Sep 14, 2022
d763e04
[Slash] Add governanceAdmin in initializing contract (#9)
nxqbao Sep 14, 2022
b65b5d4
Add `fixture` for integration test. Add `bonusAmount` for `BlockRewar…
nxqbao Sep 14, 2022
28e71aa
Validator set contract excludes under balance validators in updating …
nxqbao Sep 15, 2022
f9a0b3e
Refine structure & optimize gas cost (#12)
ducthotran2010 Sep 15, 2022
4bee32f
Fix getter of governanceAdmin (#13)
nxqbao Sep 15, 2022
7bedd71
Add github action to dpos (#14)
huyngopt1994 Sep 15, 2022
c4f6bd4
Update emitted arguments for the event `SettledRewardUpdated` (#16)
ducthotran2010 Sep 19, 2022
ebf5785
Refine structures & fix TODO in Staking contract
ducthotran2010 Sep 20, 2022
bcfaa97
Adopt RON transfer helper for Ronin Validator set
ducthotran2010 Sep 20, 2022
8283dd9
Add Has contracts
ducthotran2010 Sep 20, 2022
ea5c475
Remove swapping contract & its test cases
ducthotran2010 Sep 20, 2022
71ffd3c
[Staking] Fix staking TODOs & remove candidate struct
ducthotran2010 Sep 20, 2022
b7ed226
[Validator] Add candidate storage
ducthotran2010 Sep 20, 2022
0966227
Adopt changes for the rest
ducthotran2010 Sep 20, 2022
035d327
Work around for proxy admin compile
ducthotran2010 Sep 20, 2022
c93051c
[Validator] Adopt candidates storage
ducthotran2010 Sep 20, 2022
edd0b7b
Fix test compile
ducthotran2010 Sep 21, 2022
3be9d5b
Fix test/integration/ActionSlashValidators.test.ts issue
ducthotran2010 Sep 21, 2022
2156c7c
Skip event issue in test/integration/ActionWrapUpEpoch.test.ts
ducthotran2010 Sep 21, 2022
e3752e9
Fix test/slash/SlashIndicator.test.ts issue
ducthotran2010 Sep 21, 2022
35d5b1d
Fix test/staking/Staking.test.ts issue
ducthotran2010 Sep 21, 2022
17eaa27
Fix test/validator/RoninValidatorSet.test.ts issue
ducthotran2010 Sep 21, 2022
7df7feb
Fix test event
ducthotran2010 Sep 21, 2022
19d784a
Update explaination msg
ducthotran2010 Sep 21, 2022
4259fa0
Update contract comments
ducthotran2010 Sep 21, 2022
cdf9a19
Only delegate to a validator candidate
ducthotran2010 Sep 21, 2022
76b1ad9
Fix comment for test/staking/Staking.test.ts
ducthotran2010 Sep 21, 2022
fee1024
Add comment for contracts/validator/CandidateManager.sol
ducthotran2010 Sep 21, 2022
46f07a3
[Staking/Validator] Refine structures & fix TODO (#18)
ducthotran2010 Sep 21, 2022
55a02b3
Add prioritized validators. Filter prioritized validators when updati…
nxqbao Sep 22, 2022
fc62de3
Add scheduled maintenance contract (#17)
ducthotran2010 Sep 26, 2022
fa7a5cb
Implement double signing slash (#19)
nxqbao Sep 28, 2022
e40d6e5
[Validator] Fix epoch index & order of deleting (#21)
ducthotran2010 Sep 28, 2022
684682a
Add renounce function (#20)
ducthotran2010 Sep 29, 2022
723b014
Add Ronin Trusted Organization contract (#23)
ducthotran2010 Oct 5, 2022
d3e4841
Update method name (#25)
ducthotran2010 Oct 5, 2022
8c3bb33
Fix BlockHeader and precompile call in double sign slashing (#27)
nxqbao Oct 14, 2022
4ed91d5
[Staking] Fix reward calculation (#24)
ducthotran2010 Oct 15, 2022
3036923
Use precompile for sorting validators. Add assembly test of precompil…
nxqbao Oct 17, 2022
75c736d
Add Governance Admin contracts (#29)
ducthotran2010 Oct 18, 2022
871d85a
[CandidateManager] Expose function to get candidate info (#31)
ducthotran2010 Oct 21, 2022
2d51d29
[RewardCalculation] Store period number instead of block number (#33)
ducthotran2010 Oct 24, 2022
3eb0823
Integrate with precompile of picking validator set. Feat bridge opera…
nxqbao Oct 24, 2022
c380c4d
Add Bridge contracts (#30)
ducthotran2010 Oct 24, 2022
b1dcb6b
Fix array issue (#37)
ducthotran2010 Oct 25, 2022
14d566d
Replace method `periodOf` by `currentPeriod`. No longer rescale unava…
ducthotran2010 Oct 25, 2022
7b44d4d
Update bridge reward distribution & add slashing feature for bridge o…
ducthotran2010 Oct 31, 2022
5607fa9
[RoninValidatorSet] Update event & refactor test (#42)
ducthotran2010 Nov 1, 2022
4bfcd66
Fix undelegate from deprecated candidates issue (#41)
ducthotran2010 Nov 1, 2022
98c85b3
Feat/credit score and bail out rebased (#43)
ducthotran2010 Nov 2, 2022
ee6e3eb
[StakingVesting] Request bonus should not be reverted (#45)
nxqbao Nov 2, 2022
2ef51c7
Update comment for contracts/interfaces/IMaintenance.sol
ducthotran2010 Nov 4, 2022
4f089cb
Fix typo in pull request template (#55)
nxqbao Nov 4, 2022
b65905e
Update README.md
ducthotran2010 Nov 4, 2022
a11290d
Update contracts/ronin/staking/StakingManager.sol
ducthotran2010 Nov 4, 2022
9ef4972
[Staking] Update reward calculation mechanism (#44)
ducthotran2010 Nov 4, 2022
4ea5137
Optimize slashing method in contracts/ronin/validator/RoninValidatorS…
ducthotran2010 Nov 4, 2022
603d7f6
Add reserved space for future upgrade
ducthotran2010 Nov 4, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Devnet configs
DEVNET_PK=0x0000000000000000000000000000000000000000000000000000000000000000
DEVNET_URL=http://localhost:8545

# Testnet configs
TESTNET_PK=0x0000000000000000000000000000000000000000000000000000000000000000
TESTNET_URL=https://testnet.skymavis.one/rpc

# Mainnet configs
MAINNET_PK=0x0000000000000000000000000000000000000000000000000000000000000000
MAINNET_URL=https://api.roninchain.com/rpc

# Report gas
REPORT_GAS=false
14 changes: 14 additions & 0 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Run tests
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
uses: axieinfinity/workflows-samples/.github/workflows/testing-node.yml@main
with:
node_version: v14.18.1
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
artifacts/
cache/
node_modules/
src/types/
src/web3-types/
dist/
yarn-error.log
.env
.idea

refs/
.vscode
remix-compiler.config.js
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
7 changes: 7 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

set -ex

yarn lint-staged
yarn compile
21 changes: 21 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": true,
"explicitTypes": "preserve",
"overrides": [
{
"files": "*.sol",
"options": {
"singleQuote": false
}
},
{
"files": "*.{js,ts}",
"options": {
"singleQuote": true
}
}
]
}
130 changes: 129 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,129 @@
# Ronin DPos Contracts
# Ronin DPoS Contracts

The next version of smart contracts that power Ronin DPoS network.

_NOTE: This smart contract version does not includes method to prevent the 51% attack. It is not finalized at the implementation time._
ducthotran2010 marked this conversation as resolved.
Show resolved Hide resolved

- [Overview](#ronin-dpos-contracts)
- [Staking contract](#staking-contract)
- [Validator Candidate](#validator-candidate)
- [Delegator](#delegator)
- [Reward Calculation](#reward-calculation)
- [Validator Contract](#validator-contract)
- [Slashing](#slashing)
- [Contract Interaction flow](#contract-interaction-flow)
- [Development](#development)
- [Requirement](#requirement)
- [Compile & test](#compile---test)
- [Deployment](#deployment)

## Staking contract

An user can propose themselves to be a validator candidate by staking their RON. Other users are allowed to register as delegators by staking any amount of RON to the staking contract, he/she can choose a validator to stake their coins.

The ones on top `N` users with the highest amount of staked coins will become validators.

### Validator Candidate

**Proposing validator**

| Params | Explanation |
| ------------------------ | ----------------------------------------------------------------------------------------------- |
| `uint256 commissionRate` | The rate to share for the validator. Values in range [0; 100_00] stands for [0; 100%] |
| `address consensusAddr` | Address to produce block |
| `address treasuryAddr` | Address to receive block reward |
| `msg.value` | The amount of RON to stake, require to be larger than the minimum RON threshold to be validator |

The validator candidates can deposit or withdraw their funds afterwards as long as the staking balance must be greater than the minimum RON threshold.

**Renounce validator**

The candidates can renounce the validator propose and take back their deposited RON.

### Delegator

The delegator can choose the validator to stake and receive the commission reward:

| Methods | Explanation |
| --------------------------------------------------------- | ---------------------------------------------------------------------------------- |
| `delegate(consensusAddr)` | Stakes `msg.value` amount of RON for a validator `consensusAddr` |
| `undelegate(consensusAddr, amount)` | Unstakes from a validator |
| `redelegate(consensusAddrSrc, consensusAddrDst, amount)` | Unstakes `amount` RON from the `consensusAddrSrc` and stake for `consensusAddrDst` |
| `getRewards(consensusAddrList)` | Returns the pending rewards and the claimable rewards |
| `claimRewards(consensusAddrList)` | Claims all the reward from the validators |
| `delegateRewards(consensusAddrList, consensusAddr)` | Claims all the reward and delegates them to the consensus address |

### Reward Calculation

- Read how the reward is calculated for delegator at [Staking Problem: Reward Calculation](https://skymavis.notion.site/Staking-Problem-Reward-Calculation-bd47bbcefde24bbd8e959bee45dfd4a5).
- See [`RewardCalculation` contract](./contracts/staking/RewardCalculation.sol) for the implementation.

## Validator Contract

The validator contract collects and distributes staking rewards, syncs the validator set from the staking contract at the end of every epoch.

![image](./assets/Validator%20Contract%20Overview.drawio.png)
_Validator contract flow overview_

1. The block producer trigger to the Validator contract to wrap up the epoch.
2. Validator contract counts the validator/delegator rewards to the Staking contract.
3. Validator contract syncs validator set from staking contract.

At the end of period, the validator contract:

4. Transfers the rewards for validators.
5. Resets the slashing counter.

## Slashing

The validators will be slashed when they do not provide the good service for Ronin network.

**Unavailability**

- If a validator missed >= `misdemeanorThreshold` blocks in a day: Cannot claim the reward on that day.

- If a validator missed >= `felonyThreshold` blocks in a day:
- Cannot claim the reward on that day.
- Be slashed `slashFelonyAmount` amount of self-delegated RON.
- Be put in jail for `57600` blocks.

**Double Sign**

- If a validator submit more than 1 block at the same `block.number`:
- Cannot claim the reward.
- Be put in jail for `type(uint256).max` blocks.
- Be slashed `slashDoubleSignAmount` amount of self-delegated RON.

## Contract Interaction flow

Read the contract interaction flow at [DPoS Contract: Interaction Flow](https://skymavis.notion.site/DPoS-Contract-Interaction-Flow-3a535cf9048f46f69dd9a45958ad9b85).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doc is out date


## Development

### Requirement

- Node@>=14 + Solc@^0.8.0

### Compile & test

```shell
$ yarn install
$ yarn compile
$ yarn test
```

## Deployment

- Init the environment variables:

```shell
$ cp .env.example .env && vim .env
```

- Update the contract configuration in [`config.ts`](./src/config.ts) file

- Deploy the contracts:

```shell
$ yarn hardhat deploy --network <ronin-devnet|ronin-mainnet|ronin-testnet>
```
Binary file added assets/Validator Contract Overview.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 98 additions & 0 deletions contracts/extensions/GatewayV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/security/Pausable.sol";
import "../interfaces/IQuorum.sol";
import "./collections/HasProxyAdmin.sol";

abstract contract GatewayV2 is HasProxyAdmin, Pausable, IQuorum {
uint256 internal _num;
uint256 internal _denom;

address private ______deprecated;
uint256 public nonce;

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
*/
uint256[50] private ______gap;

/**
* @dev See {IQuorum-getThreshold}.
*/
function getThreshold() external view virtual returns (uint256, uint256) {
return (_num, _denom);
}

/**
* @dev See {IQuorum-checkThreshold}.
*/
function checkThreshold(uint256 _voteWeight) external view virtual returns (bool) {
return _voteWeight * _denom >= _num * _getTotalWeight();
}

/**
* @dev See {IQuorum-setThreshold}.
*/
function setThreshold(uint256 _numerator, uint256 _denominator)
external
virtual
onlyAdmin
returns (uint256, uint256)
{
return _setThreshold(_numerator, _denominator);
}

/**
* @dev Triggers paused state.
*/
function pause() external onlyAdmin {
_pause();
}

/**
* @dev Triggers unpaused state.
*/
function unpause() external onlyAdmin {
_unpause();
}

/**
* @dev See {IQuorum-minimumVoteWeight}.
*/
function minimumVoteWeight() public view virtual returns (uint256) {
return _minimumVoteWeight(_getTotalWeight());
}

/**
* @dev Sets threshold and returns the old one.
*
* Emits the `ThresholdUpdated` event.
*
*/
function _setThreshold(uint256 _numerator, uint256 _denominator)
internal
virtual
returns (uint256 _previousNum, uint256 _previousDenom)
{
require(_numerator <= _denominator, "GatewayV2: invalid threshold");
_previousNum = _num;
_previousDenom = _denom;
_num = _numerator;
_denom = _denominator;
emit ThresholdUpdated(nonce++, _numerator, _denominator, _previousNum, _previousDenom);
}

/**
* @dev Returns minimum vote weight.
*/
function _minimumVoteWeight(uint256 _totalWeight) internal view virtual returns (uint256) {
return (_num * _totalWeight + _denom - 1) / _denom;
}

/**
* @dev Returns the total weight.
*/
function _getTotalWeight() internal view virtual returns (uint256);
}
Loading