diff --git a/README.md b/README.md index 07431fc..14a1611 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,9 @@ To further improve contract state evaluation time, one can additionally use AWS - [Installation and import](#installation-and-import) - [Examples](#examples) - [Source code structure](#source-code-structure) + - [core package](#core-package) - [contract package](#contract-package) - [cache package](#cache-package) - - [core package](#core-package) - [plugins package](#plugins-package) - [legacy package](#legacy-package) - [logger package](#logger-package) @@ -51,22 +51,9 @@ Please follow instructions in its README.md (and detail-ish comments in the exam ### Source code structure SDK's source code is divided into few main modules. -#### Contract package -Code located in the `contract` package contains base contract interface - `Contract` and its -"reference" implementation - `HandlerBasedContract` - that allows to interact with contracts. -To connect to a contract, first an instance of the `SmartWeave` must be created. -This package contains `SmartWeave` factories that supply some most common configurations (e.g. cached or non-cached). -Refer the TSDocs for more information. - -#### Cache package -Code located in the `cache` package contains base interfaces - `SwCache` and `BlockHeightSwCache` -and some example implementations. These caches can be used while configuring `SmartWeave` -instance - to greatly improve processing speed (i.e. contract's state evaluation) . -Refer the TSDocs for more information. - #### Core package Code located in the `core` package contains all the main modules of the reference SDK v2 implementation. -These modules are used to create instances of `SmartWeave`. +These modules are used to create instances of `SmartWeave` - main class that allows to connect to contracts. There are currently 5 core interfaces: 1. `DefinitionLoader` - it is responsible for loading contract's definition (i.e. its source code, initial state, etc.) Its reference implementation is `ContractDefinitionLoader`. @@ -87,6 +74,18 @@ Additionally, the core package contains the definition of all the tags used by t All interfaces and implementations are further described in TSDocs. +#### Contract package +Code located in the `contract` package contains base contract interface - `Contract` and its +"reference" implementation - `HandlerBasedContract` - that allows to interact with contracts. +To connect to a contract, first an instance of the `SmartWeave` must be created. +Refer the TSDocs for more information. + +#### Cache package +Code located in the `cache` package contains base interfaces - `SwCache` and `BlockHeightSwCache` +and some example implementations. These caches can be used while configuring `SmartWeave` +instance - to greatly improve processing speed (i.e. contract's state evaluation) . +Refer the TSDocs for more information. + #### Plugins package This package contains some example extensions to base implementation, adding features like "Evolve", caching capabilities to `InteractionsLoader`, `ExecutorFactory` and `StateEvaluator`, etc. diff --git a/_scripts/single-state-comparator.ts b/_scripts/single-state-comparator.ts index fdc3b61..2d391aa 100644 --- a/_scripts/single-state-comparator.ts +++ b/_scripts/single-state-comparator.ts @@ -16,7 +16,7 @@ async function main() { const logger = LoggerFactory.INST.create(__filename); const smartWeave = SmartWeaveNodeFactory.memCached(arweave); - const contractTxId = 'SJ3l7474UHh3Dw6dWVT1bzsJ-8JvOewtGoDdOecWIZo'; + const contractTxId = 'YLVpmhSq5JmLltfg6R-5fL04rIRPrlSU22f6RQ6VyYE'; // Kyve: // C_1uo08qRuQAeDi9Y1I8fkaWYUC9IWkOrKDNe9EphJo // OFD4GqQcqp-Y_Iqh8DN_0s3a_68oMvvnekeOEu_a45I diff --git a/src/contract/index.ts b/src/contract/index.ts index ee9a6d4..1990682 100644 --- a/src/contract/index.ts +++ b/src/contract/index.ts @@ -1,6 +1,6 @@ export * from './Contract'; export * from './HandlerBasedContract'; -export * from './web/SmartWeaveWebFactory'; -export * from './node/SmartWeaveNodeFactory'; -export * from './SmartWeave'; -export * from './SmartWeaveBuilder'; +export * from '../core/web/SmartWeaveWebFactory'; +export * from '../core/node/SmartWeaveNodeFactory'; +export * from '../core/SmartWeave'; +export * from '../core/SmartWeaveBuilder'; diff --git a/src/core/ContractCreate.ts b/src/core/ContractCreate.ts deleted file mode 100644 index d1d8d21..0000000 --- a/src/core/ContractCreate.ts +++ /dev/null @@ -1,2 +0,0 @@ -// TODO ;-) -// ...though there isn't that much to change/fix from the current version. diff --git a/src/contract/SmartWeave.ts b/src/core/SmartWeave.ts similarity index 95% rename from src/contract/SmartWeave.ts rename to src/core/SmartWeave.ts index e0c0151..96cc6e4 100644 --- a/src/contract/SmartWeave.ts +++ b/src/core/SmartWeave.ts @@ -23,7 +23,7 @@ export class SmartWeave { readonly definitionLoader: DefinitionLoader, readonly interactionsLoader: InteractionsLoader, readonly interactionsSorter: InteractionsSorter, - readonly executorFactory: ExecutorFactory>, // TODO: really struggling with TS generics here... + readonly executorFactory: ExecutorFactory>, readonly stateEvaluator: StateEvaluator ) {} diff --git a/src/contract/SmartWeaveBuilder.ts b/src/core/SmartWeaveBuilder.ts similarity index 81% rename from src/contract/SmartWeaveBuilder.ts rename to src/core/SmartWeaveBuilder.ts index e34852e..125eec1 100644 --- a/src/contract/SmartWeaveBuilder.ts +++ b/src/core/SmartWeaveBuilder.ts @@ -1,5 +1,6 @@ import Arweave from 'arweave'; import { + DebuggableExecutorFactory, DefinitionLoader, ExecutorFactory, HandlerApi, @@ -43,6 +44,14 @@ export class SmartWeaveBuilder { return this; } + public overwriteSource(sourceCode: { [key: string]: string }): SmartWeave { + if (this._executorFactory == null) { + throw new Error('Set base ExecutorFactory first'); + } + this._executorFactory = new DebuggableExecutorFactory(this._executorFactory, sourceCode); + return this.build(); + } + build() { return new SmartWeave( this._arweave, diff --git a/src/core/index.ts b/src/core/index.ts index 76e5d38..b8cc10b 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -1,16 +1,16 @@ -export * from './DefinitionLoader'; +export * from './modules/DefinitionLoader'; export * from './ExecutionContextModifier'; -export * from './ExecutorFactory'; -export * from './InteractionsLoader'; -export * from './InteractionsSorter'; -export * from './StateEvaluator'; +export * from './modules/ExecutorFactory'; +export * from './modules/InteractionsLoader'; +export * from './modules/InteractionsSorter'; +export * from './modules/StateEvaluator'; export * from './SmartWeaveTags'; export * from './ExecutionContext'; export * from './ContractDefinition'; -export * from './impl/BlockHeightInteractionsSorter'; -export * from './impl/ContractDefinitionLoader'; -export * from './impl/ContractInteractionsLoader'; -export * from './impl/DefaultStateEvaluator'; -export * from './impl/HandlerExecutorFactory'; -export * from './impl/LexicographicalInteractionsSorter'; +export * from './modules/impl/BlockHeightInteractionsSorter'; +export * from './modules/impl/ContractDefinitionLoader'; +export * from './modules/impl/ContractInteractionsLoader'; +export * from './modules/impl/DefaultStateEvaluator'; +export * from './modules/impl/HandlerExecutorFactory'; +export * from './modules/impl/LexicographicalInteractionsSorter'; diff --git a/src/core/DefinitionLoader.ts b/src/core/modules/DefinitionLoader.ts similarity index 100% rename from src/core/DefinitionLoader.ts rename to src/core/modules/DefinitionLoader.ts diff --git a/src/core/ExecutorFactory.ts b/src/core/modules/ExecutorFactory.ts similarity index 100% rename from src/core/ExecutorFactory.ts rename to src/core/modules/ExecutorFactory.ts diff --git a/src/core/InteractionsLoader.ts b/src/core/modules/InteractionsLoader.ts similarity index 100% rename from src/core/InteractionsLoader.ts rename to src/core/modules/InteractionsLoader.ts diff --git a/src/core/InteractionsSorter.ts b/src/core/modules/InteractionsSorter.ts similarity index 100% rename from src/core/InteractionsSorter.ts rename to src/core/modules/InteractionsSorter.ts diff --git a/src/core/StateEvaluator.ts b/src/core/modules/StateEvaluator.ts similarity index 100% rename from src/core/StateEvaluator.ts rename to src/core/modules/StateEvaluator.ts diff --git a/src/core/impl/BlockHeightInteractionsSorter.ts b/src/core/modules/impl/BlockHeightInteractionsSorter.ts similarity index 100% rename from src/core/impl/BlockHeightInteractionsSorter.ts rename to src/core/modules/impl/BlockHeightInteractionsSorter.ts diff --git a/src/core/impl/ContractDefinitionLoader.ts b/src/core/modules/impl/ContractDefinitionLoader.ts similarity index 100% rename from src/core/impl/ContractDefinitionLoader.ts rename to src/core/modules/impl/ContractDefinitionLoader.ts diff --git a/src/core/impl/ContractInteractionsLoader.ts b/src/core/modules/impl/ContractInteractionsLoader.ts similarity index 100% rename from src/core/impl/ContractInteractionsLoader.ts rename to src/core/modules/impl/ContractInteractionsLoader.ts diff --git a/src/core/impl/DefaultStateEvaluator.ts b/src/core/modules/impl/DefaultStateEvaluator.ts similarity index 100% rename from src/core/impl/DefaultStateEvaluator.ts rename to src/core/modules/impl/DefaultStateEvaluator.ts diff --git a/src/core/impl/HandlerExecutorFactory.ts b/src/core/modules/impl/HandlerExecutorFactory.ts similarity index 100% rename from src/core/impl/HandlerExecutorFactory.ts rename to src/core/modules/impl/HandlerExecutorFactory.ts diff --git a/src/core/impl/LexicographicalInteractionsSorter.ts b/src/core/modules/impl/LexicographicalInteractionsSorter.ts similarity index 100% rename from src/core/impl/LexicographicalInteractionsSorter.ts rename to src/core/modules/impl/LexicographicalInteractionsSorter.ts diff --git a/src/contract/node/SmartWeaveNodeFactory.ts b/src/core/node/SmartWeaveNodeFactory.ts similarity index 70% rename from src/contract/node/SmartWeaveNodeFactory.ts rename to src/core/node/SmartWeaveNodeFactory.ts index ade9320..5bfee64 100644 --- a/src/contract/node/SmartWeaveNodeFactory.ts +++ b/src/core/node/SmartWeaveNodeFactory.ts @@ -1,4 +1,10 @@ -import { Contract, HandlerBasedContract, SmartWeave, SmartWeaveWebFactory } from '@smartweave/contract'; +import { + Contract, + HandlerBasedContract, + SmartWeave, + SmartWeaveBuilder, + SmartWeaveWebFactory +} from '@smartweave/contract'; import Arweave from 'arweave'; import { ContractDefinitionLoader, @@ -20,10 +26,18 @@ import { BsonFileBlockHeightSwCache, MemBlockHeightSwCache, MemCache } from '@sm */ export class SmartWeaveNodeFactory extends SmartWeaveWebFactory { /** - * Returns a {@link SmartWeave} that is using file-based cache for {@link StateEvaluator} layer + * Returns a fully configured {@link SmartWeave} that is using file-based cache for {@link StateEvaluator} layer * and mem cache for the rest. */ static fileCached(arweave: Arweave, cacheBasePath?: string): SmartWeave { + return this.fileCachedBased(arweave, cacheBasePath).build(); + } + + /** + * Returns a preconfigured, fileCached {@link SmartWeaveBuilder}, that allows for customization of the SmartWeave instance. + * Use {@link SmartWeaveBuilder.build()} to finish the configuration. + */ + static fileCachedBased(arweave: Arweave, cacheBasePath?: string): SmartWeaveBuilder { const definitionLoader = new ContractDefinitionLoader(arweave, new MemCache()); const interactionsLoader = new CacheableContractInteractionsLoader( @@ -44,7 +58,6 @@ export class SmartWeaveNodeFactory extends SmartWeaveWebFactory { .setInteractionsLoader(interactionsLoader) .setInteractionsSorter(interactionsSorter) .setExecutorFactory(executorFactory) - .setStateEvaluator(stateEvaluator) - .build(); + .setStateEvaluator(stateEvaluator); } } diff --git a/src/contract/web/SmartWeaveWebFactory.ts b/src/core/web/SmartWeaveWebFactory.ts similarity index 71% rename from src/contract/web/SmartWeaveWebFactory.ts rename to src/core/web/SmartWeaveWebFactory.ts index 864dc4b..3b04ad3 100644 --- a/src/contract/web/SmartWeaveWebFactory.ts +++ b/src/core/web/SmartWeaveWebFactory.ts @@ -1,5 +1,5 @@ import Arweave from 'arweave'; -import { HandlerBasedContract, Contract, SmartWeave } from '@smartweave/contract'; +import { HandlerBasedContract, Contract, SmartWeave, SmartWeaveBuilder } from '@smartweave/contract'; import { CacheableContractInteractionsLoader, CacheableExecutorFactory, @@ -24,9 +24,17 @@ import { BsonFileBlockHeightSwCache, MemBlockHeightSwCache, MemCache } from '@sm */ export class SmartWeaveWebFactory { /** - * Returns a {@link SmartWeave} that is using mem cache for all layers. + * Returns a fully configured {@link SmartWeave} that is using mem cache for all layers. */ static memCached(arweave: Arweave): SmartWeave { + return this.memCachedBased(arweave).build(); + } + + /** + * Returns a preconfigured, memCached {@link SmartWeaveBuilder}, that allows for customization of the SmartWeave instance. + * Use {@link SmartWeaveBuilder.build()} to finish the configuration. + */ + static memCachedBased(arweave: Arweave): SmartWeaveBuilder { const definitionLoader = new ContractDefinitionLoader(arweave, new MemCache()); const interactionsLoader = new CacheableContractInteractionsLoader( @@ -47,15 +55,22 @@ export class SmartWeaveWebFactory { .setInteractionsLoader(interactionsLoader) .setInteractionsSorter(interactionsSorter) .setExecutorFactory(executorFactory) - .setStateEvaluator(stateEvaluator) - .build(); + .setStateEvaluator(stateEvaluator); } /** - * Returns a {@link SmartWeave} that (yup, you've guessed it!) does not use any caches. - * This one is gonna be slooow! + * Returns a fully configured, nonCached {@link SmartWeave}. */ static nonCached(arweave: Arweave): SmartWeave { + return this.nonCachedBased(arweave).build(); + } + + /** + * Returns a preconfigured {@link SmartWeave} that (yup, you've guessed it!) does not use any caches. + * This one is gonna be slooow! + * Use {@link SmartWeaveBuilder.build()} to finish the configuration. + */ + static nonCachedBased(arweave: Arweave): SmartWeaveBuilder { const definitionLoader = new ContractDefinitionLoader(arweave); const interactionsLoader = new ContractInteractionsLoader(arweave); const executorFactory = new HandlerExecutorFactory(arweave); @@ -67,7 +82,6 @@ export class SmartWeaveWebFactory { .setInteractionsLoader(interactionsLoader) .setInteractionsSorter(interactionsSorter) .setExecutorFactory(executorFactory) - .setStateEvaluator(stateEvaluator) - .build(); + .setStateEvaluator(stateEvaluator); } } diff --git a/src/plugins/DebuggableExecutorFactor.ts b/src/plugins/DebuggableExecutorFactor.ts index 714fb80..9c6aed3 100644 --- a/src/plugins/DebuggableExecutorFactor.ts +++ b/src/plugins/DebuggableExecutorFactor.ts @@ -10,10 +10,9 @@ import { ContractDefinition, ExecutorFactory } from '@smartweave/core'; export class DebuggableExecutorFactory implements ExecutorFactory { constructor( private readonly baseImplementation: ExecutorFactory, - private readonly sourceCode: { [key: string]: string } - ) { // contract source code before default "normalization" - } + private readonly sourceCode: { [key: string]: string } + ) {} async create(contractDefinition: ContractDefinition): Promise { if (Object.prototype.hasOwnProperty.call(this.sourceCode, contractDefinition.txId)) {