Skip to content

Commit

Permalink
feat: deposit_v4 with cheaper depositTopUp
Browse files Browse the repository at this point in the history
  • Loading branch information
86667 committed Jan 7, 2025
1 parent 7dea975 commit 6911fa3
Showing 1 changed file with 43 additions and 14 deletions.
57 changes: 43 additions & 14 deletions zilliqa/src/contracts/deposit_v4.sol
Original file line number Diff line number Diff line change
Expand Up @@ -403,34 +403,61 @@ contract Deposit is UUPSUpgradeable {
i <= currentEpoch() + 2 && i < $.latestComputedEpoch + 3;
i++
) {
// The operation we want to do is: `_committee[i % 3] = latestComputedCommittee` but we need to do it
// explicitly because `stakers` is a mapping.
// The operation we want to do is: `_committee[i % 3] = latestComputedCommittee` but we are careful to write only when necessary
Committee storage committeeToUpdate = $._committee[i % 3];
bool stakerIndexChanged = false;

// Delete old keys from `_committee[i % 3].stakers`.
// Overwrite existing staker's data
for (
uint256 j = 0;
j < $._committee[i % 3].stakerKeys.length;
j < committeeToUpdate.stakerKeys.length;
j++
) {
delete $._committee[i % 3].stakers[
$._committee[i % 3].stakerKeys[j]
];
bytes memory stakerKey = committeeToUpdate.stakerKeys[j];
CommitteeStakerEntry storage stakerInLatestCommittee = latestComputedCommittee.stakers[stakerKey];
// If staker exists in latest then update in new
if (stakerInLatestCommittee.index != 0) {
CommitteeStakerEntry storage stakerInCommitteeToUpdate = committeeToUpdate.stakers[stakerKey];
if (stakerInLatestCommittee.index != stakerInCommitteeToUpdate.index) {
stakerIndexChanged = true;
stakerInCommitteeToUpdate.index = stakerInLatestCommittee.index;
}
if (stakerInLatestCommittee.balance != stakerInCommitteeToUpdate.balance) {
stakerInCommitteeToUpdate.balance = stakerInLatestCommittee.balance;
}
// Otherwise remove them
} else {
delete committeeToUpdate.stakers[stakerKey];
stakerIndexChanged = true;
}
}

$._committee[i % 3].totalStake = latestComputedCommittee
.totalStake;
$._committee[i % 3].stakerKeys = latestComputedCommittee
.stakerKeys;
// Now add any new stakers
for (
uint256 j = 0;
j < latestComputedCommittee.stakerKeys.length;
j++
) {
bytes storage stakerKey = latestComputedCommittee
.stakerKeys[j];
$._committee[i % 3].stakers[
stakerKey
] = latestComputedCommittee.stakers[stakerKey];
if (committeeToUpdate.stakers[stakerKey].index == 0) {
committeeToUpdate.stakers[
stakerKey
] = latestComputedCommittee.stakers[stakerKey];
stakerIndexChanged = true;
}
}

if (latestComputedCommittee.totalStake != committeeToUpdate.totalStake) {
committeeToUpdate.totalStake = latestComputedCommittee
.totalStake;
}
if (stakerIndexChanged) {
uint256 gasBefore = gasleft();
committeeToUpdate.stakerKeys = latestComputedCommittee
.stakerKeys;
console.log("stakerKeys write: Gas used", gasBefore - gasleft());

}
}

Expand Down Expand Up @@ -537,10 +564,12 @@ contract Deposit is UUPSUpgradeable {
futureCommittee.stakerKeys.push(blsPubKey);

emit StakerAdded(blsPubKey, nextUpdate(), msg.value);

}

function depositTopup() public payable {
DepositStorage storage $ = _getDepositStorage();

bytes storage stakerKey = $._stakerKeys[msg.sender];
if (stakerKey.length == 0) {
revert KeyNotStaked();
Expand Down

0 comments on commit 6911fa3

Please sign in to comment.