From bf2242c4897f57607ce42f44cfc4a4848b2674a2 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Thu, 29 Feb 2024 10:04:36 -0700 Subject: [PATCH] fix(sortKey): readStateBatch not properly handling sortKey We can look further into the warp implementation to understand why it is not properly respecting sortKeys as expected. --- src/api/warp.ts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/api/warp.ts b/src/api/warp.ts index 1b59966..d01e088 100644 --- a/src/api/warp.ts +++ b/src/api/warp.ts @@ -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'; @@ -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); @@ -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, }; }