Skip to content

Commit

Permalink
Merge branch 'master' into docker
Browse files Browse the repository at this point in the history
  • Loading branch information
pouya-eghbali committed Dec 20, 2023
2 parents d049cb6 + 9c1b737 commit 91333db
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "@kenshi.io/unchained",
"version": "0.8.3",
"version": "0.8.4",
"description": "",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"lint": "tslint -p tsconfig.json"
"lint": "tslint -p tsconfig.json",
"prepublishOnly": "npm run build"
},
"keywords": [],
"bin": {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ program
.option("--log <level>", "log level")
.option("--lite", "run in lite mode")
.option("--generate", "generate a secret key")
.option("--gossip <size>", "set gossip bucket size")
.action(startAction);

program.parse();
2 changes: 2 additions & 0 deletions src/lib/cli/actions/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface StartOptions {
log?: string;
lite?: boolean;
generate?: boolean;
gossip?: string;
}

export const startAction = async (
Expand All @@ -30,6 +31,7 @@ export const startAction = async (

logger.level = options.log || config.log || "info";
config.lite = options.lite || config.lite || false;
config.gossip = parseInt(options.gossip || "0") || config.gossip || 5;

if (!config.secretKey && !options.generate) {
logger.error("No secret key supplied");
Expand Down
3 changes: 2 additions & 1 deletion src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
StringGossipMethodObject,
} from "./types.js";

export const version = "0.8.3";
export const version = "0.8.4";
export const protocolVersion = "0.8.0";

export const topic = sha(`Kenshi.Unchained.Testnet.Topic.V${protocolVersion}`);
Expand All @@ -27,6 +27,7 @@ export const config: Config = {
},
secretKey: "",
lite: false,
gossip: 5,
};

export const rpcMethods: StringAnyObject = {};
Expand Down
17 changes: 13 additions & 4 deletions src/lib/gossip/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gossipMethods, errors, keys, sockets } from "../constants.js";
import { gossipMethods, errors, keys, sockets, config } from "../constants.js";
import { encoder } from "../bls/keys.js";
import { Gossip, GossipRequest, MetaData, NodeSystemError } from "../types.js";

Expand Down Expand Up @@ -27,6 +27,15 @@ const gossipTo = async (nodes: MetaData[], data: any): Promise<void> => {
await Promise.all(promises).catch(() => null);
};

const randomDistinct = (length: number, count: number): number[] => {
const set = new Set<number>();
while (set.size < count) {
const random = randomIndex(length);
set.add(random);
}
return [...set];
};

export const gossip = async (
request: GossipRequest<any>,
seen: string[]
Expand All @@ -47,11 +56,11 @@ export const gossip = async (
if (!nodes.length) {
return;
}
if (nodes.length <= 3) {
if (nodes.length <= config.gossip) {
await gossipTo(nodes, payload);
} else {
const random = new Array(3).fill(null).map(() => randomIndex(nodes.length));
const chosen = [...new Set(random)].map((index) => nodes[index]);
const indexes = randomDistinct(nodes.length, config.gossip);
const chosen = indexes.map((index) => nodes[index]);
await gossipTo(chosen, payload);
}
};
Expand Down
26 changes: 22 additions & 4 deletions src/lib/plugins/uniswap/uniswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,25 @@ const ws = (endpoint: string): WebSocketLike =>
retry: { forever: true },
}) as WebSocketLike;

const getProvider = (config: Config) => {
if (!provider) {
const endpoint = config.rpc.ethereum;
let currentProvider: number = 0;

const getNextConnectionUrl = (config: Config): string => {
if (typeof config.rpc.ethereum === "string") {
return config.rpc.ethereum;
} else {
if (currentProvider > config.rpc.ethereum.length) {
currentProvider = 0;
}
return config.rpc.ethereum[currentProvider++];
}
};

const getProvider = (config: Config, fresh: boolean = false) => {
if (fresh || !provider) {
if (fresh) {
provider?.destroy();
}
const endpoint = getNextConnectionUrl(config);
provider = endpoint.startsWith("wss://")
? new ethers.WebSocketProvider(ws(endpoint))
: new ethers.JsonRpcProvider(endpoint);
Expand Down Expand Up @@ -264,7 +280,9 @@ export const work = async (
...signed,
};
} catch (error) {
logger.warn("Could not get the Ethereum price. Check your RPC.");
logger.warn("Could not get the Ethereum price.");
logger.warn("Getting a new RPC connection.");
getProvider(config, true);
throw error;
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface KeyPair {
}

interface RPCList {
ethereum: string;
ethereum: string | string[];
}

interface DatabaseConfig {
Expand All @@ -26,6 +26,7 @@ export interface Config {
lite: boolean;
database?: DatabaseConfig;
secretKey: string;
gossip: number;
}

export interface MetaData {
Expand Down

0 comments on commit 91333db

Please sign in to comment.