Skip to content

Commit

Permalink
Deployment script (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
seunlanlege authored Aug 30, 2023
1 parent bc82294 commit 411ae5a
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ out/
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

broadcast/
# Dotenv file
.env

Expand Down
3 changes: 3 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
src = 'src'
out = 'out'
libs = ['lib']

[rpc_endpoints]
goerli = "${GOERLI_RPC_URL}"
69 changes: 69 additions & 0 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Script.sol";
import "../src/HandlerV1.sol";
import "../src/EvmHost.sol";
import "../test/TestHost.sol";
import "../src/modules/CrossChainGovernor.sol";
import "../src/beefy/BeefyV1.sol";

contract DeployScript is Script {
address public deployer = 0x4e59b44847b379578588920cA78FbF26c0B4956C;
bytes32 public salt = 0xc023405e9aa0e9ea794732e4076a4f5baca75496cfc2179e94829661ba6396d8;
address public admin = 0x123463a4B065722E99115D6c222f267d9cABb524;
uint256 public paraId = 2021;

function run() external {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);

BeefyV1 c = new BeefyV1{salt: salt}(paraId);
address consensusClient = getAddress(type(BeefyV1).creationCode, abi.encode(paraId));
console.logAddress(consensusClient);
assert(consensusClient == address(c));

HandlerV1 hv1 = new HandlerV1{salt: salt}();
address handler = getAddress(type(HandlerV1).creationCode, new bytes(0));
console.logAddress(handler);
assert(handler == address(hv1));

GovernorParams memory gParams = GovernorParams(admin, address(0), paraId);
CrossChainGovernor g = new CrossChainGovernor{salt: salt}(gParams);
address governor = getAddress(type(CrossChainGovernor).creationCode, abi.encode(gParams));
console.logAddress(governor);
assert(governor == address(g));

HostParams memory params = HostParams({
admin: admin,
crosschainGovernor: governor,
handler: handler,
defaultTimeout: 5000,
unStakingPeriod: 5000,
// for this test
challengePeriod: 0,
consensusClient: consensusClient,
lastUpdated: 0,
consensusState: new bytes(0)
});
TestHost h = new TestHost{salt: salt}(params);
address host = getAddress(type(TestHost).creationCode, abi.encode(params));
console.logAddress(host);
assert(host == address(h));

// set the ismphost on the cross-chain governor
g.setIsmpHost(host);

vm.stopBroadcast();
}

function getAddress(bytes memory code, bytes memory init) public returns (address) {
return address(
uint160(
uint256(
keccak256(abi.encodePacked(bytes1(0xff), deployer, salt, keccak256(abi.encodePacked(code, init))))
)
)
);
}
}
19 changes: 19 additions & 0 deletions script/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# How to deploy

Ensure you have a local beacon chain testnet running, see [polytope-labs/eth-pos-devnet](https://github.com/polytope-labs/eth-pos-devnet).

Fill out an `.env` file at the root of this repo with the given contents.

```dotenv
GOERLI_RPC_URL=ws://127.0.0.1:8545
PRIVATE_KEY=2e0834786285daccd064ca17f1654f67b4aef298acbb82cef9ec422fb4975622
ETHERSCAN_API_KEY=
```

The given private key is for the prefunded `0x123463a4B065722E99115D6c222f267d9cABb524` account in the devnet.

Run the command below to deploy

```shell
forge script script/Deploy.s.sol:DeployScript --rpc-url http://127.0.0.1:8545 --broadcast --sender=0x123463a4b065722e99115d6c222f267d9cabb524
```
32 changes: 24 additions & 8 deletions src/modules/CrossChainGovernor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,40 @@ import "../interfaces/IIsmpModule.sol";
import "../interfaces/IIsmpHost.sol";
import "../interfaces/StateMachine.sol";

struct GovernorParams {
address admin;
address host;
uint256 paraId;
}

contract CrossChainGovernor is IIsmpModule {
using Bytes for bytes;

address private _host;
uint256 private _paraId;
GovernorParams private _params;

modifier onlyIsmpHost() {
require(msg.sender == _host, "CrossChainGovernance: Invalid caller");
require(msg.sender == _params.host, "CrossChainGovernor: Invalid caller");
_;
}

constructor(address host, uint256 paraId) {
_host = host;
_paraId = paraId;
modifier onlyAdmin() {
require(msg.sender == _params.admin, "CrossChainGovernor: Invalid caller");
_;
}

constructor(GovernorParams memory params) {
_params = params;
}

// This function can only be called once by the admin to set the IsmpHost.
// This exists to seal the cyclic dependency between this contract & the ismp host.
function setIsmpHost(address host) public onlyAdmin {
_params.host = host;
_params.admin = address(0);
}

function onAccept(PostRequest memory request) external onlyIsmpHost {
require(request.source.equals(StateMachine.polkadot(_paraId)), "Unauthorized request");
require(request.source.equals(StateMachine.polkadot(_params.paraId)), "Unauthorized request");
(
address admin,
address consensus,
Expand All @@ -37,7 +53,7 @@ contract CrossChainGovernor is IIsmpModule {
BridgeParams memory params =
BridgeParams(admin, consensus, handler, challengePeriod, unstakingPeriod, defaultTimeout);

IIsmpHost(_host).setBridgeParams(params);
IIsmpHost(_params.host).setBridgeParams(params);
}

function onPostResponse(PostResponse memory response) external pure {
Expand Down

0 comments on commit 411ae5a

Please sign in to comment.