Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
fix(sortKey): readStateBatch not properly handling sortKey
Browse files Browse the repository at this point in the history
We can look further into the warp implementation to understand why it is not properly respecting sortKeys as expected.
  • Loading branch information
dtfiedler committed Feb 29, 2024
1 parent 6a429cb commit bf2242c
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/api/warp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from 'warp-contracts';
import {
DEFAULT_EVALUATION_OPTIONS,
DEFAULT_PAGES_PER_BATCH,
// DEFAULT_PAGES_PER_BATCH,
allowedContractTypes,
} from '../constants';
import { ContractType, EvaluatedContractState } from '../types';
Expand Down Expand Up @@ -219,17 +219,20 @@ async function readThroughToContractState(
});

// only use batch read if no block height provided (it does not currently support block heights)
const doBatchRead = providedSortKey || providedBlockHeight === undefined;
const doBatchRead = !!providedSortKey || providedBlockHeight === undefined;
logger?.debug('Evaluating contract state...', {
contractTxId,
cacheKey: cacheKey.toString(),
doBatchRead,
doBatchRead, // there is a bug in warp where readStateBatch is not properly using sortKeys
evaluationOptions,
});

const readStatePromise = doBatchRead
? contract.readStateBatch(DEFAULT_PAGES_PER_BATCH, providedSortKey, signal)
: contract.readState(providedBlockHeight, undefined, signal);
// TODO: re-enable readStateBatch after sortKey is fixed
const readStatePromise = contract.readState(
providedSortKey || providedBlockHeight,
undefined,
signal,
);

// set cached value for multiple requests during initial promise
stateRequestMap.set(cacheId, readStatePromise);
Expand All @@ -256,20 +259,22 @@ async function readThroughToContractState(
logger?.error('Contract state did not return a result!', {
contractTxId,
cacheKey: cacheKey.toString(),
sortKey: providedSortKey,
});
throw new UnknownError(`Unknown error occurred evaluating contract state.`);
}

const { cachedValue, sortKey } = stateEvaluationResult;
const { cachedValue, sortKey: evaluatedSortKey } = stateEvaluationResult;
logger?.debug('Successfully evaluated contract state.', {
contractTxId,
cacheKey: cacheKey.toString(),
sortKey,
evaluatedSortKey,
providedSortKey,
});

return {
...cachedValue,
sortKey,
sortKey: evaluatedSortKey,
evaluationOptions,
};
}
Expand Down

0 comments on commit bf2242c

Please sign in to comment.