Skip to content

Commit

Permalink
Merge pull request #39 from KenshiTech/0.10.7
Browse files Browse the repository at this point in the history
0.10.7
  • Loading branch information
pouya-eghbali authored Jan 13, 2024
2 parents a1f2cf4 + 1a1d37b commit 2f971b5
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 23 deletions.
8 changes: 7 additions & 1 deletion conf.lite.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@ rpc:
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com
peers:
max: 64
max: 128
parallel: 8
jail:
duration: 5
strikes: 5
gossip:
infect: 24
die: 8
8 changes: 7 additions & 1 deletion conf.local.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ rpc:
database:
url: postgres://<user>:<pass>@<host>:<port>/<db>
peers:
max: 64
max: 128
parallel: 8
jail:
duration: 5
strikes: 5
gossip:
infect: 24
die: 8
8 changes: 7 additions & 1 deletion conf.remote.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ rpc:
database:
url: postgres://<user>:<pass>@<host>:<port>/<db>
peers:
max: 64
max: 128
parallel: 8
jail:
duration: 5
strikes: 5
gossip:
infect: 24
die: 8
23 changes: 20 additions & 3 deletions quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,14 @@ rpc:
database:
url: postgres://<user>:<pass>@<host>:<port>/<db>
peers:
max: 64
max: 128
parallel: 8
jail:
duration: 5
strikes: 5
gossip:
infect: 24
die: 8
```
Save the above configuration in a file named `conf.yaml` on your system and make
Expand All @@ -216,11 +222,22 @@ the following modifications if required:
- `name`: This name will be associated with your validator node, and is published to
all peers.
- `lite`: To run a lite node, set this to `true`, otherwise set it to `false`.
- `rpc.ethereum`: Unchained testnet has automatic RPC rotation and renewal when issues are detected with the RPC connection. You can find a list of Ethereum RPC nodes on
[Chainlist](https://chainlist.org/chain/1).
- `rpc.ethereum`: Unchained testnet has automatic RPC rotation and renewal when
issues are detected with the RPC connection. You can find a list of Ethereum
RPC nodes on [Chainlist](https://chainlist.org/chain/1).
- `database.url`: Your Postgres connection string goes here. Ignore this if
you're running a lite node.

Advanced options:

- `peers.max`: Maximum number of peers to connect to.
- `peers.parallel`: Maximum number of peers in connecting state. A peer is in
connecting state when it is discovered but hasn't finished connecting yet.
- `jail.duration`: Number of minutes to jail a peer on bad behavior.
- `jail.strikes`: Number of strikes to wait before jailing a peer.
- `gossip.infect`: Number of peers to infect when a packet is available.
- `gossip.die`: Number of times a packet can infect others before it dies.

You can also use RPC nodes that start with `wss://` instead of `https://`.

## Migrations / Database Initialization
Expand Down
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ program
.option("--log <level>", "log level")
.option("--lite", "run in lite mode")
.option("--generate", "generate a secret key")
.option("--max-peers <max>", "set max allowed active peer connections")
.option(
"--parallel-peers <parallel>",
"set max allowed parallel peer connections"
)
.option("--max-peers <max>", "max active peer connections")
.option("--parallel-peers <parallel>", "max parallel peer connections")
.option("--infect <number>", "number of nodes to infect in one go")
.option("--die <times>", "times a packet infects others before it dies")
.action(startAction);

