Skip to content

Commit

Permalink
getMinCommitmentAgeForPostconfirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
apenzk committed Mar 1, 2025
1 parent 0f6c7ec commit 138d1cd
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ contract MCR is Initializable, BaseSettlement, MCRStorage, IMCR {
minCommitmentAgeForPostconfirmation = _minCommitmentAgeForPostconfirmation;
}


function getMinCommitmentAgeForPostconfirmation() public view returns (uint256) {
return minCommitmentAgeForPostconfirmation;
}

function initialize(
IMovementStaking _stakingContract,
uint256 _lastPostconfirmedSuperBlockHeight,
Expand Down
141 changes: 76 additions & 65 deletions protocol-units/settlement/mcr/contracts/test/settlement/MCR.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ contract MCRTest is Test, IMCR {

// check that the setup was correctly performed
assertEq(staking.getEpochDuration(address(mcr)), epochDuration, "Epoch duration not set correctly");
assertEq(mcr.getMinCommitmentAgeForPostconfirmation(), 0, "The unset min commitment age should be 0");
}

// Helper function to setup genesis with 1 attester and their stake
Expand Down Expand Up @@ -766,8 +767,43 @@ contract MCRTest is Test, IMCR {
assertEq(mcr.getLastPostconfirmedSuperBlockHeight(), 1, "Last postconfirmed superblock height should be 1, as supermajority was reached (2/2 > threshold)");
}



function testSetMinCommitmentAge() public {
// Set min commitment age to a too long value
vm.expectRevert(MCR.MinCommitmentAgeTooLong.selector);
mcr.setMinCommitmentAge(epochDuration);

// Set min commitment age to 1/10 of epochDuration
uint256 minAge = epochDuration/10;
mcr.setMinCommitmentAge(minAge);
assertEq(mcr.minCommitmentAgeForPostconfirmation(), minAge, "Min commitment age should be updated to 1/10 of epochDuration");
}

function testMinCommitmentAge() public {
// Setup with Alice having supermajority stake
address alice = setupGenesisWithOneAttester(1);
assertEq(mcr.getMinCommitmentAgeForPostconfirmation(), 0, "The unset min commitment age should be 0");
uint256 minAge = 1 minutes;
mcr.setMinCommitmentAge(minAge);
assertEq(mcr.getMinCommitmentAgeForPostconfirmation(), minAge, "Min commitment age should be updated to 1 minutes");

vm.prank(alice);
mcr.submitSuperBlockCommitment(makeHonestCommitment(1));
vm.prank(alice);
mcr.postconfirmSuperBlocksAndRollover();
assertEq(mcr.getLastPostconfirmedSuperBlockHeight(), 0, "Immediate postconfirmation should fail.");

vm.warp(block.timestamp + minAge);
// Now postconfirmation should succeed
vm.prank(alice);
mcr.postconfirmSuperBlocksAndRollover();
assertEq(mcr.getLastPostconfirmedSuperBlockHeight(), 1);
}


// ----------------------------------------------------------------
// -------- Acceptor tests --------------------------------------
// -------- Acceptor --------------------------------------
// ----------------------------------------------------------------

/// @notice Test that getAcceptorStartTime correctly calculates term start times
Expand Down Expand Up @@ -840,47 +876,9 @@ contract MCRTest is Test, IMCR {
assertTrue( newAcceptor == alice, "New acceptor should be Alice");
}

// An acceptor that is in place for acceptorTerm time should be replaced by a new acceptor after their term ended.
// TODO reward logic is not yet implemented
function testAcceptorRewards() public {
(address alice, address bob, ) = setupGenesisWithThreeAttesters(1, 1, 0);
assertEq(mcr.getAcceptor(), bob, "Bob should be the acceptor");

// make superBlock commitments
MCRStorage.SuperBlockCommitment memory initCommitment = makeHonestCommitment(1);
vm.prank(alice);
mcr.submitSuperBlockCommitment(initCommitment);
vm.prank(bob);
mcr.submitSuperBlockCommitment(initCommitment);

// bob postconfirms and gets a reward
vm.prank(bob);
mcr.postconfirmSuperBlocksAndRollover();
assertEq(mcr.getLastPostconfirmedSuperBlockHeight(), 1);

// make second superblock commitment
MCRStorage.SuperBlockCommitment memory secondCommitment = makeHonestCommitment(2);
vm.prank(alice);
mcr.submitSuperBlockCommitment(secondCommitment);
vm.prank(bob);
mcr.submitSuperBlockCommitment(secondCommitment);

// alice can postconfirm, but does not get the reward
// TODO check that bob did not get the reward
vm.prank(alice);
mcr.postconfirmSuperBlocksAndRollover();
assertEq(mcr.getLastPostconfirmedSuperBlockHeight(), 2);

// bob tries to postconfirm, but already done by alice
// TODO: bob should still get the reward
vm.prank(bob);
mcr.postconfirmSuperBlocksAndRollover();
assertEq(mcr.getLastPostconfirmedSuperBlockHeight(), 2);
}


// ----------------------------------------------------------------
// -------- Reward attester tests --------------------------------------
// -------- Attester rewards --------------------------------------
// ----------------------------------------------------------------

function testRewardPoints() public {
Expand Down Expand Up @@ -967,36 +965,49 @@ contract MCRTest is Test, IMCR {
}


function testSetMinCommitmentAge() public {
// Set min commitment age to a too long value
vm.expectRevert(MCR.MinCommitmentAgeTooLong.selector);
mcr.setMinCommitmentAge(epochDuration);

// Set min commitment age to 1/10 of epochDuration
uint256 minAge = epochDuration/10;
mcr.setMinCommitmentAge(minAge);
assertEq(mcr.minCommitmentAgeForPostconfirmation(), minAge, "Min commitment age should be updated to 1/10 of epochDuration");
}
// ----------------------------------------------------------------
// -------- Acceptor rewards --------------------------------------
// ----------------------------------------------------------------

function testMinCommitmentAge() public {
// Setup with Alice having supermajority stake
address alice = setupGenesisWithOneAttester(1);
uint256 minAge = 1 minutes;
mcr.setMinCommitmentAge(minAge);

vm.prank(alice);
mcr.submitSuperBlockCommitment(makeHonestCommitment(1));


// An acceptor that is in place for acceptorTerm time should be replaced by a new acceptor after their term ended.
// TODO reward logic is not yet implemented
function testAcceptorRewards() public {
(address alice, address bob, ) = setupGenesisWithThreeAttesters(1, 1, 0);
assertEq(mcr.getAcceptor(), bob, "Bob should be the acceptor");

// make superBlock commitments
MCRStorage.SuperBlockCommitment memory initCommitment = makeHonestCommitment(1);
vm.prank(alice);
mcr.submitSuperBlockCommitment(initCommitment);
vm.prank(bob);
mcr.submitSuperBlockCommitment(initCommitment);

// bob postconfirms and gets a reward
vm.prank(bob);
mcr.postconfirmSuperBlocksAndRollover();
assertEq(mcr.getLastPostconfirmedSuperBlockHeight(), 0, "Immediate postconfirmation should fail.");

// Wait for min age
vm.warp(block.timestamp + minAge);

// Now postconfirmation should succeed
assertEq(mcr.getLastPostconfirmedSuperBlockHeight(), 1);

// make second superblock commitment
MCRStorage.SuperBlockCommitment memory secondCommitment = makeHonestCommitment(2);
vm.prank(alice);
mcr.submitSuperBlockCommitment(secondCommitment);
vm.prank(bob);
mcr.submitSuperBlockCommitment(secondCommitment);

// alice can postconfirm, but does not get the reward
// TODO check that bob did not get the reward
vm.prank(alice);
mcr.postconfirmSuperBlocksAndRollover();
assertEq(mcr.getLastPostconfirmedSuperBlockHeight(), 1);
assertEq(mcr.getLastPostconfirmedSuperBlockHeight(), 2);

// bob tries to postconfirm, but already done by alice
// TODO: bob should still get the reward
vm.prank(bob);
mcr.postconfirmSuperBlocksAndRollover();
assertEq(mcr.getLastPostconfirmedSuperBlockHeight(), 2);
}


}

0 comments on commit 138d1cd

Please sign in to comment.