This repository contains the smart contracts for the RL Swarm project, focusing on coordinating swarm behavior onchain.
If you want to interact with the contract, use the SwarmCoordinatorProxy
.
- v0.6.0
- v0.5.1
- v0.5.0
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.1
- v0.3.0
- v0.2.0 - 0xcD1351B125b0ae4f023ADA5D09443087a7d99101
- v0.1.0 - 0x77bd0fcB5349F67C8fA1236E98e2b93334F4Db6E
The main contract SwarmCoordinator
manages a round-based system for coordinating swarm participants, tracking winners, reporting rewards, and managing bootnode infrastructure. The contract includes features for:
- Round and stage management
- Peer registration and tracking
- Bootnode management
- Winner submission and reward tracking
- Unique voter tracking across rounds
- Unique voted peer tracking across rounds
-
Owner
- Can set stage count
- Can assign bootnode manager role
- Can set stage updater
- Can grant and revoke any role
- Initially deployed contract owner
-
Stage Manager
- Can advance stages and rounds
- Initially set to contract owner
-
Bootnode Manager
- Can add and remove bootnodes
- Can clear all bootnodes
- Initially set to contract owner
function registerPeer(string calldata peerId) external
function currentRound() external view returns (uint256)
function currentStage() external view returns (uint256)
function getTotalWins(string calldata peerId) external view returns (uint256)
function winnerLeaderboard(uint256 start, uint256 end) external view returns (string[] memory peerIds, uint256[] memory wins)
function voterLeaderboard(uint256 start, uint256 end) external view returns (string[] memory peerIds, uint256[] memory voteCounts)
Returns slices of the leaderboards:
winnerLeaderboard
: Returns peer IDs and their win counts, sorted by number of wins (descending)voterLeaderboard
: Returns peer IDs and their vote counts, sorted by number of votes (descending)
Both leaderboards track up to 100 top entries. The start
and end
parameters define the range of positions to return (inclusive start, exclusive end).
function uniqueVoters() external view returns (uint256)
Returns the total number of unique addresses that have participated in voting across all rounds. Each address is counted only once, regardless of how many times they have voted.
function uniqueVotedPeers() external view returns (uint256)
Returns the total number of unique peer IDs that have received votes across all rounds. Each peer is counted only once, regardless of how many times they have been voted for.
function getPeerId(address[] calldata eoas) external view returns (string[][] memory)
function getEoa(string[] calldata peerIds) external view returns (address[] memory)
Get peer IDs for multiple EOAs or EOAs for multiple peer IDs.
function getVoterVoteCount(string calldata peerId) external view returns (uint256)
function getVoterVotes(uint256 roundNumber, string calldata peerId) external view returns (string[] memory)
function getPeerVoteCount(uint256 roundNumber, string calldata peerId) external view returns (uint256)
Get detailed voting information including:
- Number of times a voter has voted
- Votes cast by a specific voter in a round
- Number of votes received by a peer in a round
Manages contract configuration and roles.
function grantRole(bytes32 role, address account)
function revokeRole(bytes32 role, address account)
The deployer of the contract is automatically granted the roles:
OWNER_ROLE
STAGE_MANAGER_ROLE
BOOTNODE_MANAGER_ROLE
To grant a role to a new account, the owner can call grantRole
with:
role
: The role identifier (e.g.,OWNER_ROLE
,STAGE_MANAGER_ROLE
,BOOTNODE_MANAGER_ROLE
)account
: The address to grant the role to
For example, to grant the STAGE_MANAGER_ROLE to an address:
grantRole(STAGE_MANAGER_ROLE, 0x1234...5678)
Roles can be revoked using revokeRole
with the same parameters.
Advances stages and rounds.
function updateStageAndRound() external returns (uint256, uint256)
Manages bootnode list.
function addBootnodes(string[] calldata newBootnodes)
function removeBootnode(uint256 index)
function clearBootnodes()
function getBootnodesCount() external view returns (uint256)
Run the test suite:
forge test
Run with verbosity for more details:
forge test -vvv
- Use Conventional Commits
- Use Solidity style guide
- Run formatter before committing:
forge fmt
- Or set up a git hook to format pre-commit:
- Create a pre-commit hook file
.git/hooks/pre-commit
with this content:
- Create a pre-commit hook file
#!/bin/bash
# Format staged files using forge fmt
git diff --cached |forge fmt
# Add the formatted changes back to the index
git add .
# Proceed with commit
exit 0
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
One can set up a local environment for testing.
Requirements:
Foundry comes with a local Ethereum node called anvil
. To set up your local environment:
- Start the local Ethereum node:
anvil
- Keep this terminal running and open a new terminal to deploy the mock data:
forge script script/DeployLocalMockData.s.sol --rpc-url=http://localhost:8545 --broadcast
This script will:
- Deploy the SwarmCoordinator contract
- Register mock peers
- Add bootnode entries
- Submit winners and rewards
- Display leaderboard
You can now interact with the contract at the address printed in the deployment output.
To deploy to a network (either testnet, mainnet, ..), you need to set up these environment variables in a file such as .env
:
ETH_RPC_URL=https://gensyn-testnet.g.alchemy.com/public
ETH_PRIVATE_KEY=0xPRIVATEKEY
# Array of proxy addresses to deploy to
PROXY_ADDRESSES=("0x69C6e1D608ec64885E7b185d39b04B491a71768C" "0x6947c6E196a48B77eFa9331EC1E3e45f3Ee5Fd58")
The repository includes a script that handles both deployment and verification of contracts. You can use it in different ways:
# Deploy and verify all contracts
./scripts/deploy-and-verify.sh
# Only deploy all contracts
./scripts/deploy-and-verify.sh --deploy-only
# Only verify all contracts
./scripts/deploy-and-verify.sh --verify-only
# Show help
./scripts/deploy-and-verify.sh --help
The script will:
- Deploy a new implementation for each proxy address
- Extract the implementation address
- Verify the implementation on Blockscout
- Show clear progress for each operation
I used https://www.asciiart.eu/text-to-ascii-art with:
- font DOS Rebel
- border simple
forge coverage --report lcov ; genhtml lcov.info -o report
Once that's done you can use either: