Skip to content

Commit 3099b38

Browse files
committed
Finished signed settlement
1 parent 9bb93cf commit 3099b38

File tree

11 files changed

+64
-46
lines changed

11 files changed

+64
-46
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595

9696
"overrides": [
9797
{
98-
"files": ["*.test.ts"],
98+
"files": ["*.test.ts", "**/test/**/*.ts"],
9999
"rules": {
100100
"no-console": "off",
101101
"import/no-extraneous-dependencies": "off",

packages/protocol/src/settlement/contracts/SettlementSmartContract.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class SettlementSmartContract
9797
};
9898

9999
@state(Field) public sequencerKey = State<Field>();
100-
@state(UInt32) public lastSettlementL1Block = State<UInt32>();
100+
@state(UInt32) public lastSettlementL1BlockHeight = State<UInt32>();
101101

102102
@state(Field) public stateRoot = State<Field>();
103103
@state(Field) public networkStateHash = State<Field>();
@@ -148,8 +148,8 @@ export class SettlementSmartContract
148148
const networkStateHash = this.networkStateHash.getAndRequireEquals();
149149
const blockHashRoot = this.blockHashRoot.getAndRequireEquals();
150150
const sequencerKey = this.sequencerKey.getAndRequireEquals();
151-
const lastSettlementL1Block =
152-
this.lastSettlementL1Block.getAndRequireEquals();
151+
const lastSettlementL1BlockHeight =
152+
this.lastSettlementL1BlockHeight.getAndRequireEquals();
153153
const onChainDispatchContractAddressX =
154154
this.dispatchContractAddressX.getAndRequireEquals();
155155

@@ -168,11 +168,11 @@ export class SettlementSmartContract
168168
const promisedMessagesHash = dispatchContract.promisedMessagesHash.get();
169169

170170
// Get block height and use the lower bound for all ops
171-
const minBlockIncluded = this.network.globalSlotSinceGenesis.get();
172-
this.network.globalSlotSinceGenesis.requireBetween(
173-
minBlockIncluded,
171+
const minBlockHeightIncluded = this.network.blockchainLength.get();
172+
this.network.blockchainLength.requireBetween(
173+
minBlockHeightIncluded,
174174
// 5 because that is the length the newPromisedMessagesHash will be valid
175-
minBlockIncluded.add(4)
175+
minBlockHeightIncluded.add(4)
176176
);
177177

178178
// Check signature/escape catch
@@ -182,22 +182,18 @@ export class SettlementSmartContract
182182
);
183183
const signatureValid = signature.verify(publicKey, [
184184
BATCH_SIGNATURE_PREFIX,
185-
lastSettlementL1Block.value,
185+
lastSettlementL1BlockHeight.value,
186186
]);
187-
const escapeHatchActivated = lastSettlementL1Block
187+
const escapeHatchActivated = lastSettlementL1BlockHeight
188188
.add(UInt32.from(escapeHatchSlotsInterval))
189-
.lessThan(minBlockIncluded);
189+
.lessThan(minBlockHeightIncluded);
190190
signatureValid
191191
.or(escapeHatchActivated)
192192
.assertTrue(
193193
"Sequencer signature not valid and escape hatch not activated"
194194
);
195195

196196
// Assert correctness of networkState witness
197-
Provable.log("Network State Hash ", networkStateHash);
198-
Provable.log("input Hash ", inputNetworkState.hash());
199-
Provable.log("equals ", inputNetworkState.hash().equals(networkStateHash));
200-
201197
inputNetworkState
202198
.hash()
203199
.assertEquals(networkStateHash, "InputNetworkState witness not valid");
@@ -218,7 +214,7 @@ export class SettlementSmartContract
218214
blockHashRoot,
219215
stateRoot,
220216
networkStateHash,
221-
lastSettlementL1Block,
217+
lastSettlementL1BlockHeight,
222218
sequencerKey: publicKey,
223219
};
224220
const inputs: SettlementHookInputs = {
@@ -227,7 +223,7 @@ export class SettlementSmartContract
227223
newPromisedMessagesHash,
228224
fromNetworkState: inputNetworkState,
229225
toNetworkState: outputNetworkState,
230-
currentL1Block: minBlockIncluded,
226+
currentL1BlockHeight: minBlockHeightIncluded,
231227
};
232228
await mapSequential(hooks, async (hook) => {
233229
await hook.beforeSettlement(this, inputs);
@@ -238,9 +234,6 @@ export class SettlementSmartContract
238234
blockProof.publicInput.stateRoot,
239235
"Input state root not matching"
240236
);
241-
Provable.log("Network State Hash ", networkStateHash);
242-
Provable.log("input Hash ", inputNetworkState.hash());
243-
Provable.log("Proof Hash ", blockProof.publicInput.networkStateHash);
244237

245238
networkStateHash.assertEquals(
246239
blockProof.publicInput.networkStateHash,
@@ -270,7 +263,7 @@ export class SettlementSmartContract
270263
newPromisedMessagesHash
271264
);
272265

273-
this.lastSettlementL1Block.set(minBlockIncluded);
266+
this.lastSettlementL1BlockHeight.set(minBlockHeightIncluded);
274267
}
275268

276269
@method

packages/protocol/src/settlement/modularity/ProvableSettlementHook.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { SettlementSmartContract } from "../contracts/SettlementSmartContra
77

88
export type SettlementStateRecord = {
99
sequencerKey: PublicKey;
10-
lastSettlementL1Block: UInt32;
10+
lastSettlementL1BlockHeight: UInt32;
1111

1212
stateRoot: Field;
1313
networkStateHash: Field;
@@ -20,7 +20,7 @@ export type SettlementHookInputs = {
2020
toNetworkState: NetworkState;
2121
newPromisedMessagesHash: Field;
2222
contractState: SettlementStateRecord;
23-
currentL1Block: UInt32;
23+
currentL1BlockHeight: UInt32;
2424
};
2525

2626
export abstract class ProvableSettlementHook<

packages/protocol/src/settlement/modules/NetworkStateSettlementModule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ export class NetworkStateSettlementModule extends ProvableSettlementHook<Network
1919
fromNetworkState,
2020
toNetworkState,
2121
contractState,
22-
currentL1Block,
22+
currentL1BlockHeight,
2323
}: SettlementHookInputs
2424
) {
25-
const { lastSettlementL1Block } = contractState;
25+
const { lastSettlementL1BlockHeight } = contractState;
2626

2727
const blocksPerL1Block = this.config.blocksPerL1Block.toConstant();
2828

29-
const numL1Blocks = currentL1Block.sub(lastSettlementL1Block);
29+
const numL1Blocks = currentL1BlockHeight.sub(lastSettlementL1BlockHeight);
3030
const expectedHeightDiff = numL1Blocks.toUInt64().mul(blocksPerL1Block);
3131

3232
const actualHeightDiff = toNetworkState.block.height.sub(

packages/sequencer/src/protocol/baselayer/accounts/MinaBlockchainAccounts.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { injectable, inject } from "tsyringe";
22
import { range } from "@proto-kit/common";
3-
import { BaseLayer } from "../BaseLayer";
4-
import { MinaBaseLayer } from "../MinaBaseLayer";
53
import { PrivateKey, Mina, Lightnet, PublicKey, AccountUpdate } from "o1js";
4+
65
import { MinaTransactionSender } from "../../../settlement/transactions/MinaTransactionSender";
6+
import { BaseLayer } from "../BaseLayer";
7+
import { MinaBaseLayer } from "../MinaBaseLayer";
78
import { FeeStrategy } from "../fees/FeeStrategy";
89

910
type LocalBlockchain = Awaited<ReturnType<typeof Mina.LocalBlockchain>>;

packages/sequencer/src/protocol/baselayer/fees/ConstantFeeStrategy.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { noop } from "@proto-kit/common";
2+
13
import {
24
sequencerModule,
35
SequencerModule,
46
} from "../../../sequencer/builder/SequencerModule";
5-
import { noop } from "@proto-kit/common";
7+
68
import { FeeStrategy } from "./FeeStrategy";
79

810
export type ConstantFeeStrategyConfig = {

packages/sequencer/src/settlement/SettlementModule.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,15 @@ export class SettlementModule
269269

270270
private async fetchContractAccounts() {
271271
const contracts = this.getContracts();
272-
if (contracts !== undefined) {
273-
const c1 = await fetchAccount({
272+
if (
273+
contracts !== undefined &&
274+
this.baseLayer.config.network.type !== "local"
275+
) {
276+
await fetchAccount({
274277
publicKey: contracts.settlement.address,
275278
tokenId: contracts.settlement.tokenId,
276279
});
277-
const c2 = await fetchAccount({
280+
await fetchAccount({
278281
publicKey: contracts.dispatch.address,
279282
tokenId: contracts.dispatch.tokenId,
280283
});

packages/sequencer/src/settlement/messages/MinaIncomingMessageAdapter.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ export class MinaIncomingMessageAdapter implements IncomingMessageAdapter {
102102
);
103103
}
104104

105-
if (actions.length > 0) {
106-
console.log(params.fromL1BlockHeight);
107-
}
108-
109105
const events = await network.fetchEvents(address, undefined, {
110106
from: UInt32.from(Math.max(params.fromL1BlockHeight - 5, 0)),
111107
});

packages/sequencer/src/settlement/transactions/MinaTransactionSender.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,36 @@ interface TxEvents extends EventsRecord {
2424
rejected: [any];
2525
}
2626

27-
class OnceEventEmitter<
27+
class OnceReplayingEventEmitter<
2828
Events extends EventsRecord,
2929
> extends EventEmitter<Events> {
30+
public emitted: Partial<Events> = {};
31+
3032
public emit<Key extends keyof Events>(
3133
event: Key,
3234
...parameters: Events[Key]
3335
) {
3436
super.emit(event, ...parameters);
37+
this.emitted[event] = parameters;
3538
this.listeners[event] = [];
3639
}
40+
41+
public onAll(listener: (event: keyof Events, args: unknown[]) => void) {
42+
Object.entries(this.emitted).forEach(([key, params]) => {
43+
if (params !== undefined) listener(key, params);
44+
});
45+
super.onAll(listener);
46+
}
47+
48+
public on<Key extends keyof Events>(
49+
event: Key,
50+
listener: (...args: Events[Key]) => void
51+
) {
52+
if (this.emitted[event] !== undefined) {
53+
listener(...this.emitted[event]!);
54+
}
55+
super.on(event, listener);
56+
}
3757
}
3858

3959
@injectable()
@@ -111,7 +131,7 @@ export class MinaTransactionSender {
111131
// eslint-disable-next-line no-plusplus
112132
const id = this.txIdCursor++;
113133
this.cache.push({ tx, id });
114-
const eventEmitter = new OnceEventEmitter<TxEvents>();
134+
const eventEmitter = new OnceReplayingEventEmitter<TxEvents>();
115135
this.txStatusEmitters[id] = eventEmitter;
116136

117137
let removedLastIteration = 0;

packages/sequencer/test/TestingSequencer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
BlockProducerModule,
1515
VanillaTaskWorkerModules,
1616
} from "../src";
17+
import { ConstantFeeStrategy } from "../src/protocol/baselayer/fees/ConstantFeeStrategy";
1718

1819
export interface DefaultTestingSequencerModules extends SequencerModulesRecord {
1920
Database: typeof InMemoryDatabase;
@@ -24,6 +25,7 @@ export interface DefaultTestingSequencerModules extends SequencerModulesRecord {
2425
BlockProducerModule: typeof BlockProducerModule;
2526
BlockTrigger: typeof ManualBlockTrigger;
2627
TaskQueue: typeof LocalTaskQueue;
28+
FeeStrategy: typeof ConstantFeeStrategy;
2729
}
2830

2931
export function testingSequencerFromModules<
@@ -38,7 +40,6 @@ export function testingSequencerFromModules<
3840
...additionalTaskWorkerModules,
3941
});
4042

41-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
4243
const defaultModules: DefaultTestingSequencerModules = {
4344
Database: InMemoryDatabase,
4445
Mempool: PrivateMempool,
@@ -48,6 +49,7 @@ export function testingSequencerFromModules<
4849
BlockProducerModule,
4950
BlockTrigger: ManualBlockTrigger,
5051
TaskQueue: LocalTaskQueue,
52+
FeeStrategy: ConstantFeeStrategy,
5153
} as DefaultTestingSequencerModules;
5254

5355
return Sequencer.from({

0 commit comments

Comments
 (0)