From 90ed27247254b4cd309fdce43c02da3866c613b2 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Sat, 29 Apr 2023 14:14:27 +0200 Subject: [PATCH] Filter by min-max key when loading pubkeys from db --- .../attestation/attestationByTargetRepository.ts | 7 ++++++- .../src/slashingProtection/block/blockBySlotRepository.ts | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/validator/src/slashingProtection/attestation/attestationByTargetRepository.ts b/packages/validator/src/slashingProtection/attestation/attestationByTargetRepository.ts index f59d1c5dfe9..b825e153e56 100644 --- a/packages/validator/src/slashingProtection/attestation/attestationByTargetRepository.ts +++ b/packages/validator/src/slashingProtection/attestation/attestationByTargetRepository.ts @@ -27,6 +27,9 @@ export class AttestationByTargetRepository { private readonly bucketId: string; private readonly dbReqOpts: DbReqOpts; + private readonly minKey: Uint8Array; + private readonly maxKey: Uint8Array; + constructor(opts: DatabaseApiOptions) { this.db = opts.controller; this.type = new ContainerType({ @@ -36,6 +39,8 @@ export class AttestationByTargetRepository { }); // casing doesn't matter this.bucketId = getBucketNameByValue(this.bucket); this.dbReqOpts = {bucketId: this.bucketId}; + this.minKey = encodeKey(this.bucket, Buffer.alloc(0)); + this.maxKey = encodeKey(this.bucket + 1, Buffer.alloc(0)); } async getAll(pubkey: BLSPubkey, limit?: number): Promise { @@ -64,7 +69,7 @@ export class AttestationByTargetRepository { } async listPubkeys(): Promise { - const keys = await this.db.keys(this.dbReqOpts); + const keys = await this.db.keys({...this.dbReqOpts, gte: this.minKey, lt: this.maxKey}); return uniqueVectorArr(keys.map((key) => this.decodeKey(key).pubkey)); } diff --git a/packages/validator/src/slashingProtection/block/blockBySlotRepository.ts b/packages/validator/src/slashingProtection/block/blockBySlotRepository.ts index d61e9e3503e..366ef0088c6 100644 --- a/packages/validator/src/slashingProtection/block/blockBySlotRepository.ts +++ b/packages/validator/src/slashingProtection/block/blockBySlotRepository.ts @@ -27,6 +27,9 @@ export class BlockBySlotRepository { private readonly bucketId: string; private readonly dbReqOpts: DbReqOpts; + private readonly minKey: Uint8Array; + private readonly maxKey: Uint8Array; + constructor(opts: DatabaseApiOptions) { this.db = opts.controller; this.type = new ContainerType({ @@ -35,6 +38,8 @@ export class BlockBySlotRepository { }); // casing doesn't matter this.bucketId = getBucketNameByValue(this.bucket); this.dbReqOpts = {bucketId: this.bucketId}; + this.minKey = encodeKey(this.bucket, Buffer.alloc(0)); + this.maxKey = encodeKey(this.bucket + 1, Buffer.alloc(0)); } async getAll(pubkey: BLSPubkey, limit?: number): Promise { @@ -68,7 +73,7 @@ export class BlockBySlotRepository { } async listPubkeys(): Promise { - const keys = await this.db.keys(this.dbReqOpts); + const keys = await this.db.keys({...this.dbReqOpts, gte: this.minKey, lt: this.maxKey}); return uniqueVectorArr(keys.map((key) => this.decodeKey(key).pubkey)); }