Skip to content

Commit

Permalink
chore: state cache interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeuchi committed May 11, 2023
1 parent cde7b07 commit 051e80d
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 98 deletions.
71 changes: 71 additions & 0 deletions src/cache/BasicSortKeyCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { CacheKey, PruneStats, SortKeyCacheResult } from './SortKeyCache';

/**
* A cache that stores its values per dedicated key and sort key.
* A sort key is a value that the SmartWeave protocol is using
* to sort contract transactions ({@link LexicographicalInteractionsSorter}.
*
* All values should be stored in a lexicographical order (per key) -
* sorted by the sort key.
*/
export interface BasicSortKeyCache<V> {
getLessOrEqual(key: string, sortKey: string): Promise<SortKeyCacheResult<V> | null>;

/**
* returns value stored for a given key and last sortKey
*/
getLast(key: string): Promise<SortKeyCacheResult<V> | null>;

/**
* returns last cached sort key - takes all keys into account
*/
getLastSortKey(): Promise<string | null>;

/**
* returns value for the key and exact sortKey
*/
get(cacheKey: CacheKey): Promise<SortKeyCacheResult<V> | null>;

/**
* puts new value in cache under given {@link CacheKey.key} and {@link CacheKey.sortKey}.
*/
put(cacheKey: CacheKey, value: V): Promise<void>;

/**
* removes all data stored under a specified key
*/
delete(key: string): Promise<void>;

open(): Promise<void>;

close(): Promise<void>;

begin(): Promise<void>;

rollback(): Promise<void>;

commit(): Promise<void>;

/**
* used mostly for debugging, allows to dump the current content cache
* It's slow.
*/
dump(): Promise<any>; // eslint-disable-line @typescript-eslint/no-explicit-any

/**
* returns underlying storage (LevelDB, LMDB, sqlite...)
* - useful for performing low-level operations
*/
storage<S>(): S;

/**
* leaves n-latest (i.e. with latest (in lexicographic order) sort keys)
* entries for each cached key
*
* @param entriesStored - how many latest entries should be left
* for each cached key
*
* @retun PruneStats if getting them doesn't introduce a delay, null otherwise
*/
prune(entriesStored: number): Promise<PruneStats | null>;
}
75 changes: 9 additions & 66 deletions src/cache/SortKeyCache.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,26 @@
import { SortKeyCacheRangeOptions } from './SortKeyCacheRangeOptions';
import { BasicSortKeyCache } from './BasicSortKeyCache';

