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

feat: [FEATURE] Safe Block Fetch #464 #467

Closed
wants to merge 1 commit 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 jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ module.exports = {
'^.+\\.(ts|js)$': 'ts-jest'
},

silent: true
silent: false
};
36 changes: 35 additions & 1 deletion src/__tests__/integration/basic/pst.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { LoggerFactory } from '../../../logging/LoggerFactory';
import { DeployPlugin } from 'warp-contracts-plugin-deploy';
import { VM2Plugin } from 'warp-contracts-plugin-vm2';
import { InteractionCompleteEvent } from '../../../core/modules/StateEvaluator';
import { NetworkCommunicationError } from "../../../utils/utils";

describe('Testing the Profit Sharing Token', () => {
let contractSrc: string;
Expand Down Expand Up @@ -117,7 +118,7 @@ describe('Testing the Profit Sharing Token', () => {
expect(resultVM.target).toEqual('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M');
});

it('should properly dispatch en event', async () => {
it('should properly dispatch an event', async () => {
let handlerCalled = false;
const interactionResult = await pst.writeInteraction({
function: 'dispatchEvent'
Expand Down Expand Up @@ -150,6 +151,39 @@ describe('Testing the Profit Sharing Token', () => {
}
});


it('should allow to safe fetch from external api', async () => {

const blockData = await arweave.blocks.getCurrent();

await pst.writeInteraction({
function: 'loadBlockData',
height: blockData.height
});

await mineBlock(warp);

const result = await pst.readState();

expect((result.cachedValue.state as any).blocks["" + blockData.height]).toEqual(blockData.indep_hash);

});

it('should stop evaluation on safe fetch error', async () => {

const blockData = await arweave.blocks.getCurrent();

await pst.writeInteraction({
function: 'loadBlockData',
height: blockData.height,
throwError: true
});

await mineBlock(warp);

await expect(pst.readState()).rejects.toThrowError(NetworkCommunicationError);
});

it("should properly evolve contract's source code", async () => {
expect((await pst.currentState()).balances[walletAddress]).toEqual(555114);
expect((await pstVM.currentState()).balances[walletAddress]).toEqual(555114);
Expand Down
18 changes: 18 additions & 0 deletions src/__tests__/integration/data/token-pst.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ export async function handle(state, action) {
return {state};
}

if (input.function === 'loadBlockData') {
const height = input.height;
const throwError = input.throwError;

const blockData = await SmartWeave.safeGet(
throwError
? `http://localhost:1820/blockkkk/height/${height}`
: `http://localhost:1820/block/height/${height}`
);

if (!state.blocks) {
state.blocks = {};
}

state.blocks["" + height] = blockData.indep_hash;
return { state };
}

if (input.function === 'dispatchEvent') {
return {
state,
Expand Down
3 changes: 3 additions & 0 deletions src/legacy/smartweave-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { GQLNodeInterface, GQLTagInterface, VrfData } from './gqlResult';
import { CacheKey, SortKeyCache } from '../cache/SortKeyCache';
import { SortKeyCacheRangeOptions } from '../cache/SortKeyCacheRangeOptions';
import { InteractionState } from '../contract/states/InteractionState';
import { safeGet } from "../utils/utils";

/**
*
Expand Down Expand Up @@ -47,6 +48,7 @@ export class SmartWeaveGlobal {
owner: string;
};
unsafeClient: Arweave;
safeGet: (input: RequestInfo | URL, init?: RequestInit) => Promise<unknown>;

contracts: {
readContractState: (contractId: string) => Promise<any>;
Expand Down Expand Up @@ -79,6 +81,7 @@ export class SmartWeaveGlobal {
wallets: arweave.wallets,
crypto: arweave.crypto
};
this.safeGet = safeGet;

this.evaluationOptions = evaluationOptions;

Expand Down
4 changes: 4 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ export async function getJsonResponse<T>(response: Promise<Response>): Promise<T
throw new NetworkCommunicationError(`Error while parsing json response: ${JSON.stringify(e)}`);
}
}

export async function safeGet<T>(input: RequestInfo | URL, init?: RequestInit): Promise<T> {
return getJsonResponse(fetch(input, init));
}
Loading
Loading