Skip to content

Commit

Permalink
caching contracts loading from arweave #483
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeuchi authored and ppedziwiatr committed Jan 18, 2024
1 parent 2ea27dd commit 0550773
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 191 deletions.
9 changes: 3 additions & 6 deletions src/__tests__/integration/basic/contract-loading.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { LoggerFactory } from '../../../logging/LoggerFactory';
import { WarpGatewayContractDefinitionLoader } from '../../../core/modules/impl/WarpGatewayContractDefinitionLoader';
import { DefaultEvaluationOptions } from '../../../core/modules/StateEvaluator';
import { LexicographicalInteractionsSorter } from '../../../core/modules/impl/LexicographicalInteractionsSorter';
import { LevelDbCache } from '../../../cache/impl/LevelDbCache';
import { DeployPlugin } from 'warp-contracts-plugin-deploy';
import { CacheableContractDefinitionLoader } from "../../../core/modules/impl/CacheableContractDefinitionLoader";

interface ExampleContractState {
counter: number;
Expand All @@ -22,7 +22,7 @@ interface ExampleContractState {
describe('Testing WarpGatewayContractDefinitionLoader', () => {
let contractSrc: string;
let wallet: JWKInterface;
let loader: WarpGatewayContractDefinitionLoader;
let loader: CacheableContractDefinitionLoader;

const evalOptions = new DefaultEvaluationOptions();
let sorter: LexicographicalInteractionsSorter;
Expand All @@ -44,10 +44,7 @@ describe('Testing WarpGatewayContractDefinitionLoader', () => {

const { arweave } = warp;

const contractCache = new LevelDbCache<any>({ ...defaultCacheOptions, inMemory: true });
const srcCache = new LevelDbCache<any>({ ...defaultCacheOptions, inMemory: true });

loader = new WarpGatewayContractDefinitionLoader(arweave, contractCache, srcCache, 'local');
loader = new CacheableContractDefinitionLoader(new WarpGatewayContractDefinitionLoader(arweave, 'local'), 'local', { ...defaultCacheOptions, inMemory: true });
loader.warp = warp;
sorter = new LexicographicalInteractionsSorter(arweave);

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/integration/basic/vrf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Testing the Profit Sharing Token', () => {
},
'local'
)
.useArweaveGateway()
.useArweaveGateway(defaultCacheOptions)
.setInteractionsLoader(loader)
.build()
.use(new DeployPlugin())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { ArweaveGatewayBundledContractDefinitionLoader } from '../../core/module
import { ArweaveGatewayBundledInteractionLoader } from '../../core/modules/impl/ArweaveGatewayBundledInteractionLoader';
import { SourceType, WarpGatewayInteractionsLoader } from '../../core/modules/impl/WarpGatewayInteractionsLoader';
import { EvaluationOptions } from '../../core/modules/StateEvaluator';
import { WarpFactory } from '../../core/WarpFactory';
import { defaultCacheOptions, WarpFactory } from "../../core/WarpFactory";
import { LoggerFactory } from '../../logging/LoggerFactory';
import { WarpGatewayContractDefinitionLoader } from '../../core/modules/impl/WarpGatewayContractDefinitionLoader';
import { LevelDbCache } from '../../cache/impl/LevelDbCache';
import { ContractCache, SrcCache } from '../../core/ContractDefinition';
import stringify from 'safe-stable-stringify';
import Arweave from 'arweave/node/common';
import { CacheableContractDefinitionLoader } from "../../core/modules/impl/CacheableContractDefinitionLoader";

const EXAMPLE_CONTRACT_TX_ID = 'T8Fakv0Sol6ALQ4Mt6FTxEJVDJWT-HDUmcI3qIA49U4';
const EXAMPLE_CONTRACT_SRC_TX_ID = 'QEIweYIpdMSer_E33VreYzmuTIx33FQ4Sq32XJqlLQw';
Expand All @@ -33,23 +32,7 @@ describe('Arweave Gateway interaction loader', () => {
it('should load contract definition', async () => {
const warp = WarpFactory.forMainnet();

const contractsCache = new LevelDbCache<ContractCache<unknown>>({
inMemory: true,
dbLocation: ''
});

// Separate cache for sources to minimize duplicates
const sourceCache = new LevelDbCache<SrcCache>({
inMemory: true,
dbLocation: ''
});

const wrLoader = new WarpGatewayContractDefinitionLoader(
warp.arweave,
contractsCache,
sourceCache,
warp.environment
);
const wrLoader = new CacheableContractDefinitionLoader(new WarpGatewayContractDefinitionLoader(warp.arweave, 'local'), 'local', { ...defaultCacheOptions, inMemory: true })
wrLoader.warp = warp;

const arLoader = new ArweaveGatewayBundledContractDefinitionLoader(warp.environment);
Expand Down Expand Up @@ -100,21 +83,10 @@ describe('Arweave Gateway interaction loader', () => {
});

it('warp interaction loader and arweave interaction loader evaluates to same state', async () => {
const contractsCache = new LevelDbCache<ContractCache<unknown>>({
inMemory: true,
dbLocation: ''
});

// Separate cache for sources to minimize duplicates
const sourceCache = new LevelDbCache<SrcCache>({
inMemory: true,
dbLocation: ''
});

const arweave = Arweave.init({ host: 'arweave.net', port: 443, protocol: 'https' });

const arLoader = new ArweaveGatewayBundledInteractionLoader(arweave, 'mainnet');
const wrLoader = new WarpGatewayContractDefinitionLoader(arweave, contractsCache, sourceCache, 'mainnet');
const wrLoader = new CacheableContractDefinitionLoader(new WarpGatewayContractDefinitionLoader(arweave, 'local'), 'local', { ...defaultCacheOptions, inMemory: true });
const withArLoader = WarpFactory.custom(arweave, { inMemory: true, dbLocation: '' }, 'mainnet')
.setInteractionsLoader(arLoader)
.setDefinitionLoader(wrLoader)
Expand Down
4 changes: 2 additions & 2 deletions src/core/Warp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { HandlerBasedContract } from '../contract/HandlerBasedContract';
import { PstContract } from '../contract/PstContract';
import { PstContractImpl } from '../contract/PstContractImpl';
import { Testing, Wallet } from '../contract/testing/Testing';
import { DefinitionLoader } from './modules/DefinitionLoader';
import { CacheableDefinitionLoader } from './modules/DefinitionLoader';
import { ExecutorFactory } from './modules/ExecutorFactory';
import { HandlerApi } from './modules/impl/HandlerExecutorFactory';
import { InteractionsLoader } from './modules/InteractionsLoader';
Expand Down Expand Up @@ -72,7 +72,7 @@ export class Warp {

constructor(
readonly arweave: Arweave,
readonly definitionLoader: DefinitionLoader,
readonly definitionLoader: CacheableDefinitionLoader,
readonly interactionsLoader: InteractionsLoader,
readonly executorFactory: ExecutorFactory<HandlerApi<unknown>>,
readonly stateEvaluator: StateEvaluator,
Expand Down
40 changes: 16 additions & 24 deletions src/core/WarpBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import Arweave from 'arweave';
import { DebuggableExecutorFactory } from '../plugins/DebuggableExecutorFactor';
import { DefinitionLoader } from './modules/DefinitionLoader';
import { CacheableDefinitionLoader } from './modules/DefinitionLoader';
import { ExecutorFactory } from './modules/ExecutorFactory';
import { ArweaveGatewayInteractionsLoader } from './modules/impl/ArweaveGatewayInteractionsLoader';
import { CacheableInteractionsLoader } from './modules/impl/CacheableInteractionsLoader';
import { ContractDefinitionLoader } from './modules/impl/ContractDefinitionLoader';
import { ArweaveContractDefinitionLoader } from './modules/impl/ArweaveContractDefinitionLoader';
import { HandlerApi } from './modules/impl/HandlerExecutorFactory';
import { WarpGatewayContractDefinitionLoader } from './modules/impl/WarpGatewayContractDefinitionLoader';
import { WarpGatewayInteractionsLoader } from './modules/impl/WarpGatewayInteractionsLoader';
import { InteractionsLoader } from './modules/InteractionsLoader';
import { StateEvaluator, EvalStateResult } from './modules/StateEvaluator';
import { WarpEnvironment, Warp } from './Warp';
import { CacheOptions, GatewayOptions } from './WarpFactory';
import { LevelDbCache } from '../cache/impl/LevelDbCache';
import { ContractCache, SrcCache } from './ContractDefinition';
import { BasicSortKeyCache } from '../cache/BasicSortKeyCache';
import { CacheableContractDefinitionLoader } from './modules/impl/CacheableContractDefinitionLoader';

export class WarpBuilder {
private _definitionLoader?: DefinitionLoader;
private _definitionLoader?: CacheableDefinitionLoader;
private _interactionsLoader?: InteractionsLoader;
private _executorFactory?: ExecutorFactory<HandlerApi<unknown>>;
private _stateEvaluator?: StateEvaluator;
Expand All @@ -28,7 +27,7 @@ export class WarpBuilder {
private readonly _environment: WarpEnvironment = 'custom'
) {}

public setDefinitionLoader(value: DefinitionLoader): WarpBuilder {
public setDefinitionLoader(value: CacheableDefinitionLoader): WarpBuilder {
this._definitionLoader = value;
return this;
}
Expand Down Expand Up @@ -61,31 +60,24 @@ export class WarpBuilder {
new WarpGatewayInteractionsLoader(gatewayOptions.confirmationStatus, gatewayOptions.source)
);

const contractsCache = new LevelDbCache<ContractCache<unknown>>({
...cacheOptions,
dbLocation: `${cacheOptions.dbLocation}/contracts`
});

// Separate cache for sources to minimize duplicates
const sourceCache = new LevelDbCache<SrcCache>({
...cacheOptions,
dbLocation: `${cacheOptions.dbLocation}/source`
});

this._definitionLoader = new WarpGatewayContractDefinitionLoader(
this._arweave,
contractsCache,
sourceCache,
this._environment
this._definitionLoader = new CacheableContractDefinitionLoader(
new WarpGatewayContractDefinitionLoader(this._arweave, this._environment),
this._environment,
cacheOptions
);
return this;
}

public useArweaveGateway(): WarpBuilder {
this._definitionLoader = new ContractDefinitionLoader(this._arweave, this._environment);
public useArweaveGateway(cacheOptions: CacheOptions): WarpBuilder {
this._interactionsLoader = new CacheableInteractionsLoader(
new ArweaveGatewayInteractionsLoader(this._arweave, this._environment)
);

this._definitionLoader = new CacheableContractDefinitionLoader(
new ArweaveContractDefinitionLoader(this._arweave, this._environment),
this._environment,
cacheOptions
);
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/WarpFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class WarpFactory {
cacheOptions: CacheOptions = defaultCacheOptions,
environment: WarpEnvironment
): Warp {
return this.custom(arweave, cacheOptions, environment).useArweaveGateway().build();
return this.custom(arweave, cacheOptions, environment).useArweaveGateway(cacheOptions).build();
}

private static customWarpGw(
Expand Down
2 changes: 2 additions & 0 deletions src/core/modules/DefinitionLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export interface DefinitionLoader extends GwTypeAware, WarpAware {
load<State>(contractTxId: string, evolvedSrcTxId?: string): Promise<ContractDefinition<State>>;

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

export interface CacheableDefinitionLoader extends DefinitionLoader {
setCache(cache: BasicSortKeyCache<ContractCache<unknown>>): void;

// Cache for storing common source code or binaries
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import Arweave from 'arweave';
import { ContractType } from '../../../contract/deploy/CreateContract';
import {
ContractDefinition,
ContractSource,
ContractCache,
SrcCache,
SUPPORTED_SRC_CONTENT_TYPES
} from '../../../core/ContractDefinition';
import { ContractDefinition, ContractSource, SUPPORTED_SRC_CONTENT_TYPES } from '../../../core/ContractDefinition';
import { SMART_WEAVE_TAGS, WARP_TAGS } from '../../KnownTags';
import { Benchmark } from '../../../logging/Benchmark';
import { LoggerFactory } from '../../../logging/LoggerFactory';
Expand All @@ -17,11 +11,9 @@ import { TagsParser } from './TagsParser';
import { WasmSrc } from './wasm/WasmSrc';
import { Warp, WarpEnvironment } from '../../Warp';
import { Transaction } from '../../../utils/types/arweave-types';
import { BasicSortKeyCache } from '../../../cache/BasicSortKeyCache';

export class ContractDefinitionLoader implements DefinitionLoader {
export class ArweaveContractDefinitionLoader implements DefinitionLoader {
private readonly logger = LoggerFactory.INST.create('ContractDefinitionLoader');

protected arweaveWrapper: ArweaveWrapper;
private readonly tagsParser: TagsParser;

Expand Down Expand Up @@ -146,24 +138,6 @@ export class ContractDefinitionLoader implements DefinitionLoader {
return 'arweave';
}

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

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

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

set warp(warp: Warp) {
this.arweaveWrapper = new ArweaveWrapper(warp);
}
Expand Down
Loading

0 comments on commit 0550773

Please sign in to comment.