Skip to content

Commit

Permalink
Audit part2 (#27)
Browse files Browse the repository at this point in the history
* Audit part2

* clean

* fix tests

* deploy & verify

* fix ci

* add log and inf

* fix loss of precision in votes

* fmt

* verify

* deposit nft metadata

* fix

* fix

* defined collator sorting order

* redeploy

* fix ci

* fix tests
  • Loading branch information
hujw77 authored Sep 23, 2024
1 parent 2249e3b commit ec63535
Show file tree
Hide file tree
Showing 18 changed files with 273 additions and 131 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ fmt :; @forge fmt
clean :; @forge clean
test :; @forge test
dry-run:; @forge script script/Deploy.s.sol:DeployScript
deploy :; @forge script script/Deploy.s.sol:DeployScript --broadcast --verify
deploy :; @forge script script/Deploy.s.sol:DeployScript --broadcast --verify --verifier blockscout
migrate:; @forge script script/Migrate.s.sol:MigrateScript --broadcast --verify --verifier blockscout

sync :; @git submodule update --recursive

Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

## Deployments
```sh
Depoist: 0x7FAcDaFB282028E4B3264fB08cd633A9142514df
Depoist_Logic: 0x545391AA3cd0f8795000D790dE2734822c255305
gRING: 0x87BD07263D0Ed5687407B80FEB16F2E32C2BA44f
gRING_Logic: 0x6d275b13bD83B08BF203155AeC1319FCC81d83b9
Hub: 0x279a1aaDb6eC9d213350f95C3Da1A9580FB3326B
Hub_Logic: 0xA6331C23f12c8Fb19ED427e94044156D45A432AF
Depoist: 0x46275d29113f065c2aac262f34C7a3d8a8B7377D
gRING: 0xdafa555e2785DC8834F4Ea9D1ED88B6049142999
Hub: 0xa4fFAC7A5Da311D724eD47393848f694Baee7930
```


8 changes: 5 additions & 3 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
src = "src"
out = "out"
libs = ["lib"]
sender = "0x0f14341A7f464320319025540E8Fe48Ad0fe5aec"
sender = "0x5d3c814F82Ef2b22101635f8C6A3e7C8E09E7DD6"
force = true
ffi = true
ast = true
Expand All @@ -15,13 +15,15 @@ bytecode_hash = "ipfs"
extra_output = ["storageLayout"]
extra_output_files = ["metadata"]
fs_permissions = [{ access = "read", path = "out" }]
eth_rpc_url = "https://koi-rpc.darwinia.network"
etherscan_api_key = "xxx"
# eth_rpc_url = "https://rpc.darwinia.network"
# etherscan_api_key = "xxx"

[rpc_endpoints]
koi = "https://koi-rpc.darwinia.network"
crab = "https://crab-rpc.darwinia.network"
darwinia = "https://rpc.darwinia.network"

[etherscan]
koi = { key = "xxx", url = "https://koi-scan.darwinia.network/api", chain = 701 }
crab = { key = "xxx", url = "https://crab-scan.darwinia.network/api", chain = 44 }
darwinia = { key = "xxx", url = "https://explorer.darwinia.network/api", chain = 46 }
5 changes: 5 additions & 0 deletions ipfs/deposit/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Deposit NFT",
"description": "Deposit NFT is generated through Deposit contract collateralized RING, It is a voucher for Deposited RING.",
"image": "ipfs://bafybeih57kauz6npkbafh2x3cv3hyljcakdccuckb4huwnwxxxrtqr5pqe"
}
47 changes: 41 additions & 6 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,42 @@ import {Deposit} from "../src/deposit/Deposit.sol";
import {GovernanceRing} from "../src/governance/GovernanceRing.sol";

