Skip to content

Commit

Permalink
Merge branch 'main' into init-staking
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk authored Mar 14, 2024
2 parents 3690bef + 0babe1b commit e39180f
Show file tree
Hide file tree
Showing 97 changed files with 2,136 additions and 349 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/auto-deploy-contracts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ jobs:
RPC_URL: https://calibration.filfox.info/rpc/v1
PRIVATE_KEY: ${{ secrets.CONTRACTS_DEPLOYER_PRIVATE_KEY }}
steps:
- name: Checkout cd/contract branch
- name: Checkout cd/contracts branch
uses: actions/checkout@v4
with:
ref: cd/contracts
submodules: recursive
fetch-depth: 0
token: ${{ secrets.WORKFLOW_PAT_JIE }}

- name: (Dry run) Try merge from main branch to see if there's any conflicts that can't be resolved itself
run: |
Expand Down Expand Up @@ -86,12 +87,13 @@ jobs:
echo "gateway_address: $gateway_address"
echo "registry_address: $registry_address"
- name: Switch code repo to cd/contract branch
- name: Switch code repo to cd/contracts branch
uses: actions/checkout@v4
with:
ref: cd/contracts
submodules: recursive
fetch-depth: 0
token: ${{ secrets.WORKFLOW_PAT_JIE }}

- name: Merge from main branch and update cd/contracts branch
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/contracts-sast.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
run: cargo install aderyn

- name: Run aderyn
run: cd contracts && aderyn ./
run: cd contracts && aderyn ./ -o report.json

- name: Check results
run: cd contracts && ./tools/check_aderyn.sh
Expand Down
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ bollard = "0.15"
blake2b_simd = "1.0"
bloom = "0.3"
bytes = "1.4"
clap = { version = "4.1", features = ["derive", "env"] }
clap = { version = "4.1", features = ["derive", "env", "string"] }
byteorder = "1.5.0"
config = "0.13"
dirs = "5.0"
dircpy = "0.3"
Expand Down
1 change: 1 addition & 0 deletions contracts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ lcov.info

# Aderyn scanner
report.md
report.json

#vim
*.un~
4 changes: 4 additions & 0 deletions contracts/audit-resolve.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
"1096544|json5": {
"decision": "ignore",
"madeAt": 1708963077846
},
"1096644|browserify-sign": {
"decision": "ignore",
"madeAt": 1710415772020
}
},
"rules": {},
Expand Down
1 change: 1 addition & 0 deletions contracts/scripts/deploy-gateway.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export async function deploy(libs: { [key in string]: string }) {
libs: xnetMessagingFacetLibs,
},
{ name: 'TopDownFinalityFacet', libs: topDownFinalityFacetLibs },
{ name: 'OwnershipFacet', libs: {} },
]

