Skip to content

Commit

Permalink
Pass edvClient list classes; use indexAllocator to cache shards.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Feb 17, 2024
1 parent d3089d8 commit 0e877b0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 40 deletions.
12 changes: 6 additions & 6 deletions lib/CredentialStatusIssuer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {constants as rlConstants} from '@bedrock/vc-revocation-list-context';
import {constants as slConstants} from '@bedrock/vc-status-list-context';

export class CredentialStatusIssuer {
constructor({config, documentLoader, documentStore} = {}) {
constructor({config, documentLoader, edvClient} = {}) {
assert.object(config, 'config');
assert.func(documentLoader, 'documentLoader');
assert.object(documentStore, 'documentStore');
assert.object(edvClient, 'edvClient');
this.config = config;
this.documentLoader = documentLoader;
this.documentStore = documentStore;
this.edvClient = edvClient;
this.credential = null;
this.writers = [];
this.statusResultMap = null;
Expand All @@ -26,7 +26,7 @@ export class CredentialStatusIssuer {
this.credential = credential;

// see if config indicates a credential status should be set
const {config, documentLoader, documentStore, writers} = this;
const {config, documentLoader, edvClient, writers} = this;
const {statusListOptions = []} = config;

if(statusListOptions.length === 0) {
Expand Down Expand Up @@ -68,10 +68,10 @@ export class CredentialStatusIssuer {

const {issuer, suite} = await getIssuerAndSuite({config, suiteName});
writers.push(new CredentialStatusWriter({
statusListConfig,
documentLoader,
documentStore,
edvClient,
issuer,
statusListConfig,
suite
}));
}
Expand Down
44 changes: 17 additions & 27 deletions lib/CredentialStatusWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,43 +78,38 @@ writer.finish():
*/
export class CredentialStatusWriter {
constructor({
documentLoader, documentStore,
issuer, statusListConfig, suite
statusListConfig, documentLoader, edvClient,
issuer, suite
} = {}) {
assert.object(statusListConfig, 'statusListConfig');
assert.func(documentLoader, 'documentLoader');
// FIXME: use `edvClient` directly
assert.object(documentStore, 'documentStore');
assert.object(edvClient, 'edvClient');
assert.string(issuer, 'issuer');
assert.object(statusListConfig, 'statusListConfig');
assert.object(suite, 'suite');
if(!issuer) {
throw new TypeError('"issuer" must be a non-empty string.');
}
this.statusListConfig = statusListConfig;
this.documentLoader = documentLoader;
this.documentStore = documentStore;
this.edvClient = edvClient;
this.issuer = issuer;
this.statusListConfig = statusListConfig;
this.suite = suite;
this.listShard = null;
}

async write({credential} = {}) {
assert.object(credential, 'credential');

const {documentStore: {serviceObjectId}, statusListConfig} = this;
let shardQueue = SHARD_QUEUE_CACHE.get(serviceObjectId);
const {edvClient, statusListConfig, listShard} = this;
const {indexAllocator} = statusListConfig;
let shardQueue = SHARD_QUEUE_CACHE.get(indexAllocator);
if(!shardQueue) {
shardQueue = [];
SHARD_QUEUE_CACHE.set(serviceObjectId, shardQueue);
SHARD_QUEUE_CACHE.set(indexAllocator, shardQueue);
}

// get `edvClient` directly; do not use cache in `documentStore` to ensure
// latest docs are used
const {documentStore: {edvClient}} = this;

// 1. If an LS has been assigned to the writer instance (then a duplicate
// error for the VC is being handled):
const {listShard} = this;
let existingCredentialStatus;
if(listShard) {
// FIXME: might be multiple `credentialStatus` results for a given
Expand Down Expand Up @@ -183,13 +178,11 @@ export class CredentialStatusWriter {
if(shardQueue.length === 0) {
// 2.1. Create a ListManager instance `listManager`.
// 2.2. Call listManager.getShard() and store result in the instance.
const {documentLoader, documentStore, issuer, suite} = this;
// pass `edvClient` directly; do not use cache in `documentStore` to
// ensure latest docs are used in list management
const {documentLoader, edvClient, issuer, suite} = this;
const listManager = new ListManager({
statusListConfig,
documentLoader,
edvClient: documentStore.edvClient,
edvClient,
issuer,
suite
});
Expand Down Expand Up @@ -222,17 +215,13 @@ export class CredentialStatusWriter {
}

async finish() {
const {listShard} = this;
const {edvClient, listShard} = this;
if(!listShard) {
// `finish` should never be called when no `listShard` is available
throw new Error(
'Invalid state error; "finish()" must only be called after "write()".');
}

// get `edvClient` directly; do not use cache in `documentStore` to ensure
// latest docs are used
const {documentStore: {edvClient, serviceObjectId}} = this;

// 1. Increment instance's IAD's latest index value.
const {
indexAssignmentDoc,
Expand Down Expand Up @@ -282,10 +271,11 @@ export class CredentialStatusWriter {
}
} else {
// 4. Otherwise, add LS back to in-memory set.
let shardQueue = SHARD_QUEUE_CACHE.get(serviceObjectId);
const {statusListConfig: {indexAllocator}} = this;
let shardQueue = SHARD_QUEUE_CACHE.get(indexAllocator);
if(!shardQueue) {
shardQueue = [];
SHARD_QUEUE_CACHE.set(serviceObjectId, shardQueue);
SHARD_QUEUE_CACHE.set(indexAllocator, shardQueue);
}
shardQueue.push(listShard);
}
Expand All @@ -297,7 +287,7 @@ export class CredentialStatusWriter {
assert.object(statusMeta, 'statusMeta');
// check every `statusMeta`'s `credentialStatus.id` for duplicates
const counts = await Promise.all(statusMeta.map(
async ({credentialStatus}) => this.documentStore.edvClient.count({
async ({credentialStatus}) => this.edvClient.count({
equals: {'meta.credentialStatus.id': credentialStatus.id}
})));
return counts.some(count => count !== 0);
Expand Down
4 changes: 2 additions & 2 deletions lib/ListManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ export class ListManager {

// FIXME: pass in list source instance
this.listSource = new ListSource({
documentLoader, edvClient,
issuer, statusListConfig, suite
statusListConfig, documentLoader, edvClient,
issuer, suite
});
}

Expand Down
8 changes: 4 additions & 4 deletions lib/ListSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ import {issue} from './helpers.js';

export class ListSource {
constructor({
documentLoader, edvClient,
issuer, statusListConfig, suite
statusListConfig, documentLoader, edvClient,
issuer, suite
} = {}) {
assert.object(statusListConfig, 'statusListConfig');
assert.func(documentLoader, 'documentLoader');
assert.object(edvClient, 'edvClient');
assert.object(statusListConfig, 'statusListConfig');
assert.string(issuer, 'issuer');
assert.object(suite, 'suite');
if(!issuer) {
throw new TypeError('"issuer" must be a non-empty string.');
}
this.statusListConfig = statusListConfig;
this.documentLoader = documentLoader;
this.edvClient = edvClient;
this.issuer = issuer;
this.statusListConfig = statusListConfig;
this.suite = suite;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/issuer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function issue({credential, config} = {}) {

// initialize `CredentialStatusIssuer` for handling any credential statuses
const credentialStatusIssuer = new CredentialStatusIssuer({
config, documentLoader, documentStore
config, documentLoader, edvClient
});
await credentialStatusIssuer.initialize({credential});

Expand Down

0 comments on commit 0e877b0

Please sign in to comment.