program
Expand Down
29 changes: 28 additions & 1 deletion src/lib/cli/actions/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ import {
import { runTasks } from "../../daemon/index.js";
import { initDB } from "../../db/db.js";
import assert from "node:assert";
import { minutes } from "../../utils/time.js";

interface StartOptions {
log?: string;
lite?: boolean;
generate?: boolean;
maxPeers?: string;
parallelPeers?: string;
infect?: string;
die?: string;
}

export const startAction = async (
Expand All @@ -37,6 +40,7 @@ export const startAction = async (
logger.level = options.log || config.log || "info";
config.lite = options.lite || config.lite || false;

// Peers
config.peers ||= globalConfig.peers;
config.peers.max =
parseInt(options.maxPeers || "0") ||
Expand All @@ -49,10 +53,33 @@ export const startAction = async (
globalConfig.peers.parallel;

// TODO: We need sanity checks; e.g. did the user set jail time to a string?
// Jailing
config.jail ||= globalConfig.jail;
config.jail.duration = config.jail.duration || globalConfig.jail.duration;

config.jail.duration = config.jail.duration
? minutes(config.jail.duration)
: globalConfig.jail.duration;

config.jail.strikes = config.jail.strikes || globalConfig.jail.strikes;

// Gossip
config.gossip ||= globalConfig.gossip;

if (Object.getPrototypeOf(config.gossip) !== Object.prototype) {
logger.error("Invalid gossip option");
return process.exit(1);
}

config.gossip.infect =
parseInt(options.infect || "0") ||
config.gossip.infect ||
globalConfig.gossip.infect;

config.gossip.die =
parseInt(options.die || "0") ||
config.gossip.die ||
globalConfig.gossip.die;

if (!config.secretKey && !options.generate) {
logger.error("No secret key supplied");
logger.warn("Run me with --generate to generate a new secret for you");
Expand Down
6 changes: 5 additions & 1 deletion src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Murmur,
} from "./types.js";

export const version = "0.10.6";
export const version = "0.10.7";
export const protocolVersion = "0.10.5";

export const topic = sha(`Kenshi.Unchained.Testnet.Topic.V${protocolVersion}`);
Expand Down Expand Up @@ -38,6 +38,10 @@ export const config: Config = {
duration: minutes(5),
strikes: 5,
},
gossip: {
infect: 24,
die: 8,
},
};

export const rpcMethods: StringAnyObject = {};
Expand Down
13 changes: 8 additions & 5 deletions src/lib/gossip/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gossipMethods, errors, sockets } from "../constants.js";
import { gossipMethods, errors, sockets, config } from "../constants.js";
import { Gossip, GossipRequest, MetaData, NodeSystemError } from "../types.js";
import { brotliCompressSync } from "zlib";
import { hashObject } from "../utils/hash.js";
Expand Down Expand Up @@ -33,6 +33,11 @@ const notSeen = (seen: Set<string>) => (meta: MetaData) =>

const isFree = (meta: MetaData) => !meta.needsDrain;

const randomNodes = (nodes: MetaData[]) =>
randomDistinct(nodes.length, config.gossip.infect).map(
(index) => nodes[index]
);

export const gossip = async (
request: GossipRequest<any, any>
): Promise<void> => {
Expand All @@ -41,7 +46,7 @@ export const gossip = async (
const seen = seenCache.get(payloadHash);
const ttl = ttlCache.get(payloadHash) || 0;

if (ttl > 5) {
if (ttl > config.gossip.die) {
return;
}

Expand All @@ -55,9 +60,7 @@ export const gossip = async (
}

const selected =
nodes.length > 16
? randomDistinct(nodes.length, 16).map((index) => nodes[index])
: nodes;
nodes.length > config.gossip.infect ? randomNodes(nodes) : nodes;

await gossipTo(selected, payload);
ttlCache.set(payloadHash, 1 + ttl);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/plugins/uniswap/uniswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const updateAssetPrice = debounceAsync(
const signer = await db.signer.upsert({
where: { key },
// see https://github.com/prisma/prisma/issues/18883
update: { key },
update: { key, name },
create: { key, name },
select: { id: true },
});
Expand Down
12 changes: 8 additions & 4 deletions src/lib/score/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const upsertCache = cache<number, boolean>(15 * 60 * 1000);
const peerScoreMap = new Map<string, number>();
// TODO: Better share this with the UniSwap plugin (and others)
const keyToIdCache = new Map<string, number>();
const keyToNameCache = new Map<string, string | null>();

export const addOnePoint = (peer: string) => {
const current = peerScoreMap.get(peer) || 0;
Expand Down Expand Up @@ -135,19 +136,22 @@ export const storeSprintScores = async () => {
);

for (const key of Object.keys(sprintScores)) {
if (!keyToIdCache.has(key)) {
const name = signerNames.get(key);
const name = signerNames.get(key);
if (!keyToIdCache.has(key) || keyToNameCache.get(key) !== name) {
const signer = await db.signer.upsert({
where: { key },
// see https://github.com/prisma/prisma/issues/18883
update: { key },
update: { key, name },
create: { key, name },
select: { id: true },
select: { id: true, name: true },
});
keyToIdCache.set(key, signer.id);
keyToNameCache.set(key, signer.name);
}
}

// Update node names

for (const peer of Object.keys(sprintScores)) {
// TODO: We're not doing any verification on this data
const scores = Object.values(sprintScores[peer]);
Expand Down
6 changes: 6 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ interface JailConfig {
strikes: number;
}

interface GossipConfig {
infect: number;
die: number;
}

export interface Config {
name: string;
log: string;
Expand All @@ -38,6 +43,7 @@ export interface Config {
publicKey: string;
peers: PeerConfig;
jail: JailConfig;
gossip: GossipConfig;
}

export interface MetaData {
Expand Down

0 comments on commit 2f971b5

Please sign in to comment.