Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
noandrea committed May 27, 2024
1 parent 95c880c commit d710815
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 49 deletions.
39 changes: 19 additions & 20 deletions src/tools/block-eip1559-sim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ const main = async () => {

// collect block fullness data
let bfData = {
max:0,
maxBlock:0,
min:100,
minBlock:0,
sum:0,
max: 0,
maxBlock: 0,
min: 100,
minBlock: 0,
sum: 0,
values: [],
}
};

let txData = {
max: 0,
Expand All @@ -84,13 +84,12 @@ const main = async () => {
minBlock: 0,
sum: 0,
txPerBlock: [],
}

};

type FeeData = {
t: number; // target block fullness
f: number; // baseFee
}
f: number; // baseFee
};
type BlockData = {
h: number; // block number (height)
d: Array<FeeData>; // fees data
Expand All @@ -101,37 +100,37 @@ const main = async () => {
return;
}

let blocks:BlockData[] = [];
let blocks: BlockData[] = [];
for (let i = fromBlockNumber; i <= toBlockNumber; i++) {
blocks.push({ h: i, d: []});
blocks.push({ h: i, d: [] });
}

const minBaseFee = 1;
const maxBaseFee = 10e75;

let currentFees: FeeData[] = argv.target.map((t:number) => ({t:t, f: minBaseFee}));
let currentFees: FeeData[] = argv.target.map((t: number) => ({ t: t, f: minBaseFee }));
await promiseConcurrent(
20,
async (block: BlockData, i:number) => {
async (block: BlockData, i: number) => {
const blockDetails = await api.rpc.chain
.getBlockHash(block.h)
.then((blockHash) => getBlockDetails(api, blockHash));

currentFees = currentFees.map((feeData: FeeData) => {
const eip1559 = 1 + ((blockDetails.weightPercentage - feeData.t)/feeData.t) * (1/8);
const eip1559 = 1 + ((blockDetails.weightPercentage - feeData.t) / feeData.t) * (1 / 8);
const newFee = Math.min(Math.max(feeData.f * eip1559, minBaseFee), maxBaseFee);
return {t: feeData.t, f: newFee};
return { t: feeData.t, f: newFee };
});
blocks[i].d = currentFees;
},
blocks,
);

// write to file
const fs = require('fs');
const path = require('path');
const fs = require("fs");
const path = require("path");
const fileName = `block-eip1559-sim-${fromBlockNumber}-${toBlockNumber}.json`;
const filePath = path.join(__dirname,"..","..", "notebooks", "data", fileName);
const filePath = path.join(__dirname, "..", "..", "notebooks", "data", fileName);
fs.writeFileSync(filePath, JSON.stringify(blocks, null, 2));

console.log(`output written to ${filePath}`);
Expand Down
61 changes: 32 additions & 29 deletions src/tools/block-fullness-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ const main = async () => {

// collect block fullness data
let bfData = {
max:0,
maxBlock:0,
min:100,
minBlock:0,
sum:0,
max: 0,
maxBlock: 0,
min: 100,
minBlock: 0,
sum: 0,
values: [],
}
};

let txData = {
max: 0,
Expand All @@ -79,31 +79,30 @@ const main = async () => {
minBlock: 0,
sum: 0,
txPerBlock: [],
}
};

type BlockData = {
number: number;
fill: number;
};

let blocks:BlockData[] = [];
let blocks: BlockData[] = [];
for (let i = fromBlockNumber; i <= toBlockNumber; i++) {
blocks.push({ number: i, fill: 0 });
}

await promiseConcurrent(
20,
async (block: BlockData, i:number) => {
async (block: BlockData, i: number) => {
const blockHash = await api.rpc.chain.getBlockHash(block.number);
const records = await api.query.system.events.at(blockHash);


const blockDetails = await api.rpc.chain
.getBlockHash(block.number)
.then((blockHash) => getBlockDetails(api, blockHash));

blocks[i].fill = blockDetails.weightPercentage;

if (blockDetails.weightPercentage > bfData.max) {
bfData.max = blockDetails.weightPercentage;
bfData.maxBlock = block.number;
Expand All @@ -115,7 +114,6 @@ const main = async () => {
bfData.sum += blockDetails.weightPercentage;
bfData.values.push(blockDetails.weightPercentage);


if (blockDetails.txWithEvents.length > txData.max) {
txData.max = blockDetails.txWithEvents.length;
txData.maxBlock = block.number;
Expand All @@ -130,43 +128,48 @@ const main = async () => {
blocks,
);


await api.disconnect();

console.log(`Total blocks: ${toBlockNumber - fromBlockNumber}`);
console.log(`Block Fullness Max ${bfData.maxBlock}: ${bfData.max} (whole block: ${(bfData.max+25).toFixed(2)})`);
console.log(`Block Fullness Min ${bfData.minBlock}: ${bfData.min} (whole block: ${(bfData.min+25).toFixed(2)})`);
console.log(
`Block Fullness Max ${bfData.maxBlock}: ${bfData.max} (whole block: ${(bfData.max + 25).toFixed(2)})`,
);
console.log(
`Block Fullness Min ${bfData.minBlock}: ${bfData.min} (whole block: ${(bfData.min + 25).toFixed(2)})`,
);
console.log(`Block Fullness Avg: ${(bfData.sum / blocks.length).toFixed(2)}`);

const percentiles = [90, 80, 70, 60, 50, 45, 40, 37.5, 35, 30, 20, 10];
for (let i of percentiles){
const full75 = percentile(i, bfData.values)

for (let i of percentiles) {
const full75 = percentile(i, bfData.values);
const full = full75 + 25;
console.log(`Block Fullness ${i}perc: ${full75.toFixed(2)} (whole block: ${full.toFixed(2)})`);
}

// simulate EIP1559
console.log(`------- Simulated EIP1559 -------`)
let sim = percentiles.reduce((p, i) => ({...p, [i]:1}), {});
console.log(`------- Simulated EIP1559 -------`);
let sim = percentiles.reduce((p, i) => ({ ...p, [i]: 1 }), {});
let targetFullness = percentiles.map((i) => ({
p: i,
target: percentile(i, bfData.values)
p: i,
target: percentile(i, bfData.values),
}));

blocks.forEach((block) => {
targetFullness.forEach(({p, target}) => {
const eip1559 = 1 + (block.fill - target)/target * (1/8);
targetFullness.forEach(({ p, target }) => {
const eip1559 = 1 + ((block.fill - target) / target) * (1 / 8);
sim[p] = sim[p] * eip1559;
// sim[p] = Math.max(sim[p], eip1559);
});
});

Object.entries(sim).reverse().forEach(([p, price]) => {
console.log(`Simulated ${p}perc, ${percentile(p, bfData.values).toFixed(2)}%: ${price}`);
});
Object.entries(sim)
.reverse()
.forEach(([p, price]) => {
console.log(`Simulated ${p}perc, ${percentile(p, bfData.values).toFixed(2)}%: ${price}`);
});

console.log(`=========== Tx stats ===========`)
console.log(`=========== Tx stats ===========`);
console.log(`Total tx: ${txData.sum}`);
console.log(`Max TXs in a block: ${txData.max} in block ${txData.maxBlock}`);
console.log(`Min TXs in a block: ${txData.min} in block ${txData.minBlock}`);
Expand Down

0 comments on commit d710815

Please sign in to comment.