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

Commit

Permalink
feat(PE-5550): support sortKey for read interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Feb 29, 2024
1 parent 00c56a4 commit 163918c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"koa2-swagger-ui": "^5.10.0",
"lodash": "^4.17.21",
"prom-client": "^14.2.0",
"warp-contracts": "^1.4.32",
"warp-contracts": "^1.4.36",
"warp-contracts-lmdb": "^1.1.10",
"warp-contracts-sqlite": "^1.0.2",
"winston": "^3.8.2",
Expand Down
50 changes: 40 additions & 10 deletions src/api/warp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import {
DEFAULT_EVALUATION_OPTIONS,
DEFAULT_PAGES_PER_BATCH,
DEFAULT_STATE_EVALUATION_TIMEOUT_MS,
allowedContractTypes,
} from '../constants';
import { ContractType, EvaluatedContractState } from '../types';
Expand Down Expand Up @@ -108,15 +109,18 @@ class ContractReadInteractionCacheKey {
public readonly contractTxId: string,
public readonly functionName: string,
public readonly input: any,

Check warning on line 111 in src/api/warp.ts

View workflow job for this annotation

GitHub Actions / build (lint:check)

Unexpected any. Specify a different type
public readonly sortKey?: string,
public readonly warp: Warp,
public readonly evaluationOptions: Partial<EvaluationOptions>,
public readonly logger?: winston.Logger,
) {}

toString(): string {
return `${this.contractTxId}-${this.functionName}-${createQueryParamHash(
this.input,
)}-${createQueryParamHash(this.evaluationOptions)}`;
return `${this.contractTxId}-${this.functionName}-${
this.sortKey || 'latest'
}-${createQueryParamHash(this.input)}-${createQueryParamHash(
this.evaluationOptions,
)}`;
}

