Skip to content

Commit

Permalink
Merge pull request #42 from jagerman/small-contributor-delay
Browse files Browse the repository at this point in the history
Enforce small contributor leave restriction
  • Loading branch information
Doy-lee authored Jul 5, 2024
2 parents 76639b3 + a0dcfcd commit 25b9eba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions contracts/ServiceNodeRewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ contract ServiceNodeRewards is Initializable, Ownable2StepUpgradeable, PausableU
uint64 public constant LIST_SENTINEL = 0;
uint256 public constant MAX_SERVICE_NODE_REMOVAL_WAIT_TIME = 30 days;
uint256 public constant MAX_PERMITTED_PUBKEY_AGGREGATIONS_LOWER_BOUND = 5;
// A small contributor is one who contributes less than 1/DIVISOR of the total; such a
// contributor may not initiate a leave request within the initial LEAVE_DELAY:
uint256 public constant SMALL_CONTRIBUTOR_LEAVE_DELAY = 30 days;
uint256 public constant SMALL_CONTRIBUTOR_DIVISOR = 4;

uint64 public nextServiceNodeID;
uint256 public totalNodes;
Expand Down Expand Up @@ -153,6 +157,7 @@ contract ServiceNodeRewards is Initializable, Ownable2StepUpgradeable, PausableU
error ContractNotStarted();
error ContributionTotalMismatch(uint256 required, uint256 provided);
error EarlierLeaveRequestMade(uint64 serviceNodeID, address recipient);
error SmallContributorLeaveTooEarly(uint64 serviceNodeID, address recipient);
error FirstContributorMismatch(address operator, address contributor);
error InsufficientBLSSignatures(uint256 numSigners, uint256 requiredSigners);
error InvalidBLSSignature();
Expand Down Expand Up @@ -378,16 +383,22 @@ contract ServiceNodeRewards is Initializable, Ownable2StepUpgradeable, PausableU
/// node that is initiating the removal.
function _initiateRemoveBLSPublicKey(uint64 serviceNodeID, address caller) internal whenStarted {
bool isContributor = false;
bool isSmall = false; // "small" means less than 25% of the SN total stake
for (uint256 i = 0; i < _serviceNodes[serviceNodeID].contributors.length; i++) {
if (_serviceNodes[serviceNodeID].contributors[i].addr == caller) {
isContributor = true;
isSmall =
SMALL_CONTRIBUTOR_DIVISOR * _serviceNodes[serviceNodeID].contributors[i].stakedAmount
< _serviceNodes[serviceNodeID].deposit;
break;
}
}
if (!isContributor) revert CallerNotContributor(serviceNodeID, caller);

if (_serviceNodes[serviceNodeID].leaveRequestTimestamp != 0)
revert EarlierLeaveRequestMade(serviceNodeID, caller);
if (isSmall && block.timestamp < _serviceNodes[serviceNodeID].addedTimestamp + SMALL_CONTRIBUTOR_LEAVE_DELAY)
revert SmallContributorLeaveTooEarly(serviceNodeID, caller);
_serviceNodes[serviceNodeID].leaveRequestTimestamp = block.timestamp;
emit ServiceNodeRemovalRequest(serviceNodeID, caller, _serviceNodes[serviceNodeID].pubkey);
}
Expand Down Expand Up @@ -617,6 +628,8 @@ contract ServiceNodeRewards is Initializable, Ownable2StepUpgradeable, PausableU
// NOTE: Assign BLS pubkey
_serviceNodes[result].pubkey = pubkey;

_serviceNodes[result].addedTimestamp = block.timestamp;

// NOTE: Create mapping from <BLS Key> -> <SN Linked List Index>
serviceNodeIDs[pubkeyBytes] = result;

Expand Down
1 change: 1 addition & 0 deletions contracts/interfaces/IServiceNodeRewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface IServiceNodeRewards {
uint64 prev;
address operator;
BN256G1.G1Point pubkey;
uint256 addedTimestamp;
uint256 leaveRequestTimestamp;
uint256 deposit;
Contributor[] contributors;
Expand Down

0 comments on commit 25b9eba

Please sign in to comment.