Skip to content

Commit 6746d32

Browse files
authored
chore: Reduce gas cost for tally slash proposer (#16617)
Reduce gas cost for executing a slash in the tally slash proposer. Builds on [16514](#16514). ## L1 changes - Votes are represented with fixed-size arrays, so we save a storage operation by not having to store or load the array length. - Slash payloads are implemented using EIP1167 clones to reduce deployment cost. - Tally matrix is compacted to reduce memory usage, and votes are processed in batches with early exits (since most vote arrays will be sparse). - Add a config option to deploy no slasher. - Generic improvements like unchecked operations, loop unrolling, using calldata over memory, etc. ## Other changes - Add extra checks during deployment to alert if a tx reverted. - Default tests to deploy no slasher (required to avoid triggering new validations in the tally slashing proposer contract that failed when epoch lengths were set to very small values). ## Gas benchmarks Given the following settings: ``` uint256 constant VALIDATOR_COUNT = 128; uint256 constant COMMITTEE_SIZE = 48; uint256 constant ROUND_SIZE_IN_EPOCHS = 6; uint256 constant EPOCH_DURATION = 32; ``` Gas costs before and after this change: | How many slashed | Before | After | Reduction | | -------- | -------- | -------- | -- | | 1 | 4003168 | 1461217 | 63% | | 48 | 12299046 | 8362076 | 32% |
2 parents 6689a2f + d4f6c55 commit 6746d32

23 files changed

+1202
-140
lines changed

l1-contracts/src/core/RollupCore.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ contract RollupCore is EIP712("Aztec Rollup", "1"), Ownable, IStakingCore, IVali
238238
// We call one external library or another based on the slasher flavor
239239
// This allows us to keep the slash flavors in separate external libraries so we do not exceed max contract size
240240
// Note that we do not deploy a slasher if we run with no committees (i.e. targetCommitteeSize == 0)
241-
if (_config.targetCommitteeSize == 0) {
241+
if (_config.targetCommitteeSize == 0 || _config.slasherFlavor == SlasherFlavor.NONE) {
242242
slasher = ISlasher(address(0));
243243
} else if (_config.slasherFlavor == SlasherFlavor.TALLY) {
244244
slasher = TallySlasherDeploymentExtLib.deployTallySlasher(

l1-contracts/src/core/interfaces/ISlasher.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ pragma solidity >=0.8.27;
55
import {IPayload} from "@aztec/governance/interfaces/IPayload.sol";
66

77
enum SlasherFlavor {
8-
EMPIRE,
9-
TALLY
8+
NONE,
9+
TALLY,
10+
EMPIRE
1011
}
1112

1213
interface ISlasher {

l1-contracts/src/core/libraries/Errors.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,8 @@ library Errors {
197197
error TallySlashingProposer__VotingNotOpen(SlashRound currentRound);
198198
error TallySlashingProposer__SlashOffsetMustBeGreaterThanZero(uint256 slashOffset);
199199
error TallySlashingProposer__InvalidEpochIndex(uint256 epochIndex, uint256 roundSizeInEpochs);
200+
error TallySlashingProposer__VoteSizeTooBig(uint256 voteSize, uint256 maxSize);
201+
202+
// SlashPayloadLib
203+
error SlashPayload_ArraySizeMismatch(uint256 expected, uint256 actual);
200204
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright 2024 Aztec Labs.
3+
pragma solidity >=0.8.27;
4+
5+
import {Errors} from "./Errors.sol";
6+
7+
/**
8+
* @title SlashPayloadLib
9+
* @author Aztec Labs
10+
* @notice Library for encoding immutable arguments for SlashPayloadCloneable contracts
11+
* @dev Provides utilities for encoding validator addresses and amounts into a format
12+
* suitable for use with EIP-1167 minimal proxy clones with immutable arguments
13+
*/
14+
library SlashPayloadLib {
15+
/**
16+
* @notice Encode immutable arguments for SlashPayloadCloneable clones
17+
* @dev Encodes data in the format expected by SlashPayloadCloneable._getImmutableArgs()
18+
* Layout: [validatorSelection(20 bytes)][arrayLength(32 bytes)][validators+amounts array data]
19+
* Each validator entry: [address(20 bytes)][amount(12 bytes for uint96)]
20+
* @param _validatorSelection Address of the validator selection contract
21+
* @param _validators Array of validator addresses to slash
22+
* @param _amounts Array of amounts to slash for each validator (uint96 values)
23+
* @return Encoded arguments for use with cloneDeterministicWithImmutableArgs
24+
*/
25+
function encodeImmutableArgs(address _validatorSelection, address[] memory _validators, uint96[] memory _amounts)
26+
internal
27+
pure
28+
returns (bytes memory)
29+
{
30+
require(
31+
_validators.length == _amounts.length, Errors.SlashPayload_ArraySizeMismatch(_validators.length, _amounts.length)
32+
);
33+
34+
// Calculate total size: 20 bytes (address) + 32 bytes (length) + (20 + 12) * length
35+
uint256 dataSize = 52 + 32 * _validators.length;
36+
bytes memory data = new bytes(dataSize);
37+
38+
assembly {
39+
let ptr := add(data, 0x20)
40+
41+
// Store validator selection address (20 bytes)
42+
// Shift left by 96 bits (12 bytes) to align to the left of the 32-byte slot
43+
mstore(ptr, shl(96, _validatorSelection))
44+
ptr := add(ptr, 0x14) // Move 20 bytes forward
45+
46+
// Store array length (32 bytes)
47+
mstore(ptr, mload(_validators))
48+
ptr := add(ptr, 0x20) // Move 32 bytes forward
49+
50+
// Store validators and amounts
51+
let len := mload(_validators)
52+
let validatorsPtr := add(_validators, 0x20)
53+
let amountsPtr := add(_amounts, 0x20)
54+
55+
for { let i := 0 } lt(i, len) { i := add(i, 1) } {
56+
// Store validator address (20 bytes)
57+
// Shift left by 96 bits to align to the left of the 32-byte slot
58+
mstore(ptr, shl(96, mload(add(validatorsPtr, mul(i, 0x20)))))
59+
ptr := add(ptr, 0x14) // Move 20 bytes forward
60+
61+
// Store amount (12 bytes for uint96)
62+
// Shift left by 160 bits (20 bytes) to align to the left of the remaining space
63+
mstore(ptr, shl(160, mload(add(amountsPtr, mul(i, 0x20)))))
64+
ptr := add(ptr, 0x0c) // Move 12 bytes forward
65+
}
66+
}
67+
68+
return data;
69+
}
70+
}

0 commit comments

Comments
 (0)