Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smart contracts: Registry + Challenger #22

Merged
merged 28 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e2e4526
chore: contracts dir
merklefruit Apr 29, 2024
eeb1fb7
forge install: openzeppelin-contracts
merklefruit Apr 29, 2024
42344d5
forge install: relic-sdk
merklefruit Apr 29, 2024
32bd4ec
chore: init basic registry
merklefruit Apr 29, 2024
839a7fd
feat: basic registry
merklefruit Apr 30, 2024
1cc992f
Merge branch 'feat/bolt-frontend-gt' of github.com:chainbound/bolt-v0…
merklefruit Apr 30, 2024
4ca139f
chore: rm forge-std files from git
merklefruit Apr 30, 2024
f62d61a
forge install: forge-std
merklefruit Apr 30, 2024
88095eb
feat: bolt challenger stub
merklefruit Apr 30, 2024
8b2a799
feat: challenger progress and refactor
merklefruit May 1, 2024
b007b1a
feat: ssz libraries
merklefruit May 1, 2024
f9aafca
feat: challenger resolution progress
merklefruit May 2, 2024
838085e
chore: fmt + comments
merklefruit May 2, 2024
71f10e8
feat: challenger update: account data proof
merklefruit May 3, 2024
485c8d0
fix: some compile errors, formatting. wip
thedevbirb May 13, 2024
11f4c0d
feat: solve challenge in case of invalid protocol rules
merklefruit May 21, 2024
4269cce
chore: fmt
merklefruit May 22, 2024
a6dba78
chore: small nit
merklefruit May 22, 2024
63af8e8
feat: updated registry to confirm opt out methods
merklefruit May 22, 2024
71fdaac
chore: fmt
merklefruit May 22, 2024
7c942c3
feat: refactored libraries, cleaned challenger contract
merklefruit May 22, 2024
0528dd7
chore: moved scripts to their own dir for cleaner repo organization
merklefruit May 22, 2024
4559704
chore: fix compile errors
merklefruit May 22, 2024
1b6693a
feat(challenger): added challenge opening tests
merklefruit May 23, 2024
3934b77
feat: added more registry tests
merklefruit May 24, 2024
abd9726
chore: fmt
merklefruit May 24, 2024
aba2381
chore: adapted justfile to scripts dir
merklefruit May 24, 2024
9cc5217
chore: address challenger review
merklefruit May 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[submodule "bolt-contracts/lib/forge-std"]
path = bolt-contracts/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "bolt-contracts/lib/openzeppelin-contracts"]
path = bolt-contracts/lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
[submodule "bolt-contracts/lib/relic-sdk"]
path = bolt-contracts/lib/relic-sdk
url = https://github.com/Relic-Protocol/relic-sdk
2 changes: 2 additions & 0 deletions bolt-contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cache/
out/
6 changes: 6 additions & 0 deletions bolt-contracts/foundry.toml
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
1 change: 1 addition & 0 deletions bolt-contracts/lib/forge-std
Submodule forge-std added at bb4cee
1 change: 1 addition & 0 deletions bolt-contracts/lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at dbb610
1 change: 1 addition & 0 deletions bolt-contracts/lib/relic-sdk
Submodule relic-sdk added at 8d6c88
12 changes: 12 additions & 0 deletions bolt-contracts/script/Counter.s.sol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it in the upstream PR #35
Will leave it here for now

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Script, console} from "forge-std/Script.sol";

contract CounterScript is Script {
function setUp() public {}

function run() public {
vm.broadcast();
}
}
86 changes: 86 additions & 0 deletions bolt-contracts/src/BoltRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract BoltRegistry {
uint256 public constant OPT_OUT_COOLDOWN = 1 days;

// Struct to hold opted-in proposers
struct BasedProposer {
// The address of the proposer opted in
address addr;
// The status of the proposer in the protocol
BoltStatus status;
// The timestamp of the last time the proposer opted in.
// This is used to enforce the opt-out cooldown period
uint256 lastOptedInTimestamp;
}

// Enum to hold the status of the based proposers
enum BoltStatus {
Active,
Inactive
}

// Mapping to hold the based proposers
mapping(address => BasedProposer) public basedProposers;

// Error messages
error BasedProposerAlreadyExists();
error BasedProposerDoesNotExist();
error InvalidStatusChange();
error CooldownNotElapsed();
error Unauthorized();
error NotFound();

// Event to log the status change of a based proposer
event BasedProposerStatusChanged(address indexed basedProposer, BoltStatus status);

/// @notice Constructor
constructor() {}

/// @notice Allows a based proposer to opt-in to the protocol
function optIn() external {
if (basedProposers[msg.sender].addr != address(0)) {
revert BasedProposerAlreadyExists();
}

basedProposers[msg.sender] = BasedProposer(msg.sender, BoltStatus.Active, block.timestamp);
emit BasedProposerStatusChanged(msg.sender, BoltStatus.Active);
}

/// @notice Allows a based proposer to opt-out of the protocol
function optOut() external {
BasedProposer memory basedProposer = basedProposers[msg.sender];

if (basedProposer.addr != msg.sender) {
revert BasedProposerDoesNotExist();
}
if (basedProposer.status == BoltStatus.Inactive) {
revert InvalidStatusChange();
}
if (block.timestamp - basedProposer.lastOptedInTimestamp < OPT_OUT_COOLDOWN) {
revert CooldownNotElapsed();
}
merklefruit marked this conversation as resolved.
Show resolved Hide resolved

basedProposer.status = BoltStatus.Inactive;
emit BasedProposerStatusChanged(msg.sender, BoltStatus.Inactive);
}

/// @notice Check if an address is a based proposer
/// @param _basedProposer The address to check
/// @return True if the address is a based proposer, false otherwise
function isBasedProposer(address _basedProposer) external view returns (bool) {
return basedProposers[_basedProposer].addr != address(0);
}

/// @notice Get the status of a based proposer
/// @param _basedProposers The address of the based proposer
/// @return The status of the based proposer
function getBasedProposerStatus(address _basedProposers) external view returns (BoltStatus) {
if (basedProposers[_basedProposers].addr == address(0)) {
revert BasedProposerDoesNotExist();
}

return basedProposers[_basedProposers].status;
}
}
23 changes: 23 additions & 0 deletions bolt-contracts/test/BoltRegistry.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Test, console} from "forge-std/Test.sol";
import {BoltRegistry} from "../src/BoltRegistry.sol";

contract BoltRegistryTest is Test {
BoltRegistry public registry;

address alice = address(0x1);
address bob = address(0x2);

function setUp() public {
registry = new BoltRegistry();
}

function testAddBasedProposerToRegistry() public {
vm.prank(alice);
registry.optIn();

assertEq(uint8(registry.getBasedProposerStatus(alice)), uint8(BoltRegistry.BoltStatus.Active));
}
}