Skip to content

Commit

Permalink
feat(ref-imp): #766 - addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai committed Dec 3, 2020
1 parent ae14086 commit 3ac7f22
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 117 deletions.
10 changes: 5 additions & 5 deletions lib/core/versions/latest/BatchWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default class BatchWriter implements IBatchWriter {

/**
* Create and write chunk file if needed.
* @returns CASE URI of the chunk file. `undefined` if there is no need to create and write the file.
* @returns CAS URI of the chunk file. `undefined` if there is no need to create and write the file.
*/
private async createAndWriteChunkFileIfNeeded (
createOperations: CreateOperation[], recoverOperations: RecoverOperation[], updateOperations: UpdateOperation[]
Expand All @@ -122,17 +122,17 @@ export default class BatchWriter implements IBatchWriter {

/**
* Create and write provisional index file if needed.
* @returns CASE URI of the chunk file. `undefined` if there is no need to create and write the file.
* @returns URI of the provisional index file. `undefined` if there is no need to create and write the file.
*/
private async createAndWriteProvisionalIndexFileIfNeeded (
chunkFileUri: string | undefined, provisionalProofFileHash: string | undefined, updateOperations: UpdateOperation[]
chunkFileUri: string | undefined, provisionalProofFileUri: string | undefined, updateOperations: UpdateOperation[]
): Promise<string | undefined> {
// If `chunkFileHash` is `undefined` it means there are only deactivates, and a batch with only deactivates does not reference a provisional index file.
// If `chunkFileUri` is `undefined` it means there are only deactivates, and a batch with only deactivates does not reference a provisional index file.
if (chunkFileUri === undefined) {
return undefined;
}

const provisionalIndexFileBuffer = await ProvisionalIndexFile.createBuffer(chunkFileUri!, provisionalProofFileHash, updateOperations);
const provisionalIndexFileBuffer = await ProvisionalIndexFile.createBuffer(chunkFileUri!, provisionalProofFileUri, updateOperations);
const provisionalIndexFileUri = await this.cas.write(provisionalIndexFileBuffer);
console.info(LogColor.lightBlue(`Wrote provisional index file ${LogColor.green(provisionalIndexFileUri)} to content addressable store.`));

Expand Down
2 changes: 1 addition & 1 deletion lib/core/versions/latest/ChunkFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class ChunkFile {

/**
* Creates chunk file buffer.
* @returns Chunk file buffer. Returns `undefined` arrays passed in contains no operations.
* @returns Chunk file buffer. Returns `undefined` if arrays passed in contains no operations.
*/
public static async createBuffer (createOperations: CreateOperation[], recoverOperations: RecoverOperation[], updateOperations: UpdateOperation[])
: Promise<Buffer | undefined> {
Expand Down
10 changes: 5 additions & 5 deletions lib/core/versions/latest/CoreIndexFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export default class CoreIndexFile {
public static async createModel (
writerLockId: string | undefined,
provisionalIndexFileUri: string | undefined,
coreProofFileHash: string | undefined,
coreProofFileUri: string | undefined,
createOperationArray: CreateOperation[],
recoverOperationArray: RecoverOperation[],
deactivateOperationArray: DeactivateOperation[]
Expand Down Expand Up @@ -236,8 +236,8 @@ export default class CoreIndexFile {
}

// Only insert `coreProofFileUri` property if a value is given.
if (coreProofFileHash !== undefined) {
coreIndexFileModel.coreProofFileUri = coreProofFileHash;
if (coreProofFileUri !== undefined) {
coreIndexFileModel.coreProofFileUri = coreProofFileUri;
}

return coreIndexFileModel;
Expand All @@ -249,13 +249,13 @@ export default class CoreIndexFile {
public static async createBuffer (
writerLockId: string | undefined,
provisionalIndexFileUri: string | undefined,
coreProofFileHash: string | undefined,
coreProofFileUri: string | undefined,
createOperations: CreateOperation[],
recoverOperations: RecoverOperation[],
deactivateOperations: DeactivateOperation[]
): Promise<Buffer> {
const coreIndexFileModel = await CoreIndexFile.createModel(
writerLockId, provisionalIndexFileUri, coreProofFileHash, createOperations, recoverOperations, deactivateOperations
writerLockId, provisionalIndexFileUri, coreProofFileUri, createOperations, recoverOperations, deactivateOperations
);
const coreIndexFileJson = JSON.stringify(coreIndexFileModel);
const coreIndexFileBuffer = Buffer.from(coreIndexFileJson);
Expand Down
2 changes: 1 addition & 1 deletion lib/core/versions/latest/ErrorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
AnchoredDataNumberOfOperationsGreaterThanMax: 'anchored_data_number_of_operations_greater_than_max',
AnchoredDataNumberOfOperationsNotPositiveInteger: 'anchored_data_number_of_operations_not_positive_integer',
BatchWriterAlreadyHasOperationForDid: 'batch_writer_already_has_operation_for_did',
CasFileHashNotValid: 'cas_file_hash_not_valid',
CasFileUriNotValid: 'cas_file_hash_not_valid',
CasFileNotAFile: 'cas_file_not_a_file',
CasFileNotFound: 'cas_file_not_found',
CasFileTooLarge: 'cas_file_too_large',
Expand Down
8 changes: 4 additions & 4 deletions lib/core/versions/latest/ProvisionalIndexFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default class ProvisionalIndexFile {
* Creates the Map File buffer.
*/
public static async createBuffer (
chunkFileHash: string, provisionalProofFileHash: string | undefined, updateOperationArray: UpdateOperation[]
chunkFileUri: string, provisionalProofFileUri: string | undefined, updateOperationArray: UpdateOperation[]
): Promise<Buffer> {
const updateReferences = updateOperationArray.map(operation => {
const revealValue = Multihash.canonicalizeThenHashThenEncode(operation.signedData.updateKey);
Expand All @@ -134,16 +134,16 @@ export default class ProvisionalIndexFile {
});

const provisionalIndexFileModel: ProvisionalIndexFileModel = {
chunks: [{ chunkFileUri: chunkFileHash }]
chunks: [{ chunkFileUri }]
};

// Only insert `operations` and `provisionalProofFileHash` properties if there are update operations.
// Only insert `operations` and `provisionalProofFileUri` properties if there are update operations.
if (updateReferences.length > 0) {
provisionalIndexFileModel.operations = {
update: updateReferences
};

provisionalIndexFileModel.provisionalProofFileUri = provisionalProofFileHash;
provisionalIndexFileModel.provisionalProofFileUri = provisionalProofFileUri;
}

const rawData = JSON.stringify(provisionalIndexFileModel);
Expand Down
24 changes: 12 additions & 12 deletions lib/core/versions/latest/TransactionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ export default class TransactionProcessor implements ITransactionProcessor {
return undefined;
}

const chunkFileHash = provisionalIndexFile.model.chunks[0].chunkFileUri;
console.info(`Downloading chunk file '${chunkFileHash}', max size limit ${ProtocolParameters.maxChunkFileSizeInBytes}...`);
const chunkFileUri = provisionalIndexFile.model.chunks[0].chunkFileUri;
console.info(`Downloading chunk file '${chunkFileUri}', max size limit ${ProtocolParameters.maxChunkFileSizeInBytes}...`);

const fileBuffer = await this.downloadFileFromCas(chunkFileHash, ProtocolParameters.maxChunkFileSizeInBytes);
const fileBuffer = await this.downloadFileFromCas(chunkFileUri, ProtocolParameters.maxChunkFileSizeInBytes);
const chunkFileModel = await ChunkFile.parse(fileBuffer);

const totalCountOfOperationsWithDelta =
Expand Down Expand Up @@ -506,32 +506,32 @@ export default class TransactionProcessor implements ITransactionProcessor {
return anchoredOperationModels;
}

private async downloadFileFromCas (fileHash: string, maxFileSizeInBytes: number): Promise<Buffer> {
console.info(`Downloading file '${fileHash}', max size limit ${maxFileSizeInBytes}...`);
private async downloadFileFromCas (fileUri: string, maxFileSizeInBytes: number): Promise<Buffer> {
console.info(`Downloading file '${fileUri}', max size limit ${maxFileSizeInBytes}...`);

const fileFetchResult = await this.downloadManager.download(fileHash, maxFileSizeInBytes);
const fileFetchResult = await this.downloadManager.download(fileUri, maxFileSizeInBytes);

if (fileFetchResult.code === FetchResultCode.InvalidHash) {
throw new SidetreeError(ErrorCode.CasFileHashNotValid, `File hash '${fileHash}' is not a valid hash.`);
throw new SidetreeError(ErrorCode.CasFileUriNotValid, `File hash '${fileUri}' is not a valid hash.`);
}

if (fileFetchResult.code === FetchResultCode.MaxSizeExceeded) {
throw new SidetreeError(ErrorCode.CasFileTooLarge, `File '${fileHash}' exceeded max size limit of ${maxFileSizeInBytes} bytes.`);
throw new SidetreeError(ErrorCode.CasFileTooLarge, `File '${fileUri}' exceeded max size limit of ${maxFileSizeInBytes} bytes.`);
}

if (fileFetchResult.code === FetchResultCode.NotAFile) {
throw new SidetreeError(ErrorCode.CasFileNotAFile, `File hash '${fileHash}' points to a content that is not a file.`);
throw new SidetreeError(ErrorCode.CasFileNotAFile, `File hash '${fileUri}' points to a content that is not a file.`);
}

if (fileFetchResult.code === FetchResultCode.CasNotReachable) {
throw new SidetreeError(ErrorCode.CasNotReachable, `CAS not reachable for file '${fileHash}'.`);
throw new SidetreeError(ErrorCode.CasNotReachable, `CAS not reachable for file '${fileUri}'.`);
}

if (fileFetchResult.code === FetchResultCode.NotFound) {
throw new SidetreeError(ErrorCode.CasFileNotFound, `File '${fileHash}' not found.`);
throw new SidetreeError(ErrorCode.CasFileNotFound, `File '${fileUri}' not found.`);
}

console.info(`File '${fileHash}' of size ${fileFetchResult.content!.length} downloaded.`);
console.info(`File '${fileUri}' of size ${fileFetchResult.content!.length} downloaded.`);

return fileFetchResult.content!;
}
Expand Down
38 changes: 19 additions & 19 deletions tests/core/AnchorFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ describe('CoreIndexFile', async () => {
it('should throw if provisional index file hash is not string.', async () => {
const createOperationData = await OperationGenerator.generateCreateOperation();
const createOperation = createOperationData.createOperation;
const coreProofFileHash = undefined;
const coreIndexFileModel = await CoreIndexFile.createModel('writerLock', 'unusedMockFileHash', coreProofFileHash, [createOperation], [], []);
const coreProofFileUri = undefined;
const coreIndexFileModel = await CoreIndexFile.createModel('writerLock', 'unusedMockFileUri', coreProofFileUri, [createOperation], [], []);

(coreIndexFileModel as any).provisionalIndexFileUri = 1234; // Intentionally setting the provisionalIndexFileUri as an incorrect type.

Expand All @@ -178,8 +178,8 @@ describe('CoreIndexFile', async () => {
it('should throw if provisional index file hash is invalid.', async () => {
const createOperationData = await OperationGenerator.generateCreateOperation();
const createOperation = createOperationData.createOperation;
const coreProofFileHash = undefined;
const coreIndexFileModel = await CoreIndexFile.createModel('writerLock', 'invalidProvisionalIndexFileHash', coreProofFileHash, [createOperation], [], []);
const coreProofFileUri = undefined;
const coreIndexFileModel = await CoreIndexFile.createModel('writerLock', 'invalidProvisionalIndexFileUri', coreProofFileUri, [createOperation], [], []);

try {
const coreIndexFileBuffer = Buffer.from(JSON.stringify(coreIndexFileModel));
Expand All @@ -194,8 +194,8 @@ describe('CoreIndexFile', async () => {
it('should throw if writer lock id is not string.', async () => {
const createOperationData = await OperationGenerator.generateCreateOperation();
const createOperation = createOperationData.createOperation;
const coreProofFileHash = undefined;
const coreIndexFileModel = await CoreIndexFile.createModel('unusedWriterLockId', 'unusedMockFileHash', coreProofFileHash, [createOperation], [], []);
const coreProofFileUri = undefined;
const coreIndexFileModel = await CoreIndexFile.createModel('unusedWriterLockId', 'unusedMockFileUri', coreProofFileUri, [createOperation], [], []);

(coreIndexFileModel as any).writerLockId = {}; // intentionally set to invalid value

Expand All @@ -208,9 +208,9 @@ describe('CoreIndexFile', async () => {
it('should throw if writer lock ID exceeded max size.', async () => {
const createOperationData = await OperationGenerator.generateCreateOperation();
const createOperation = createOperationData.createOperation;
const coreProofFileHash = undefined;
const coreProofFileUri = undefined;
const coreIndexFileModel =
await CoreIndexFile.createModel('unusedWriterLockId', 'unusedMockFileHash', coreProofFileHash, [createOperation], [], []);
await CoreIndexFile.createModel('unusedWriterLockId', 'unusedMockFileUri', coreProofFileUri, [createOperation], [], []);

(coreIndexFileModel as any).writerLockId = crypto.randomBytes(2000).toString('hex'); // Intentionally larger than maximum.

Expand Down Expand Up @@ -292,8 +292,8 @@ describe('CoreIndexFile', async () => {

describe('createModel()', async () => {
it('should created an core index file model correctly.', async () => {
const provisionalIndexFileHash = 'EiB4ypIXxG9aFhXv2YC8I2tQvLEBbQAsNzHmph17vMfVYA';
const coreProofFileHash = 'EiBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB';
const provisionalIndexFileUri = 'EiB4ypIXxG9aFhXv2YC8I2tQvLEBbQAsNzHmph17vMfVYA';
const coreProofFileUri = 'EiBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB';

// Create operation.
const createOperationData = await OperationGenerator.generateCreateOperation();
Expand All @@ -310,10 +310,10 @@ describe('CoreIndexFile', async () => {
const deactivateOperation = deactivateOperationData.deactivateOperation;

const coreIndexFileModel = await CoreIndexFile.createModel(
undefined, provisionalIndexFileHash, coreProofFileHash, [createOperation], [recoverOperation], [deactivateOperation]
undefined, provisionalIndexFileUri, coreProofFileUri, [createOperation], [recoverOperation], [deactivateOperation]
);

expect(coreIndexFileModel.provisionalIndexFileUri).toEqual(provisionalIndexFileHash);
expect(coreIndexFileModel.provisionalIndexFileUri).toEqual(provisionalIndexFileUri);
expect(coreIndexFileModel.operations!.create![0].suffixData).toEqual({
deltaHash: createOperation.suffixData.deltaHash, recoveryCommitment: createOperation.suffixData.recoveryCommitment, type: undefined
});
Expand All @@ -333,26 +333,26 @@ describe('CoreIndexFile', async () => {

it('should not create `operations` property if there is no create, recover, and deactivates.', async () => {
const writerLockId = undefined;
const provisionalIndexFileHash = OperationGenerator.generateRandomHash();
const coreProofFileHash = undefined;
const coreIndexFileModel = await CoreIndexFile.createModel(writerLockId, provisionalIndexFileHash, coreProofFileHash, [], [], []);
const provisionalIndexFileUri = OperationGenerator.generateRandomHash();
const coreProofFileUri = undefined;
const coreIndexFileModel = await CoreIndexFile.createModel(writerLockId, provisionalIndexFileUri, coreProofFileUri, [], [], []);

expect(coreIndexFileModel.operations).toBeUndefined();
});
});

describe('createBuffer()', async () => {
it('should created a compressed buffer correctly.', async () => {
const provisionalIndexFileHash = 'EiB4ypIXxG9aFhXv2YC8I2tQvLEBbQAsNzHmph17vMfVYA';
const coreProofFileHash = undefined;
const provisionalIndexFileUri = 'EiB4ypIXxG9aFhXv2YC8I2tQvLEBbQAsNzHmph17vMfVYA';
const coreProofFileUri = undefined;
const createOperationData = await OperationGenerator.generateCreateOperation();
const createOperation = createOperationData.createOperation;

const coreIndexFileBuffer = await CoreIndexFile.createBuffer(undefined, provisionalIndexFileHash, coreProofFileHash, [createOperation], [], []);
const coreIndexFileBuffer = await CoreIndexFile.createBuffer(undefined, provisionalIndexFileUri, coreProofFileUri, [createOperation], [], []);

const coreIndexFile = await CoreIndexFile.parse(coreIndexFileBuffer);

expect(coreIndexFile.model.provisionalIndexFileUri).toEqual(provisionalIndexFileHash);
expect(coreIndexFile.model.provisionalIndexFileUri).toEqual(provisionalIndexFileUri);
expect(coreIndexFile.model.operations!.create![0].suffixData).toEqual({
deltaHash: createOperation.suffixData.deltaHash, recoveryCommitment: createOperation.suffixData.recoveryCommitment
});
Expand Down
20 changes: 10 additions & 10 deletions tests/core/Observer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,41 +163,41 @@ describe('Observer', async () => {
const operation2Data = await OperationGenerator.generateAnchoredCreateOperation({ transactionTime: 1, transactionNumber: 1, operationIndex: 2 });
const createOperations = [operation1Data.createOperation, operation2Data.createOperation];

const coreProofFileHash = undefined;
const coreProofFileUri = undefined;

// Generating chunk file data.
const mockChunkFileBuffer = await ChunkFile.createBuffer(createOperations, [], []);
const mockChunkFileFetchResult: FetchResult = {
code: FetchResultCode.Success,
content: mockChunkFileBuffer
};
const mockChunkFileHash = Encoder.encode(Multihash.hash(Buffer.from('MockChunkFileHash')));
const mockChunkFileUri = Encoder.encode(Multihash.hash(Buffer.from('MockChunkFileUri')));

// Generating provisional index file data.
const mockProvisionalProofFileUri = undefined;
const mockProvisionalIndexFileBuffer = await ProvisionalIndexFile.createBuffer(mockChunkFileHash, mockProvisionalProofFileUri, []);
const mockProvisionalIndexFileHash = Encoder.encode(Multihash.hash(Buffer.from('MockProvisionalIndexFileHash')));
const mockProvisionalIndexFileBuffer = await ProvisionalIndexFile.createBuffer(mockChunkFileUri, mockProvisionalProofFileUri, []);
const mockProvisionalIndexFileUri = Encoder.encode(Multihash.hash(Buffer.from('MockProvisionalIndexFileUri')));
const mockProvisionalIndexFileFetchResult: FetchResult = {
code: FetchResultCode.Success,
content: mockProvisionalIndexFileBuffer
};

// Generating core index file data.
const mockCoreIndexFileBuffer =
await CoreIndexFile.createBuffer('writerLock', mockProvisionalIndexFileHash, coreProofFileHash, createOperations, [], []);
await CoreIndexFile.createBuffer('writerLock', mockProvisionalIndexFileUri, coreProofFileUri, createOperations, [], []);
const mockAnchoredFileFetchResult: FetchResult = {
code: FetchResultCode.Success,
content: mockCoreIndexFileBuffer
};
const mockCoreIndexFileHash = Encoder.encode(Multihash.hash(Buffer.from('MockCoreIndexFileHash')));
const mockCoreIndexFileUri = Encoder.encode(Multihash.hash(Buffer.from('MockCoreIndexFileUri')));

// Prepare the mock fetch results from the `DownloadManager.download()`.
const mockDownloadFunction = async (hash: string) => {
if (hash === mockCoreIndexFileHash) {
if (hash === mockCoreIndexFileUri) {
return mockAnchoredFileFetchResult;
} else if (hash === mockProvisionalIndexFileHash) {
} else if (hash === mockProvisionalIndexFileUri) {
return mockProvisionalIndexFileFetchResult;
} else if (hash === mockChunkFileHash) {
} else if (hash === mockChunkFileUri) {
return mockChunkFileFetchResult;
} else {
throw new Error('Test failed, unexpected hash given');
Expand All @@ -216,7 +216,7 @@ describe('Observer', async () => {
1
);

const anchoredData = AnchoredDataSerializer.serialize({ coreIndexFileUri: mockCoreIndexFileHash, numberOfOperations: createOperations.length });
const anchoredData = AnchoredDataSerializer.serialize({ coreIndexFileUri: mockCoreIndexFileUri, numberOfOperations: createOperations.length });
const mockTransaction: TransactionModel = {
transactionNumber: 1,
transactionTime: 1000000,
Expand Down
Loading

0 comments on commit 3ac7f22

Please sign in to comment.