Skip to content

Commit

Permalink
feat(ri-scripting): add light client to send tx at scale, add scripts…
Browse files Browse the repository at this point in the history
… for stresstest 1
  • Loading branch information
alvrs committed May 20, 2022
1 parent f93ee23 commit 903b25c
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/ri/scripting/.eslintignore
@@ -0,0 +1,2 @@
node_modules
dist
24 changes: 24 additions & 0 deletions packages/ri/scripting/.eslintrc
@@ -0,0 +1,24 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"no-restricted-imports": [
"error",
{
"paths": [
{
"importNames": ["delay"],
"name": "rxjs",
"message": "Use delayTime from utils/rx instead."
}
]
}
]
}
}
1 change: 1 addition & 0 deletions packages/ri/scripting/.gitignore
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions packages/ri/scripting/README.md
@@ -0,0 +1 @@
# Reference Implementation Scripting
25 changes: 25 additions & 0 deletions packages/ri/scripting/package.json
@@ -0,0 +1,25 @@
{
"name": "ri-scripting",
"version": "0.0.1",
"dependencies": {
"@ethersproject/providers": "^5.6.1",
"@mud/network": "0.0.1",
"@mud/utils": "0.0.1",
"async-mutex": "^0.3.2",
"ethers": "^5.6.6",
"proxy-deep": "^3.1.1",
"ri-contracts": "0.0.1"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.12.1",
"@typescript-eslint/parser": "^5.12.1",
"eslint": "^8.9.0",
"prettier": "^2.5.1",
"ts-node": "^10.7.0",
"typescript": "^4.5.5"
},
"scripts": {
"start": "ts-node ./src/index.ts",
"txpool": "curl -X POST -H \"Content-Type: application/json\" --data '{\"jsonrpc\": \"2.0\", \"method\": \"txpool_content\", \"params\": [], \"id\":1}' https://degen-chain.lattice.xyz/"
}
}
4 changes: 4 additions & 0 deletions packages/ri/scripting/src/constants.ts
@@ -0,0 +1,4 @@
export const RPC_URL = "https://degen-chain.lattice.xyz/";
export const RPC_WS_URL = "wss://degen-chain.lattice.xyz/ws/";
export const DEV_PRIVATE_KEY = "0xb20486f2dadf532076b54c65b945f7df6728945d8bff5df06088dc1286bee1c3";
export const DIAMOND_ADDRESS = "0x39903eA6F367D765001A26f5C64264BEfd42D26f";
88 changes: 88 additions & 0 deletions packages/ri/scripting/src/index.ts
@@ -0,0 +1,88 @@
/* eslint-disable no-constant-condition */
import { awaitValue, keccak256, PromiseValue, random, sleep } from "@mud/utils";
import { setupContracts } from "./setupContracts";
import { defaultAbiCoder as abi } from "ethers/lib/utils";
import { Signer } from "ethers";
import { JsonRpcProvider } from "@ethersproject/providers";

type Context = PromiseValue<ReturnType<typeof main>>;
const GAS = 107;

export async function main() {
const { txQueue, provider: computedProvider, signer: computedSigner } = await setupContracts();
const provider = await awaitValue(computedProvider);
const signer = await awaitValue(computedSigner);
const pendingNonces = await getPendingNonces(provider, signer);
console.log("Pending nonces", pendingNonces);

const Position = await txQueue.World.getComponent(keccak256("ember.component.positionComponent"));
const EntityType = await txQueue.World.getComponent(keccak256("ember.component.entityTypeComponent"));

const context = { txQueue, components: { Position, EntityType }, provider, signer, pendingNonces };

// Send tx fast
const promises: Promise<unknown>[] = [];
const numEntities = 400;

async function step(i: number) {
if (i < numEntities) await spawnAtRandomPosition(i, context);
else await setRandomPosition(i % numEntities, context);
}

let i = 0;
while (true) {
console.log("Iteration", i);
// try {
// await step(i);
// } catch (e) {
// console.warn(e);
// }
promises.push(step(i));
i++;
await sleep(2);
}

console.log("Waiting for promises");
await Promise.all(promises);
console.log("Done");
return context;
}

async function getPendingNonces(provider: JsonRpcProvider, signer: Signer): Promise<Set<number>> {
const pendingTx = await provider.send("txpool_content", []);
return new Set(Object.keys(pendingTx?.queued[await signer.getAddress()] || {}).map((key) => parseInt(key)));
}

async function setPosition(
entity: number,
pos: { x: number; y: number },
{ txQueue, components: { Position } }: Context
) {
await txQueue.Ember.addComponentToEntityExternally(
entity,
Position,
abi.encode(["uint256", "uint256"], [pos.x, pos.y]),
{ gasPrice: GAS, gasLimit: 250000 }
);
}

