Skip to content

Commit 4b44136

Browse files
committed
Renamed transaction hooks
1 parent 12aefff commit 4b44136

File tree

10 files changed

+186
-114
lines changed

10 files changed

+186
-114
lines changed

packages/protocol/src/hooks/AccountStateHook.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export class AccountStateHook extends ProvableTransactionHook {
2121
AccountState
2222
);
2323

24-
public async onTransaction({ transaction }: BeforeTransactionHookArguments) {
24+
public async beforeTransaction({
25+
transaction,
26+
}: BeforeTransactionHookArguments) {
2527
const sender = transaction.sender.value;
2628

2729
const aso = await this.accountState.get(sender);
@@ -49,7 +51,7 @@ export class AccountStateHook extends ProvableTransactionHook {
4951
await this.accountState.set(sender, new AccountState({ nonce: newNonce }));
5052
}
5153

52-
public async onAfterTransaction() {
54+
public async afterTransaction() {
5355
noop();
5456
}
5557
}

packages/protocol/src/hooks/NoopTransactionHook.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { noop } from "@proto-kit/common";
33
import { ProvableTransactionHook } from "../protocol/ProvableTransactionHook";
44

55
export class NoopTransactionHook extends ProvableTransactionHook {
6-
public async onTransaction() {
6+
public async beforeTransaction() {
77
noop();
88
}
99

10-
public async onAfterTransaction() {
10+
public async afterTransaction() {
1111
noop();
1212
}
1313
}

packages/protocol/src/protocol/ProvableTransactionHook.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,11 @@ export interface AfterTransactionHookArguments
6767
export abstract class ProvableTransactionHook<
6868
Config = NoConfig,
6969
> extends TransitioningProtocolModule<Config> {
70-
// TODO Rename to onBeforeTransaction
71-
public abstract onTransaction(
70+
public abstract beforeTransaction(
7271
executionData: BeforeTransactionHookArguments
7372
): Promise<void>;
7473

75-
public abstract onAfterTransaction(
74+
public abstract afterTransaction(
7675
execution: AfterTransactionHookArguments
7776
): Promise<void>;
7877
}

packages/protocol/src/prover/block/BlockProver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export class BlockProverProgrammable extends ZkProgrammable<
169169

170170
// Apply beforeTransaction hook state transitions
171171
const beforeBatch = await this.executeTransactionHooks(
172-
async (module, args) => await module.onTransaction(args),
172+
async (module, args) => await module.beforeTransaction(args),
173173
beforeTxHookArguments
174174
);
175175

@@ -198,7 +198,7 @@ export class BlockProverProgrammable extends ZkProgrammable<
198198
this.stateServiceProvider.popCurrentStateService();
199199

200200
const afterBatch = await this.executeTransactionHooks(
201-
async (module, args) => await module.onAfterTransaction(args),
201+
async (module, args) => await module.afterTransaction(args),
202202
afterTxHookArguments
203203
);
204204
state.pendingSTBatches.push(afterBatch);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Testing strategy:
3+
*
4+
* - Test that hooks are executed and batches are created correctly
5+
* - Transaction
6+
* - Block
7+
* - Test the various static checks on the transaction (signature, verificationKey, network state hash)
8+
* - Test correct construction of the batch and list commitments
9+
* - Test correct integration of the STProof - both defer and notDefer
10+
* - proveBlock: correct blockNumber progression, closed flag (doesn't accepts closed proofs as tx proofs)
11+
*/

packages/protocol/test/prover/statetransition/StateTransitionProver.test.ts

Lines changed: 159 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -44,137 +44,197 @@ describe("StateTransitionProver", () => {
4444
});
4545
}
4646

47-
it.each([
48-
[true, true],
49-
[true, false],
50-
[false, false],
51-
])(
52-
"should retain empty currentBatchHash for padded dummies",
53-
async (applied, witnessRoot) => {
47+
function applyToTree(
48+
tree: RollupMerkleTree,
49+
batches: StateTransitionProvableBatch[],
50+
indizes: number[]
51+
) {
52+
const flat = batches.flatMap((batch) => batch.batch);
53+
indizes.forEach((index) => {
54+
const st = flat[index].stateTransition;
55+
if (st.to.isSome) {
56+
tree.setLeaf(st.path.toBigInt(), st.to.value);
57+
}
58+
});
59+
}
60+
61+
describe("currentBatchHash", () => {
62+
it.each([
63+
[true, true],
64+
[true, false],
65+
[false, false],
66+
])(
67+
"should retain empty currentBatchHash for padded dummies",
68+
async (applied, witnessRoot) => {
69+
const batch = StateTransitionProvableBatch.fromBatches([
70+
{
71+
stateTransitions: [createST(Field(1), Field(0), Field(2))],
72+
applied: Bool(applied),
73+
witnessRoot: Bool(witnessRoot),
74+
},
75+
]);
76+
77+
const tree = new RollupMerkleTree(new InMemoryMerkleTreeStorage());
78+
const witness = tree.getWitness(1n);
79+
80+
const result = await prover.proveBatch(
81+
{
82+
root: tree.getRoot(),
83+
rootAccumulator: Field(0),
84+
batchesHash: Field(0),
85+
currentBatchStateHash: Field(0),
86+
},
87+
batch[0],
88+
{
89+
witnesses: padArray([witness], 4, () =>
90+
RollupMerkleTreeWitness.dummy()
91+
),
92+
},
93+
new AppliedStateTransitionBatchState({
94+
root: tree.getRoot(),
95+
batchHash: Field(0),
96+
})
97+
);
98+
99+
expect(result.currentBatchStateHash.toString()).toStrictEqual("0");
100+
}
101+
);
102+
});
103+
104+
describe("dummies", () => {
105+
it("should fail if dummy is type close", async () => {
106+
expect.assertions(1);
107+
54108
const batch = StateTransitionProvableBatch.fromBatches([
55109
{
56-
stateTransitions: [createST(Field(1), Field(0), Field(2))],
57-
applied: Bool(applied),
58-
witnessRoot: Bool(witnessRoot),
110+
stateTransitions: [ProvableStateTransition.dummy()],
111+
applied: Bool(true),
112+
witnessRoot: Bool(true),
59113
},
60114
]);
61115

62-
const tree = new RollupMerkleTree(new InMemoryMerkleTreeStorage());
116+
const prove = async () =>
117+
await prover.proveBatch(
118+
{
119+
root: Field(RollupMerkleTree.EMPTY_ROOT),
120+
rootAccumulator: Field(0),
121+
batchesHash: Field(0),
122+
currentBatchStateHash: Field(0),
123+
},
124+
batch[0],
125+
{
126+
witnesses: padArray([], 4, RollupMerkleTreeWitness.dummy),
127+
},
128+
new AppliedStateTransitionBatchState({
129+
root: Field(RollupMerkleTree.EMPTY_ROOT),
130+
batchHash: Field(0),
131+
})
132+
);
133+
134+
await expect(prove).rejects.toThrow(
135+
/Dummies have to be of type 'nothing'.*/
136+
);
137+
});
63138

64-
const witness = tree.getWitness(1n);
139+
it("should fail if dummy is in the middle", async () => {
140+
expect.assertions(1);
65141

66-
const result = await prover.proveBatch(
67-
{
68-
root: tree.getRoot(),
69-
rootAccumulator: Field(0),
70-
batchesHash: Field(0),
71-
currentBatchStateHash: Field(0),
72-
},
73-
batch[0],
142+
const batch = StateTransitionProvableBatch.fromBatches([
74143
{
75-
witnesses: padArray([witness], 4, () =>
76-
RollupMerkleTreeWitness.dummy()
77-
),
144+
stateTransitions: [
145+
createST(Field(1), Field(0), Field(2)),
146+
ProvableStateTransition.dummy(),
147+
createST(Field(2), Field(0), Field(3)),
148+
],
149+
applied: Bool(true),
150+
witnessRoot: Bool(true),
78151
},
79-
new AppliedStateTransitionBatchState({
80-
root: tree.getRoot(),
81-
batchHash: Field(0),
82-
})
83-
);
84-
85-
tree.setLeaf(1n, Field(2));
152+
]);
86153

87-
// expect(result.root.toString()).toStrictEqual(tree.getRoot().toString());
88-
expect(result.currentBatchStateHash.toString()).toStrictEqual("0");
89-
}
90-
);
154+
const tree = new RollupMerkleTree(new InMemoryMerkleTreeStorage());
91155

92-
it("should fail if dummy is type close", async () => {
93-
expect.assertions(1);
156+
const inputRoot = tree.getRoot();
94157

95-
const batch = StateTransitionProvableBatch.fromBatches([
96-
{
97-
stateTransitions: [ProvableStateTransition.dummy()],
98-
applied: Bool(true),
99-
witnessRoot: Bool(true),
100-
},
101-
]);
158+
const witness = tree.getWitness(1n);
159+
tree.setLeaf(1n, Field(2));
160+
const witness2 = tree.getWitness(2n);
161+
162+
const prove = async () =>
163+
await prover.proveBatch(
164+
{
165+
root: inputRoot,
166+
rootAccumulator: Field(0),
167+
batchesHash: Field(0),
168+
currentBatchStateHash: Field(0),
169+
},
170+
batch[0],
171+
{
172+
witnesses: [
173+
witness,
174+
RollupMerkleTreeWitness.dummy(),
175+
witness2,
176+
RollupMerkleTreeWitness.dummy(),
177+
],
178+
},
179+
new AppliedStateTransitionBatchState({
180+
root: inputRoot,
181+
batchHash: Field(0),
182+
})
183+
);
184+
185+
await expect(prove).rejects.toThrow(
186+
/Dummies can only be placed on closed batchLists.*/
187+
);
188+
});
189+
});
102190

103-
const prove = async () =>
104-
await prover.proveBatch(
191+
describe("batch progression", () => {
192+
it("should throw away non-applied batches", async () => {
193+
const batch = StateTransitionProvableBatch.fromBatches([
105194
{
106-
root: Field(RollupMerkleTree.EMPTY_ROOT),
107-
rootAccumulator: Field(0),
108-
batchesHash: Field(0),
109-
currentBatchStateHash: Field(0),
195+
stateTransitions: [
196+
createST(Field(1), Field(0), Field(2)),
197+
createST(Field(2), Field(0), Field(3)),
198+
],
199+
applied: Bool(true),
200+
witnessRoot: Bool(true),
110201
},
111-
batch[0],
112202
{
113-
witnesses: padArray([], 4, RollupMerkleTreeWitness.dummy),
203+
stateTransitions: [
204+
createST(Field(2), Field(3), Field(4)),
205+
createST(Field(2), Field(4), Field(5)),
206+
],
207+
applied: Bool(false),
208+
witnessRoot: Bool(true),
114209
},
115-
new AppliedStateTransitionBatchState({
116-
root: Field(RollupMerkleTree.EMPTY_ROOT),
117-
batchHash: Field(0),
118-
})
119-
);
120-
121-
await expect(prove).rejects.toThrow(
122-
/Dummies have to be of type 'nothing'.*/
123-
);
124-
});
125-
126-
it("should fail if dummy is in the middle", async () => {
127-
expect.assertions(1);
128-
129-
const batch = StateTransitionProvableBatch.fromBatches([
130-
{
131-
stateTransitions: [
132-
createST(Field(1), Field(0), Field(2)),
133-
ProvableStateTransition.dummy(),
134-
createST(Field(2), Field(0), Field(3)),
135-
],
136-
applied: Bool(true),
137-
witnessRoot: Bool(true),
138-
},
139-
]);
140-
141-
const tree = new RollupMerkleTree(new InMemoryMerkleTreeStorage());
210+
]);
142211

143-
const inputRoot = tree.getRoot();
212+
const tree = new RollupMerkleTree(new InMemoryMerkleTreeStorage());
144213

145-
const witness = tree.getWitness(1n);
146-
tree.setLeaf(1n, Field(2));
147-
const witness2 = tree.getWitness(2n);
214+
const witness1 = tree.getWitness(1n);
215+
tree.setLeaf(1n, Field(2));
216+
const witness2 = tree.getWitness(2n);
217+
tree.setLeaf(2n, Field(3));
148218

149-
const prove = async () =>
150-
await prover.proveBatch(
219+
const result = await prover.proveBatch(
151220
{
152-
root: inputRoot,
221+
root: Field(RollupMerkleTree.EMPTY_ROOT),
153222
rootAccumulator: Field(0),
154223
batchesHash: Field(0),
155224
currentBatchStateHash: Field(0),
156225
},
157226
batch[0],
158227
{
159-
witnesses: [
160-
witness,
161-
RollupMerkleTreeWitness.dummy(),
162-
witness2,
163-
RollupMerkleTreeWitness.dummy(),
164-
],
228+
witnesses: [witness1, witness2, witness2, witness2],
165229
},
166230
new AppliedStateTransitionBatchState({
167-
root: inputRoot,
231+
root: Field(RollupMerkleTree.EMPTY_ROOT),
168232
batchHash: Field(0),
169233
})
170234
);
171235

172-
await expect(prove).rejects.toThrow(
173-
/Dummies can only be placed on closed batchLists.*/
174-
);
175-
});
176-
177-
describe("batch progression", () => {
178-
it("should apply", () => {}); // TODO
236+
expect(result.root.toString()).toStrictEqual(tree.getRoot().toString());
237+
expect(result.currentBatchStateHash.toString()).toStrictEqual("0");
238+
});
179239
});
180240
});

packages/sdk/test/blockProof/blockProof.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe.skip("blockProof", () => {
8888
);
8989

9090
await mapSequential(txHooks, async (hook) => {
91-
await hook.onTransaction({
91+
await hook.beforeTransaction({
9292
transaction: RuntimeTransaction.fromTransaction({
9393
sender: alice,
9494
nonce: O1UInt64.from(0),

packages/sequencer/src/mempool/private/PrivateMempool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class PrivateMempool extends SequencerModule implements Mempool {
166166

167167
const signedTransaction = tx.toProtocolTransaction();
168168
// eslint-disable-next-line no-await-in-loop
169-
await this.accountStateHook.onTransaction({
169+
await this.accountStateHook.beforeTransaction({
170170
networkState: networkState,
171171
transaction: signedTransaction.transaction,
172172
signature: signedTransaction.signature,

0 commit comments

Comments
 (0)