-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## tl;dr - Adds smart contracts to repo - Sets up Foundry in the repo - Adds tests for smart contracts - Sets up `abigen` for generating code out of smart contracts ## Warning You will need to run `./dev/up` to generate some code and start the blockchain node You may also have to run `git submodule update --init --recursive` to pull down the submodules.
- Loading branch information
Showing
28 changed files
with
1,380 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ bin/ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
build/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[submodule "lib/forge-std"] | ||
path = contracts/lib/forge-std | ||
url = https://github.com/foundry-rs/forge-std | ||
[submodule "lib/openzeppelin-contracts"] | ||
path = contracts/lib/openzeppelin-contracts | ||
url = https://github.com/openzeppelin/openzeppelin-contracts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Compiler files | ||
cache/ | ||
out/ | ||
|
||
# Ignores development broadcast logs | ||
!/broadcast | ||
/broadcast/*/31337/ | ||
/broadcast/**/dry-run/ | ||
|
||
# Docs | ||
docs/ | ||
|
||
# Dotenv file | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
## Foundry | ||
|
||
**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.** | ||
|
||
Foundry consists of: | ||
|
||
- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools). | ||
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data. | ||
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network. | ||
- **Chisel**: Fast, utilitarian, and verbose solidity REPL. | ||
|
||
## Documentation | ||
|
||
https://book.getfoundry.sh/ | ||
|
||
## Usage | ||
|
||
### Build | ||
|
||
```shell | ||
$ forge build | ||
``` | ||
|
||
### Test | ||
|
||
```shell | ||
$ forge test | ||
``` | ||
|
||
### Format | ||
|
||
```shell | ||
$ forge fmt | ||
``` | ||
|
||
### Gas Snapshots | ||
|
||
```shell | ||
$ forge snapshot | ||
``` | ||
|
||
### Anvil | ||
|
||
```shell | ||
$ anvil | ||
``` | ||
|
||
### Deploy | ||
|
||
```shell | ||
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key> | ||
``` | ||
|
||
### Cast | ||
|
||
```shell | ||
$ cast <subcommand> | ||
``` | ||
|
||
### Help | ||
|
||
```shell | ||
$ forge --help | ||
$ anvil --help | ||
$ cast --help | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[profile.default] | ||
src = "src" | ||
out = "out" | ||
libs = ["lib"] | ||
|
||
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options |
Submodule openzeppelin-contracts
added at
c304b6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import {Script, console} from "forge-std/Script.sol"; | ||
import "../src/Nodes.sol"; | ||
|
||
contract Deployer is Script { | ||
function setUp() public {} | ||
|
||
function run() public { | ||
vm.startBroadcast(); | ||
new Nodes(); | ||
|
||
vm.broadcast(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.13; | ||
|
||
contract GroupMessages { | ||
event MessageSent(bytes32 groupId, bytes message, uint64 sequenceId); | ||
|
||
uint64 sequenceId; | ||
|
||
function addMessage(bytes32 groupId, bytes memory message) public { | ||
sequenceId++; | ||
|
||
emit MessageSent(groupId, message, sequenceId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.13; | ||
|
||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
|
||
contract Nodes is Ownable { | ||
constructor() Ownable(msg.sender) {} | ||
|
||
struct Node { | ||
string httpAddress; | ||
uint256 originatorId; | ||
bool isHealthy; | ||
// Maybe we want a TLS cert separate from the public key for MTLS authenticated connections? | ||
} | ||
|
||
event NodeUpdate( | ||
bytes publicKey, | ||
string httpAddress, | ||
uint256 originatorId, | ||
bool isHealthy | ||
); | ||
|
||
// List of public keys | ||
bytes[] publicKeys; | ||
|
||
// Mapping of publicKey to node | ||
mapping(bytes => Node) public nodes; | ||
|
||
/** | ||
Add a node to the network | ||
*/ | ||
function addNode( | ||
bytes calldata publicKey, | ||
string calldata httpAddress | ||
) public onlyOwner { | ||
require( | ||
bytes(nodes[publicKey].httpAddress).length == 0, | ||
"Node already exists" | ||
); | ||
|
||
require(bytes(httpAddress).length != 0, "HTTP address is required"); | ||
|
||
nodes[publicKey] = Node({ | ||
httpAddress: httpAddress, | ||
originatorId: publicKeys.length + 1, | ||
isHealthy: true | ||
}); | ||
|
||
publicKeys.push(publicKey); | ||
|
||
emit NodeUpdate(publicKey, httpAddress, publicKeys.length, true); | ||
} | ||
|
||
/** | ||
The contract owner can use this function to mark a node as unhealthy | ||
triggering all other nodes to stop replicating to/from this node | ||
*/ | ||
function markNodeUnhealthy(bytes calldata publicKey) public onlyOwner { | ||
require( | ||
bytes(nodes[publicKey].httpAddress).length != 0, | ||
"Node does not exist" | ||
); | ||
nodes[publicKey].isHealthy = false; | ||
|
||
emit NodeUpdate( | ||
publicKey, | ||
nodes[publicKey].httpAddress, | ||
nodes[publicKey].originatorId, | ||
false | ||
); | ||
} | ||
|
||
/** | ||
The contract owner can use this function to mark a node as healthy | ||
triggering all other nodes to | ||
*/ | ||
function markNodeHealthy(bytes calldata publicKey) public onlyOwner { | ||
require( | ||
bytes(nodes[publicKey].httpAddress).length != 0, | ||
"Node does not exist" | ||
); | ||
nodes[publicKey].isHealthy = true; | ||
|
||
emit NodeUpdate( | ||
publicKey, | ||
nodes[publicKey].httpAddress, | ||
nodes[publicKey].originatorId, | ||
true | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import {Test, console} from "forge-std/Test.sol"; | ||
import {GroupMessages} from "../src/GroupMessages.sol"; | ||
|
||
contract CounterTest is Test { | ||
GroupMessages public groupMessages; | ||
|
||
function setUp() public { | ||
groupMessages = new GroupMessages(); | ||
} | ||
|
||
function test_AddMessage2kb() public { | ||
bytes32 groupId = bytes32( | ||
0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef | ||
); | ||
bytes memory message = new bytes(1024); | ||
for (uint256 i = 0; i < message.length; i++) { | ||
message[i] = bytes1(uint8(i % 256)); // Set each byte to its index modulo 256 | ||
} | ||
|
||
groupMessages.addMessage(groupId, message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
rm -f ./build/*.abi.json | ||
rm -f ./pkg/abis/*.go | ||
|
||
cd contracts | ||
|
||
# Generate the abi files out of the solidity code | ||
forge inspect ./src/Nodes.sol:Nodes abi > ../build/Nodes.abi.json | ||
forge inspect ./src/GroupMessages.sol:GroupMessages abi > ../build/GroupMessages.abi.json | ||
|
||
cd .. | ||
# Generate Go code out of the ABI files | ||
abigen --abi ./build/Nodes.abi.json --pkg abis --type Nodes --out ./pkg/abis/nodes.go | ||
abigen --abi ./build/GroupMessages.abi.json --pkg abis --type GroupMessages --out ./pkg/abis/groupMessages.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# This is the first default private key for anvil. Nothing sensitive here. | ||
export PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 | ||
export DOCKER_RPC_URL=http://localhost:7545 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
# Deploy the smart contracts to the local anvil node | ||
|
||
source dev/contracts/.env | ||
|
||
cd ./contracts | ||
|
||
# Deploy a contract and save the output (which includes the contract address) to a JSON file to be used in tests | ||
function deploy_contract() { | ||
forge create --legacy --json --rpc-url $DOCKER_RPC_URL --private-key $PRIVATE_KEY "$1:$2" > ../build/$2.json | ||
} | ||
|
||
deploy_contract src/GroupMessages.sol GroupMessages | ||
deploy_contract src/Nodes.sol Nodes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FROM ghcr.io/foundry-rs/foundry | ||
|
||
WORKDIR /anvil | ||
|
||
ENTRYPOINT anvil --host 0.0.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
. dev/docker/env | ||
|
||
docker_compose "$@" | ||
docker_compose "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#!/usr/bin/env sh | ||
#!/usr/bin/env bash | ||
set -e | ||
|
||
. dev/docker/env | ||
|
||
docker_compose down |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.