for (const facet of facets) {
Expand Down
1 change: 1 addition & 0 deletions contracts/scripts/deploy-registry.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export async function deploy() {
{ name: 'SubnetGetterFacet', libs: {} },
{ name: 'DiamondLoupeFacet', libs: {} },
{ name: 'DiamondCutFacet', libs: {} },
{ name: 'OwnershipFacet', libs: {} },
]

for (const facet of facets) {
Expand Down
1 change: 1 addition & 0 deletions contracts/scripts/deploy-sa-diamond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ async function deploySubnetActorDiamond(
{ name: 'SubnetActorRewardFacet', libs: rewarderFacetLibs },
{ name: 'SubnetActorCheckpointingFacet', libs: checkpointerFacetLibs },
{ name: 'SubnetActorPauseFacet', libs: pauserFacetLibs },
{ name: 'OwnershipFacet', libs: {} },
]
// The `facetCuts` variable is the FacetCut[] that contains the functions to add during diamond deployment
const facetCuts = []
Expand Down
1 change: 1 addition & 0 deletions contracts/scripts/python/build_selector_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def main():
'src/GatewayDiamond.sol',
'src/SubnetActorDiamond.sol',
'src/SubnetRegistryDiamond.sol',
'src/OwnershipFacet.sol',
'src/diamond/DiamondCutFacet.sol',
'src/diamond/DiamondLoupeFacet.sol',
'src/gateway/GatewayGetterFacet.sol',
Expand Down
18 changes: 11 additions & 7 deletions contracts/scripts/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const fs = require('fs')

export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'

const isolatedPort = 18678

export async function deployContractWithDeployer(
deployer: SignerWithAddress,
contractName: string,
Expand Down Expand Up @@ -89,9 +91,11 @@ export async function getFacets(diamondAddress: string): Promise<FacetMap> {
async function startGanache() {
return new Promise((resolve, reject) => {
const server = ganache.server({
gasPrice: '0x0', // Set gas price to 0
miner: { defaultGasPrice: '0x0' },
chain: { hardfork: 'berlin' },
logging: { quiet: true },
})
server.listen(8545, (err) => {
server.listen(isolatedPort, (err) => {
if (err) reject(err)
else resolve(server)
})
Expand All @@ -114,16 +118,16 @@ export async function getRuntimeBytecode(bytecode) {
}
const ganacheServer = await startGanache()

const provider = new providers.JsonRpcProvider('http://127.0.0.1:8545')
const provider = new providers.JsonRpcProvider(
`http://127.0.0.1:${isolatedPort}`,
)
const wallet = new Wallet(process.env.PRIVATE_KEY, provider)
const contractFactory = new ContractFactory([], bytecode, wallet)
const contract = await contractFactory.deploy()
const contract = await contractFactory.deploy({ gasPrice: 0 })
await contract.deployed()

const runtimeBytecode = await provider.getCode(contract.address)

await stopGanache(ganacheServer)

stopGanache(ganacheServer)
return runtimeBytecode
}

Expand Down
14 changes: 14 additions & 0 deletions contracts/src/OwnershipFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {LibDiamond} from "./lib/LibDiamond.sol";

contract OwnershipFacet {
function transferOwnership(address _newOwner) external {
LibDiamond.transferOwnership(_newOwner);
}

function owner() external view returns (address owner_) {
owner_ = LibDiamond.contractOwner();
}
}
5 changes: 2 additions & 3 deletions contracts/src/SubnetActorDiamond.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {SubnetIDHelper} from "./lib/SubnetIDHelper.sol";
import {LibStaking} from "./lib/LibStaking.sol";
import {IERC20} from "openzeppelin-contracts/token/ERC20/IERC20.sol";
import {SupplySourceHelper} from "./lib/SupplySourceHelper.sol";

error FunctionNotFound(bytes4 _functionSelector);

contract SubnetActorDiamond {
Expand All @@ -38,7 +37,7 @@ contract SubnetActorDiamond {
SubnetID parentId;
}

constructor(IDiamond.FacetCut[] memory _diamondCut, ConstructorParams memory params) {
constructor(IDiamond.FacetCut[] memory _diamondCut, ConstructorParams memory params, address owner) {
if (params.ipcGatewayAddr == address(0)) {
revert GatewayCannotBeZero();
}
Expand All @@ -58,7 +57,7 @@ contract SubnetActorDiamond {

params.supplySource.validate();

LibDiamond.setContractOwner(msg.sender);
LibDiamond.setContractOwner(owner);
LibDiamond.diamondCut({_diamondCut: _diamondCut, _init: address(0), _calldata: new bytes(0)});

LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
Expand Down
1 change: 1 addition & 0 deletions contracts/src/SubnetRegistryDiamond.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {IERC165} from "./interfaces/IERC165.sol";
import {SubnetRegistryActorStorage} from "./lib/LibSubnetRegistryStorage.sol";
import {GatewayCannotBeZero, FacetCannotBeZero} from "./errors/IPCErrors.sol";
import {LibDiamond} from "./lib/LibDiamond.sol";

error FunctionNotFound(bytes4 _functionSelector);

contract SubnetRegistryDiamond {
Expand Down
17 changes: 17 additions & 0 deletions contracts/src/lib/LibDiamond.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {IDiamond} from "../interfaces/IDiamond.sol";
library LibDiamond {
bytes32 public constant DIAMOND_STORAGE_POSITION = keccak256("libdiamond.lib.diamond.storage");

error InvalidAddress();
error NotOwner();
error NoBytecodeAtAddress(address _contractAddress, string _message);
error IncorrectFacetCutAction(IDiamondCut.FacetCutAction _action);
Expand All @@ -24,6 +25,8 @@ library LibDiamond {
error CannotRemoveFunctionThatDoesNotExist(bytes4 _selector);
error CannotRemoveImmutableFunction(bytes4 _selector);

event OwnershipTransferred(address oldOwner, address newOwner);

struct FacetAddressAndSelectorPosition {
address facetAddress;
uint16 selectorPosition;
Expand All @@ -38,6 +41,17 @@ library LibDiamond {
address contractOwner;
}

/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) internal onlyOwner {
if (newOwner == address(0)) {
revert InvalidAddress();
}
setContractOwner(newOwner);
}

function diamondStorage() internal pure returns (DiamondStorage storage ds) {
bytes32 position = DIAMOND_STORAGE_POSITION;
assembly {
Expand All @@ -47,7 +61,10 @@ library LibDiamond {

function setContractOwner(address _newOwner) internal {
DiamondStorage storage ds = diamondStorage();

address oldOwner = ds.contractOwner;
ds.contractOwner = _newOwner;
emit OwnershipTransferred(oldOwner, _newOwner);
}

function contractOwner() internal view returns (address contractOwner_) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/subnetregistry/RegisterSubnetFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ contract RegisterSubnetFacet is ReentrancyGuard {
});

// slither-disable-next-line reentrancy-benign
subnetAddr = address(new SubnetActorDiamond(diamondCut, _params));
subnetAddr = address(new SubnetActorDiamond(diamondCut, _params, msg.sender));

//nonces start with 1, similar to eip 161
++s.userNonces[msg.sender];
Expand Down
Loading

0 comments on commit e39180f

Please sign in to comment.