@aztec/archiver 0.0.0-test.1 → 0.0.1-commit.5476d83
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -6
- package/dest/archiver/archiver.d.ts +147 -57
- package/dest/archiver/archiver.d.ts.map +1 -1
- package/dest/archiver/archiver.js +841 -333
- package/dest/archiver/archiver_store.d.ts +85 -50
- package/dest/archiver/archiver_store.d.ts.map +1 -1
- package/dest/archiver/archiver_store_test_suite.d.ts +1 -1
- package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
- package/dest/archiver/archiver_store_test_suite.js +708 -213
- package/dest/archiver/config.d.ts +5 -21
- package/dest/archiver/config.d.ts.map +1 -1
- package/dest/archiver/config.js +21 -12
- package/dest/archiver/data_retrieval.d.ts +32 -27
- package/dest/archiver/data_retrieval.d.ts.map +1 -1
- package/dest/archiver/data_retrieval.js +197 -94
- package/dest/archiver/errors.d.ts +9 -1
- package/dest/archiver/errors.d.ts.map +1 -1
- package/dest/archiver/errors.js +12 -0
- package/dest/archiver/index.d.ts +3 -4
- package/dest/archiver/index.d.ts.map +1 -1
- package/dest/archiver/index.js +1 -2
- package/dest/archiver/instrumentation.d.ts +12 -6
- package/dest/archiver/instrumentation.d.ts.map +1 -1
- package/dest/archiver/instrumentation.js +58 -17
- package/dest/archiver/kv_archiver_store/block_store.d.ts +48 -11
- package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/block_store.js +216 -63
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +3 -3
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/contract_class_store.js +12 -18
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +11 -8
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/contract_instance_store.js +30 -16
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +50 -35
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.js +88 -46
- package/dest/archiver/kv_archiver_store/log_store.d.ts +2 -2
- package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/log_store.js +18 -46
- package/dest/archiver/kv_archiver_store/message_store.d.ts +23 -17
- package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/message_store.js +150 -48
- package/dest/archiver/structs/data_retrieval.d.ts +1 -1
- package/dest/archiver/structs/inbox_message.d.ts +15 -0
- package/dest/archiver/structs/inbox_message.d.ts.map +1 -0
- package/dest/archiver/structs/inbox_message.js +38 -0
- package/dest/archiver/structs/published.d.ts +3 -11
- package/dest/archiver/structs/published.d.ts.map +1 -1
- package/dest/archiver/structs/published.js +1 -1
- package/dest/archiver/validation.d.ts +17 -0
- package/dest/archiver/validation.d.ts.map +1 -0
- package/dest/archiver/validation.js +98 -0
- package/dest/factory.d.ts +8 -13
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +18 -49
- package/dest/index.d.ts +2 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -1
- package/dest/rpc/index.d.ts +2 -3
- package/dest/rpc/index.d.ts.map +1 -1
- package/dest/rpc/index.js +1 -4
- package/dest/test/index.d.ts +1 -1
- package/dest/test/mock_archiver.d.ts +2 -2
- package/dest/test/mock_archiver.d.ts.map +1 -1
- package/dest/test/mock_l1_to_l2_message_source.d.ts +5 -3
- package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
- package/dest/test/mock_l1_to_l2_message_source.js +14 -1
- package/dest/test/mock_l2_block_source.d.ts +38 -10
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +119 -8
- package/dest/test/mock_structs.d.ts +9 -0
- package/dest/test/mock_structs.d.ts.map +1 -0
- package/dest/test/mock_structs.js +37 -0
- package/package.json +28 -30
- package/src/archiver/archiver.ts +1087 -410
- package/src/archiver/archiver_store.ts +97 -55
- package/src/archiver/archiver_store_test_suite.ts +664 -210
- package/src/archiver/config.ts +28 -41
- package/src/archiver/data_retrieval.ts +279 -125
- package/src/archiver/errors.ts +21 -0
- package/src/archiver/index.ts +2 -3
- package/src/archiver/instrumentation.ts +77 -22
- package/src/archiver/kv_archiver_store/block_store.ts +270 -72
- package/src/archiver/kv_archiver_store/contract_class_store.ts +13 -23
- package/src/archiver/kv_archiver_store/contract_instance_store.ts +35 -27
- package/src/archiver/kv_archiver_store/kv_archiver_store.ts +127 -63
- package/src/archiver/kv_archiver_store/log_store.ts +24 -62
- package/src/archiver/kv_archiver_store/message_store.ts +209 -53
- package/src/archiver/structs/inbox_message.ts +41 -0
- package/src/archiver/structs/published.ts +2 -11
- package/src/archiver/validation.ts +124 -0
- package/src/factory.ts +24 -66
- package/src/index.ts +1 -1
- package/src/rpc/index.ts +1 -5
- package/src/test/mock_archiver.ts +1 -1
- package/src/test/mock_l1_to_l2_message_source.ts +14 -3
- package/src/test/mock_l2_block_source.ts +158 -13
- package/src/test/mock_structs.ts +49 -0
- package/dest/archiver/kv_archiver_store/nullifier_store.d.ts +0 -12
- package/dest/archiver/kv_archiver_store/nullifier_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/nullifier_store.js +0 -73
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +0 -23
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +0 -1
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +0 -49
- package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts +0 -175
- package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts.map +0 -1
- package/dest/archiver/memory_archiver_store/memory_archiver_store.js +0 -636
- package/src/archiver/kv_archiver_store/nullifier_store.ts +0 -97
- package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +0 -61
- package/src/archiver/memory_archiver_store/memory_archiver_store.ts +0 -801
|
@@ -1,71 +1,172 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { L1BlockId } from '@aztec/ethereum';
|
|
2
|
+
import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
|
|
2
3
|
import { Fr } from '@aztec/foundation/fields';
|
|
4
|
+
import { toArray } from '@aztec/foundation/iterable';
|
|
3
5
|
import { createLogger } from '@aztec/foundation/log';
|
|
4
|
-
import
|
|
6
|
+
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
7
|
+
import {
|
|
8
|
+
type AztecAsyncKVStore,
|
|
9
|
+
type AztecAsyncMap,
|
|
10
|
+
type AztecAsyncSingleton,
|
|
11
|
+
type CustomRange,
|
|
12
|
+
mapRange,
|
|
13
|
+
} from '@aztec/kv-store';
|
|
5
14
|
import { InboxLeaf } from '@aztec/stdlib/messaging';
|
|
6
15
|
|
|
7
|
-
import
|
|
16
|
+
import {
|
|
17
|
+
type InboxMessage,
|
|
18
|
+
deserializeInboxMessage,
|
|
19
|
+
serializeInboxMessage,
|
|
20
|
+
updateRollingHash,
|
|
21
|
+
} from '../structs/inbox_message.js';
|
|
22
|
+
|
|
23
|
+
export class MessageStoreError extends Error {
|
|
24
|
+
constructor(
|
|
25
|
+
message: string,
|
|
26
|
+
public readonly inboxMessage: InboxMessage,
|
|
27
|
+
) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = 'MessageStoreError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
8
32
|
|
|
9
|
-
/**
|
|
10
|
-
* LMDB implementation of the ArchiverDataStore interface.
|
|
11
|
-
*/
|
|
12
33
|
export class MessageStore {
|
|
13
|
-
|
|
34
|
+
/** Maps from message index to serialized InboxMessage */
|
|
35
|
+
#l1ToL2Messages: AztecAsyncMap<number, Buffer>;
|
|
36
|
+
/** Maps from hex-stringified message leaf to its index */
|
|
14
37
|
#l1ToL2MessageIndices: AztecAsyncMap<string, bigint>;
|
|
15
|
-
|
|
38
|
+
/** Stores L1 block number and hash of the L1 synchpoint */
|
|
39
|
+
#lastSynchedL1Block: AztecAsyncSingleton<Buffer>;
|
|
40
|
+
/** Stores total messages stored */
|
|
16
41
|
#totalMessageCount: AztecAsyncSingleton<bigint>;
|
|
17
42
|
|
|
18
43
|
#log = createLogger('archiver:message_store');
|
|
19
44
|
|
|
20
|
-
#l1ToL2MessagesSubtreeSize = 2 ** L1_TO_L2_MSG_SUBTREE_HEIGHT;
|
|
21
|
-
|
|
22
45
|
constructor(private db: AztecAsyncKVStore) {
|
|
23
46
|
this.#l1ToL2Messages = db.openMap('archiver_l1_to_l2_messages');
|
|
24
47
|
this.#l1ToL2MessageIndices = db.openMap('archiver_l1_to_l2_message_indices');
|
|
25
|
-
this.#lastSynchedL1Block = db.openSingleton('
|
|
48
|
+
this.#lastSynchedL1Block = db.openSingleton('archiver_last_l1_block_id');
|
|
26
49
|
this.#totalMessageCount = db.openSingleton('archiver_l1_to_l2_message_count');
|
|
27
50
|
}
|
|
28
51
|
|
|
29
|
-
async getTotalL1ToL2MessageCount(): Promise<bigint> {
|
|
52
|
+
public async getTotalL1ToL2MessageCount(): Promise<bigint> {
|
|
30
53
|
return (await this.#totalMessageCount.getAsync()) ?? 0n;
|
|
31
54
|
}
|
|
32
55
|
|
|
33
|
-
/**
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
56
|
+
/** Gets the last L1 block synced. */
|
|
57
|
+
public async getSynchedL1Block(): Promise<L1BlockId | undefined> {
|
|
58
|
+
const buffer = await this.#lastSynchedL1Block.getAsync();
|
|
59
|
+
if (!buffer) {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const reader = BufferReader.asReader(buffer);
|
|
64
|
+
return { l1BlockNumber: reader.readUInt256(), l1BlockHash: Buffer32.fromBuffer(reader.readBytes(Buffer32.SIZE)) };
|
|
39
65
|
}
|
|
40
66
|
|
|
41
|
-
|
|
42
|
-
|
|
67
|
+
/** Sets the last L1 block synced */
|
|
68
|
+
public async setSynchedL1Block(l1Block: L1BlockId): Promise<void> {
|
|
69
|
+
const buffer = serializeToBuffer([l1Block.l1BlockNumber, l1Block.l1BlockHash]);
|
|
70
|
+
await this.#lastSynchedL1Block.set(buffer);
|
|
43
71
|
}
|
|
44
72
|
|
|
45
73
|
/**
|
|
46
74
|
* Append L1 to L2 messages to the store.
|
|
47
|
-
*
|
|
48
|
-
*
|
|
75
|
+
* Requires new messages to be in order and strictly after the last message added.
|
|
76
|
+
* Throws if out of order messages are added or if the rolling hash is invalid.
|
|
49
77
|
*/
|
|
50
|
-
addL1ToL2Messages(messages:
|
|
78
|
+
public addL1ToL2Messages(messages: InboxMessage[]): Promise<void> {
|
|
79
|
+
if (messages.length === 0) {
|
|
80
|
+
return Promise.resolve();
|
|
81
|
+
}
|
|
82
|
+
|
|
51
83
|
return this.db.transactionAsync(async () => {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
84
|
+
let lastMessage = await this.getLastMessage();
|
|
85
|
+
let messageCount = 0;
|
|
86
|
+
|
|
87
|
+
for (const message of messages) {
|
|
88
|
+
// Check messages are inserted in increasing order, but allow reinserting messages.
|
|
89
|
+
if (lastMessage && message.index <= lastMessage.index) {
|
|
90
|
+
const existing = await this.#l1ToL2Messages.getAsync(this.indexToKey(message.index));
|
|
91
|
+
if (existing && deserializeInboxMessage(existing).rollingHash.equals(message.rollingHash)) {
|
|
92
|
+
// We reinsert instead of skipping in case the message was re-orged and got added in a different L1 block.
|
|
93
|
+
this.#log.trace(`Reinserting message with index ${message.index} in the store`);
|
|
94
|
+
await this.#l1ToL2Messages.set(this.indexToKey(message.index), serializeInboxMessage(message));
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
56
97
|
|
|
57
|
-
|
|
98
|
+
throw new MessageStoreError(
|
|
99
|
+
`Cannot insert L1 to L2 message with index ${message.index} before last message with index ${lastMessage.index}`,
|
|
100
|
+
message,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
58
103
|
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
104
|
+
// Check rolling hash is valid.
|
|
105
|
+
const previousRollingHash = lastMessage?.rollingHash ?? Buffer16.ZERO;
|
|
106
|
+
const expectedRollingHash = updateRollingHash(previousRollingHash, message.leaf);
|
|
107
|
+
if (!expectedRollingHash.equals(message.rollingHash)) {
|
|
108
|
+
throw new MessageStoreError(
|
|
109
|
+
`Invalid rolling hash for incoming L1 to L2 message ${message.leaf.toString()} ` +
|
|
110
|
+
`with index ${message.index} ` +
|
|
111
|
+
`(expected ${expectedRollingHash.toString()} from previous hash ${previousRollingHash} but got ${message.rollingHash.toString()})`,
|
|
112
|
+
message,
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Check index corresponds to the L2 block number.
|
|
117
|
+
const [expectedStart, expectedEnd] = InboxLeaf.indexRangeFromL2Block(message.l2BlockNumber);
|
|
118
|
+
if (message.index < expectedStart || message.index >= expectedEnd) {
|
|
119
|
+
throw new MessageStoreError(
|
|
120
|
+
`Invalid index ${message.index} for incoming L1 to L2 message ${message.leaf.toString()} ` +
|
|
121
|
+
`at block ${message.l2BlockNumber} (expected value in range [${expectedStart}, ${expectedEnd}))`,
|
|
122
|
+
message,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Check there are no gaps in the indices within the same block.
|
|
127
|
+
if (
|
|
128
|
+
lastMessage &&
|
|
129
|
+
message.l2BlockNumber === lastMessage.l2BlockNumber &&
|
|
130
|
+
message.index !== lastMessage.index + 1n
|
|
131
|
+
) {
|
|
132
|
+
throw new MessageStoreError(
|
|
133
|
+
`Missing prior message for incoming L1 to L2 message ${message.leaf.toString()} ` +
|
|
134
|
+
`with index ${message.index}`,
|
|
135
|
+
message,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Check the first message in a block has the correct index.
|
|
140
|
+
if (
|
|
141
|
+
(!lastMessage || message.l2BlockNumber > lastMessage.l2BlockNumber) &&
|
|
142
|
+
message.index !== InboxLeaf.smallestIndexFromL2Block(message.l2BlockNumber)
|
|
143
|
+
) {
|
|
144
|
+
throw new MessageStoreError(
|
|
145
|
+
`Message ${message.leaf.toString()} for L2 block ${message.l2BlockNumber} has wrong index ` +
|
|
146
|
+
`${message.index} (expected ${InboxLeaf.smallestIndexFromL2Block(message.l2BlockNumber)})`,
|
|
147
|
+
message,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Perform the insertions.
|
|
152
|
+
await this.#l1ToL2Messages.set(this.indexToKey(message.index), serializeInboxMessage(message));
|
|
153
|
+
await this.#l1ToL2MessageIndices.set(this.leafToIndexKey(message.leaf), message.index);
|
|
154
|
+
messageCount++;
|
|
155
|
+
this.#log.trace(`Inserted L1 to L2 message ${message.leaf} with index ${message.index} into the store`);
|
|
156
|
+
lastMessage = message;
|
|
63
157
|
}
|
|
64
158
|
|
|
65
|
-
|
|
66
|
-
await this
|
|
159
|
+
// Update the L1 sync point to that of the last message added.
|
|
160
|
+
const currentSyncPoint = await this.getSynchedL1Block();
|
|
161
|
+
if (!currentSyncPoint || currentSyncPoint.l1BlockNumber < lastMessage!.l1BlockNumber) {
|
|
162
|
+
await this.setSynchedL1Block({
|
|
163
|
+
l1BlockNumber: lastMessage!.l1BlockNumber,
|
|
164
|
+
l1BlockHash: lastMessage!.l1BlockHash,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
67
167
|
|
|
68
|
-
|
|
168
|
+
// Update total message count with the number of inserted messages.
|
|
169
|
+
await this.increaseTotalMessageCount(messageCount);
|
|
69
170
|
});
|
|
70
171
|
}
|
|
71
172
|
|
|
@@ -74,29 +175,84 @@ export class MessageStore {
|
|
|
74
175
|
* @param l1ToL2Message - The L1 to L2 message.
|
|
75
176
|
* @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
|
|
76
177
|
*/
|
|
77
|
-
getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
|
|
78
|
-
return this.#l1ToL2MessageIndices.getAsync(
|
|
178
|
+
public getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
|
|
179
|
+
return this.#l1ToL2MessageIndices.getAsync(this.leafToIndexKey(l1ToL2Message));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public async getLastMessage(): Promise<InboxMessage | undefined> {
|
|
183
|
+
const [msg] = await toArray(this.#l1ToL2Messages.valuesAsync({ reverse: true, limit: 1 }));
|
|
184
|
+
return msg ? deserializeInboxMessage(msg) : undefined;
|
|
79
185
|
}
|
|
80
186
|
|
|
81
|
-
async getL1ToL2Messages(blockNumber:
|
|
187
|
+
public async getL1ToL2Messages(blockNumber: number): Promise<Fr[]> {
|
|
82
188
|
const messages: Fr[] = [];
|
|
83
|
-
|
|
84
|
-
const startIndex =
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
} else {
|
|
95
|
-
|
|
96
|
-
// We continue iterating over messages here to verify that there are no more messages after the undefined one.
|
|
97
|
-
// --> If this was the case this would imply there is some issue with log fetching.
|
|
189
|
+
|
|
190
|
+
const [startIndex, endIndex] = InboxLeaf.indexRangeFromL2Block(blockNumber);
|
|
191
|
+
let lastIndex = startIndex - 1n;
|
|
192
|
+
|
|
193
|
+
for await (const msgBuffer of this.#l1ToL2Messages.valuesAsync({
|
|
194
|
+
start: this.indexToKey(startIndex),
|
|
195
|
+
end: this.indexToKey(endIndex),
|
|
196
|
+
})) {
|
|
197
|
+
const msg = deserializeInboxMessage(msgBuffer);
|
|
198
|
+
if (msg.l2BlockNumber !== blockNumber) {
|
|
199
|
+
throw new Error(`L1 to L2 message with index ${msg.index} has invalid block number ${msg.l2BlockNumber}`);
|
|
200
|
+
} else if (msg.index !== lastIndex + 1n) {
|
|
201
|
+
throw new Error(`Expected L1 to L2 message with index ${lastIndex + 1n} but got ${msg.index}`);
|
|
98
202
|
}
|
|
203
|
+
lastIndex = msg.index;
|
|
204
|
+
messages.push(msg.leaf);
|
|
99
205
|
}
|
|
206
|
+
|
|
100
207
|
return messages;
|
|
101
208
|
}
|
|
209
|
+
|
|
210
|
+
public async *iterateL1ToL2Messages(range: CustomRange<bigint> = {}): AsyncIterableIterator<InboxMessage> {
|
|
211
|
+
const entriesRange = mapRange(range, this.indexToKey);
|
|
212
|
+
for await (const msgBuffer of this.#l1ToL2Messages.valuesAsync(entriesRange)) {
|
|
213
|
+
yield deserializeInboxMessage(msgBuffer);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
public removeL1ToL2Messages(startIndex: bigint): Promise<void> {
|
|
218
|
+
this.#log.debug(`Deleting L1 to L2 messages from index ${startIndex}`);
|
|
219
|
+
let deleteCount = 0;
|
|
220
|
+
|
|
221
|
+
return this.db.transactionAsync(async () => {
|
|
222
|
+
for await (const [key, msgBuffer] of this.#l1ToL2Messages.entriesAsync({
|
|
223
|
+
start: this.indexToKey(startIndex),
|
|
224
|
+
})) {
|
|
225
|
+
this.#log.trace(`Deleting L1 to L2 message with index ${key - 1} from the store`);
|
|
226
|
+
await this.#l1ToL2Messages.delete(key);
|
|
227
|
+
await this.#l1ToL2MessageIndices.delete(this.leafToIndexKey(deserializeInboxMessage(msgBuffer).leaf));
|
|
228
|
+
deleteCount++;
|
|
229
|
+
}
|
|
230
|
+
await this.increaseTotalMessageCount(-deleteCount);
|
|
231
|
+
this.#log.warn(`Deleted ${deleteCount} L1 to L2 messages from index ${startIndex} from the store`);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
public rollbackL1ToL2MessagesToL2Block(targetBlockNumber: number): Promise<void> {
|
|
236
|
+
this.#log.debug(`Deleting L1 to L2 messages up to target L2 block ${targetBlockNumber}`);
|
|
237
|
+
const startIndex = InboxLeaf.smallestIndexFromL2Block(targetBlockNumber + 1);
|
|
238
|
+
return this.removeL1ToL2Messages(startIndex);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private indexToKey(index: bigint): number {
|
|
242
|
+
return Number(index);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private leafToIndexKey(leaf: Fr): string {
|
|
246
|
+
return leaf.toString();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private async increaseTotalMessageCount(count: bigint | number): Promise<void> {
|
|
250
|
+
if (count === 0) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
return await this.db.transactionAsync(async () => {
|
|
254
|
+
const lastTotalMessageCount = await this.getTotalL1ToL2MessageCount();
|
|
255
|
+
await this.#totalMessageCount.set(lastTotalMessageCount + BigInt(count));
|
|
256
|
+
});
|
|
257
|
+
}
|
|
102
258
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
|
|
2
|
+
import { keccak256 } from '@aztec/foundation/crypto';
|
|
3
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
4
|
+
import { BufferReader, bigintToUInt64BE, numToUInt32BE, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
5
|
+
import type { UInt32 } from '@aztec/stdlib/types';
|
|
6
|
+
|
|
7
|
+
export type InboxMessage = {
|
|
8
|
+
index: bigint;
|
|
9
|
+
leaf: Fr;
|
|
10
|
+
l2BlockNumber: UInt32;
|
|
11
|
+
l1BlockNumber: bigint;
|
|
12
|
+
l1BlockHash: Buffer32;
|
|
13
|
+
rollingHash: Buffer16;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function updateRollingHash(currentRollingHash: Buffer16, leaf: Fr): Buffer16 {
|
|
17
|
+
const input = Buffer.concat([currentRollingHash.toBuffer(), leaf.toBuffer()]);
|
|
18
|
+
return Buffer16.fromBuffer(keccak256(input));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function serializeInboxMessage(message: InboxMessage): Buffer {
|
|
22
|
+
return serializeToBuffer([
|
|
23
|
+
bigintToUInt64BE(message.index),
|
|
24
|
+
message.leaf,
|
|
25
|
+
message.l1BlockHash,
|
|
26
|
+
numToUInt32BE(Number(message.l1BlockNumber)),
|
|
27
|
+
numToUInt32BE(message.l2BlockNumber),
|
|
28
|
+
message.rollingHash,
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function deserializeInboxMessage(buffer: Buffer): InboxMessage {
|
|
33
|
+
const reader = BufferReader.asReader(buffer);
|
|
34
|
+
const index = reader.readUInt64();
|
|
35
|
+
const leaf = reader.readObject(Fr);
|
|
36
|
+
const l1BlockHash = reader.readObject(Buffer32);
|
|
37
|
+
const l1BlockNumber = BigInt(reader.readNumber());
|
|
38
|
+
const l2BlockNumber = reader.readNumber();
|
|
39
|
+
const rollingHash = reader.readObject(Buffer16);
|
|
40
|
+
return { index, leaf, l1BlockHash, l1BlockNumber, l2BlockNumber, rollingHash };
|
|
41
|
+
}
|
|
@@ -1,11 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export type
|
|
3
|
-
data: T;
|
|
4
|
-
l1: L1PublishedData;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
export type L1PublishedData = {
|
|
8
|
-
blockNumber: bigint;
|
|
9
|
-
timestamp: bigint;
|
|
10
|
-
blockHash: string;
|
|
11
|
-
};
|
|
1
|
+
export type { PublishedL2Block } from '@aztec/stdlib/block';
|
|
2
|
+
export type { L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { EpochCache } from '@aztec/epoch-cache';
|
|
2
|
+
import { EpochNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import { compactArray } from '@aztec/foundation/collection';
|
|
4
|
+
import type { Logger } from '@aztec/foundation/log';
|
|
5
|
+
import {
|
|
6
|
+
type AttestationInfo,
|
|
7
|
+
type ValidateBlockNegativeResult,
|
|
8
|
+
type ValidateBlockResult,
|
|
9
|
+
getAttestationInfoFromPayload,
|
|
10
|
+
} from '@aztec/stdlib/block';
|
|
11
|
+
import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
12
|
+
import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
13
|
+
import { ConsensusPayload } from '@aztec/stdlib/p2p';
|
|
14
|
+
|
|
15
|
+
export type { ValidateBlockResult };
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Extracts attestation information from a published checkpoint.
|
|
19
|
+
* Returns info for each attestation, preserving array indices.
|
|
20
|
+
*/
|
|
21
|
+
export function getAttestationInfoFromPublishedCheckpoint({
|
|
22
|
+
checkpoint,
|
|
23
|
+
attestations,
|
|
24
|
+
}: PublishedCheckpoint): AttestationInfo[] {
|
|
25
|
+
const payload = ConsensusPayload.fromCheckpoint(checkpoint);
|
|
26
|
+
return getAttestationInfoFromPayload(payload, attestations);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Validates the attestations submitted for the given checkpoint.
|
|
31
|
+
* Returns true if the attestations are valid and sufficient, false otherwise.
|
|
32
|
+
*/
|
|
33
|
+
export async function validateCheckpointAttestations(
|
|
34
|
+
publishedCheckpoint: PublishedCheckpoint,
|
|
35
|
+
epochCache: EpochCache,
|
|
36
|
+
constants: Pick<L1RollupConstants, 'epochDuration'>,
|
|
37
|
+
logger?: Logger,
|
|
38
|
+
): Promise<ValidateBlockResult> {
|
|
39
|
+
const attestorInfos = getAttestationInfoFromPublishedCheckpoint(publishedCheckpoint);
|
|
40
|
+
const attestors = compactArray(attestorInfos.map(info => ('address' in info ? info.address : undefined)));
|
|
41
|
+
const { checkpoint, attestations } = publishedCheckpoint;
|
|
42
|
+
const headerHash = checkpoint.header.hash();
|
|
43
|
+
const archiveRoot = checkpoint.archive.root.toString();
|
|
44
|
+
const slot = checkpoint.header.slotNumber;
|
|
45
|
+
const epoch: EpochNumber = getEpochAtSlot(slot, constants);
|
|
46
|
+
const { committee, seed } = await epochCache.getCommitteeForEpoch(epoch);
|
|
47
|
+
const logData = { checkpointNumber: checkpoint.number, slot, epoch, headerHash, archiveRoot };
|
|
48
|
+
|
|
49
|
+
logger?.debug(`Validating attestations for checkpoint ${checkpoint.number} at slot ${slot} in epoch ${epoch}`, {
|
|
50
|
+
committee: (committee ?? []).map(member => member.toString()),
|
|
51
|
+
recoveredAttestors: attestorInfos,
|
|
52
|
+
postedAttestations: attestations.map(a => (a.address.isZero() ? a.signature : a.address).toString()),
|
|
53
|
+
...logData,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (!committee || committee.length === 0) {
|
|
57
|
+
logger?.warn(
|
|
58
|
+
`No committee found for epoch ${epoch} at slot ${slot}. Accepting checkpoint without validation.`,
|
|
59
|
+
logData,
|
|
60
|
+
);
|
|
61
|
+
return { valid: true };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const requiredAttestationCount = Math.floor((committee.length * 2) / 3) + 1;
|
|
65
|
+
|
|
66
|
+
const failedValidationResult = <TReason extends ValidateBlockNegativeResult['reason']>(reason: TReason) => ({
|
|
67
|
+
valid: false as const,
|
|
68
|
+
reason,
|
|
69
|
+
block: checkpoint.blocks[0].toBlockInfo(),
|
|
70
|
+
committee,
|
|
71
|
+
seed,
|
|
72
|
+
epoch,
|
|
73
|
+
attestors,
|
|
74
|
+
attestations,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
for (let i = 0; i < attestorInfos.length; i++) {
|
|
78
|
+
const info = attestorInfos[i];
|
|
79
|
+
|
|
80
|
+
// Fail on invalid signatures (no address recovered)
|
|
81
|
+
if (info.status === 'invalid-signature' || info.status === 'empty') {
|
|
82
|
+
logger?.warn(`Attestation with empty or invalid signature at slot ${slot}`, {
|
|
83
|
+
committee,
|
|
84
|
+
invalidIndex: i,
|
|
85
|
+
...logData,
|
|
86
|
+
});
|
|
87
|
+
return { ...failedValidationResult('invalid-attestation'), invalidIndex: i };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Check if the attestor at this index matches the committee member at the same index
|
|
91
|
+
if (info.status === 'recovered-from-signature' || info.status === 'provided-as-address') {
|
|
92
|
+
const signer = info.address.toString();
|
|
93
|
+
const expectedCommitteeMember = committee[i]?.toString();
|
|
94
|
+
|
|
95
|
+
if (!expectedCommitteeMember || signer !== expectedCommitteeMember) {
|
|
96
|
+
logger?.warn(
|
|
97
|
+
`Attestation at index ${i} from ${signer} does not match expected committee member ${expectedCommitteeMember} at slot ${slot}`,
|
|
98
|
+
{
|
|
99
|
+
committee,
|
|
100
|
+
invalidIndex: i,
|
|
101
|
+
...logData,
|
|
102
|
+
},
|
|
103
|
+
);
|
|
104
|
+
return { ...failedValidationResult('invalid-attestation'), invalidIndex: i };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const validAttestationCount = attestorInfos.filter(info => info.status === 'recovered-from-signature').length;
|
|
110
|
+
if (validAttestationCount < requiredAttestationCount) {
|
|
111
|
+
logger?.warn(`Insufficient attestations for checkpoint at slot ${slot}`, {
|
|
112
|
+
requiredAttestations: requiredAttestationCount,
|
|
113
|
+
actualAttestations: validAttestationCount,
|
|
114
|
+
...logData,
|
|
115
|
+
});
|
|
116
|
+
return failedValidationResult('insufficient-attestations');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
logger?.debug(
|
|
120
|
+
`Checkpoint attestations validated successfully for checkpoint ${checkpoint.number} at slot ${slot}`,
|
|
121
|
+
logData,
|
|
122
|
+
);
|
|
123
|
+
return { valid: true };
|
|
124
|
+
}
|
package/src/factory.ts
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
|
-
import type { BlobSinkClientInterface } from '@aztec/blob-sink/client';
|
|
2
1
|
import { createLogger } from '@aztec/foundation/log';
|
|
3
2
|
import type { DataStoreConfig } from '@aztec/kv-store/config';
|
|
4
3
|
import { createStore } from '@aztec/kv-store/lmdb-v2';
|
|
5
|
-
import {
|
|
6
|
-
import { TokenBridgeContractArtifact } from '@aztec/noir-contracts.js/TokenBridge';
|
|
7
|
-
import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
|
|
8
|
-
import { protocolContractNames, protocolContractTreeRoot } from '@aztec/protocol-contracts';
|
|
4
|
+
import { protocolContractNames } from '@aztec/protocol-contracts';
|
|
9
5
|
import { BundledProtocolContractsProvider } from '@aztec/protocol-contracts/providers/bundle';
|
|
10
6
|
import { FunctionType, decodeFunctionSignature } from '@aztec/stdlib/abi';
|
|
11
|
-
import type
|
|
12
|
-
import {
|
|
13
|
-
type ContractClassPublic,
|
|
14
|
-
computePublicBytecodeCommitment,
|
|
15
|
-
getContractClassFromArtifact,
|
|
16
|
-
} from '@aztec/stdlib/contract';
|
|
17
|
-
import type { ArchiverApi, Service } from '@aztec/stdlib/interfaces/server';
|
|
18
|
-
import { getComponentsVersionsFromConfig } from '@aztec/stdlib/versioning';
|
|
19
|
-
import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
|
|
7
|
+
import { type ContractClassPublic, computePublicBytecodeCommitment } from '@aztec/stdlib/contract';
|
|
20
8
|
|
|
21
|
-
import { Archiver } from './archiver/archiver.js';
|
|
9
|
+
import { Archiver, type ArchiverDeps } from './archiver/archiver.js';
|
|
22
10
|
import type { ArchiverConfig } from './archiver/config.js';
|
|
23
|
-
import { KVArchiverDataStore } from './archiver/
|
|
24
|
-
|
|
11
|
+
import { ARCHIVER_DB_VERSION, KVArchiverDataStore } from './archiver/kv_archiver_store/kv_archiver_store.js';
|
|
12
|
+
|
|
13
|
+
export const ARCHIVER_STORE_NAME = 'archiver';
|
|
14
|
+
|
|
15
|
+
/** Creates an archiver store. */
|
|
16
|
+
export async function createArchiverStore(
|
|
17
|
+
userConfig: Pick<ArchiverConfig, 'archiverStoreMapSizeKb' | 'maxLogs'> & DataStoreConfig,
|
|
18
|
+
) {
|
|
19
|
+
const config = {
|
|
20
|
+
...userConfig,
|
|
21
|
+
dataStoreMapSizeKb: userConfig.archiverStoreMapSizeKb ?? userConfig.dataStoreMapSizeKb,
|
|
22
|
+
};
|
|
23
|
+
const store = await createStore(ARCHIVER_STORE_NAME, ARCHIVER_DB_VERSION, config, createLogger('archiver:lmdb'));
|
|
24
|
+
return new KVArchiverDataStore(store, config.maxLogs);
|
|
25
|
+
}
|
|
25
26
|
|
|
26
27
|
/**
|
|
27
28
|
* Creates a local archiver.
|
|
@@ -33,36 +34,12 @@ import { createArchiverClient } from './rpc/index.js';
|
|
|
33
34
|
*/
|
|
34
35
|
export async function createArchiver(
|
|
35
36
|
config: ArchiverConfig & DataStoreConfig,
|
|
36
|
-
|
|
37
|
+
deps: ArchiverDeps,
|
|
37
38
|
opts: { blockUntilSync: boolean } = { blockUntilSync: true },
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const store = await createStore(
|
|
41
|
-
'archiver',
|
|
42
|
-
KVArchiverDataStore.SCHEMA_VERSION,
|
|
43
|
-
config,
|
|
44
|
-
createLogger('archiver:lmdb'),
|
|
45
|
-
);
|
|
46
|
-
const archiverStore = new KVArchiverDataStore(store, config.maxLogs);
|
|
39
|
+
): Promise<Archiver> {
|
|
40
|
+
const archiverStore = await createArchiverStore(config);
|
|
47
41
|
await registerProtocolContracts(archiverStore);
|
|
48
|
-
|
|
49
|
-
return Archiver.createAndSync(config, archiverStore, { telemetry, blobSinkClient }, opts.blockUntilSync);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Creates a remote archiver client.
|
|
54
|
-
* @param config - The archiver configuration.
|
|
55
|
-
* @returns The remote archiver client.
|
|
56
|
-
*/
|
|
57
|
-
export function createRemoteArchiver(config: ArchiverConfig): ArchiverApi {
|
|
58
|
-
if (!config.archiverUrl) {
|
|
59
|
-
throw new Error('Archiver URL is required');
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return createArchiverClient(
|
|
63
|
-
config.archiverUrl,
|
|
64
|
-
getComponentsVersionsFromConfig(config, protocolContractTreeRoot, getVKTreeRoot()),
|
|
65
|
-
);
|
|
42
|
+
return Archiver.createAndSync(config, archiverStore, deps, opts.blockUntilSync);
|
|
66
43
|
}
|
|
67
44
|
|
|
68
45
|
async function registerProtocolContracts(store: KVArchiverDataStore) {
|
|
@@ -73,35 +50,16 @@ async function registerProtocolContracts(store: KVArchiverDataStore) {
|
|
|
73
50
|
const contractClassPublic: ContractClassPublic = {
|
|
74
51
|
...contract.contractClass,
|
|
75
52
|
privateFunctions: [],
|
|
76
|
-
|
|
53
|
+
utilityFunctions: [],
|
|
77
54
|
};
|
|
78
55
|
|
|
79
56
|
const publicFunctionSignatures = contract.artifact.functions
|
|
80
57
|
.filter(fn => fn.functionType === FunctionType.PUBLIC)
|
|
81
58
|
.map(fn => decodeFunctionSignature(fn.name, fn.parameters));
|
|
82
59
|
|
|
83
|
-
await store.registerContractFunctionSignatures(
|
|
60
|
+
await store.registerContractFunctionSignatures(publicFunctionSignatures);
|
|
84
61
|
const bytecodeCommitment = await computePublicBytecodeCommitment(contractClassPublic.packedBytecode);
|
|
85
62
|
await store.addContractClasses([contractClassPublic], [bytecodeCommitment], blockNumber);
|
|
86
63
|
await store.addContractInstances([contract.instance], blockNumber);
|
|
87
64
|
}
|
|
88
65
|
}
|
|
89
|
-
|
|
90
|
-
// TODO(#10007): Remove this method. We are explicitly registering these contracts
|
|
91
|
-
// here to ensure they are available to all nodes and all prover nodes, since the PXE
|
|
92
|
-
// was tweaked to automatically push contract classes to the node it is registered,
|
|
93
|
-
// but other nodes in the network may require the contract classes to be registered as well.
|
|
94
|
-
// TODO(#10007): Remove the dependency on noir-contracts.js from this package once we remove this.
|
|
95
|
-
async function registerCommonContracts(store: KVArchiverDataStore) {
|
|
96
|
-
const blockNumber = 0;
|
|
97
|
-
const artifacts = [TokenBridgeContractArtifact, TokenContractArtifact];
|
|
98
|
-
const classes = await Promise.all(
|
|
99
|
-
artifacts.map(async artifact => ({
|
|
100
|
-
...(await getContractClassFromArtifact(artifact)),
|
|
101
|
-
privateFunctions: [],
|
|
102
|
-
unconstrainedFunctions: [],
|
|
103
|
-
})),
|
|
104
|
-
);
|
|
105
|
-
const bytecodeCommitments = await Promise.all(classes.map(x => computePublicBytecodeCommitment(x.packedBytecode)));
|
|
106
|
-
await store.addContractClasses(classes, bytecodeCommitments, blockNumber);
|
|
107
|
-
}
|
package/src/index.ts
CHANGED
|
@@ -2,4 +2,4 @@ export * from './archiver/index.js';
|
|
|
2
2
|
export * from './factory.js';
|
|
3
3
|
export * from './rpc/index.js';
|
|
4
4
|
|
|
5
|
-
export {
|
|
5
|
+
export { retrieveL2ProofVerifiedEvents } from './archiver/data_retrieval.js';
|
package/src/rpc/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createSafeJsonRpcClient } from '@aztec/foundation/json-rpc/client';
|
|
2
2
|
import { type ArchiverApi, ArchiverApiSchema } from '@aztec/stdlib/interfaces/server';
|
|
3
3
|
import { type ComponentsVersions, getVersioningResponseHandler } from '@aztec/stdlib/versioning';
|
|
4
|
-
import {
|
|
4
|
+
import { makeTracedFetch } from '@aztec/telemetry-client';
|
|
5
5
|
|
|
6
6
|
export function createArchiverClient(
|
|
7
7
|
url: string,
|
|
@@ -14,7 +14,3 @@ export function createArchiverClient(
|
|
|
14
14
|
onResponse: getVersioningResponseHandler(versions),
|
|
15
15
|
});
|
|
16
16
|
}
|
|
17
|
-
|
|
18
|
-
export function createArchiverRpcServer(handler: ArchiverApi) {
|
|
19
|
-
return createTracedJsonRpcServer(handler, ArchiverApiSchema);
|
|
20
|
-
}
|
|
@@ -15,7 +15,7 @@ export class MockArchiver extends MockL2BlockSource implements L2BlockSource, L1
|
|
|
15
15
|
this.messageSource.setL1ToL2Messages(blockNumber, msgs);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
getL1ToL2Messages(blockNumber:
|
|
18
|
+
getL1ToL2Messages(blockNumber: number): Promise<Fr[]> {
|
|
19
19
|
return this.messageSource.getL1ToL2Messages(blockNumber);
|
|
20
20
|
}
|
|
21
21
|
|