// Facilitate ReadThroughPromiseCache key derivation
Expand Down Expand Up @@ -368,14 +372,23 @@ export async function readThroughToContractReadInteraction(
cacheKey: ContractReadInteractionCacheKey,
): Promise<{
result: unknown;
sortKey: string | undefined;
evaluationOptions: Partial<EvaluationOptions>;
input: unknown;
}> {
const { contractTxId, evaluationOptions, warp, logger, functionName, input } =
cacheKey;
const {
contractTxId,
evaluationOptions,
sortKey,
warp,
logger,
functionName,
input,
} = cacheKey;
logger?.debug('Reading through to contract read interaction...', {
contractTxId,
cacheKey: cacheKey.toString(),
sortKey,
});
const cacheId = cacheKey.toString();

Expand All @@ -386,11 +399,13 @@ export async function readThroughToContractReadInteraction(
logger?.debug('Deduplicating in flight requests for read interaction...', {
contractTxId,
cacheKey: cacheKey.toString(),
sortKey,
});
const { result } = await inFlightRequest;
return {
result,
input,
sortKey,
evaluationOptions,
};
}
Expand All @@ -406,18 +421,25 @@ export async function readThroughToContractReadInteraction(
.setEvaluationOptions(evaluationOptions);

// set cached value for multiple requests during initial promise
// TODO: add abort signal when view state supports it
const readInteractionPromise = contract.viewState({
function: functionName,
...input,
});
const readInteractionPromise = contract.viewState(
{
function: functionName,
...input,
},
undefined, // tags
undefined, // transfer
undefined, // caller
AbortSignal.timeout(DEFAULT_STATE_EVALUATION_TIMEOUT_MS),
sortKey,
);
readRequestMap.set(cacheId, readInteractionPromise);

readInteractionPromise
.catch((error: unknown) => {
logger?.debug('Failed to evaluate read interaction on contract!', {
contractTxId,
cacheKey: cacheKey.toString(),
sortKey,
error: error instanceof Error ? error.message : 'Unknown error',
});
})
Expand All @@ -436,6 +458,7 @@ export async function readThroughToContractReadInteraction(
if (!readInteractionResult) {
logger?.error('Read interaction did not return a result!', {
contractTxId,
sortKey,
cacheKey: cacheKey.toString(),
input,
});
Expand All @@ -451,6 +474,7 @@ export async function readThroughToContractReadInteraction(
contractTxId,
cacheKey: cacheKey.toString(),
input,
sortKey,
error,
errorMessage,
});
Expand All @@ -459,12 +483,15 @@ export async function readThroughToContractReadInteraction(

logger?.debug('Successfully evaluated read interaction on contract.', {
contractTxId,
sortKey,

cacheKey: cacheKey.toString(),
});

return {
result,
input,
sortKey,
evaluationOptions,
};
}
Expand All @@ -475,12 +502,14 @@ export async function getContractReadInteraction({
logger,
functionName,
input,
sortKey,
}: {
contractTxId: string;
warp: Warp;
logger: winston.Logger;
functionName: string;
input: ParsedUrlQuery;
sortKey?: string | undefined;
}): Promise<{
result: any;

Check warning on line 514 in src/api/warp.ts

View workflow job for this annotation

GitHub Actions / build (lint:check)

Unexpected any. Specify a different type
evaluationOptions: Partial<EvaluationOptions>;
Expand All @@ -496,6 +525,7 @@ export async function getContractReadInteraction({
contractTxId,
functionName,
input,
sortKey,
warp,
evaluationOptions,
logger,
Expand Down
6 changes: 5 additions & 1 deletion src/routes/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ export async function contractReservedHandler(ctx: KoaContext) {

const queryParamsCastedToNumbers = ['qty', 'years', 'height'];
export async function contractReadInteractionHandler(ctx: KoaContext) {
const { warp, logger: _logger } = ctx.state;
const { warp, logger: _logger, sortKey: requestedSortKey } = ctx.state;
const { contractTxId, functionName } = ctx.params;
const { query: input } = ctx.request;

Expand All @@ -550,6 +550,8 @@ export async function contractReadInteractionHandler(ctx: KoaContext) {
functionName,
});

// TODO: compute sortKey from blockHeight when provided

const parsedInput = Object.entries(input).reduce(
(parsedParams: { [x: string]: any }, [key, value]) => {

Check warning on line 556 in src/routes/contract.ts

View workflow job for this annotation

GitHub Actions / build (lint:check)

Unexpected any. Specify a different type
// parse known integer values for parameters we care about
Expand All @@ -571,13 +573,15 @@ export async function contractReadInteractionHandler(ctx: KoaContext) {
contractTxId,
warp,
logger,
sortKey: requestedSortKey,
functionName,
input: parsedInput,
});

ctx.body = {
contractTxId,
result,
sortKey: requestedSortKey,
evaluationOptions,
};
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8223,10 +8223,10 @@ warp-contracts-sqlite@^1.0.2:
better-sqlite3 "^8.3.0"
safe-stable-stringify "^2.4.3"

warp-contracts@^1.4.32:
version "1.4.32"
resolved "https://registry.yarnpkg.com/warp-contracts/-/warp-contracts-1.4.32.tgz#3d5c1ce973dde9d481618b1e060fac2de2870049"
integrity sha512-33OGr5xmarXFAfdmnJvAOrFLIT/Z6mp5BJEXw3dfaffM7mOGDsegWMDxNRoqWC2LFQFvvqnmuxq/FhEC6QRNgg==
warp-contracts@^1.4.36:
version "1.4.36"
resolved "https://registry.yarnpkg.com/warp-contracts/-/warp-contracts-1.4.36.tgz#dfdb69a34e314b2be5cb2ea8e51078a064c2fcff"
integrity sha512-2UeRxkry/wm0jme4d4CXvxUef+G3o3Al6Q2Tj2PQ5cvvK5GOfRfFiooaQIik88E6ZQNC9NJlvFUYBMVS9837CA==
dependencies:
archiver "^5.3.0"
arweave "1.13.7"
Expand Down

0 comments on commit 163918c

Please sign in to comment.