Skip to content

Commit

Permalink
feat: readState - state parameter added
Browse files Browse the repository at this point in the history
  • Loading branch information
asiaziola committed Feb 13, 2024
1 parent 46fb180 commit 6c825e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
20 changes: 14 additions & 6 deletions src/contract/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ export interface Contract<State = unknown> {
* Returns state of the contract at required sortKey or blockHeight.
*
* @param sortKeyOrBlockHeight - either a sortKey or block height at which the contract should be read
*
* @param currentTx - a set of currently evaluating interactions, that should
* be skipped during contract inner calls - to prevent the infinite call loop issue
* (mostly related to contract that use the Foreign Call Protocol)
* @param interactions - optional interactions to be applied on state
* @param signal - allows to communicate with a DOM request (such as Fetch) and abort it
* @param state - uses specified state to read contract state (overrides reading state from the cache)
*/
readState(
sortKeyOrBlockHeight?: string | number,
interactions?: GQLNodeInterface[],
signal?: AbortSignal
signal?: AbortSignal,
state?: SortKeyCacheResult<EvalStateResult<State>>
): Promise<SortKeyCacheResult<EvalStateResult<State>>>;

/**
Expand All @@ -128,10 +128,18 @@ export interface Contract<State = unknown> {
signal?: AbortSignal
): Promise<SortKeyCacheResult<EvalStateResult<State>>>;

/**
* Reads state at a specified sortKey and applies indicated interactions
* @param sortKey - sortKey at which the contract should be read
* @param interactions - optional interactions to be applied on state
* @param signal - allows to communicate with a DOM request (such as Fetch) and abort it
* @param state - uses specified state to read contract state (overrides reading state from the cache)
*/
readStateFor(
sortKey: string,
interactions: GQLNodeInterface[],
signal?: AbortSignal
signal?: AbortSignal,
state?: SortKeyCacheResult<EvalStateResult<State>>
): Promise<SortKeyCacheResult<EvalStateResult<State>>>;

/**
Expand Down
16 changes: 10 additions & 6 deletions src/contract/HandlerBasedContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ export class HandlerBasedContract<State> implements Contract<State> {
async readState(
sortKeyOrBlockHeight?: string | number,
interactions?: GQLNodeInterface[],
signal?: AbortSignal
signal?: AbortSignal,
state?: SortKeyCacheResult<EvalStateResult<State>>
): Promise<SortKeyCacheResult<EvalStateResult<State>>> {
this.logger.info('Read state for', {
contractTxId: this._contractTxId,
Expand Down Expand Up @@ -181,7 +182,8 @@ export class HandlerBasedContract<State> implements Contract<State> {
sortKey,
false,
interactions,
signal
signal,
state
);
this.logger.info('Execution Context', {
srcTxId: executionContext.contractDefinition?.srcTxId,
Expand Down Expand Up @@ -221,9 +223,10 @@ export class HandlerBasedContract<State> implements Contract<State> {
async readStateFor(
sortKey: string,
interactions: GQLNodeInterface[],
signal?: AbortSignal
signal?: AbortSignal,
state?: SortKeyCacheResult<EvalStateResult<State>>
): Promise<SortKeyCacheResult<EvalStateResult<State>>> {
return this.readState(sortKey, interactions, signal);
return this.readState(sortKey, interactions, signal, state);
}

async readStateBatch(
Expand Down Expand Up @@ -635,7 +638,8 @@ export class HandlerBasedContract<State> implements Contract<State> {
upToSortKey?: string,
forceDefinitionLoad = false,
interactions?: GQLNodeInterface[],
signal?: AbortSignal
signal?: AbortSignal,
state?: SortKeyCacheResult<EvalStateResult<State>>
): Promise<ExecutionContext<State, HandlerApi<State>>> {
const { definitionLoader, interactionsLoader, stateEvaluator } = this.warp;
let cachedState: SortKeyCacheResult<EvalStateResult<State>>;
Expand All @@ -646,7 +650,7 @@ export class HandlerBasedContract<State> implements Contract<State> {
EvalStateResult<State>
>;
}
cachedState = cachedState || (await stateEvaluator.latestAvailableState<State>(contractTxId, upToSortKey));
cachedState = state || cachedState || (await stateEvaluator.latestAvailableState<State>(contractTxId, upToSortKey));
if (upToSortKey && this.evaluationOptions().strictSortKey && cachedState?.sortKey != upToSortKey) {
throw new Error(`State not cached at the exact required ${upToSortKey} sortKey`);
}
Expand Down

0 comments on commit 6c825e6

Please sign in to comment.