Skip to content

Commit

Permalink
feat: read state in batches
Browse files Browse the repository at this point in the history
  • Loading branch information
ppedziwiatr committed Dec 14, 2023
1 parent eb044fe commit 00514f1
Show file tree
Hide file tree
Showing 16 changed files with 296 additions and 63 deletions.
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 @@ -27,6 +27,7 @@ describe('Evaluation options evaluator', () => {
saveState: false
},
throwOnInternalWriteError: true,
transactionsPagesPerBatch: null,
unsafeClient: 'throw',
updateCacheForEachInteraction: false,
useKVStorage: false,
Expand Down Expand Up @@ -65,6 +66,7 @@ describe('Evaluation options evaluator', () => {
saveState: false
},
throwOnInternalWriteError: true,
transactionsPagesPerBatch: null,
unsafeClient: 'throw',
updateCacheForEachInteraction: false,
useKVStorage: false,
Expand Down Expand Up @@ -98,6 +100,7 @@ describe('Evaluation options evaluator', () => {
saveState: false
},
throwOnInternalWriteError: true,
transactionsPagesPerBatch: null,
unsafeClient: 'allow',
updateCacheForEachInteraction: false,
useKVStorage: false,
Expand Down Expand Up @@ -128,6 +131,7 @@ describe('Evaluation options evaluator', () => {
saveState: false
},
throwOnInternalWriteError: true,
transactionsPagesPerBatch: null,
unsafeClient: 'allow',
updateCacheForEachInteraction: false,
useKVStorage: false,
Expand Down Expand Up @@ -158,6 +162,7 @@ describe('Evaluation options evaluator', () => {
saveState: false
},
throwOnInternalWriteError: true,
transactionsPagesPerBatch: null,
unsafeClient: 'throw',
updateCacheForEachInteraction: false,
useKVStorage: false,
Expand Down Expand Up @@ -188,6 +193,7 @@ describe('Evaluation options evaluator', () => {
saveState: false
},
throwOnInternalWriteError: true,
transactionsPagesPerBatch: null,
unsafeClient: 'skip',
updateCacheForEachInteraction: false,
useKVStorage: false,
Expand Down
31 changes: 24 additions & 7 deletions src/contract/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,22 @@ export interface Contract<State = unknown> {
*/
readState(
sortKeyOrBlockHeight?: string | number,
caller?: string,
interactions?: GQLNodeInterface[]
interactions?: GQLNodeInterface[],
signal?: AbortSignal
): Promise<SortKeyCacheResult<EvalStateResult<State>>>;

readStateFor(sortKey: string, interactions: GQLNodeInterface[]): Promise<SortKeyCacheResult<EvalStateResult<State>>>;
/**
* Reads state in batches - i.e. it first loads max. 5k interactions, evaluates them, then reads another 5k..and so on.
*
* Consider this as an experimental feature
*/
readStateBatch(pagesPerBatch: number, signal: AbortSignal): Promise<SortKeyCacheResult<EvalStateResult<State>>>;

readStateFor(
sortKey: string,
interactions: GQLNodeInterface[],
signal?: AbortSignal
): Promise<SortKeyCacheResult<EvalStateResult<State>>>;

/**
* Returns the "view" of the state, computed by the SWC -
Expand All @@ -138,7 +149,8 @@ export interface Contract<State = unknown> {
input: Input,
tags?: Tags,
transfer?: ArTransfer,
caller?: string
caller?: string,
signal?: AbortSignal
): Promise<InteractionResult<State, View>>;

/**
Expand All @@ -155,7 +167,8 @@ export interface Contract<State = unknown> {
*/
viewStateForTx<Input = unknown, View = unknown>(
input: Input,
transaction: GQLNodeInterface
transaction: GQLNodeInterface,
signal?: AbortSignal
): Promise<InteractionResult<State, View>>;

/**
Expand All @@ -177,7 +190,11 @@ export interface Contract<State = unknown> {
vrf?: boolean
): Promise<InteractionResult<State, unknown>>;

applyInput<Input>(input: Input, transaction: GQLNodeInterface): Promise<InteractionResult<State, unknown>>;
applyInput<Input>(
input: Input,
transaction: GQLNodeInterface,
signal?: AbortSignal
): Promise<InteractionResult<State, unknown>>;

/**
* Writes a new "interaction" transaction - i.e. such transaction that stores input for the contract.
Expand All @@ -189,7 +206,7 @@ export interface Contract<State = unknown> {

/**
* Returns the full call tree report the last
* interaction with contract (eg. after reading state)
* interaction with contract (e.g. after reading state)
*/
getCallStack(): ContractCallRecord;

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

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

/**
Expand Down
Loading

0 comments on commit 00514f1

Please sign in to comment.