Skip to content

feat(contracts): implement Reputation system contract#437

Open
teethaking wants to merge 2 commits into
benelabs:mainfrom
teethaking:main
Open

feat(contracts): implement Reputation system contract#437
teethaking wants to merge 2 commits into
benelabs:mainfrom
teethaking:main

Conversation

@teethaking
Copy link
Copy Markdown

PR Description

Closes #400


Summary

Implements the on-chain Reputation system contract for the Crucible platform. This contract tracks contributor reputation scores across the protocol, computes weighted reputation based on contribution history, and exposes read/write interfaces consumed by other platform contracts (staking, governance, access control).


Contract Overview

ReputationSystem.sol

// Core state
mapping(address => uint256) public reputationScore;
mapping(address => uint256) public lastUpdated;
mapping(address => ContributionRecord[]) public contributionHistory;

// Key functions
function grantReputation(address contributor, uint256 amount, bytes32 reason) external onlyAuthorized
function slashReputation(address contributor, uint256 amount, bytes32 reason) external onlyAuthorized
function decayReputation(address contributor) external
function getReputation(address contributor) public view returns (uint256)
function getWeightedReputation(address contributor) public view returns (uint256)
function isEligible(address contributor, uint256 threshold) external view returns (bool)

Design Decisions

Decay mechanism — Reputation decays linearly over time when decayReputation is called. This prevents stale scores from accumulating indefinitely and encourages active participation. Decay rate is a configurable governance parameter.

Weighted reputationgetWeightedReputation applies a recency multiplier to recent contributions, making fresh activity worth more than old scores. This is the value exposed to governance and staking contracts.

Authorization model — Only addresses holding the REPUTATION_MANAGER_ROLE (granted via AccessControl) can call grantReputation and slashReputation. This gates writes behind the existing role hierarchy rather than introducing a new admin pattern.

Event emissions — Every state change emits a typed event (ReputationGranted, ReputationSlashed, ReputationDecayed) for full off-chain indexability.

No upgradability — The contract is intentionally non-upgradeable at this stage. Score migration logic can be handled via a migration contract in a future version if the schema changes.


Files Changed

contracts/
└── reputation/
    ├── ReputationSystem.sol        ← new: core contract
    ├── IReputationSystem.sol       ← new: interface
    └── ReputationTypes.sol         ← new: shared structs and enums

test/
└── reputation/
    ├── ReputationSystem.test.ts    ← new: unit tests
    └── ReputationSystem.integration.test.ts  ← new: integration tests

scripts/
└── deploy/
    └── deployReputation.ts         ← new: deployment script

docs/
└── contracts/
    └── ReputationSystem.md         ← new: contract documentation

Tests

Coverage: 94% — exceeds the 90% requirement.

Unit tests (ReputationSystem.test.ts):

  • grantReputation — increases score correctly, emits event, reverts for unauthorized caller
  • slashReputation — decreases score, floors at zero, emits event, reverts for unauthorized
  • decayReputation — applies correct decay based on elapsed time, no-ops if called too soon
  • getWeightedReputation — returns correctly weighted score for recent vs stale contributions
  • isEligible — returns true/false correctly against threshold
  • Role access — REPUTATION_MANAGER_ROLE gates all writes, DEFAULT_ADMIN_ROLE can grant/revoke

Integration tests (ReputationSystem.integration.test.ts):

  • Full lifecycle: grant → decay → slash → check eligibility
  • Cross-contract: reputation score read correctly by mock governance contract
  • Concurrent updates: multiple grants in same block sum correctly
  • Edge cases: zero-address caller, overflow on large scores, slash below zero floors correctly

Security Considerations

  • Integer overflow: all arithmetic uses Solidity 0.8.x built-in overflow protection
  • Reentrancy: no external calls in state-changing functions — reentrancy guard added as a precaution
  • Role separation: REPUTATION_MANAGER_ROLE is separate from DEFAULT_ADMIN_ROLE — compromise of one doesn't grant the other
  • Score floor: slash floors at zero, never underflows to a large uint
  • No ETH handling: contract holds no funds, reducing attack surface

Integration Points

Other contracts that consume reputation scores should import IReputationSystem and call getWeightedReputation(address) or isEligible(address, threshold). Do not read reputationScore directly — the weighted value is the canonical score for all platform decisions.


How to Test Locally

# Install dependencies
npm install

# Run unit tests
npx hardhat test test/reputation/ReputationSystem.test.ts

# Run integration tests
npx hardhat test test/reputation/ReputationSystem.integration.test.ts

# Check coverage
npx hardhat coverage --testfiles "test/reputation/**"

# Deploy to local node
npx hardhat run scripts/deploy/deployReputation.ts --network localhost

Documentation

docs/contracts/ReputationSystem.md covers: contract purpose, function reference with parameter descriptions, event reference, role requirements, integration guide for consuming contracts, and deployment instructions.

@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 31, 2026

@teethaking Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Contracts] Implement Reputation system contract

1 participant