Skip to content

Commit

Permalink
rename state history max vvariable
Browse files Browse the repository at this point in the history
  • Loading branch information
alysiahuggins committed Aug 15, 2024
1 parent 97d6842 commit fac2e76
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 268 deletions.
2 changes: 1 addition & 1 deletion contract-bindings/artifacts/LightClientMock_bytecode.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contract-bindings/artifacts/LightClient_bytecode.json

Large diffs are not rendered by default.

225 changes: 115 additions & 110 deletions contract-bindings/src/light_client.rs

Large diffs are not rendered by default.

253 changes: 129 additions & 124 deletions contract-bindings/src/light_client_mock.rs

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions contracts/src/LightClient.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ contract LightClient is Initializable, OwnableUpgradeable, UUPSUpgradeable {

/// @notice Max number of seconds worth of state commitments to record based on this block
/// timestamp
uint32 public maxStateHistoryDuration;
uint32 public stateHistoryRetentionPeriod;

/// @notice index of first block in block state series
///@dev use this instead of index 0 since old states would be set to zero to keep storage costs
/// constant to maxStateHistoryDuration
/// constant to stateHistoryRetentionPeriod
uint64 public stateHistoryFirstIndex;

/// @notice an array to store the L1 block heights, HotShot Block Heights and their respective
Expand Down Expand Up @@ -244,7 +244,7 @@ contract LightClient is Initializable, OwnableUpgradeable, UUPSUpgradeable {

blocksPerEpoch = numBlockPerEpoch;

maxStateHistoryDuration = maxHistorySeconds;
stateHistoryRetentionPeriod = maxHistorySeconds;

bytes32 initStakeTableComm = computeStakeTableComm(genesis);
votingStakeTableCommitment = initStakeTableComm;
Expand All @@ -264,7 +264,7 @@ contract LightClient is Initializable, OwnableUpgradeable, UUPSUpgradeable {
/// become the snapshots used for vote verifications later on.
/// @dev in this version, only a permissioned prover doing the computations
/// can call this function
/// @dev the state history for `maxStateHistoryDuration` L1 blocks are also recorded in the
/// @dev the state history for `stateHistoryRetentionPeriod` L1 blocks are also recorded in the
/// `stateHistoryCommitments` array
/// @notice While `newState.stakeTable*` refers to the (possibly) new stake table states,
/// the entire `newState` needs to be signed by stakers in `finalizedState`
Expand Down Expand Up @@ -405,12 +405,13 @@ contract LightClient is Initializable, OwnableUpgradeable, UUPSUpgradeable {
/// @notice updates the stateHistoryCommitments array each time a new
/// finalized state is added to the LightClient contract.
/// Ensures that the array contains only the most recent data and does not
/// exceed the maxStateHistoryDuration duration in seconds.
/// exceed the stateHistoryRetentionPeriod duration in seconds.
/// @dev the block timestamp is used to determine if the stateHistoryCommitments array
/// should be pruned, based on the maxStateHistoryDuration duration in seconds.
/// should be pruned, based on the stateHistoryRetentionPeriod duration in seconds.
/// @dev a FIFO (First-In-First-Out) approach is used to delete elements from the start of the
/// array,
/// ensuring that only the most recent states are retained within the maxStateHistoryDuration
/// ensuring that only the most recent states are retained within the
/// stateHistoryRetentionPeriod
/// duration.
/// @dev the `delete` method does not reduce the array length but resets the value at the
/// specified index to zero.
Expand All @@ -426,7 +427,7 @@ contract LightClient is Initializable, OwnableUpgradeable, UUPSUpgradeable {
stateHistoryCommitments.length != 0
&& stateHistoryCommitments[stateHistoryCommitments.length - 1].l1BlockTimestamp
- stateHistoryCommitments[stateHistoryFirstIndex].l1BlockTimestamp
>= maxStateHistoryDuration
>= stateHistoryRetentionPeriod
) {
// The stateHistoryCommitments array has reached the maximum allowed duration
// deleting the oldest (first) non-empty element to maintain the FIFO structure.
Expand Down Expand Up @@ -534,9 +535,9 @@ contract LightClient is Initializable, OwnableUpgradeable, UUPSUpgradeable {
/// @notice set Max Block States allowed
/// @param historySeconds The maximum duration worth of state history updates to store based on
/// the block timestamp
function setMaxStateHistoryDuration(uint32 historySeconds) public onlyOwner {
function setstateHistoryRetentionPeriod(uint32 historySeconds) public onlyOwner {
if (historySeconds < 1 days) revert InvalidMaxStateHistory();

maxStateHistoryDuration = historySeconds;
stateHistoryRetentionPeriod = historySeconds;
}
}
38 changes: 19 additions & 19 deletions contracts/test/LightClient.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -631,15 +631,15 @@ contract LightClient_StateUpdatesTest is LightClientCommonTest {
function assertInitialStateHistoryConditions() internal view {
// assert that stateHistoryFirstIndex starts at 0.
assertEq(lc.stateHistoryFirstIndex(), 0);
// asset maxStateHistoryDuration is greater or equal to at least one day in seconds.
assertGe(lc.maxStateHistoryDuration(), 1 days);
// asset stateHistoryRetentionPeriod is greater or equal to at least one day in seconds.
assertGe(lc.stateHistoryRetentionPeriod(), 1 days);
}

function setMaxStateHistoryDuration(uint32 duration) internal {
function setstateHistoryRetentionPeriod(uint32 duration) internal {
// Set the new max block states allowed to half of the number of states available
vm.prank(admin);
lc.setMaxStateHistoryDuration(duration);
assertEq(lc.maxStateHistoryDuration(), duration);
lc.setstateHistoryRetentionPeriod(duration);
assertEq(lc.stateHistoryRetentionPeriod(), duration);
}

/**
Expand All @@ -664,7 +664,7 @@ contract LightClient_StateUpdatesTest is LightClientCommonTest {

//assert initial conditions
assertEq(lc.stateHistoryFirstIndex(), 0);
assertGe(lc.maxStateHistoryDuration(), 1 days);
assertGe(lc.stateHistoryRetentionPeriod(), 1 days);

bytes memory result = vm.ffi(cmds);
(LC.LightClientState[] memory states, V.PlonkProof[] memory proofs) =
Expand All @@ -686,22 +686,22 @@ contract LightClient_StateUpdatesTest is LightClientCommonTest {
assertEq(lc.getStateHistoryCount(), blockUpdatesCount + 1);
}

function testFuzz_setMaxStateHistoryDuration(uint32 maxHistorySeconds) public {
function testFuzz_setstateHistoryRetentionPeriod(uint32 maxHistorySeconds) public {
vm.prank(admin);
vm.assume(maxHistorySeconds >= 1 days);
lc.setMaxStateHistoryDuration(maxHistorySeconds);
assertEq(maxHistorySeconds, lc.maxStateHistoryDuration());
lc.setstateHistoryRetentionPeriod(maxHistorySeconds);
assertEq(maxHistorySeconds, lc.stateHistoryRetentionPeriod());
}

function test_revertNonAdminSetMaxStateHistoryAllowed() public {
vm.expectRevert();
lc.setMaxStateHistoryDuration(1 days);
lc.setstateHistoryRetentionPeriod(1 days);
}

function test_revertSetMaxStateHistoryAllowedWhenInvalidValueSent() public {
vm.prank(admin);
vm.expectRevert(LC.InvalidMaxStateHistory.selector);
lc.setMaxStateHistoryDuration(1 days - 1);
lc.setstateHistoryRetentionPeriod(1 days - 1);
}

function test_stateHistoryHandlingWithOneDayMaxHistory() public {
Expand All @@ -722,7 +722,7 @@ contract LightClient_StateUpdatesTest is LightClientCommonTest {
abi.decode(result, (LC.LightClientState[], V.PlonkProof[]));

// set the new max block states allowed to half of the number of states available
setMaxStateHistoryDuration(initialEpoch);
setstateHistoryRetentionPeriod(initialEpoch);

// Add one numDays worth of a new state
uint256 i;
Expand All @@ -745,7 +745,7 @@ contract LightClient_StateUpdatesTest is LightClientCommonTest {
(, uint256 oldestBlockTimestamp,) = lc.stateHistoryCommitments(lc.stateHistoryFirstIndex());
// assert that the latest Commitment timestamp - oldest Commitment timestamp is == the max
// history allowed
assertEq(latestBlockTimestamp - oldestBlockTimestamp, lc.maxStateHistoryDuration());
assertEq(latestBlockTimestamp - oldestBlockTimestamp, lc.stateHistoryRetentionPeriod());

// Add a new state so that the state history duration is only surpassed by one element
vm.prank(permissionedProver);
Expand All @@ -755,7 +755,7 @@ contract LightClient_StateUpdatesTest is LightClientCommonTest {
lc.newFinalizedState(states[i], proofs[i]);
i++;

// the duration between the updates are more than maxStateHistoryDuration, so the first
// the duration between the updates are more than stateHistoryRetentionPeriod, so the first
// index should be one
assertEq(lc.stateHistoryFirstIndex(), 1);

Expand Down Expand Up @@ -798,8 +798,8 @@ contract LightClient_StateUpdatesTest is LightClientCommonTest {
assertInitialStateHistoryConditions();

// set the new max block states allowed to half of the number of states available
setMaxStateHistoryDuration(1 days * numDays);
assertEq(lc.maxStateHistoryDuration(), 1 days * numDays);
setstateHistoryRetentionPeriod(1 days * numDays);
assertEq(lc.stateHistoryRetentionPeriod(), 1 days * numDays);

// Update the states to max state history allowed
uint256 i;
Expand All @@ -822,7 +822,7 @@ contract LightClient_StateUpdatesTest is LightClientCommonTest {
// assert that the latest Commitment timestamp - oldest Commitment timestamp is == the max
// history allowed

assertEq(latestBlockTimestamp - oldestBlockTimestamp, lc.maxStateHistoryDuration());
assertEq(latestBlockTimestamp - oldestBlockTimestamp, lc.stateHistoryRetentionPeriod());

// Add a new state so that the state history duration is only surpassed by one element
vm.prank(permissionedProver);
Expand All @@ -832,7 +832,7 @@ contract LightClient_StateUpdatesTest is LightClientCommonTest {
lc.newFinalizedState(states[i], proofs[i]);
i++;

// the duration between the updates are more than maxStateHistoryDuration, so the first
// the duration between the updates are more than stateHistoryRetentionPeriod, so the first
// index should be one
assertEq(lc.stateHistoryFirstIndex(), 1);

Expand Down Expand Up @@ -1059,7 +1059,7 @@ contract LightClient_StateUpdatesTest is LightClientCommonTest {
function test_revertWhenSetZeroMaxStateUpdatesAllowed() public {
vm.prank(admin);
vm.expectRevert(LC.InvalidMaxStateHistory.selector);
lc.setMaxStateHistoryDuration(0);
lc.setstateHistoryRetentionPeriod(0);
}

function test_hotShotIsDownWhenBlockIsHigherThanLastRecordedAndTheDelayThresholdHasPassed()
Expand Down
6 changes: 3 additions & 3 deletions contracts/test/LightClientV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ contract LightClientV2 is Initializable, OwnableUpgradeable, UUPSUpgradeable {

///@notice Max number of seconds worth of state commitments to record based on this block
/// timestamp
uint32 public maxStateHistoryDuration;
uint32 public stateHistoryRetentionPeriod;

///@notice index of first block in block state series
///@dev use this instead of index 0 since old states would be set to zero to keep storage costs
/// constant to maxStateHistoryDuration
/// constant to stateHistoryRetentionPeriod
uint64 public stateHistoryFirstIndex;

/// @notice an array to store the L1 block heights, HotShot Block Heights and their respective
Expand Down Expand Up @@ -192,7 +192,7 @@ contract LightClientV2 is Initializable, OwnableUpgradeable, UUPSUpgradeable {

blocksPerEpoch = numBlockPerEpoch;

maxStateHistoryDuration = maxHistorySeconds;
stateHistoryRetentionPeriod = maxHistorySeconds;

bytes32 initStakeTableComm = computeStakeTableComm(genesis);
votingStakeTableCommitment = initStakeTableComm;
Expand Down

0 comments on commit fac2e76

Please sign in to comment.