async function setEntityType(entity: number, entityType: number, { txQueue, components: { EntityType } }: Context) {
await txQueue.Ember.addComponentToEntityExternally(entity, EntityType, abi.encode(["uint256"], [entityType]), {
gasPrice: GAS,
gasLimit: 250000,
});
}

async function setRandomPosition(entity: number, context: Context) {
const x = random(0, 40);
const y = random(0, 40);
await setPosition(entity, { x, y }, context);
}

async function spawnAtRandomPosition(entity: number, context: Context) {
await Promise.all([setRandomPosition(entity, context), setEntityType(entity, 0, context)]);
}

main().then(() => {
process.exit();
});
76 changes: 76 additions & 0 deletions packages/ri/scripting/src/setupContracts.ts
@@ -0,0 +1,76 @@
import { createNetwork, createContracts, createSigner, createTxQueue } from "@mud/network";
import { DEV_PRIVATE_KEY, DIAMOND_ADDRESS, RPC_URL, RPC_WS_URL } from "./constants";
import { World as WorldContract } from "ri-contracts/types/ethers-contracts/World";
import { CombinedFacets } from "ri-contracts/types/ethers-contracts/CombinedFacets";
import WorldABI from "ri-contracts/abi/World.json";
import EmberABI from "ri-contracts/abi/CombinedFacets.json";
import { combineLatest, from, map, mergeMap, ReplaySubject } from "rxjs";
import { JsonRpcProvider } from "@ethersproject/providers";
import { Signer } from "ethers";
import { awaitValue, filterNullish, streamToComputed } from "@mud/utils";

export async function setupContracts() {
const connected$ = new ReplaySubject<boolean>(1);
const contracts$ = new ReplaySubject<{ Ember: CombinedFacets; World: WorldContract }>(1);
const ethersSigner$ = new ReplaySubject<Signer>(1);
const provider$ = new ReplaySubject<JsonRpcProvider>(1);

const { txQueue, ready } = createTxQueue(contracts$, ethersSigner$, provider$, connected$, {
concurrency: Number.MAX_SAFE_INTEGER,
ignoreConfirmation: true,
});

try {
const network = createNetwork({
time: {
period: 5000,
},
chainId: 1337,
rpcSupportsBatchQueries: false,
rpcUrl: RPC_URL,
rpcWsUrl: RPC_WS_URL,
});

// Connect the connected stream to the outer scope connected stream
network.connected$.subscribe(connected$);

// Connect the signer to the outer scope signer
createSigner({ privateKey: DEV_PRIVATE_KEY }, network.providers$).ethersSigner$.subscribe(ethersSigner$);

// Connect the provider to the outer scope provider
network.providers$
.pipe(map(([json, ws]) => ws))
.pipe(filterNullish())
.subscribe(provider$);

// Create a stream of ember contracts
const emberContract = createContracts<{ Ember: CombinedFacets }>(
{ Ember: { abi: EmberABI.abi, address: DIAMOND_ADDRESS } },
ethersSigner$
);

// Create a stream of world contracts
const worldContract$ = emberContract.contracts$.pipe(
mergeMap(({ Ember }) => from(Ember.world())), // Get the world address
mergeMap(
(address) =>
createContracts<{ World: WorldContract }>({ World: { abi: WorldABI.abi, address } }, ethersSigner$).contracts$
)
);

// Connect the contract stream to the outer scope contracts
combineLatest([emberContract.contracts$, worldContract$])
.pipe(
map(([{ Ember }, { World }]) => ({
Ember,
World,
}))
)
.subscribe(contracts$);
} catch (e) {
console.warn(e);
}

await awaitValue(ready);
return { txQueue, provider: streamToComputed(provider$), signer: streamToComputed(ethersSigner$) };
}
3 changes: 3 additions & 0 deletions packages/ri/scripting/src/setupNetwork.ts
@@ -0,0 +1,3 @@
export function setupNetwork() {
//
}
101 changes: 101 additions & 0 deletions packages/ri/scripting/tsconfig.json
@@ -0,0 +1,101 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Projects */
// "incremental": true, /* Enable incremental compilation */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */

/* Language and Environment */
"target": "esnext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve" /* Specify what JSX code is generated. */,
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
"useDefineForClassFields": true /* Emit ECMAScript-standard-compliant class fields. */,
/* Modules */
"module": "commonjs" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
// "baseUrl": "./" /* Specify the base directory to resolve non-relative module names. */,
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [] /* Specify multiple folders that act like `./node_modules/@types`. */,
// "types": [] /* Specify type package names to be included without being referenced in a source file. */,
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
"resolveJsonModule": true /* Enable importing .json files */,
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */

/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
"sourceMap": true /* Create source map files for emitted JavaScript files. */,
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
"outDir": "dist" /* Specify an output folder for all emitted files. */,
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */

/* Interop Constraints */
"isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */,
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,

/* Type Checking */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */

/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"exclude": ["dist", "packages"]
}

0 comments on commit 903b25c

Please sign in to comment.