Skip to content

gensyn-ai/rl-swarm-contracts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RL Swarm Contracts

This repository contains the smart contracts for the RL Swarm project, focusing on coordinating swarm behavior onchain.

Deployed contract

Gensyn testnet

If you want to interact with the contract, use the SwarmCoordinatorProxy.

Overview

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

Roles

  1. Owner

    • Can set stage count
    • Can assign bootnode manager role
    • Can set stage updater
    • Can grant and revoke any role
    • Initially deployed contract owner
  2. Stage Manager

    • Can advance stages and rounds
    • Initially set to contract owner
  3. Bootnode Manager

    • Can add and remove bootnodes
    • Can clear all bootnodes
    • Initially set to contract owner

Interacting with the Contract

For Participants

Register your peer

function registerPeer(string calldata peerId) external

View current round and stage

function currentRound() external view returns (uint256)
function currentStage() external view returns (uint256)

Check total wins

function getTotalWins(string calldata peerId) external view returns (uint256)

View the leaderboard

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).

Check unique voter count

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.

Check unique voted peer count

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.

Get peer and EOA mappings

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.

Get voting information

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

For Administrators

Owner

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.

Stage Manager

Advances stages and rounds.

function updateStageAndRound() external returns (uint256, uint256)

Bootnode manager

Manages bootnode list.

function addBootnodes(string[] calldata newBootnodes)
function removeBootnode(uint256 index)
function clearBootnodes()
function getBootnodesCount() external view returns (uint256)

Development

Prerequisites

Testing

Run the test suite:

forge test

Run with verbosity for more details:

forge test -vvv

Code Style

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:
#!/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

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

Deploy locally with mock data

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:

  1. Start the local Ethereum node:
anvil
  1. 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.

Deploy

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")

Deploy and verify contracts

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:

  1. Deploy a new implementation for each proxy address
  2. Extract the implementation address
  3. Verify the implementation on Blockscout
  4. Show clear progress for each operation

FAQ

How did you generate the ascii sections in the source code?

I used https://www.asciiart.eu/text-to-ascii-art with:

  • font DOS Rebel
  • border simple

How do I generate a code coverage report?

forge coverage --report lcov ; genhtml lcov.info -o report

Once that's done you can use either:

About

Smart contracts for the RL Swarm project, focusing on coordinating swarm behavior onchain.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 5