Skip to content

Commit

Permalink
optimize storage by removing a non efficient variable
Browse files Browse the repository at this point in the history
  • Loading branch information
alysiahuggins committed Jul 22, 2024
1 parent f36a7c4 commit a2e165f
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 145 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.

68 changes: 2 additions & 66 deletions contract-bindings/src/light_client.rs

Large diffs are not rendered by default.

68 changes: 2 additions & 66 deletions contract-bindings/src/light_client_mock.rs

Large diffs are not rendered by default.

9 changes: 1 addition & 8 deletions contracts/src/LightClient.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ contract LightClient is Initializable, OwnableUpgradeable, UUPSUpgradeable {
///@notice Max number of blockStates to record
uint64 public maxStateHistoryAllowed;

///@notice number of block states recorded
uint64 public stateHistoryCount;

///@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 maxStateHistoryAllowed
Expand Down Expand Up @@ -400,18 +397,14 @@ contract LightClient is Initializable, OwnableUpgradeable, UUPSUpgradeable {
function updateStateHistory(uint256 blockNumber, LightClientState memory state) internal {
if (maxStateHistoryAllowed == 0) revert InvalidMaxStateHistory();

if (stateHistoryCount == maxStateHistoryAllowed) {
if (stateHistoryCommitments.length - stateHistoryFirstIndex >= maxStateHistoryAllowed) {
// the arrays already has the max block states allowed so we clear the first non-empty
// element
// the arrays are cleared from a FIFO approach
delete stateHistoryCommitments[stateHistoryFirstIndex];
// and increment the first index so that you know where the first non-zero element is in
// the affected arrays
stateHistoryFirstIndex++;
} else {
// increment the num block states recorded, this increment stops when stateHistoryCount
// == maxStateHistoryAllowed
stateHistoryCount++;
}
// //add the L1 Block to stateUpdateBlockNumbers & HotShot commitment for the genesis state
stateHistoryCommitments.push(
Expand Down
4 changes: 1 addition & 3 deletions contracts/test/LightClient.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ contract LightClient_StateUpdatesTest is LightClientCommonTest {
assertEq(lc.getStateHistoryCount(), blockUpdatesCount + 1);
}

function test_1lBlockUpdatesIsUpdatedOverMaxButOnlyMaxNonZeroElements() public {
function test_1lBlockUpdatesIsUpdatedOverMaxButStateHistoryLimitNotSurpassed() public {
string[] memory cmds = new string[](6);
cmds[0] = "diff-test";
cmds[1] = "mock-consecutive-finalized-states";
Expand Down Expand Up @@ -691,8 +691,6 @@ contract LightClient_StateUpdatesTest is LightClientCommonTest {
emit LC.NewState(states[i].viewNum, states[i].blockHeight, states[i].blockCommRoot);
lc.newFinalizedState(states[i], proofs[i]);
}
// assert that the number of blocks recorded is the same as maxStateHistoryAllowed
assertEq(lc.stateHistoryCount(), lc.maxStateHistoryAllowed());

// because we recorded more blocks than the maxStateHistoryAllowed, the
// stateHistoryFirstIndex can't be zero
Expand Down

0 comments on commit a2e165f

Please sign in to comment.