Skip to content

Commit

Permalink
Filter by min-max key when loading pubkeys from db
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Apr 29, 2023
1 parent 2fbf178 commit 90ed272
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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<SlashingProtectionAttestation[]> {
Expand Down Expand Up @@ -64,7 +69,7 @@ export class AttestationByTargetRepository {
}

async listPubkeys(): Promise<BLSPubkey[]> {
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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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<SlashingProtectionBlock[]> {
Expand Down Expand Up @@ -68,7 +73,7 @@ export class BlockBySlotRepository {
}

async listPubkeys(): Promise<BLSPubkey[]> {
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));
}

Expand Down

0 comments on commit 90ed272

Please sign in to comment.