/**
* A cache that stores its values per dedicated key and sort key.
* A sort key is a value that the SmartWeave protocol is using
* to sort contract transactions ({@link LexicographicalInteractionsSorter}.
* Key-value cache storage.
* Just as {@link BasicSortKeyCache}, items are stored
* in lexicographical order using by sort key.
*
* All values should be stored in a lexicographical order (per key) -
* sorted by the sort key.
* In addition, this interface provide functionality related to
* fetching keys and values using range options. {@link SortKeyCacheRangeOptions}
*/
export interface SortKeyCache<V> {
getLessOrEqual(key: string, sortKey: string): Promise<SortKeyCacheResult<V> | null>;

/**
* returns value stored for a given key and last sortKey
*/
getLast(key: string): Promise<SortKeyCacheResult<V> | null>;

/**
* returns last cached sort key - takes all keys into account
*/
getLastSortKey(): Promise<string | null>;

/**
* returns value for the key and exact sortKey
*/
get(cacheKey: CacheKey): Promise<SortKeyCacheResult<V> | null>;

/**
* puts new value in cache under given {@link CacheKey.key} and {@link CacheKey.sortKey}.
*/
put(cacheKey: CacheKey, value: V): Promise<void>;

export interface SortKeyCache<V> extends BasicSortKeyCache<V> {
/**
* deletes value in cache under given {@link CacheKey.key} from {@link CacheKey.sortKey}.
* the value will be still available if fetched using a lower sortKey
*/
del(cacheKey: CacheKey): Promise<void>;

/**
* removes all data stored under a specified key
*/
delete(key: string): Promise<void>;

/**
* executes a list of stacked operations
*/
batch(opStack: BatchDBOp<V>[]);

open(): Promise<void>;

close(): Promise<void>;

begin(): Promise<void>;

rollback(): Promise<void>;

commit(): Promise<void>;

/**
* used mostly for debugging, allows to dump the current content cache
* It's slow.
*/
dump(): Promise<any>; // eslint-disable-line @typescript-eslint/no-explicit-any

/**
* Returns keys for a specified range
*/
Expand All @@ -72,23 +30,6 @@ export interface SortKeyCache<V> {
* Returns a key value map for a specified range
*/
kvMap(sortKey: string, options?: SortKeyCacheRangeOptions): Promise<Map<string, V>>;

/**
* returns underlying storage (LevelDB, LMDB, sqlite...)
* - useful for performing low-level operations
*/
storage<S>(): S;

/**
* leaves n-latest (i.e. with latest (in lexicographic order) sort keys)
* entries for each cached key
*
* @param entriesStored - how many latest entries should be left
* for each cached key
*
* @retun PruneStats if getting them doesn't introduce a delay, null otherwise
*/
prune(entriesStored: number): Promise<PruneStats | null>;
}

export interface PruneStats {
Expand All @@ -107,11 +48,13 @@ export class SortKeyCacheResult<V> {
}

export declare type BatchDBOp<V> = PutBatch<V> | DelBatch;

export interface PutBatch<V> {
type: 'put';
key: CacheKey;
value: V;
}

export interface DelBatch {
type: 'del';
key: string;
Expand Down
7 changes: 7 additions & 0 deletions src/cache/SortKeyCacheRangeOptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Range option for fetching items from kv storage {@link SortKeyCache}
* @param gte - greater than equals
* @param lt - less than
* @param reverse - reverses the order
* @param limit - limits output elements
*/
export interface SortKeyCacheRangeOptions {
gte?: string;
lt?: string;
Expand Down
7 changes: 4 additions & 3 deletions src/core/Warp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { DEFAULT_LEVEL_DB_LOCATION, WARP_GW_URL } from './WarpFactory';
import { LevelDbCache } from '../cache/impl/LevelDbCache';
import { SourceData } from '../contract/deploy/Source';
import { BundlerSigner, DataItem } from '../contract/deploy/DataItem';
import { BasicSortKeyCache } from '../cache/BasicSortKeyCache';

export type WarpEnvironment = 'local' | 'testnet' | 'mainnet' | 'custom';
export type KVStorageFactory = (contractTxId: string) => SortKeyCache<unknown>;
Expand Down Expand Up @@ -87,7 +88,7 @@ export class Warp {

static builder(
arweave: Arweave,
stateCache: SortKeyCache<EvalStateResult<unknown>>,
stateCache: BasicSortKeyCache<EvalStateResult<unknown>>,
environment: WarpEnvironment
): WarpBuilder {
return new WarpBuilder(arweave, stateCache, environment);
Expand Down Expand Up @@ -138,12 +139,12 @@ export class Warp {
return new PstContractImpl(contractTxId, this);
}

useStateCache(stateCache: SortKeyCache<EvalStateResult<unknown>>): Warp {
useStateCache(stateCache: BasicSortKeyCache<EvalStateResult<unknown>>): Warp {
this.stateEvaluator.setCache(stateCache);
return this;
}

useContractCache(definition: SortKeyCache<ContractDefinition<unknown>>, src: SortKeyCache<SrcCache>): Warp {
useContractCache(definition: BasicSortKeyCache<ContractDefinition<unknown>>, src: SortKeyCache<SrcCache>): Warp {
this.definitionLoader.setSrcCache(src);
this.definitionLoader.setCache(definition);
return this;
Expand Down
4 changes: 2 additions & 2 deletions src/core/WarpBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { InteractionsLoader } from './modules/InteractionsLoader';
import { StateEvaluator, EvalStateResult } from './modules/StateEvaluator';
import { WarpEnvironment, Warp } from './Warp';
import { CacheOptions, GatewayOptions } from './WarpFactory';
import { SortKeyCache } from '../cache/SortKeyCache';
import { LevelDbCache } from '../cache/impl/LevelDbCache';
import { ContractCache, SrcCache } from './ContractDefinition';
import { BasicSortKeyCache } from '../cache/BasicSortKeyCache';

export class WarpBuilder {
private _definitionLoader?: DefinitionLoader;
Expand All @@ -24,7 +24,7 @@ export class WarpBuilder {

constructor(
private readonly _arweave: Arweave,
private readonly _stateCache: SortKeyCache<EvalStateResult<unknown>>,
private readonly _stateCache: BasicSortKeyCache<EvalStateResult<unknown>>,
private readonly _environment: WarpEnvironment = 'custom'
) {}

Expand Down
10 changes: 5 additions & 5 deletions src/core/modules/DefinitionLoader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ContractCache, ContractDefinition, ContractSource, SrcCache } from '../../core/ContractDefinition';
import { GwTypeAware } from './InteractionsLoader';
import { SortKeyCache } from '../../cache/SortKeyCache';
import { WarpAware } from '../Warp';
import { BasicSortKeyCache } from '../../cache/BasicSortKeyCache';

/**
* Implementors of this interface are responsible for loading contract's definitions -
Expand All @@ -13,12 +13,12 @@ export interface DefinitionLoader extends GwTypeAware, WarpAware {

loadContractSource(srcTxId: string): Promise<ContractSource>;

setCache(cache: SortKeyCache<ContractCache<unknown>>): void;
setCache(cache: BasicSortKeyCache<ContractCache<unknown>>): void;

// Cache for storing common source code or binaries
setSrcCache(cacheSrc?: SortKeyCache<SrcCache>): void;
setSrcCache(cacheSrc?: BasicSortKeyCache<SrcCache>): void;

getCache(): SortKeyCache<ContractCache<unknown>>;
getCache(): BasicSortKeyCache<ContractCache<unknown>>;

getSrcCache(): SortKeyCache<SrcCache>;
getSrcCache(): BasicSortKeyCache<SrcCache>;
}
7 changes: 4 additions & 3 deletions src/core/modules/StateEvaluator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SortKeyCache, SortKeyCacheResult } from '../../cache/SortKeyCache';
import { SortKeyCacheResult } from '../../cache/SortKeyCache';
import { ExecutionContext } from '../ExecutionContext';
import { GQLNodeInterface } from '../../legacy/gqlResult';
import { SourceType } from './impl/WarpGatewayInteractionsLoader';
import { BasicSortKeyCache } from '../../cache/BasicSortKeyCache';

/**
* Implementors of this class are responsible for evaluating contract's state
Expand Down Expand Up @@ -87,9 +88,9 @@ export interface StateEvaluator {

lastCachedSortKey(): Promise<string | null>;

setCache(cache: SortKeyCache<EvalStateResult<unknown>>): void;
setCache(cache: BasicSortKeyCache<EvalStateResult<unknown>>): void;

getCache(): SortKeyCache<EvalStateResult<unknown>>;
getCache(): BasicSortKeyCache<EvalStateResult<unknown>>;
}

export class EvalStateResult<State> {
Expand Down
9 changes: 5 additions & 4 deletions src/core/modules/impl/CacheableStateEvaluator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Arweave from 'arweave';
import { CacheKey, SortKeyCache, SortKeyCacheResult } from '../../../cache/SortKeyCache';
import { CacheKey, SortKeyCacheResult } from '../../../cache/SortKeyCache';
import { ExecutionContext } from '../../../core/ExecutionContext';
import { ExecutionContextModifier } from '../../../core/ExecutionContextModifier';
import { GQLNodeInterface } from '../../../legacy/gqlResult';
Expand All @@ -9,6 +9,7 @@ import { EvalStateResult } from '../StateEvaluator';
import { DefaultStateEvaluator } from './DefaultStateEvaluator';
import { HandlerApi } from './HandlerExecutorFactory';
import { genesisSortKey } from './LexicographicalInteractionsSorter';
import { BasicSortKeyCache } from '../../../cache/BasicSortKeyCache';

/**
* An implementation of DefaultStateEvaluator that adds caching capabilities.
Expand All @@ -23,7 +24,7 @@ export class CacheableStateEvaluator extends DefaultStateEvaluator {

constructor(
arweave: Arweave,
private cache: SortKeyCache<EvalStateResult<unknown>>,
private cache: BasicSortKeyCache<EvalStateResult<unknown>>,
executionContextModifiers: ExecutionContextModifier[] = []
) {
super(arweave, executionContextModifiers);
Expand Down Expand Up @@ -224,11 +225,11 @@ export class CacheableStateEvaluator extends DefaultStateEvaluator {
return await this.cache.getLastSortKey();
}

setCache(cache: SortKeyCache<EvalStateResult<unknown>>): void {
setCache(cache: BasicSortKeyCache<EvalStateResult<unknown>>): void {
this.cache = cache;
}

getCache(): SortKeyCache<EvalStateResult<unknown>> {
getCache(): BasicSortKeyCache<EvalStateResult<unknown>> {
return this.cache;
}
}
10 changes: 5 additions & 5 deletions src/core/modules/impl/ContractDefinitionLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { GW_TYPE } from '../InteractionsLoader';
import { TagsParser } from './TagsParser';
import { WasmSrc } from './wasm/WasmSrc';
import { Warp, WarpEnvironment } from '../../Warp';
import { SortKeyCache } from '../../../cache/SortKeyCache';
import { Transaction } from '../../../utils/types/arweave-types';
import { BasicSortKeyCache } from '../../../cache/BasicSortKeyCache';

export class ContractDefinitionLoader implements DefinitionLoader {
private readonly logger = LoggerFactory.INST.create('ContractDefinitionLoader');
Expand Down Expand Up @@ -147,20 +147,20 @@ export class ContractDefinitionLoader implements DefinitionLoader {
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
setCache(cache: SortKeyCache<ContractDefinition<unknown>>): void {
setCache(cache: BasicSortKeyCache<ContractDefinition<unknown>>): void {
throw new Error('No cache implemented for this loader');
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
setSrcCache(cache: SortKeyCache<SrcCache>): void {
setSrcCache(cache: BasicSortKeyCache<SrcCache>): void {
throw new Error('No cache implemented for this loader');
}

getCache(): SortKeyCache<ContractCache<unknown>> {
getCache(): BasicSortKeyCache<ContractCache<unknown>> {
throw new Error('No cache implemented for this loader');
}

getSrcCache(): SortKeyCache<SrcCache> {
getSrcCache(): BasicSortKeyCache<SrcCache> {
throw new Error('No cache implemented for this loader');
}

Expand Down
7 changes: 4 additions & 3 deletions src/core/modules/impl/DefaultStateEvaluator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Arweave from 'arweave';

import { SortKeyCache, SortKeyCacheResult } from '../../../cache/SortKeyCache';
import { SortKeyCacheResult } from '../../../cache/SortKeyCache';
import { InteractionCall } from '../../ContractCallRecord';
import { ExecutionContext } from '../../../core/ExecutionContext';
import { ExecutionContextModifier } from '../../../core/ExecutionContextModifier';
Expand All @@ -12,6 +12,7 @@ import { EvalStateResult, StateEvaluator } from '../StateEvaluator';
import { ContractInteraction, HandlerApi, InteractionResult } from './HandlerExecutorFactory';
import { TagsParser } from './TagsParser';
import { VrfPluginFunctions } from '../../WarpPlugin';
import { BasicSortKeyCache } from '../../../cache/BasicSortKeyCache';

type EvaluationProgressInput = {
contractTxId: string;
Expand Down Expand Up @@ -396,9 +397,9 @@ export abstract class DefaultStateEvaluator implements StateEvaluator {

abstract lastCachedSortKey(): Promise<string | null>;

abstract setCache(cache: SortKeyCache<EvalStateResult<unknown>>): void;
abstract setCache(cache: BasicSortKeyCache<EvalStateResult<unknown>>): void;

abstract getCache(): SortKeyCache<EvalStateResult<unknown>>;
abstract getCache(): BasicSortKeyCache<EvalStateResult<unknown>>;
}

function canBeCached(tx: GQLNodeInterface): boolean {
Expand Down
Loading

0 comments on commit 051e80d

Please sign in to comment.