From f966e89616f656e37c8c131af1371ae61ec4580f Mon Sep 17 00:00:00 2001 From: ppedziwiatr Date: Thu, 26 Aug 2021 21:11:07 +0200 Subject: [PATCH] refactor: final touches for the new client api --- _scripts/state-comparator.ts | 2 +- package.json | 3 ++- src/contract/HandlerBasedContract.ts | 15 ++++++++++++++- src/contract/node/SmartWeaveNodeFactory.ts | 2 +- src/contract/web/SmartWeaveWebFactory.ts | 5 +++-- src/core/impl/DefaultStateEvaluator.ts | 8 ++++---- src/core/impl/HandlerExecutorFactory.ts | 19 +++++++++++++------ src/logging/node/TsLogFactory.ts | 4 ++-- 8 files changed, 40 insertions(+), 18 deletions(-) diff --git a/_scripts/state-comparator.ts b/_scripts/state-comparator.ts index d9fe887..c2815b6 100644 --- a/_scripts/state-comparator.ts +++ b/_scripts/state-comparator.ts @@ -47,7 +47,7 @@ async function main() { const txs = loadTxFromFile(); const resumeFromContractTxId = 'YLVpmhSq5JmLltfg6R-5fL04rIRPrlSU22f6RQ6VyYE'; - let resumeFrom = true; + let resumeFrom = false; logger.info(`Checking ${txs.length} contracts`); diff --git a/package.json b/package.json index 5849168..46a0ad7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "redstone-smartweave", - "version": "0.1.0-alpha.1", + "version": "0.2.0-alpha.1", "description": "An implementation of the SmartWeave SDK.", "main": "lib/index.js", "types": "lib/index.d.ts", @@ -15,6 +15,7 @@ "preversion": "yarn lint && yarn build", "version": "yarn format && git add -A src", "postversion": "git push && git push --tags", + "yalc:publish": "yarn build && yalc publish --push", "test": "jest" }, "license": "MIT", diff --git a/src/contract/HandlerBasedContract.ts b/src/contract/HandlerBasedContract.ts index b3f81a1..0000f0e 100644 --- a/src/contract/HandlerBasedContract.ts +++ b/src/contract/HandlerBasedContract.ts @@ -80,17 +80,21 @@ export class HandlerBasedContract implements Contract { const evalStateResult = await stateEvaluator.eval(executionContext, []); + logger.debug('Creating new intraction for view state'); + const interaction: ContractInteraction = { input, caller: executionContext.caller }; + logger.trace('interaction', interaction); + const handler = (await this.smartweave.executorFactory.create( executionContext.contractDefinition )) as HandlerApi; // TODO: what is the best way to create a transaction in this case? - return await handler.handle( + const handleResult = await handler.handle( executionContext, evalStateResult.state, interaction, @@ -107,6 +111,15 @@ export class HandlerBasedContract implements Contract { }, [] ); + + if (handleResult.type !== 'ok') { + logger.fatal('Error while interacting with contract', { + type: handleResult.type, + error: handleResult.errorMessage + }); + } + + return handleResult; } async viewStateForTx( diff --git a/src/contract/node/SmartWeaveNodeFactory.ts b/src/contract/node/SmartWeaveNodeFactory.ts index edeb791..ade9320 100644 --- a/src/contract/node/SmartWeaveNodeFactory.ts +++ b/src/contract/node/SmartWeaveNodeFactory.ts @@ -23,7 +23,7 @@ export class SmartWeaveNodeFactory extends SmartWeaveWebFactory { * Returns a {@link SmartWeave} that is using file-based cache for {@link StateEvaluator} layer * and mem cache for the rest. */ - static fileCacheClient(arweave: Arweave, cacheBasePath?: string): SmartWeave { + static fileCached(arweave: Arweave, cacheBasePath?: string): SmartWeave { const definitionLoader = new ContractDefinitionLoader(arweave, new MemCache()); const interactionsLoader = new CacheableContractInteractionsLoader( diff --git a/src/contract/web/SmartWeaveWebFactory.ts b/src/contract/web/SmartWeaveWebFactory.ts index 0f76e78..864dc4b 100644 --- a/src/contract/web/SmartWeaveWebFactory.ts +++ b/src/contract/web/SmartWeaveWebFactory.ts @@ -20,6 +20,7 @@ import { BsonFileBlockHeightSwCache, MemBlockHeightSwCache, MemCache } from '@sm /** * A factory that simplifies the process of creating different versions of {@link SmartWeave}. * All versions use the {@link Evolve} plugin. + * SmartWeave instances created by this factory can be safely used in a web environment. */ export class SmartWeaveWebFactory { /** @@ -52,9 +53,9 @@ export class SmartWeaveWebFactory { /** * Returns a {@link SmartWeave} that (yup, you've guessed it!) does not use any caches. - * This one is gonna be slooow... + * This one is gonna be slooow! */ - static noCacheClient(arweave: Arweave): SmartWeave { + static nonCached(arweave: Arweave): SmartWeave { const definitionLoader = new ContractDefinitionLoader(arweave); const interactionsLoader = new ContractInteractionsLoader(arweave); const executorFactory = new HandlerExecutorFactory(arweave); diff --git a/src/core/impl/DefaultStateEvaluator.ts b/src/core/impl/DefaultStateEvaluator.ts index d1160f6..58141b4 100644 --- a/src/core/impl/DefaultStateEvaluator.ts +++ b/src/core/impl/DefaultStateEvaluator.ts @@ -59,14 +59,14 @@ export class DefaultStateEvaluator implements StateEvaluator { executionContext.contractDefinition )) as HandlerApi; - logger.debug( + logger.trace( 'missingInteractions', missingInteractions.map((int) => { return int.node.id; }) ); - logger.debug('Init state', JSON.stringify(baseState.state)); + logger.trace('Init state', JSON.stringify(baseState.state)); for (const missingInteraction of missingInteractions) { logger.debug( @@ -125,11 +125,11 @@ export class DefaultStateEvaluator implements StateEvaluator { private logResult(result: InteractionResult, currentTx: GQLNodeInterface) { if (result.type === 'exception') { - logger.error(`${result.result}`); + logger.error(`${result.errorMessage}`); logger.error(`Executing of interaction: ${currentTx.id} threw exception.`); } if (result.type === 'error') { - logger.error(`${result.result}`); + logger.error(`${result.errorMessage}`); logger.error(`Executing of interaction: ${currentTx.id} returned error.`); } } diff --git a/src/core/impl/HandlerExecutorFactory.ts b/src/core/impl/HandlerExecutorFactory.ts index 5d1f42d..06fa6bb 100644 --- a/src/core/impl/HandlerExecutorFactory.ts +++ b/src/core/impl/HandlerExecutorFactory.ts @@ -58,7 +58,7 @@ export class HandlerExecutorFactory implements ExecutorFactory; const stateCopy = JSON.parse(JSON.stringify(state)); swGlobal._activeTx = interactionTx; - logger.debug(`SmartWeave.contract.id: ${swGlobal.contract.id}`, swGlobal.contract.id); + logger.debug(`SmartWeave.contract.id:`, swGlobal.contract.id); self.assignReadContractState(swGlobal, contractDefinition, executionContext, currentTx); self.assignViewContractState(swGlobal, contractDefinition, executionContext); @@ -80,14 +80,20 @@ export class HandlerExecutorFactory implements ExecutorFactory = ( // TODO: change to XOR between result and state? export type HandlerResult = { - result: string | Result; // this really sucks, but has to be declared this way to be backwards compatbile + result: Result; state: State; }; export type InteractionResult = HandlerResult & { type: 'ok' | 'error' | 'exception'; + errorMessage?: string; }; export type ContractInteraction = { diff --git a/src/logging/node/TsLogFactory.ts b/src/logging/node/TsLogFactory.ts index e9163a6..762bd6c 100644 --- a/src/logging/node/TsLogFactory.ts +++ b/src/logging/node/TsLogFactory.ts @@ -4,8 +4,8 @@ import { LogLevel, RedStoneLogger } from '../RedStoneLogger'; export const defaultLoggerOptions: ISettingsParam = { displayFunctionName: false, - displayFilePath: 'hideNodeModulesOnly', - displayLoggerName: false, + displayFilePath: 'hidden', + displayLoggerName: true, dateTimeTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone, minLevel: 'debug' };