Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: sortKey for dryWrite #506

Merged
merged 4 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "warp-contracts",
"version": "1.4.33",
"version": "1.4.34-beta.0",
"description": "An implementation of the SmartWeave smart contract protocol.",
"types": "./lib/types/index.d.ts",
"main": "./lib/cjs/index.js",
Expand Down
6 changes: 6 additions & 0 deletions src/__tests__/unit/evaluation-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('Evaluation options evaluator', () => {
stackTrace: {
saveState: false
},
strictSortKey: false,
throwOnInternalWriteError: true,
transactionsPagesPerBatch: null,
unsafeClient: 'throw',
Expand Down Expand Up @@ -65,6 +66,7 @@ describe('Evaluation options evaluator', () => {
stackTrace: {
saveState: false
},
strictSortKey: false,
throwOnInternalWriteError: true,
transactionsPagesPerBatch: null,
unsafeClient: 'throw',
Expand Down Expand Up @@ -99,6 +101,7 @@ describe('Evaluation options evaluator', () => {
stackTrace: {
saveState: false
},
strictSortKey: false,
throwOnInternalWriteError: true,
transactionsPagesPerBatch: null,
unsafeClient: 'allow',
Expand Down Expand Up @@ -130,6 +133,7 @@ describe('Evaluation options evaluator', () => {
stackTrace: {
saveState: false
},
strictSortKey: false,
throwOnInternalWriteError: true,
transactionsPagesPerBatch: null,
unsafeClient: 'allow',
Expand Down Expand Up @@ -161,6 +165,7 @@ describe('Evaluation options evaluator', () => {
stackTrace: {
saveState: false
},
strictSortKey: false,
throwOnInternalWriteError: true,
transactionsPagesPerBatch: null,
unsafeClient: 'throw',
Expand Down Expand Up @@ -192,6 +197,7 @@ describe('Evaluation options evaluator', () => {
stackTrace: {
saveState: false
},
strictSortKey: false,
throwOnInternalWriteError: true,
transactionsPagesPerBatch: null,
unsafeClient: 'skip',
Expand Down
7 changes: 5 additions & 2 deletions src/contract/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ export interface Contract<State = unknown> {
tags?: Tags,
transfer?: ArTransfer,
caller?: string,
signal?: AbortSignal
signal?: AbortSignal,
sortKey?: string
): Promise<InteractionResult<State, View>>;

/**
Expand Down Expand Up @@ -186,13 +187,15 @@ export interface Contract<State = unknown> {
* @param caller - an option to override the caller - if available, this value will overwrite the caller evaluated
* from the wallet connected to this contract.
* @param vrf - whether a mock VRF data should be generated
* @param sortKey - sortKey at which the state will be loaded prior to applying input
*/
dryWrite<Input>(
input: Input,
caller?: string,
tags?: Tags,
transfer?: ArTransfer,
vrf?: boolean
vrf?: boolean,
sortKey?: string
): Promise<InteractionResult<State, unknown>>;

applyInput<Input>(
Expand Down
6 changes: 4 additions & 2 deletions src/contract/EvaluationOptionsEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,16 @@ export class EvaluationOptionsEvaluator {
useKVStorage: (foreignOptions) => foreignOptions['useKVStorage'],
useConstructor: (foreignOptions) => foreignOptions['useConstructor'],
whitelistSources: () => this.rootOptions['whitelistSources'],
transactionsPagesPerBatch: () => this.rootOptions['transactionsPagesPerBatch']
transactionsPagesPerBatch: () => this.rootOptions['transactionsPagesPerBatch'],
strictSortKey: () => this.rootOptions['strictSortKey']
};

private readonly notConflictingEvaluationOptions: (keyof EvaluationOptions)[] = [
'useKVStorage',
'sourceType',
'useConstructor',
'transactionsPagesPerBatch'
'transactionsPagesPerBatch',
'strictSortKey'
];

/**
Expand Down
13 changes: 9 additions & 4 deletions src/contract/HandlerBasedContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,15 @@ export class HandlerBasedContract<State> implements Contract<State> {
tags: Tags = [],
transfer: ArTransfer = emptyTransfer,
caller?: string,
signal?: AbortSignal
signal?: AbortSignal,
sortKey?: string
): Promise<InteractionResult<State, View>> {
this.logger.info('View state for', this._contractTxId);
return await this.callContract<Input, View>(
input,
'view',
caller,
undefined,
sortKey,
tags,
transfer,
false,
Expand All @@ -312,10 +313,11 @@ export class HandlerBasedContract<State> implements Contract<State> {
caller?: string,
tags?: Tags,
transfer?: ArTransfer,
vrf?: boolean
vrf?: boolean,
sortKey?: string
): Promise<InteractionResult<State, unknown>> {
this.logger.info('Dry-write for', this._contractTxId);
return await this.callContract<Input>(input, 'write', caller, undefined, tags, transfer, undefined, vrf);
return await this.callContract<Input>(input, 'write', caller, sortKey, tags, transfer, undefined, vrf);
}

async applyInput<Input>(
Expand Down Expand Up @@ -645,6 +647,9 @@ export class HandlerBasedContract<State> implements Contract<State> {
>;
}
cachedState = 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`);
}

this.logger.debug('cache lookup', benchmark.elapsed());
benchmark.reset();
Expand Down
9 changes: 9 additions & 0 deletions src/core/modules/StateEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ export class DefaultEvaluationOptions implements EvaluationOptions {
whitelistSources = [];

transactionsPagesPerBatch = null;

strictSortKey = false;
}

// an interface for the contract EvaluationOptions - can be used to change the behaviour of some features.
Expand Down Expand Up @@ -243,9 +245,16 @@ export interface EvaluationOptions {
// remote source for fetching most recent contract state, only applicable if remoteStateSyncEnabled is set to true
remoteStateSyncSource: string;

// an array of source tx ids that are allowed to be evaluated by the SDK
whitelistSources: string[];

// how many interactions pages are evaluated in a single evaluation batch
transactionsPagesPerBatch: number;

// whether passing sortKey to some functions like viewState or readStateFor is strict
// - if it is, then we're requiring the SDK to have the state cached at this exact sortKey
// - so that SDK won't load and evaluated missing interactions
strictSortKey: boolean;
}

// https://github.com/nodejs/node/issues/40678 duh...
Expand Down
Loading