Skip to content

Commit

Permalink
fix(ref-imp): renamed put method IOperationStore to insertOrReplace
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai committed Feb 3, 2021
1 parent 966d45f commit 32d7d18
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 93 deletions.
2 changes: 1 addition & 1 deletion lib/core/MongoDbOperationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class MongoDbOperationStore implements IOperationStore {
}
}

public async put (operations: AnchoredOperationModel[]): Promise<void> {
public async insertOrReplace (operations: AnchoredOperationModel[]): Promise<void> {
const bulkOperations = this.collection!.initializeUnorderedBulkOp();

for (const operation of operations) {
Expand Down
6 changes: 3 additions & 3 deletions lib/core/interfaces/IOperationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import AnchoredOperationModel from '../models/AnchoredOperationModel';
export default interface IOperationStore {

/**
* Stores a batch of operations
* @param operations The list of operations to be stored, where the key of the map is the DID unique suffix.
* Inserts or replaces the list of anchored operations given.
* @param operations The list of anchored operations to be inserted or replaced.
*/
put (operations: AnchoredOperationModel[]): Promise<void>;
insertOrReplace (operations: AnchoredOperationModel[]): Promise<void>;

/**
* Gets all operations of the given DID unique suffix in ascending chronological order.
Expand Down
2 changes: 1 addition & 1 deletion lib/core/versions/1.0/TransactionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default class TransactionProcessor implements ITransactionProcessor {
transaction, coreIndexFile, provisionalIndexFile, coreProofFile, provisionalProofFile, chunkFileModel
);

await this.operationStore.put(operations);
await this.operationStore.insertOrReplace(operations);

Logger.info(LogColor.lightBlue(`Processed ${LogColor.green(operations.length)} operations. Retry needed: ${LogColor.green(retryNeeded)}`));

Expand Down
2 changes: 1 addition & 1 deletion lib/core/versions/latest/TransactionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default class TransactionProcessor implements ITransactionProcessor {
transaction, coreIndexFile, provisionalIndexFile, coreProofFile, provisionalProofFile, chunkFileModel
);

await this.operationStore.put(operations);
await this.operationStore.insertOrReplace(operations);

Logger.info(LogColor.lightBlue(`Processed ${LogColor.green(operations.length)} operations. Retry needed: ${LogColor.green(retryNeeded)}`));

Expand Down
59 changes: 16 additions & 43 deletions tests/core/MongoDbOperationStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@ describe('MongoDbOperationStore', async () => {
expect(collectionNames.includes(collectionName)).toBeFalsy();
});

describe('put()', () => {
it('should get a put create operation', async () => {
describe('insertOrReplace()', () => {
it('should be able to insert an create operation successfully.', async () => {
const operationData = await OperationGenerator.generateAnchoredCreateOperation({ transactionTime: 0, transactionNumber: 0, operationIndex: 0 });
const anchoredOperationModel = operationData.anchoredOperationModel;
await operationStore.put([anchoredOperationModel]);
await operationStore.insertOrReplace([anchoredOperationModel]);
const returnedOperations = await operationStore.get(anchoredOperationModel.didUniqueSuffix);
checkEqualArray([anchoredOperationModel], returnedOperations);
});

it('should get a put update operation', async () => {
it('should be able to insert an update operation successfully.', async () => {
// Use a create operation to generate a DID
const createOperationData = await OperationGenerator.generateAnchoredCreateOperation({ transactionTime: 0, transactionNumber: 0, operationIndex: 0 });
const anchoredOperationModel = createOperationData.anchoredOperationModel;
Expand All @@ -154,39 +154,12 @@ describe('MongoDbOperationStore', async () => {
operationModel, 1, 1, 0
);

await operationStore.put([anchoredUpdateOperation]);
await operationStore.insertOrReplace([anchoredUpdateOperation]);
const returnedOperations = await operationStore.get(didUniqueSuffix);
checkEqualArray([anchoredUpdateOperation], returnedOperations);
});

it('should ignore duplicate updates', async () => {
// Use a create operation to generate a DID
const createOperationData = await OperationGenerator.generateAnchoredCreateOperation({ transactionTime: 0, transactionNumber: 0, operationIndex: 0 });
const anchoredOperationModel = createOperationData.anchoredOperationModel;
const didUniqueSuffix = anchoredOperationModel.didUniqueSuffix;

// Generate an update operation.
const operationRequest = await OperationGenerator.generateUpdateOperationRequestForServices(
didUniqueSuffix,
createOperationData.signingPublicKey.publicKeyJwk,
createOperationData.signingPrivateKey,
OperationGenerator.generateRandomHash(),
'someId',
[]
);
const operationModel = await UpdateOperation.parse(Buffer.from(JSON.stringify(operationRequest)));
const anchoredUpdateOperation: AnchoredOperationModel = OperationGenerator.createAnchoredOperationModelFromOperationModel(
operationModel, 1, 1, 0
);

await operationStore.put([anchoredUpdateOperation]);
// Insert duplicate operation
await operationStore.put([anchoredUpdateOperation]);
const returnedOperations = await operationStore.get(didUniqueSuffix);
checkEqualArray([anchoredUpdateOperation], returnedOperations);
});

it('should upsert operations', async () => {
it('should replace an existing operations successfully.', async () => {
// Use a create operation to generate a DID
const createOperationData = await OperationGenerator.generateAnchoredCreateOperation({ transactionTime: 0, transactionNumber: 0, operationIndex: 0 });
const anchoredOperationModel = createOperationData.anchoredOperationModel;
Expand All @@ -200,13 +173,13 @@ describe('MongoDbOperationStore', async () => {
anchoredOperationModelWithoutDelta.operationBuffer = Buffer.from(JSON.stringify(clonedCreateRequestWithoutDelta));

// Insert the anchored operation without `delta` into DB first.
await operationStore.put([anchoredOperationModelWithoutDelta]);
await operationStore.insertOrReplace([anchoredOperationModelWithoutDelta]);
const didUniqueSuffix = anchoredOperationModel.didUniqueSuffix;
const returnedOperations1 = await operationStore.get(didUniqueSuffix);
checkEqualArray([anchoredOperationModelWithoutDelta], returnedOperations1);

// Insert the anchored operation with `delta` into DB.
await operationStore.put([anchoredOperationModel]);
await operationStore.insertOrReplace([anchoredOperationModel]);
const returnedOperations2 = await operationStore.get(didUniqueSuffix);
checkEqualArray([anchoredOperationModel], returnedOperations2);
});
Expand All @@ -222,7 +195,7 @@ describe('MongoDbOperationStore', async () => {

const chainSize = 10;
const operationChain = await createOperationChain(anchoredOperationModel, chainSize, signingPublicKey, signingPrivateKey);
await operationStore.put(operationChain);
await operationStore.insertOrReplace(operationChain);

const returnedOperations = await operationStore.get(didUniqueSuffix);
checkEqualArray(operationChain, returnedOperations);
Expand All @@ -242,7 +215,7 @@ describe('MongoDbOperationStore', async () => {
// construct an operation chain with duplicated operations
const batchWithDuplicates = operationChain.concat(operationChain);

await operationStore.put(batchWithDuplicates);
await operationStore.insertOrReplace(batchWithDuplicates);
const returnedOperations = await operationStore.get(didUniqueSuffix);
checkEqualArray(operationChain, returnedOperations);
});
Expand All @@ -258,7 +231,7 @@ describe('MongoDbOperationStore', async () => {
const chainSize = 10;
const operationChain = await createOperationChain(anchoredOperationModel, chainSize, signingPublicKey, signingPrivateKey);

await operationStore.put(operationChain);
await operationStore.insertOrReplace(operationChain);
const returnedOperations = await operationStore.get(didUniqueSuffix);
checkEqualArray(operationChain, returnedOperations);

Expand All @@ -277,7 +250,7 @@ describe('MongoDbOperationStore', async () => {

const chainSize = 10;
const operationChain = await createOperationChain(anchoredOperationModel, chainSize, signingPublicKey, signingPrivateKey);
await operationStore.put(operationChain);
await operationStore.insertOrReplace(operationChain);
const returnedOperations = await operationStore.get(didUniqueSuffix);
checkEqualArray(operationChain, returnedOperations);

Expand All @@ -298,7 +271,7 @@ describe('MongoDbOperationStore', async () => {

const chainSize = 10;
const operationChain = await createOperationChain(anchoredOperationModel, chainSize, signingPublicKey, signingPrivateKey);
await operationStore.put(operationChain);
await operationStore.insertOrReplace(operationChain);
let returnedOperations = await operationStore.get(didUniqueSuffix);
checkEqualArray(operationChain, returnedOperations);

Expand All @@ -323,7 +296,7 @@ describe('MongoDbOperationStore', async () => {

// Insert operations in reverse transaction time order
for (let i = chainSize - 1; i >= 0; i--) {
await operationStore.put([operationChain[i]]);
await operationStore.insertOrReplace([operationChain[i]]);
}

const returnedOperations = await operationStore.get(didUniqueSuffix);
Expand All @@ -342,7 +315,7 @@ describe('MongoDbOperationStore', async () => {

const chainSize = 10;
const operationChain = await createOperationChain(anchoredOperationModel, chainSize, signingPublicKey, signingPrivateKey);
await operationStore.put(operationChain);
await operationStore.insertOrReplace(operationChain);
const returnedOperations = await operationStore.get(didUniqueSuffix);
checkEqualArray(operationChain, returnedOperations);

Expand All @@ -368,7 +341,7 @@ describe('MongoDbOperationStore', async () => {
const txnNumber = 1;
const operationChain = await createOperationChain(
anchoredOperationModel, chainSize, signingPublicKey, signingPrivateKey, txnNumber);
await operationStore.put(operationChain);
await operationStore.insertOrReplace(operationChain);
const returnedOperations = await operationStore.get(didUniqueSuffix);
checkEqualArray(operationChain, returnedOperations);

Expand Down
38 changes: 19 additions & 19 deletions tests/core/OperationProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('OperationProcessor', async () => {
});

it('should return a DID Document for resolve(did) for a registered DID', async () => {
await operationStore.put([createOp]);
await operationStore.insertOrReplace([createOp]);

const didState = await resolver.resolve(didUniqueSuffix);
expect(didState).toBeDefined();
Expand All @@ -163,12 +163,12 @@ describe('OperationProcessor', async () => {
});

it('should ignore a duplicate create operation', async () => {
await operationStore.put([createOp]);
await operationStore.insertOrReplace([createOp]);

// Insert a duplicate create op with a different transaction time.
const duplicateOperation = await CreateOperation.parse(createOp.operationBuffer);
const duplicateNamedAnchoredCreateOperationModel = OperationGenerator.createAnchoredOperationModelFromOperationModel(duplicateOperation, 1, 1, 0);
await operationStore.put([duplicateNamedAnchoredCreateOperationModel]);
await operationStore.insertOrReplace([duplicateNamedAnchoredCreateOperationModel]);

const didState = await resolver.resolve(didUniqueSuffix);
expect(didState).toBeDefined();
Expand All @@ -179,7 +179,7 @@ describe('OperationProcessor', async () => {
});

it('should process update to remove a public key correctly', async () => {
await operationStore.put([createOp]);
await operationStore.insertOrReplace([createOp]);

const patches = [
{
Expand All @@ -206,7 +206,7 @@ describe('OperationProcessor', async () => {
transactionNumber: 1,
operationIndex: 0
};
await operationStore.put([updateOp]);
await operationStore.insertOrReplace([updateOp]);

const didState = await resolver.resolve(didUniqueSuffix);
expect(didState).toBeDefined();
Expand All @@ -219,7 +219,7 @@ describe('OperationProcessor', async () => {
it('should process updates correctly', async () => {
const numberOfUpdates = 10;
const ops = await createUpdateSequence(didUniqueSuffix, createOp, numberOfUpdates, signingPrivateKey);
await operationStore.put(ops);
await operationStore.insertOrReplace(ops);

const didState = await resolver.resolve(didUniqueSuffix);
expect(didState).toBeDefined();
Expand All @@ -231,7 +231,7 @@ describe('OperationProcessor', async () => {
const ops = await createUpdateSequence(didUniqueSuffix, createOp, numberOfUpdates, signingPrivateKey);

for (let i = numberOfUpdates; i >= 0; --i) {
await operationStore.put([ops[i]]);
await operationStore.insertOrReplace([ops[i]]);
}
const didState = await resolver.resolve(didUniqueSuffix);
expect(didState).toBeDefined();
Expand All @@ -250,7 +250,7 @@ describe('OperationProcessor', async () => {
operationStore = new MockOperationStore();
resolver = new Resolver(versionManager, operationStore);
const permutedOps = permutation.map(i => ops[i]);
await operationStore.put(permutedOps);
await operationStore.insertOrReplace(permutedOps);
const didState = await resolver.resolve(didUniqueSuffix);
expect(didState).toBeDefined();
validateDocumentAfterUpdates(didState!.document, numberOfUpdates);
Expand All @@ -260,7 +260,7 @@ describe('OperationProcessor', async () => {
it('should process deactivate operation correctly.', async () => {
const numberOfUpdates = 10;
const ops = await createUpdateSequence(didUniqueSuffix, createOp, numberOfUpdates, signingPrivateKey);
await operationStore.put(ops);
await operationStore.insertOrReplace(ops);

const didState = await resolver.resolve(didUniqueSuffix);
expect(didState).toBeDefined();
Expand All @@ -269,7 +269,7 @@ describe('OperationProcessor', async () => {
const deactivateOperationData = await OperationGenerator.createDeactivateOperation(didUniqueSuffix, recoveryPrivateKey);
const anchoredDeactivateOperation = OperationGenerator.createAnchoredOperationModelFromOperationModel(
deactivateOperationData.deactivateOperation, numberOfUpdates + 1, numberOfUpdates + 1, 0);
await operationStore.put([anchoredDeactivateOperation]);
await operationStore.insertOrReplace([anchoredDeactivateOperation]);

const deactivatedDidState = await resolver.resolve(didUniqueSuffix);
expect(deactivatedDidState).toBeDefined();
Expand All @@ -281,19 +281,19 @@ describe('OperationProcessor', async () => {
it('should ignore a deactivate operation of a non-existent did', async () => {
const deactivateOperationData = await OperationGenerator.createDeactivateOperation(didUniqueSuffix, recoveryPrivateKey);
const anchoredDeactivateOperation = OperationGenerator.createAnchoredOperationModelFromOperationModel(deactivateOperationData.deactivateOperation, 1, 1, 0);
await operationStore.put([anchoredDeactivateOperation]);
await operationStore.insertOrReplace([anchoredDeactivateOperation]);

const didDocumentAfterDeactivate = await resolver.resolve(didUniqueSuffix);
expect(didDocumentAfterDeactivate).toBeUndefined();
});

it('should ignore a deactivate operation with invalid signature', async () => {
await operationStore.put([createOp]);
await operationStore.insertOrReplace([createOp]);

// Intentionally signing with signing (wrong) key.
const deactivateOperationData = await OperationGenerator.createDeactivateOperation(didUniqueSuffix, signingPrivateKey);
const anchoredDeactivateOperation = OperationGenerator.createAnchoredOperationModelFromOperationModel(deactivateOperationData.deactivateOperation, 1, 1, 0);
await operationStore.put([anchoredDeactivateOperation]);
await operationStore.insertOrReplace([anchoredDeactivateOperation]);

const didState = await resolver.resolve(didUniqueSuffix);
expect(didState).toBeDefined();
Expand All @@ -309,15 +309,15 @@ describe('OperationProcessor', async () => {

// elide i = 0, the create operation
for (let i = 1; i < ops.length; ++i) {
await operationStore.put([ops[i]]);
await operationStore.insertOrReplace([ops[i]]);
}

const didDocument = await resolver.resolve(didUniqueSuffix);
expect(didDocument).toBeUndefined();
});

it('should ignore update operation with the incorrect updateKey', async () => {
await operationStore.put([createOp]);
await operationStore.insertOrReplace([createOp]);

const [anyPublicKey] = await OperationGenerator.generateKeyPair(`additionalKey`);
const [invalidKey] = await OperationGenerator.generateKeyPair('invalidKey');
Expand All @@ -329,7 +329,7 @@ describe('OperationProcessor', async () => {
const updateOperationBuffer = Buffer.from(JSON.stringify(updateOperationRequest));
const updateOperation = await UpdateOperation.parse(updateOperationBuffer);
const anchoredUpdateOperation = OperationGenerator.createAnchoredOperationModelFromOperationModel(updateOperation, 1, 1, 0);
await operationStore.put([anchoredUpdateOperation]);
await operationStore.insertOrReplace([anchoredUpdateOperation]);

const didState = await resolver.resolve(didUniqueSuffix);
expect(didState).toBeDefined();
Expand All @@ -340,7 +340,7 @@ describe('OperationProcessor', async () => {
});

it('should ignore update operation with an invalid signature', async () => {
await operationStore.put([createOp]);
await operationStore.insertOrReplace([createOp]);

const [, anyIncorrectSigningPrivateKey] = await OperationGenerator.generateKeyPair('key1');
const [anyPublicKey] = await OperationGenerator.generateKeyPair(`additionalKey`);
Expand All @@ -351,7 +351,7 @@ describe('OperationProcessor', async () => {
const updateOperationBuffer = Buffer.from(JSON.stringify(updateOperationRequest));
const updateOperation = await UpdateOperation.parse(updateOperationBuffer);
const anchoredUpdateOperation = OperationGenerator.createAnchoredOperationModelFromOperationModel(updateOperation, 1, 1, 0);
await operationStore.put([anchoredUpdateOperation]);
await operationStore.insertOrReplace([anchoredUpdateOperation]);

const didState = await resolver.resolve(didUniqueSuffix);
expect(didState).toBeDefined();
Expand All @@ -364,7 +364,7 @@ describe('OperationProcessor', async () => {
it('should resolve as undefined if all operation of a DID is rolled back.', async () => {
const numberOfUpdates = 10;
const ops = await createUpdateSequence(didUniqueSuffix, createOp, numberOfUpdates, signingPrivateKey);
await operationStore.put(ops);
await operationStore.insertOrReplace(ops);
const didState = await resolver.resolve(didUniqueSuffix);
expect(didState).toBeDefined();

Expand Down
2 changes: 1 addition & 1 deletion tests/core/RequestHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('RequestHandler', () => {
operationBuffer: createOperationBuffer,
operationIndex: 0
};
await operationStore.put([namedAnchoredCreateOperationModel]);
await operationStore.insertOrReplace([namedAnchoredCreateOperationModel]);

// Trigger the batch writing to clear the operation queue for future tests.
await batchScheduler.writeOperationBatch();
Expand Down
Loading

0 comments on commit 32d7d18

Please sign in to comment.