From 093c777d127c20bc9616d64eca1faae2cb304b20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Szynwelski?= Date: Tue, 31 Oct 2023 16:58:49 +0100 Subject: [PATCH] Script for testing sequencer switchover --- package.json | 3 +- tools/send-interactions-to-sequencer.ts | 89 +++++++++++++++++++++++++ yarn.lock | 3 + 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tools/send-interactions-to-sequencer.ts diff --git a/package.json b/package.json index 0b98557b..5e20aeb7 100644 --- a/package.json +++ b/package.json @@ -125,7 +125,8 @@ "warp-contracts-plugin-deploy": "1.0.8", "warp-contracts-plugin-vm2": "1.0.1", "warp-contracts-plugin-vrf": "^1.0.3", - "ws": "^8.11.0" + "ws": "^8.11.0", + "warp-contracts": "file:lib/cjs" }, "resolutions": { "bn.js": "5.2.1", diff --git a/tools/send-interactions-to-sequencer.ts b/tools/send-interactions-to-sequencer.ts new file mode 100644 index 00000000..2cb2bbf9 --- /dev/null +++ b/tools/send-interactions-to-sequencer.ts @@ -0,0 +1,89 @@ +import ArLocal from "arlocal"; +import Arweave from "arweave"; +import { EvalStateResult, SortKeyCacheResult, SourceType, WarpFactory, defaultCacheOptions, defaultWarpGwOptions, sleep } from "../src"; +import fs from 'fs'; +import path from 'path'; +import { ArweaveSigner } from "warp-arbundles"; +import { DeployPlugin } from "warp-contracts-plugin-deploy"; + +interface ExampleContractState { + counter: number; +} + +async function main() { + // const sequencerUrl = 'http://localhost:5666'; + const sequencerUrl = 'http://34.141.17.15:5666'; + // const sequencerUrl = 'https://gw.warp.cc'; + const arweavePort = 1983; + const arlocal = new ArLocal(arweavePort, false); + + try { + await arlocal.start(); + const arweave = Arweave.init({ + host: 'localhost', + port: arweavePort, + protocol: 'http' + }); + + const cacheOptions = { + ...defaultCacheOptions, + inMemory: true + } + const gatewayOptions = { ...defaultWarpGwOptions, source: SourceType.WARP_SEQUENCER, confirmationStatus: { notCorrupted: true } } + + const warp = WarpFactory + .custom(arweave, cacheOptions, 'custom') + .useWarpGateway(gatewayOptions, cacheOptions) + .build() + .use(new DeployPlugin()) + .useGwUrl(sequencerUrl) + + const contractSrc = fs.readFileSync(path.join(__dirname, '../src/__tests__/integration/data/example-contract.js'), 'utf8'); + const initialState = fs.readFileSync(path.join(__dirname, '../src/__tests__/integration/data/example-contract-state.json'), 'utf8'); + const wallet = (await warp.generateWallet()).jwk; + const { contractTxId } = await warp.deploy({ + wallet: new ArweaveSigner(wallet), + initState: initialState, + src: contractSrc + }); + const contract = warp.contract(contractTxId).setEvaluationOptions({ + sequencerUrl: sequencerUrl, + waitForConfirmation: true + }); + contract.connect(wallet); + + const initCounter = 555; + const numberOfInteractions = 1000; + let counter = initCounter; + let state: SortKeyCacheResult> | undefined; + let contractCounter: number; + while (counter < initCounter + numberOfInteractions) { + try { + const response = await contract.writeInteraction({ function: 'add' }) + console.log('interaction response', response); + counter++; + } catch(e) { + console.error("interaction failed", e); + } + + try { + state = await contract.readState(); + } catch(e) { + console.error("read state failed", e); + state = undefined; + } + if (state) { + contractCounter = state.cachedValue.state.counter + console.log('counter from contract', counter) + if (contractCounter !== counter) { + throw new Error("Invalid counter value. Expected: " + counter + " actual: " + contractCounter); + } + } + await sleep(1000); + } + } finally { + await arlocal.stop(); + } +} + +main().catch((e) => console.error(e)); diff --git a/yarn.lock b/yarn.lock index 9d1e2cc6..7a6cdb96 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7700,6 +7700,9 @@ warp-contracts-plugin-vrf@^1.0.3: "@idena/vrf-js" "^1.0.1" elliptic "^6.5.4" +"warp-contracts@file:lib/cjs": + version "0.0.0" + warp-isomorphic@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/warp-isomorphic/-/warp-isomorphic-1.0.0.tgz#dccccfc046bc6ac77919f8be1024ced1385c70ea"