Skip to content

Commit a656e5d

Browse files
committed
refactor: optimize events
1 parent eb2cd7d commit a656e5d

3 files changed

Lines changed: 64 additions & 8 deletions

File tree

contracts/src/ProtocolAdapter.sol

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ contract ProtocolAdapter is IProtocolAdapter, ReentrancyGuardTransient, Commitme
3939

4040
RiscZeroVerifierRouter internal immutable _TRUSTED_RISC_ZERO_VERIFIER_ROUTER;
4141

42-
uint256 internal _txCount;
43-
4442
error ZeroNotAllowed();
4543

4644
error ForwarderCallOutputMismatch(bytes expected, bytes actual);
@@ -156,7 +154,7 @@ contract ProtocolAdapter is IProtocolAdapter, ReentrancyGuardTransient, Commitme
156154
}
157155

158156
// Emit the event containing the transaction and new root
159-
emit TransactionExecuted({id: _txCount++, transaction: transaction, newRoot: newRoot});
157+
emit TransactionExecuted({tags: tags, newRoot: newRoot});
160158
}
161159
// slither-disable-end reentrancy-no-eth
162160

@@ -237,6 +235,44 @@ contract ProtocolAdapter is IProtocolAdapter, ReentrancyGuardTransient, Commitme
237235
if (input.appData.externalPayload.length != 0) {
238236
_processForwarderCalls(input, consumed);
239237
}
238+
239+
_emitBlobs(input);
240+
}
241+
242+
/// @notice Emits app data blobs.
243+
/// @param input The logic verifier input containing the app data.
244+
function _emitBlobs(Logic.VerifierInput calldata input) internal {
245+
Logic.ExpirableBlob[] calldata payload = input.appData.resourcePayload;
246+
uint256 n = payload.length;
247+
for (uint256 i = 0; i < n; ++i) {
248+
if (payload[i].deletionCriterion == Logic.DeletionCriterion.Never) {
249+
emit ResourcePayload({tag: input.tag, blob: payload[i].blob});
250+
}
251+
}
252+
253+
payload = input.appData.discoveryPayload;
254+
n = payload.length;
255+
for (uint256 i = 0; i < n; ++i) {
256+
if (payload[i].deletionCriterion == Logic.DeletionCriterion.Never) {
257+
emit DiscoveryPayload({tag: input.tag, blob: payload[i].blob});
258+
}
259+
}
260+
261+
payload = input.appData.externalPayload;
262+
n = payload.length;
263+
for (uint256 i = 0; i < n; ++i) {
264+
if (payload[i].deletionCriterion == Logic.DeletionCriterion.Never) {
265+
emit ExternalPayload({tag: input.tag, blob: payload[i].blob});
266+
}
267+
}
268+
269+
payload = input.appData.applicationPayload;
270+
n = payload.length;
271+
for (uint256 i = 0; i < n; ++i) {
272+
if (payload[i].deletionCriterion == Logic.DeletionCriterion.Never) {
273+
emit ApplicationPayload({tag: input.tag, blob: payload[i].blob});
274+
}
275+
}
240276
}
241277

242278
/// @notice Processes forwarder calls by verifying and executing them.

contracts/src/interfaces/IProtocolAdapter.sol

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,36 @@ import {Transaction} from "../Types.sol";
99
/// @custom:security-contact security@anoma.foundation
1010
interface IProtocolAdapter {
1111
/// @notice Emitted when a transaction is executed.
12-
/// @param id The transaction ID.
13-
/// @param transaction The executed transaction.
12+
/// @param tags The tags of resources being consumed and created in this transaction in alternating appearance.
1413
/// @param newRoot The new commitment tree root.
15-
event TransactionExecuted(uint256 indexed id, Transaction transaction, bytes32 newRoot);
14+
event TransactionExecuted(bytes32[] tags, bytes32 newRoot);
1615

1716
/// @notice Emitted when a forwarder call is executed.
1817
/// @param untrustedForwarder The forwarder contract forwarding the call.
1918
/// @param input The input data for the forwarded call.
2019
/// @param output The expected output data from the forwarded call.
2120
event ForwarderCallExecuted(address indexed untrustedForwarder, bytes input, bytes output);
2221

22+
/// @notice Emitted to store a resource payload blob persistently.
23+
/// @param tag The tag of the resource this blob belongs to.
24+
/// @param blob The blob.
25+
event ResourcePayload(bytes32 indexed tag, bytes blob);
26+
27+
/// @notice Emitted to store a discovery payload blob persistently.
28+
/// @param tag The tag of the resource this blob belongs to.
29+
/// @param blob The blob.
30+
event DiscoveryPayload(bytes32 indexed tag, bytes blob);
31+
32+
/// @notice Emitted to store a external call payload blob persistently.
33+
/// @param tag The tag of the resource this blob belongs to.
34+
/// @param blob The blob.
35+
event ExternalPayload(bytes32 indexed tag, bytes blob);
36+
37+
/// @notice Emitted to store an application payload blob persistently.
38+
/// @param tag The tag of the resource this blob belongs to.
39+
/// @param blob The blob.
40+
event ApplicationPayload(bytes32 indexed tag, bytes blob);
41+
2342
/// @notice Executes a transaction by adding the commitments and nullifiers to the commitment tree and nullifier
2443
/// set, respectively.
2544
/// @param transaction The transaction to execute.

contracts/test/ProtocolAdapterMock.t.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {RiscZeroUtils} from "../src/libs/RiscZeroUtils.sol";
1515
import {ProtocolAdapter} from "../src/ProtocolAdapter.sol";
1616
import {Logic} from "../src/proving/Logic.sol";
1717
import {NullifierSet} from "../src/state/NullifierSet.sol";
18-
import {Transaction, Resource} from "../src/Types.sol";
18+
import {Transaction, Action, Resource} from "../src/Types.sol";
1919

2020
import {ForwarderExample} from "./examples/Forwarder.e.sol";
2121
import {INPUT, EXPECTED_OUTPUT} from "./examples/ForwarderTarget.e.sol";
@@ -29,6 +29,7 @@ contract ProtocolAdapterMockTest is Test {
2929
using RiscZeroUtils for Logic.VerifierInput;
3030
using TxGen for RiscZeroMockVerifier;
3131
using TxGen for Transaction;
32+
using TxGen for Action[];
3233

3334
bytes32 internal constant _CARRIER_LOGIC_REF = bytes32(uint256(123));
3435

@@ -67,7 +68,7 @@ contract ProtocolAdapterMockTest is Test {
6768
bytes32 expectedRoot = cms.computeRoot({treeDepth: MerkleTree.computeMinimalTreeDepth(cms.length) + 1});
6869

6970
vm.expectEmit(address(_mockPa));
70-
emit IProtocolAdapter.TransactionExecuted({id: 0, transaction: txn, newRoot: expectedRoot});
71+
emit IProtocolAdapter.TransactionExecuted({tags: txn.actions.collectTags(), newRoot: expectedRoot});
7172
_mockPa.execute(txn);
7273
}
7374

0 commit comments

Comments
 (0)