Skip to content

Commit

Permalink
Merge pull request #97 from brkagithub/fix/historical-state-issue
Browse files Browse the repository at this point in the history
Fix choosing private repository in get operation
  • Loading branch information
NZT48 authored Aug 24, 2023
2 parents b3f3bb4 + b461cf2 commit ed89864
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions managers/asset-operations-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const {
toNQuads,
toJSONLD,
sleepForMilliseconds,
deriveRepository,
} = require('../services/utilities.js');
const {
CONTENT_TYPES,
Expand All @@ -20,6 +19,7 @@ const {
PRIVATE_ASSERTION_PREDICATE,
QUERY_TYPES,
DEFAULT_PARAMETERS,
OT_NODE_TRIPLE_STORE_REPOSITORIES,
} = require('../constants.js');
const emptyHooks = require('../util/empty-hooks');
const { STORE_TYPES, ASSET_STATES } = require('../constants');
Expand Down Expand Up @@ -345,43 +345,55 @@ class AssetOperationsManager {

const { tokenId } = resolveUAL(UAL);

let publicAssertionId;
if(state === ASSET_STATES.LATEST) {
let publicAssertionId,
stateFinalized = false;
if (state === ASSET_STATES.LATEST) {
const unfinalizedState = await this.blockchainService.getUnfinalizedState(
tokenId,
blockchain,
);

if (unfinalizedState != null && unfinalizedState !== ZeroHash) {
publicAssertionId = unfinalizedState;
stateFinalized = false;
}
}

let assertionIds = [];
const isEnumState = Object.values(ASSET_STATES).includes(state);
if(!publicAssertionId) {
if (!publicAssertionId) {
assertionIds = await this.blockchainService.getAssertionIds(tokenId, blockchain);

if (isEnumState) {
publicAssertionId = assertionIds[assertionIds.length - 1];
stateFinalized = true;
} else if (typeof state === 'number') {
if (state >= assertionIds.length) {
throw Error('State index is out of range.');
}

publicAssertionId = assertionIds[state];

if (state === assertionIds.length - 1) stateFinalized = true;
} else if (assertionIds.includes(state)) {
publicAssertionId = state;

if (state === assertionIds[assertionIds.length - 1]) stateFinalized = true;
} else if (/^0x[a-fA-F0-9]{64}$/.test(state)) {
const unfinalizedState = await this.blockchainService.getUnfinalizedState(
tokenId,
blockchain,
);

if (unfinalizedState != null && unfinalizedState !== ZeroHash && state === unfinalizedState) {

if (
unfinalizedState != null &&
unfinalizedState !== ZeroHash &&
state === unfinalizedState
) {
publicAssertionId = unfinalizedState;
stateFinalized = false;
} else {
throw Error('Given state hash isn\'t a part of the Knowledge Asset.')
throw Error("Given state hash isn't a part of the Knowledge Asset.");
}
} else {
throw Error('Incorrect state option.');
Expand Down Expand Up @@ -494,17 +506,15 @@ class AssetOperationsManager {
}
}`;

const repository = deriveRepository(
DEFAULT_PARAMETERS.GRAPH_LOCATION,
DEFAULT_PARAMETERS.GRAPH_STATE,
);
queryPrivateOperationId = await this.nodeApiService.query(
endpoint,
port,
authToken,
queryString,
QUERY_TYPES.CONSTRUCT,
repository,
stateFinalized
? OT_NODE_TRIPLE_STORE_REPOSITORIES.PRIVATE_CURRENT
: OT_NODE_TRIPLE_STORE_REPOSITORIES.PRIVATE_HISTORY,
);

queryPrivateOperationResult = await this.nodeApiService.getOperationResult(
Expand Down

0 comments on commit ed89864

Please sign in to comment.