Skip to content

Commit

Permalink
Split SubnetActor (#411)
Browse files Browse the repository at this point in the history
Split Subnet Actor manager
  • Loading branch information
dnkolegov authored Dec 21, 2023
1 parent 97fb4ea commit 2531117
Show file tree
Hide file tree
Showing 19 changed files with 540 additions and 338 deletions.
34 changes: 31 additions & 3 deletions scripts/deploy-registry.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,36 @@ export async function deploy() {
const managerFacet = await deployContractWithDeployer(
deployer,
'SubnetActorManagerFacet',
{
LibStaking: LIBMAP['LibStaking'],
},
{},
txArgs,
)
const managerSelectors = getSelectors(managerFacet)
// console.log("manager address:", managerFacet.address);

const pauserFacet = await deployContractWithDeployer(
deployer,
'SubnetActorPauseFacet',
{},
txArgs,
)
const pauserSelectors = getSelectors(pauserFacet)

const rewarderFacet = await deployContractWithDeployer(
deployer,
'SubnetActorRewardFacet',
{ LibStaking: LIBMAP['LibStaking'] },
txArgs,
)
const rewarderSelectors = getSelectors(rewarderFacet)

const checkpointerFacet = await deployContractWithDeployer(
deployer,
'SubnetActorCheckpointingFacet',
{},
txArgs,
)
const checkpointerSelectors = getSelectors(checkpointerFacet)

//deploy subnet registry diamond
const registry = await ethers.getContractFactory('SubnetRegistryDiamond', {
signer: deployer,
Expand All @@ -48,8 +70,14 @@ export async function deploy() {
gateway: gatewayAddress,
getterFacet: getterFacet.address,
managerFacet: managerFacet.address,
rewarderFacet: rewarderFacet.address,
pauserFacet: pauserFacet.address,
checkpointerFacet: checkpointerFacet.address,
subnetGetterSelectors: getterSelectors,
subnetManagerSelectors: managerSelectors,
checkpointerSelectors: checkpointerSelectors,
pauserSelectors: pauserSelectors,
rewarderSelectors: rewarderSelectors,
}

const facetCuts = [] //TODO
Expand Down
13 changes: 10 additions & 3 deletions scripts/deploy-sa-diamond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@ async function deploySubnetActorDiamond(
SubnetIDHelper: libs['SubnetIDHelper'],
}

const managerFacetLibs: Libraries = {
LibStaking: libs['LibStaking'],
}
const managerFacetLibs: Libraries = {}

const rewarderFacetLibs: Libraries = {}

const pauserFacetLibs: Libraries = {}

const checkpointerFacetLibs: Libraries = {}

const facets = [
{ name: 'DiamondLoupeFacet', libs: {} },
{ name: 'DiamondCutFacet', libs: {} },
{ name: 'SubnetActorGetterFacet', libs: getterFacetLibs },
{ name: 'SubnetActorManagerFacet', libs: managerFacetLibs },
{ name: 'SubnetActorRewardFacet', libs: rewarderFacetLibs },
{ name: 'SubnetActorCheckpointingFacet', libs: checkpointerFacetLibs },
{ name: 'SubnetActorPauseFacet', libs: pauserFacetLibs },
]
// The `facetCuts` variable is the FacetCut[] that contains the functions to add during diamond deployment
const facetCuts = []
Expand Down
5 changes: 4 additions & 1 deletion scripts/python/build_selector_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,17 @@ def main():
'src/gateway/router/XnetMessagingFacet.sol',
'src/subnet/SubnetActorGetterFacet.sol',
'src/subnet/SubnetActorManagerFacet.sol',
'src/subnet/SubnetActorPauseFacet.sol',
'src/subnet/SubnetActorRewardFacet.sol',
'src/subnet/SubnetActorCheckpointingFacet.sol',
'src/subnetregistry/RegisterSubnetFacet.sol',
'src/subnetregistry/SubnetGetterFacet.sol',
'test/helpers/ERC20PresetFixedSupply.sol',
'test/helpers/NumberContractFacetEight.sol',
'test/helpers/NumberContractFacetSeven.sol',
'test/helpers/SelectorLibrary.sol',
'test/helpers/TestUtils.sol',
'test/mocks/SubnetActorManagerFacetMock.sol',
'test/mocks/SubnetActorMock.sol',
]

for filepath in filepaths_to_target:
Expand Down
4 changes: 2 additions & 2 deletions src/gateway/router/BottomUpRouterFacet.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity 0.8.19;

import {ISubnetActor} from "../../interfaces/ISubnetActor.sol";
import {IRelayerRewardDistributor} from "../../interfaces/ISubnetActor.sol";
import {GatewayActorModifiers} from "../../lib/LibGatewayActorStorage.sol";
import {BottomUpMsgBatch} from "../../structs/CrossNet.sol";
import {LibGateway} from "../../lib/LibGateway.sol";
Expand Down Expand Up @@ -73,7 +73,7 @@ contract BottomUpRouterFacet is GatewayActorModifiers {
Address.functionCallWithValue({
target: msg.sender,
data: abi.encodeCall(
ISubnetActor.distributeRewardToRelayers,
IRelayerRewardDistributor.distributeRewardToRelayers,
(block.number, totalFee, QuorumObjKind.Checkpoint)
),
value: totalFee
Expand Down
4 changes: 2 additions & 2 deletions src/gateway/router/CheckpointingFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {LibQuorum} from "../../lib/LibQuorum.sol";
import {Subnet} from "../../structs/Subnet.sol";
import {QuorumObjKind} from "../../structs/Quorum.sol";
import {Address} from "openzeppelin-contracts/utils/Address.sol";
import {ISubnetActor} from "../../interfaces/ISubnetActor.sol";
import {IRelayerRewardDistributor} from "../../interfaces/ISubnetActor.sol";

import {InvalidBatchSource, NotEnoughBalance, MaxMsgsPerBatchExceeded, BatchWithNoMessages, InvalidCheckpointSource, InvalidCrossMsgNonce, InvalidCrossMsgDstSubnet, CheckpointAlreadyExists} from "../../errors/IPCErrors.sol";
import {NotRegisteredSubnet, SubnetNotActive, SubnetNotFound, InvalidSubnet, CheckpointNotCreated} from "../../errors/IPCErrors.sol";
Expand Down Expand Up @@ -42,7 +42,7 @@ contract CheckpointingFacet is GatewayActorModifiers {
Address.functionCallWithValue({
target: msg.sender,
data: abi.encodeCall(
ISubnetActor.distributeRewardToRelayers,
IRelayerRewardDistributor.distributeRewardToRelayers,
(checkpoint.blockHeight, 0, QuorumObjKind.Checkpoint)
),
value: 0
Expand Down
42 changes: 1 addition & 41 deletions src/interfaces/ISubnetActor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,7 @@ pragma solidity 0.8.19;
import {BottomUpCheckpoint} from "../structs/CrossNet.sol";
import {QuorumObjKind} from "../structs/Quorum.sol";

/// @title Subnet Actor interface
/// @author LimeChain team
interface ISubnetActor {
/// Called by peers looking to join a subnet.
///
/// It implements the basic logic to onboard new peers to the subnet.
function join(bytes calldata metadata) external payable;

/// Called by peers looking to leave a subnet.
function leave() external;

/// Method that allows a validator to increase their stake
function stake() external payable;

/// Method that allows to pre-fund an address in the subnet before it bootstraps.
function preFund() external payable;

/// Method that allows to recover initial balance for an address from a subnet that hasn't bootstrapped yet.
function preRelease(uint256 amount) external;

/// Method that allows a validator to unstake their collateral from a subnet
function unstake(uint256 amount) external;

/// Unregister the subnet from the hierarchy, making it no longer discoverable.
function kill() external;

/// Validator claims released collateral
function claim() external;

/// Relayer claims a reward
function claimRewardForRelayer() external;

/// Executes the checkpoint if it is valid.
/// It triggers the commitment of the checkpoint, the execution of related cross-net messages,
/// and any other side-effects that need to be triggered by the checkpoint such as relayer reward book keeping.
function submitCheckpoint(
BottomUpCheckpoint calldata checkpoint,
address[] calldata signatories,
bytes[] calldata signatures
) external;

interface IRelayerRewardDistributor {
/// reward the relayers for processing checkpoint at height `height`.
/// The reword includes the fixed reward for a relayer defined in the contract and `amount` of fees from the cross-messages.
function distributeRewardToRelayers(uint256 height, uint256 amount, QuorumObjKind kind) external payable;
Expand Down
33 changes: 23 additions & 10 deletions src/lib/LibPausable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract contract Pausable {
event Paused(address account);

/**
* @dev Emitted when the pause is lifted by `account`.
* @dev Emitted when the unpause is triggered by `account`.
*/
event Unpaused(address account);

Expand All @@ -42,26 +42,38 @@ abstract contract Pausable {
_;
}

/**
* @dev Modifier to make a function callable only when the contract is paused.
*
* Requirements:
*
* - The contract must be paused.
*/
modifier whenPaused() {
_requirePaused();
_;
}

/**
* @dev Throws if the contract is paused.
*/
function _requireNotPaused() internal view virtual {
if (paused()) {
function _requireNotPaused() internal view {
if (_paused()) {
revert EnforcedPause();
}
}

/**
* @dev Throws if the contract is not paused.
*/
function _requirePaused() internal view virtual {
if (!paused()) {
function _requirePaused() internal view {
if (!_paused()) {
revert ExpectedPause();
}
}

/// @notice sets if to pause the contract
function paused() public view returns(bool) {
/// @notice returns true if the contract is paused
function _paused() internal view returns(bool) {
PausableStorage storage s = pausableStorage();
return s.paused;
}
Expand All @@ -73,10 +85,11 @@ abstract contract Pausable {
*
* - The contract must not be paused.
*/
function _pause() internal whenNotPaused {
function _pause() internal {
_requireNotPaused();
PausableStorage storage s = pausableStorage();
s.paused = true;
emit Unpaused(msg.sender);
emit Paused(msg.sender);
}

/**
Expand All @@ -86,7 +99,7 @@ abstract contract Pausable {
*
* - The contract must be paused.
*/
function _unpause() internal {
function _unpause() internal {
_requirePaused();
PausableStorage storage s = pausableStorage();
s.paused = false;
Expand Down
Loading

0 comments on commit 2531117

Please sign in to comment.