fix: rebase fixes

This commit is contained in:
asiaziola
2022-06-14 15:22:23 +02:00
parent 978466ce5e
commit 8c77d60c66
9 changed files with 259 additions and 10 deletions

View File

@@ -10,6 +10,7 @@ import {
} from '@warp';
import { NetworkInfoInterface } from 'arweave/node/network';
import Transaction from 'arweave/node/lib/transaction';
import { Source } from './deploy/Source';
export type CurrentTx = { interactionTxId: string; contractTxId: string };
export type BenchmarkStats = { gatewayCommunication: number; stateEvaluation: number; total: number };

View File

@@ -770,7 +770,7 @@ export class HandlerBasedContract<State> implements Contract<State> {
if (!this.signer) {
throw new Error("Wallet not connected. Use 'connect' method first.");
}
const { arweave } = this.smartweave;
const { arweave } = this.warp;
const source = new SourceImpl(arweave);
const srcTx = await source.save(sourceData, this.signer);

View File

@@ -1,9 +1,9 @@
/* eslint-disable */
import { SmartWeaveTags } from '@smartweave/core';
import { SmartWeaveTags } from '@warp/core';
import Arweave from 'arweave';
import { LoggerFactory } from '@smartweave/logging';
import { LoggerFactory } from '@warp/logging';
import Transaction from 'arweave/node/lib/transaction';
import { ContractData, CreateContract, FromSrcTxContractData, SourceImpl } from '@smartweave/contract';
import { ContractData, CreateContract, FromSrcTxContractData, SourceImpl } from '@warp/contract';
export class DefaultCreateContract implements CreateContract {
private readonly logger = LoggerFactory.INST.create('DefaultCreateContract');

View File

@@ -1,4 +1,4 @@
import { WarpTags } from '@warp/core';
import { SmartWeaveTags } from '@warp/core';
import { LoggerFactory } from '@warp/logging';
import { Source, SigningFunction } from '@warp';
import metering from 'redstone-wasm-metering';
@@ -98,7 +98,7 @@ export class SourceImpl implements Source {
srcTx = await this.arweave.createTransaction({ data: allData }, signer);
}
srcTx.addTag(WarpTags.APP_NAME, 'SmartWeaveContractSource');
srcTx.addTag(SmartWeaveTags.APP_NAME, 'SmartWeaveContractSource');
// TODO: version should be taken from the current package.json version.
srcTx.addTag(SmartWeaveTags.APP_VERSION, '0.3.0');
srcTx.addTag(SmartWeaveTags.SDK, 'Warp');

View File

@@ -1,7 +1,7 @@
/**
* This type contains all data and meta-data of the given contact.
*/
import { ContractType } from '@smartweave/contract';
import { ContractType } from '@warp/contract';
export class ContractMetadata {
dtor: number;

67
src/core/SmartWeave.ts Normal file
View File

@@ -0,0 +1,67 @@
import {
CreateContract,
DefaultCreateContract,
DefinitionLoader,
ExecutorFactory,
HandlerApi,
InteractionsLoader,
InteractionsSorter,
SmartWeaveBuilder,
StateEvaluator
} from '@warp/core';
import Arweave from 'arweave';
import { Contract, HandlerBasedContract, PstContract, PstContractImpl } from '@warp/contract';
import { GQLNodeInterface } from '@warp/legacy';
/**
* The SmartWeave "motherboard" ;-).
* This is the base class that supplies the implementation of the SmartWeave protocol
* Allows to plug-in different implementation of all the modules defined in the constructor.
*
* After being fully configured, it allows to "connect" to
* contract and perform operations on them (see {@link Contract})
*/
export class SmartWeave {
readonly createContract: CreateContract;
constructor(
readonly arweave: Arweave,
readonly definitionLoader: DefinitionLoader,
readonly interactionsLoader: InteractionsLoader,
readonly interactionsSorter: InteractionsSorter,
readonly executorFactory: ExecutorFactory<HandlerApi<unknown>>,
readonly stateEvaluator: StateEvaluator,
readonly useRedstoneGwInfo: boolean = false
) {
this.createContract = new DefaultCreateContract(arweave);
}
static builder(arweave: Arweave): SmartWeaveBuilder {
return new SmartWeaveBuilder(arweave);
}
/**
* Allows to connect to any contract using its transaction id.
* @param contractTxId
* @param callingContract
*/
contract<State>(
contractTxId: string,
callingContract?: Contract,
callingInteraction?: GQLNodeInterface
): Contract<State> {
return new HandlerBasedContract<State>(contractTxId, this, callingContract, callingInteraction);
}
/**
* Allows to connect to a contract that conforms to the Profit Sharing Token standard
* @param contractTxId
*/
pst(contractTxId: string): PstContract {
return new PstContractImpl(contractTxId, this);
}
async flushCache(): Promise<void> {
await this.stateEvaluator.flushCache();
}
}

View File

@@ -0,0 +1,115 @@
import Arweave from 'arweave';
import {
ArweaveGatewayInteractionsLoader,
CacheableContractInteractionsLoader,
ConfirmationStatus,
ContractDefinitionLoader,
DebuggableExecutorFactory,
DefinitionLoader,
ExecutorFactory,
HandlerApi,
InteractionsLoader,
InteractionsSorter,
MemBlockHeightSwCache,
MemCache,
RedstoneGatewayContractDefinitionLoader,
RedstoneGatewayInteractionsLoader,
SmartWeave,
SourceType,
StateEvaluator
} from '@warp';
export const R_GW_URL = 'https://d1o5nlqr4okus2.cloudfront.net';
export class SmartWeaveBuilder {
private _definitionLoader?: DefinitionLoader;
private _interactionsLoader?: InteractionsLoader;
private _interactionsSorter?: InteractionsSorter;
private _executorFactory?: ExecutorFactory<HandlerApi<unknown>>;
private _stateEvaluator?: StateEvaluator;
private _useRedstoneGwInfo = false;
constructor(private readonly _arweave: Arweave) {}
public setDefinitionLoader(value: DefinitionLoader): SmartWeaveBuilder {
this._definitionLoader = value;
return this;
}
public setInteractionsLoader(value: InteractionsLoader): SmartWeaveBuilder {
this._interactionsLoader = value;
return this;
}
public setCacheableInteractionsLoader(
value: InteractionsLoader,
maxStoredInMemoryBlockHeights = 1
): SmartWeaveBuilder {
this._interactionsLoader = new CacheableContractInteractionsLoader(
value,
new MemBlockHeightSwCache(maxStoredInMemoryBlockHeights)
);
return this;
}
public setInteractionsSorter(value: InteractionsSorter): SmartWeaveBuilder {
this._interactionsSorter = value;
return this;
}
public setExecutorFactory(value: ExecutorFactory<HandlerApi<unknown>>): SmartWeaveBuilder {
this._executorFactory = value;
return this;
}
public setStateEvaluator(value: StateEvaluator): SmartWeaveBuilder {
this._stateEvaluator = value;
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();
}
public useRedStoneGateway(
confirmationStatus: ConfirmationStatus = null,
source: SourceType = null,
address = R_GW_URL
): SmartWeaveBuilder {
this._interactionsLoader = new RedstoneGatewayInteractionsLoader(address, confirmationStatus, source);
this._definitionLoader = new RedstoneGatewayContractDefinitionLoader(address, this._arweave, new MemCache());
this._useRedstoneGwInfo = true;
return this;
}
public useArweaveGateway(): SmartWeaveBuilder {
this._definitionLoader = new ContractDefinitionLoader(this._arweave, new MemCache());
this._interactionsLoader = new CacheableContractInteractionsLoader(
new ArweaveGatewayInteractionsLoader(this._arweave),
new MemBlockHeightSwCache(1)
);
this._useRedstoneGwInfo = false;
return this;
}
public useRedStoneGwInfo(): SmartWeaveBuilder {
this._useRedstoneGwInfo = true;
return this;
}
build(): SmartWeave {
return new SmartWeave(
this._arweave,
this._definitionLoader,
this._interactionsLoader,
this._interactionsSorter,
this._executorFactory,
this._stateEvaluator,
this._useRedstoneGwInfo
);
}
}

View File

@@ -7,9 +7,8 @@ import {
DefinitionLoader,
getTag,
LoggerFactory,
SmartWeaveTags,
WarpCache,
WarpTags
SmartWeaveTags
} from '@warp';
import Arweave from 'arweave';
import Transaction from 'arweave/web/lib/transaction';
@@ -84,7 +83,7 @@ export class ContractDefinitionLoader implements DefinitionLoader {
const benchmark = Benchmark.measure();
const contractSrcTx = await this.arweaveWrapper.tx(contractSrcTxId);
const srcContentType = getTag(contractSrcTx, WarpTags.CONTENT_TYPE);
const srcContentType = getTag(contractSrcTx, SmartWeaveTags.CONTENT_TYPE);
if (!supportedSrcContentTypes.includes(srcContentType)) {
throw new Error(`Contract source content type ${srcContentType} not supported`);
}

View File

@@ -0,0 +1,67 @@
import { ContractDefinition, getTag, LoggerFactory, stripTrailingSlash, SwCache } from '@warp';
import Arweave from 'arweave';
import { ContractDefinitionLoader } from './ContractDefinitionLoader';
import 'redstone-isomorphic';
import { WasmSrc } from './wasm/WasmSrc';
import Transaction from 'arweave/node/lib/transaction';
import { SmartWeaveTags } from '@warp/core';
/**
* An extension to {@link ContractDefinitionLoader} that makes use of
* Redstone Gateway ({@link https://github.com/redstone-finance/redstone-sw-gateway})
* to load Contract Data.
*
* If the contract data is not available on RedStone Gateway - it fallbacks to default implementation
* in {@link ContractDefinitionLoader} - i.e. loads the definition from Arweave gateway.
*/
export class RedstoneGatewayContractDefinitionLoader extends ContractDefinitionLoader {
private readonly rLogger = LoggerFactory.INST.create('RedstoneGatewayContractDefinitionLoader');
constructor(
private readonly baseUrl: string,
arweave: Arweave,
cache?: SwCache<string, ContractDefinition<unknown>>
) {
super(arweave, cache);
this.baseUrl = stripTrailingSlash(baseUrl);
}
async doLoad<State>(contractTxId: string, forcedSrcTxId?: string): Promise<ContractDefinition<State>> {
try {
const result: ContractDefinition<State> = await fetch(
`${this.baseUrl}/gateway/contract?txId=${contractTxId}${forcedSrcTxId ? `&srcTxId=${forcedSrcTxId}` : ''}`
)
.then((res) => {
return res.ok ? res.json() : Promise.reject(res);
})
.catch((error) => {
if (error.body?.message) {
this.rLogger.error(error.body.message);
}
throw new Error(
`Unable to retrieve contract data. Redstone gateway responded with status ${error.status}:${error.body?.message}`
);
});
if (result.srcBinary != null && !(result.srcBinary instanceof Buffer)) {
result.srcBinary = Buffer.from((result.srcBinary as any).data);
}
if (result.srcBinary) {
const wasmSrc = new WasmSrc(result.srcBinary);
result.srcBinary = wasmSrc.wasmBinary();
let sourceTx;
if (result.srcTx) {
sourceTx = new Transaction({ ...result.srcTx });
} else {
sourceTx = await this.arweaveWrapper.tx(result.srcTxId);
}
const srcMetaData = JSON.parse(getTag(sourceTx, SmartWeaveTags.WASM_META));
result.metadata = srcMetaData;
}
result.contractType = result.src ? 'js' : 'wasm';
return result;
} catch (e) {
this.rLogger.warn('Falling back to default contracts loader', e);
return await super.doLoad(contractTxId, forcedSrcTxId);
}
}
}