bug: updating cache for onContractCall hook does not work properly #53

This commit is contained in:
ppedziwiatr
2022-03-05 15:34:48 +01:00
committed by Piotr Pędziwiatr
parent 24a08e4dc6
commit 7562cf9fde
43 changed files with 777 additions and 1889815 deletions

View File

@@ -1,7 +1,8 @@
import Arweave from 'arweave';
import { ContractDefinition, ExecutorFactory } from '@smartweave/core';
import { SwCache } from '@smartweave/cache';
import { LoggerFactory } from '@smartweave/logging';
import {ContractDefinition, ExecutorFactory, HandlerApi} from '@smartweave/core';
import {SwCache} from '@smartweave/cache';
import {LoggerFactory} from '@smartweave/logging';
import {SmartWeaveGlobal} from '@smartweave/legacy';
/**
* An implementation of ExecutorFactory that adds caching capabilities
@@ -10,12 +11,15 @@ export class CacheableExecutorFactory<Api> implements ExecutorFactory<Api> {
private readonly logger = LoggerFactory.INST.create('CacheableExecutorFactory');
constructor(
arweave: Arweave,
private readonly arweave: Arweave,
private readonly baseImplementation: ExecutorFactory<Api>,
private readonly cache: SwCache<string, Api>
) {}
) {
}
async create<State>(contractDefinition: ContractDefinition<State>): Promise<Api> {
return await this.baseImplementation.create(contractDefinition);
// warn: do not cache on the contractDefinition.srcTxId. This might look like a good optimisation
// (as many contracts share the same source code), but unfortunately this is causing issues
// with the same SwGlobal object being cached for all contracts with the same source code
@@ -23,13 +27,10 @@ export class CacheableExecutorFactory<Api> implements ExecutorFactory<Api> {
// that share the same source).
// warn#2: cache key MUST be a combination of both txId and srcTxId -
// as "evolve" feature changes the srcTxId for the given txId...
const cacheKey = `${contractDefinition.txId}_${contractDefinition.srcTxId}`;
if (!this.cache.contains(cacheKey)) {
this.logger.debug('Updating executor factory cache');
const handler = await this.baseImplementation.create(contractDefinition);
this.cache.put(cacheKey, handler);
}
return this.cache.get(cacheKey);
// switching off caching for now
// - https://github.com/redstone-finance/redstone-smartcontracts/issues/53
// probably should be cached on a lower lower - i.e. either handler function (for js contracts)
// or wasm instance.
}
}