contract DeployScript is Script {
address deployer = 0x0f14341A7f464320319025540E8Fe48Ad0fe5aec;
address deposit = 0x7FAcDaFB282028E4B3264fB08cd633A9142514df;
address gRING = 0x87BD07263D0Ed5687407B80FEB16F2E32C2BA44f;
address hub = 0x279a1aaDb6eC9d213350f95C3Da1A9580FB3326B;
address deployer = 0x5d3c814F82Ef2b22101635f8C6A3e7C8E09E7DD6;
address deposit = 0x46275d29113f065c2aac262f34C7a3d8a8B7377D;
address gRING = 0xdafa555e2785DC8834F4Ea9D1ED88B6049142999;
address hub = 0xa4fFAC7A5Da311D724eD47393848f694Baee7930;

struct Settings {
string depositName;
string depositSymbol;
string gringName;
string gringSymbol;
}

function getSettings(uint256 chainId) public pure returns (Settings memory) {
if (chainId == 701) {
return Settings({
depositName: "KRING Deposit NFT",
depositSymbol: "KDPS",
gringName: "Governance KRING",
gringSymbol: "gKRING"
});
} else if (chainId == 44) {
return Settings({
depositName: "CRAB Deposit NFT",
depositSymbol: "CDPS",
gringName: "Governance CRAB",
gringSymbol: "gCRAB"
});
} else if (chainId == 46) {
return Settings({
depositName: "RING Deposit NFT",
depositSymbol: "RDPS",
gringName: "Governance RING",
gringSymbol: "gRING"
});
}
}

function setUp() public {}

Expand All @@ -23,16 +55,19 @@ contract DeployScript is Script {

require(msg.sender == deployer, "!deployer");

safeconsole.log("Chain Id: ", block.chainid);
Settings memory s = getSettings(block.chainid);

address deposit_PROXY = Upgrades.deployTransparentProxy(
"Deposit.sol:Deposit", deployer, abi.encodeCall(Deposit.initialize, ("RING Deposit NFT", "RDPS"))
"Deposit.sol:Deposit", deployer, abi.encodeCall(Deposit.initialize, (s.depositName, s.depositSymbol))
);
safeconsole.log("Depoist: ", deposit_PROXY);
safeconsole.log("Depoist_Logic: ", Upgrades.getImplementationAddress(deposit_PROXY));

address gRING_PROXY = Upgrades.deployTransparentProxy(
"GovernanceRing.sol:GovernanceRing",
deployer,
abi.encodeCall(GovernanceRing.initialize, (deployer, hub, deposit, "Governance RING", "gRING"))
abi.encodeCall(GovernanceRing.initialize, (deployer, hub, deposit, s.gringName, s.gringSymbol))
);
safeconsole.log("gRING: ", gRING_PROXY);
safeconsole.log("gRING_Logic: ", Upgrades.getImplementationAddress(gRING_PROXY));
Expand Down
26 changes: 26 additions & 0 deletions script/Migrate.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {Script} from "forge-std/Script.sol";
import {safeconsole} from "forge-std/safeconsole.sol";
import {Upgrades} from "openzeppelin-foundry-upgrades/Upgrades.sol";
import {Core} from "openzeppelin-foundry-upgrades/internal/Core.sol";

import {CollatorStakingHub} from "../src/collator/CollatorStakingHub.sol";
import {Deposit} from "../src/deposit/Deposit.sol";

contract MigrateScript is Script {
address proxy = 0xDeC9cD45e921F2AedE72f694743265af37d47Fa7;

function run() public {
vm.startBroadcast();

// address logic = address(new CollatorStakingHub());
address logic = address(new Deposit());
// Core.upgradeProxyTo(proxy, logic, "");
// require(logic == Upgrades.getImplementationAddress(proxy));
safeconsole.log("logic: ", logic);

vm.stopBroadcast();
}
}
24 changes: 9 additions & 15 deletions src/collator/CollatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "./CollatorStakingHubStorage.sol";

abstract contract CollatorSet is Initializable, CollatorStakingHubStorage {
address internal constant HEAD = address(0x1);
address internal constant TAIL = address(0x2);
address internal constant HEAD = address(0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF);
address internal constant TAIL = address(0x1);

event AddCollator(address indexed cur, uint256 votes, address prev);
event RemoveCollator(address indexed cur, address prev);
Expand Down Expand Up @@ -47,7 +47,7 @@ abstract contract CollatorSet is Initializable, CollatorStakingHubStorage {
require(collators[cur] == address(0), "!cur");
// Next collator must in the list.
require(next != address(0), "!prev");
require(_verifyIndex(prev, votes, next), "!votes");
require(_verifyIndex(prev, votes, next, cur), "!votes");
collators[cur] = next;
collators[prev] = cur;
votesOf[cur] = votes;
Expand All @@ -66,22 +66,14 @@ abstract contract CollatorSet is Initializable, CollatorStakingHubStorage {
emit RemoveCollator(cur, prev);
}

function _increaseVotes(address cur, uint256 votes, address oldPrev, address newPrev) internal {
_updateVotes(cur, votesOf[cur] + votes, oldPrev, newPrev);
}

function _reduceVotes(address cur, uint256 votes, address oldPrev, address newPrev) internal {
_updateVotes(cur, votesOf[cur] - votes, oldPrev, newPrev);
}

function _updateVotes(address cur, uint256 newVotes, address oldPrev, address newPrev) internal {
require(_isValid(cur), "!valid");
require(collators[cur] != address(0), "!cur");
require(collators[oldPrev] != address(0), "!oldPrev1");
require(collators[newPrev] != address(0), "!newPrev");
if (oldPrev == newPrev) {
require(_isPrevCollator(cur, oldPrev), "!oldPrev2");
require(_verifyIndex(newPrev, newVotes, collators[cur]), "!votes");
require(_verifyIndex(newPrev, newVotes, collators[cur], cur), "!votes");
votesOf[cur] = newVotes;
} else {
_removeCollator(cur, oldPrev);
Expand All @@ -90,9 +82,11 @@ abstract contract CollatorSet is Initializable, CollatorStakingHubStorage {
emit UpdateCollator(cur, newVotes, oldPrev, newPrev);
}

// prev >= cur >= next
function _verifyIndex(address prev, uint256 newValue, address next) internal view returns (bool) {
return votesOf[prev] >= newValue && newValue >= votesOf[next];
// 1. votes(prev) >= votes(cur) >= votes(next)
// 2. addr(prev) > addr(cur) > addr(next)
function _verifyIndex(address prev, uint256 newValue, address next, address cur) internal view returns (bool) {
return ((votesOf[prev] > newValue) || (votesOf[prev] == newValue && prev > cur))
&& ((newValue > votesOf[next]) || (newValue == votesOf[next] && cur > next));
}

function _isPrevCollator(address c, address prev) internal view returns (bool) {
Expand Down
Loading

0 comments on commit ec63535

Please sign in to comment.