1
1
import { AztecAddress , BlockHeader , CompleteAddress } from '@aztec/circuits.js' ;
2
- import { Fr } from '@aztec/foundation/fields' ;
2
+ import { Fr , Point } from '@aztec/foundation/fields' ;
3
3
import { AztecArray , AztecKVStore , AztecMap , AztecMultiMap , AztecSingleton } from '@aztec/kv-store' ;
4
4
import { ContractDao , MerkleTreeId , NoteFilter , PublicKey } from '@aztec/types' ;
5
5
6
6
import { NoteDao } from './note_dao.js' ;
7
7
import { PxeDatabase } from './pxe_database.js' ;
8
8
9
9
/** Serialized structure of a block header */
10
- type SerializedBlockHeader = {
10
+ type SynchronizedBlock = {
11
11
/** The tree roots when the block was created */
12
12
roots : Record < MerkleTreeId , string > ;
13
13
/** The hash of the global variables */
14
14
globalVariablesHash : string ;
15
+ /** The block number */
16
+ blockNumber : number ;
15
17
} ;
16
18
17
19
/**
18
20
* A PXE database backed by LMDB.
19
21
*/
20
22
export class KVPxeDatabase implements PxeDatabase {
21
- #blockHeader : AztecSingleton < SerializedBlockHeader > ;
23
+ #synchronizedBlock : AztecSingleton < SynchronizedBlock > ;
22
24
#addresses: AztecArray < Buffer > ;
23
25
#addressIndex: AztecMap < string , number > ;
24
26
#authWitnesses: AztecMap < string , Buffer [ ] > ;
@@ -30,6 +32,7 @@ export class KVPxeDatabase implements PxeDatabase {
30
32
#notesByStorageSlot: AztecMultiMap < string , number > ;
31
33
#notesByTxHash: AztecMultiMap < string , number > ;
32
34
#notesByOwner: AztecMultiMap < string , number > ;
35
+ #syncedBlockPerPublicKey: AztecMap < string , number > ;
33
36
#db: AztecKVStore ;
34
37
35
38
constructor ( db : AztecKVStore ) {
@@ -40,9 +43,11 @@ export class KVPxeDatabase implements PxeDatabase {
40
43
41
44
this . #authWitnesses = db . createMap ( 'auth_witnesses' ) ;
42
45
this . #capsules = db . createArray ( 'capsules' ) ;
43
- this . #blockHeader = db . createSingleton ( 'block_header' ) ;
44
46
this . #contracts = db . createMap ( 'contracts' ) ;
45
47
48
+ this . #synchronizedBlock = db . createSingleton ( 'block_header' ) ;
49
+ this . #syncedBlockPerPublicKey = db . createMap ( 'synced_block_per_public_key' ) ;
50
+
46
51
this . #notes = db . createArray ( 'notes' ) ;
47
52
this . #nullifiedNotes = db . createMap ( 'nullified_notes' ) ;
48
53
@@ -173,7 +178,7 @@ export class KVPxeDatabase implements PxeDatabase {
173
178
}
174
179
175
180
getTreeRoots ( ) : Record < MerkleTreeId , Fr > {
176
- const roots = this . #blockHeader . get ( ) ?. roots ;
181
+ const roots = this . #synchronizedBlock . get ( ) ?. roots ;
177
182
if ( ! roots ) {
178
183
throw new Error ( `Tree roots not set` ) ;
179
184
}
@@ -188,8 +193,9 @@ export class KVPxeDatabase implements PxeDatabase {
188
193
} ;
189
194
}
190
195
191
- async setBlockHeader ( blockHeader : BlockHeader ) : Promise < void > {
192
- await this . #blockHeader. set ( {
196
+ async setBlockData ( blockNumber : number , blockHeader : BlockHeader ) : Promise < void > {
197
+ await this . #synchronizedBlock. set ( {
198
+ blockNumber,
193
199
globalVariablesHash : blockHeader . globalVariablesHash . toString ( ) ,
194
200
roots : {
195
201
[ MerkleTreeId . NOTE_HASH_TREE ] : blockHeader . noteHashTreeRoot . toString ( ) ,
@@ -202,8 +208,12 @@ export class KVPxeDatabase implements PxeDatabase {
202
208
} ) ;
203
209
}
204
210
211
+ getBlockNumber ( ) : number | undefined {
212
+ return this . #synchronizedBlock. get ( ) ?. blockNumber ;
213
+ }
214
+
205
215
getBlockHeader ( ) : BlockHeader {
206
- const value = this . #blockHeader . get ( ) ;
216
+ const value = this . #synchronizedBlock . get ( ) ;
207
217
if ( ! value ) {
208
218
throw new Error ( `Block header not set` ) ;
209
219
}
@@ -261,6 +271,14 @@ export class KVPxeDatabase implements PxeDatabase {
261
271
return Promise . resolve ( Array . from ( this . #addresses) . map ( v => CompleteAddress . fromBuffer ( v ) ) ) ;
262
272
}
263
273
274
+ getSynchedBlockNumberForPublicKey ( publicKey : Point ) : number | undefined {
275
+ return this . #syncedBlockPerPublicKey. get ( publicKey . toString ( ) ) ;
276
+ }
277
+
278
+ setSynchedBlockNumberForPublicKey ( publicKey : Point , blockNumber : number ) : Promise < boolean > {
279
+ return this . #syncedBlockPerPublicKey. set ( publicKey . toString ( ) , blockNumber ) ;
280
+ }
281
+
264
282
estimateSize ( ) : number {
265
283
const notesSize = Array . from ( this . #getAllNonNullifiedNotes( ) ) . reduce ( ( sum , note ) => sum + note . getSize ( ) , 0 ) ;
266
284
const authWitsSize = Array . from ( this . #authWitnesses. values ( ) ) . reduce (
0 commit comments