Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dumb fix by a dumb guy #476

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "warp-contracts",
"version": "1.4.22",
"version": "1.4.23-beta.0",
"description": "An implementation of the SmartWeave smart contract protocol.",
"types": "./lib/types/index.d.ts",
"main": "./lib/cjs/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/contract/HandlerBasedContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ export class HandlerBasedContract<State> implements Contract<State> {

// eval current state
const evalStateResult = await stateEvaluator.eval<State>(executionContext);
this.logger.info('Current state', evalStateResult.cachedValue.state);
this.logger.debug('Current state', evalStateResult.cachedValue.state);

// create interaction transaction
const interaction: ContractInteraction<Input> = {
Expand Down
8 changes: 7 additions & 1 deletion src/core/modules/impl/handler/JsHandlerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ export class JsHandlerApi<State> extends AbstractContractHandler<State> {
const handlerResult = await Promise.race([timeoutPromise, this.contractFunction(stateClone, interaction)]);

if (handlerResult && (handlerResult.state !== undefined || handlerResult.result !== undefined)) {
await this.swGlobal.kv.commit();
if (interaction.interactionType === 'write') {
await this.swGlobal.kv.commit();
} else {
// view state function should not change anything in the kv storage
await this.swGlobal.kv.rollback();
}

let interactionEvent: InteractionCompleteEvent = null;

Expand Down Expand Up @@ -232,6 +237,7 @@ export class JsHandlerApi<State> extends AbstractContractHandler<State> {

private setupSwGlobal<Input>({ interaction, interactionTx }: InteractionData<Input>) {
this.swGlobal._activeTx = interactionTx;
this.swGlobal.interactionType = interaction.interactionType;
this.swGlobal.caller = interaction.caller; // either contract tx id (for internal writes) or transaction.owner
}

Expand Down
1 change: 1 addition & 0 deletions src/core/modules/impl/handler/WasmHandlerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class WasmHandlerApi<State> extends AbstractContractHandler<State> {

this.swGlobal._activeTx = interactionTx;
this.swGlobal.caller = interaction.caller; // either contract tx id (for internal writes) or transaction.owner
this.swGlobal.interactionType = interaction.interactionType;
this.swGlobal.gasLimit = executionContext.evaluationOptions.gasLimit;
this.swGlobal.gasUsed = 0;

Expand Down
20 changes: 20 additions & 0 deletions src/legacy/smartweave-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CacheKey, SortKeyCache } from '../cache/SortKeyCache';
import { SortKeyCacheRangeOptions } from '../cache/SortKeyCacheRangeOptions';
import { InteractionState } from '../contract/states/InteractionState';
import { safeGet } from '../utils/utils';
import { ContractError, InteractionType } from "../core/modules/impl/HandlerExecutorFactory";

/**
*
Expand Down Expand Up @@ -66,6 +67,8 @@ export class SmartWeaveGlobal {

kv: KV;

interactionType: InteractionType;

constructor(
arweave: Arweave,
contract: { id: string; owner: string },
Expand Down Expand Up @@ -218,6 +221,13 @@ export class SWTransaction {
}
return this.smartWeaveGlobal._activeTx.source === 'redstone-sequencer' ? 'L2' : 'L1';
}

get interactionType(): InteractionType {
if (!this.smartWeaveGlobal._activeTx) {
throw new Error('No current Tx');
}
return this.smartWeaveGlobal.interactionType;
}
}

// tslint:disable-next-line: max-classes-per-file
Expand Down Expand Up @@ -282,7 +292,13 @@ export class KV {
) {}

async put(key: string, value: any): Promise<void> {
if (this._transaction.interactionType === 'view') {
throw new ContractError('Forbidden write operation for "view" function: "KV.put"');
}

this.checkStorageAvailable();


await this._storage.put(new CacheKey(key, this._transaction.sortKey), value);
}

Expand All @@ -304,6 +320,10 @@ export class KV {
}

async del(key: string): Promise<void> {
if (this._transaction.interactionType === 'view') {
throw new ContractError('Forbidden write operation for "view" function: "KV.del"');
}

this.checkStorageAvailable();
const sortKey = this._transaction.sortKey;

Expand Down