Skip to content

Commit

Permalink
fix prettier errors
Browse files Browse the repository at this point in the history
  • Loading branch information
noandrea committed Mar 6, 2024
1 parent 39e49cb commit 4f2c444
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 156 deletions.
4 changes: 3 additions & 1 deletion src/libs/helpers/state-manipulator/genesis-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export interface StateManipulator {

// Will get executed for each line of the state file during the write phase
// Can decide to remove/keep the original line and also add extra lines
processWrite: (line: StateLine) => { action: Action; extraLines?: StateLine[] } | undefined | void;
processWrite: (
line: StateLine
) => { action: Action; extraLines?: StateLine[] } | undefined | void;
}

export function encodeStorageKey(module, name) {
Expand Down
246 changes: 153 additions & 93 deletions src/tools/auction-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ import "@polkadot/api-augment";
import { getApiFor } from "../utils/networks";
import { getAccountIdentity } from "../utils/monitoring";
import { BN } from "@polkadot/util";
import { isBigInt, isBn, isHex, isNumber, isU8a, u8aConcat, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
import {
isBigInt,
isBn,
isHex,
isNumber,
isU8a,
u8aConcat,
u8aToBn,
u8aToHex,
u8aToU8a,
} from "@polkadot/util";

const argv = yargs(process.argv.slice(2))
.usage("Usage: $0")
Expand All @@ -17,128 +27,178 @@ const argv = yargs(process.argv.slice(2))
demandOption: true,
},
para: {
type: "number",
description: "Para for which a lease exists",
type: "number",
description: "Para for which a lease exists",
},
}).argv;

type AuctionInfo = {
duration: Number;
lease_period: Number
duration: Number;
lease_period: Number;
};

let auctionMap = new Map<BN,AuctionInfo>();
let auctionMap = new Map<BN, AuctionInfo>();

function addSeconds(date, seconds) {
date.setSeconds(date.getSeconds() + seconds);
return date;
date.setSeconds(date.getSeconds() + seconds);
return date;
}

async function calculateTimestamp(api, futureblock: number) {
let currentBlock = (await api.rpc.chain.getHeader()).number.toNumber();
let timestamp = (await api.query.timestamp.now()).toNumber();

let currentBlock = (await api.rpc.chain.getHeader()).number.toNumber();
let timestamp = (await api.query.timestamp.now()).toNumber();
let currentDate = new Date(timestamp);

let currentDate = new Date(timestamp);
let blockDifference = futureblock - currentBlock;

let blockDifference = futureblock - currentBlock;

let futureDate = addSeconds(currentDate, (blockDifference * 6));
return[futureDate.getTime(), futureDate]
let futureDate = addSeconds(currentDate, blockDifference * 6);
return [futureDate.getTime(), futureDate];
}

async function calculateCurrentLeasePeriod(api, leasePeriod, leaseOffset) {

let currentBlock = (await api.rpc.chain.getHeader()).number.toNumber();
return (currentBlock - leaseOffset)/leasePeriod;
let currentBlock = (await api.rpc.chain.getHeader()).number.toNumber();
return (currentBlock - leaseOffset) / leasePeriod;
}

const main = async () => {
const api = await getApiFor(argv);

const scheduled = await api.query.scheduler.agenda.entries();

//scheduled.find()
for (var i in scheduled) {
for(var index in scheduled[i][1]) {
if(scheduled[i][1][index].isSome) {
let call;
if (scheduled[i][1][index].unwrap().call.isLookup) {
let lookup = scheduled[i][1][index].unwrap().call.asLookup;
let callOption = await api.query.preimage.preimageFor([lookup.hash_, lookup.len]);
if(callOption.isSome) {
call = callOption.unwrap();
}
}
else {
call = scheduled[i][1][index].unwrap().call.asInline;
}

let extrinsic = (api.createType("GenericExtrinsicV4", call) as any).toHuman();

if (extrinsic.method.method == "newAuction" && extrinsic.method.section == "auctions") {
let key = scheduled[i][0];
let sliced = key.slice(-4);

auctionMap.set(u8aToBn(sliced, {isLe: true}), {
duration: extrinsic.method.args.duration.replace(/,/g, ''),
lease_period: extrinsic.method.args.lease_period_index
}
)
}
for (var index in scheduled[i][1]) {
if (scheduled[i][1][index].isSome) {
let call;
if (scheduled[i][1][index].unwrap().call.isLookup) {
let lookup = scheduled[i][1][index].unwrap().call.asLookup;
let callOption = await api.query.preimage.preimageFor([lookup.hash_, lookup.len]);
if (callOption.isSome) {
call = callOption.unwrap();
}
} else {
call = scheduled[i][1][index].unwrap().call.asInline;
}
}
}

let sortedKeys = [...auctionMap.keys()].sort();

let currentAuctionIndex = await api.query.auctions.auctionCounter();
let leasePeriod = await api.consts.slots.leasePeriod;
let leaseOffset = await api.consts.slots.leaseOffset;

let endingPeriod = await api.consts.auctions.endingPeriod

let currentLeasePeriod = await calculateCurrentLeasePeriod(api, leasePeriod, leaseOffset)

for(var index in sortedKeys) {
let nextAuction = auctionMap.get(sortedKeys[index])

let [auctionStartTimestamp, auctionStartDate] = await calculateTimestamp(api, Number(sortedKeys[index].toString()))

let slotsLeasedlready = argv.para == undefined ? undefined : await api.query.slots.leases(argv.para);

console.log("Auction number %s will happen at block %s, timestamp %s, date %s", Number(currentAuctionIndex) + Number(index) + Number(1), sortedKeys[index], auctionStartTimestamp, auctionStartDate);
console.log("It will have a duration of %s blocks for lease period %s", nextAuction.duration, nextAuction.lease_period);

let candleBeginBlock = Number(sortedKeys[index].toString()) + Number(nextAuction.duration);

let [candleStartTimestamp, candleStartDate] = await calculateTimestamp(api, candleBeginBlock)

console.log("Candle will happen at block %s, timestamp %s, date %s", candleBeginBlock, candleStartTimestamp, candleStartDate);
let extrinsic = (api.createType("GenericExtrinsicV4", call) as any).toHuman();

let biddingEndBlock = Number(sortedKeys[index].toString()) + Number(nextAuction.duration) + Number(endingPeriod.toString());

let [biddingEndTimestamp, biddingDate] = await calculateTimestamp(api, biddingEndBlock)

console.log("Bidding end will happen at block %s, timestamp %s, date %s", biddingEndBlock, biddingEndTimestamp, biddingDate);

let yourLeaseStartSlot = slotsLeasedlready == undefined ? nextAuction.lease_period: currentLeasePeriod + (slotsLeasedlready as any).length
let leasePeriodPerSlot = await api.consts.auctions.leasePeriodsPerSlot;
let yourLeaseEndSlot = Number(nextAuction.lease_period) + Number(leasePeriodPerSlot.toString()) -1;

let lease_start_block = (new BN(nextAuction.lease_period.toString()).mul(u8aToBn(leasePeriod.toU8a()))).add(u8aToBn(leaseOffset.toU8a()));
let [leaseStartimestamp, leaseStartDate] = await calculateTimestamp(api, Number(lease_start_block.toString()))

console.log("The new lease will start at block %s, timestamp %s, date %s", lease_start_block.toString(), leaseStartimestamp, leaseStartDate);

let lease_end_block = Number(lease_start_block.toString()) + (Number(leasePeriodPerSlot.toString())* Number(leasePeriod.toString()));
let [leaseEndtimestamp, leaseEndDate] = await calculateTimestamp(api, Number(lease_end_block.toString()))

console.log("The new lease will end at block %s, timestamp %s, date %s", lease_end_block.toString(), leaseEndtimestamp, leaseEndDate);

console.log("For this auction you need to bid from %s to %s", Math.floor(yourLeaseStartSlot).toString(), yourLeaseEndSlot.toString());
if (extrinsic.method.method == "newAuction" && extrinsic.method.section == "auctions") {
let key = scheduled[i][0];
let sliced = key.slice(-4);

auctionMap.set(u8aToBn(sliced, { isLe: true }), {
duration: extrinsic.method.args.duration.replace(/,/g, ""),
lease_period: extrinsic.method.args.lease_period_index,
});
}
}
}
}

let sortedKeys = [...auctionMap.keys()].sort();

let currentAuctionIndex = await api.query.auctions.auctionCounter();
let leasePeriod = await api.consts.slots.leasePeriod;
let leaseOffset = await api.consts.slots.leaseOffset;

let endingPeriod = await api.consts.auctions.endingPeriod;

let currentLeasePeriod = await calculateCurrentLeasePeriod(api, leasePeriod, leaseOffset);

for (var index in sortedKeys) {
let nextAuction = auctionMap.get(sortedKeys[index]);

let [auctionStartTimestamp, auctionStartDate] = await calculateTimestamp(
api,
Number(sortedKeys[index].toString())
);

let slotsLeasedlready =
argv.para == undefined ? undefined : await api.query.slots.leases(argv.para);

console.log(
"Auction number %s will happen at block %s, timestamp %s, date %s",
Number(currentAuctionIndex) + Number(index) + Number(1),
sortedKeys[index],
auctionStartTimestamp,
auctionStartDate
);
console.log(
"It will have a duration of %s blocks for lease period %s",
nextAuction.duration,
nextAuction.lease_period
);

let candleBeginBlock = Number(sortedKeys[index].toString()) + Number(nextAuction.duration);

let [candleStartTimestamp, candleStartDate] = await calculateTimestamp(api, candleBeginBlock);

console.log(
"Candle will happen at block %s, timestamp %s, date %s",
candleBeginBlock,
candleStartTimestamp,
candleStartDate
);

let biddingEndBlock =
Number(sortedKeys[index].toString()) +
Number(nextAuction.duration) +
Number(endingPeriod.toString());

let [biddingEndTimestamp, biddingDate] = await calculateTimestamp(api, biddingEndBlock);

console.log(
"Bidding end will happen at block %s, timestamp %s, date %s",
biddingEndBlock,
biddingEndTimestamp,
biddingDate
);

let yourLeaseStartSlot =
slotsLeasedlready == undefined
? nextAuction.lease_period
: currentLeasePeriod + (slotsLeasedlready as any).length;
let leasePeriodPerSlot = await api.consts.auctions.leasePeriodsPerSlot;
let yourLeaseEndSlot =
Number(nextAuction.lease_period) + Number(leasePeriodPerSlot.toString()) - 1;

let lease_start_block = new BN(nextAuction.lease_period.toString())
.mul(u8aToBn(leasePeriod.toU8a()))
.add(u8aToBn(leaseOffset.toU8a()));
let [leaseStartimestamp, leaseStartDate] = await calculateTimestamp(
api,
Number(lease_start_block.toString())
);

console.log(
"The new lease will start at block %s, timestamp %s, date %s",
lease_start_block.toString(),
leaseStartimestamp,
leaseStartDate
);

let lease_end_block =
Number(lease_start_block.toString()) +
Number(leasePeriodPerSlot.toString()) * Number(leasePeriod.toString());
let [leaseEndtimestamp, leaseEndDate] = await calculateTimestamp(
api,
Number(lease_end_block.toString())
);

console.log(
"The new lease will end at block %s, timestamp %s, date %s",
lease_end_block.toString(),
leaseEndtimestamp,
leaseEndDate
);

console.log(
"For this auction you need to bid from %s to %s",
Math.floor(yourLeaseStartSlot).toString(),
yourLeaseEndSlot.toString()
);
}

await api.disconnect();
};
Expand Down
3 changes: 2 additions & 1 deletion src/tools/export-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ async function main() {
if (line.key.startsWith(storagePrefix)) {
storageCount[storagePrefix] += 1n;
storageKeySize[storagePrefix] = line.key.length / 2 - 1;
storageSize[storagePrefix] += BigInt(line.key.length / 2) + BigInt(line.value.length / 2) - 2n;
storageSize[storagePrefix] +=
BigInt(line.key.length / 2) + BigInt(line.value.length / 2) - 2n;
}
}
}
Expand Down
39 changes: 24 additions & 15 deletions src/tools/run-moonbeam-fork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,15 @@ const main = async () => {
],
],
];
if (relayChainSpec.genesis?.runtimeGenesis && "patch" in relayChainSpec.genesis?.runtimeGenesis) {
if (
relayChainSpec.genesis?.runtimeGenesis &&
"patch" in relayChainSpec.genesis?.runtimeGenesis
) {
relayChainSpec.genesis.runtimeGenesis.patch.paras = paras;
} else if (relayChainSpec.genesis?.runtime && "runtime_genesis_config" in relayChainSpec.genesis?.runtime) {
} else if (
relayChainSpec.genesis?.runtime &&
"runtime_genesis_config" in relayChainSpec.genesis?.runtime
) {
relayChainSpec.genesis.runtime.runtime_genesis_config.paras = paras;
} else if (relayChainSpec.genesis?.runtime && "paras" in relayChainSpec.genesis?.runtime) {
relayChainSpec.genesis.runtime.paras = paras;
Expand Down Expand Up @@ -495,12 +501,7 @@ const main = async () => {
let bobLogHandler: fs.FileHandle;

// these params are used by both relay and parachain nodes
const commonParams = [
"--database paritydb",
"--rpc-cors all",
"--no-private-ipv4",
"--no-mdns",
]
const commonParams = ["--database paritydb", "--rpc-cors all", "--no-private-ipv4", "--no-mdns"];

if (!argv.dev) {
process.stdout.write(`\t - ${chalk.yellow(`Starting`)} relay nodes...\n`);
Expand All @@ -521,7 +522,9 @@ const main = async () => {
`--node-key ${Object.keys(NODE_KEYS)[0]}`,
"--validator",
];
aliceProcess = await spawnTask(`${polkadotBinaryPath} ${commonParams.join(" ")} ${aliceValidatorParams.join(" ")}`);
aliceProcess = await spawnTask(
`${polkadotBinaryPath} ${commonParams.join(" ")} ${aliceValidatorParams.join(" ")}`
);
process.stdout.write(` ✓\n`);
process.stdout.write(`\t\t - ${chalk.green(`Starting`)} Bob node...\n`);
const bobFolder = path.join(baseDataFolder, `relay-bob`);
Expand All @@ -539,7 +542,9 @@ const main = async () => {
`--node-key ${Object.keys(NODE_KEYS)[1]}`,
"--validator",
];
bobProcess = await spawnTask(`${polkadotBinaryPath} ${commonParams.join(" ")} ${bobValidatorParams.join(" ")}`);
bobProcess = await spawnTask(
`${polkadotBinaryPath} ${commonParams.join(" ")} ${bobValidatorParams.join(" ")}`
);
process.stdout.write(` ✓\n`);
}

Expand All @@ -566,21 +571,25 @@ const main = async () => {
"--no-hardware-benchmarks",
"--no-prometheus",
"--no-telemetry",
`--sealing=${argv.sealing}`
]
`--sealing=${argv.sealing}`,
];
const noDevParams = [
"--", // TODO is this needed?
"--", // TODO is this needed?
`--chain ${relayRawSpecFile}`,
"--rpc-port 12003",
"--port 10003",
`--node-key ${Object.keys(NODE_KEYS)[2]}`,
];
const alithProcess = argv.dev
? await spawnTask(
`${moonbeamBinaryPath} ${logs} ${commonParams.join(" ")} ${alithCollatorBaseParams.join(" ")} ${devParams.join(" ")}`
`${moonbeamBinaryPath} ${logs} ${commonParams.join(" ")} ${alithCollatorBaseParams.join(
" "
)} ${devParams.join(" ")}`
)
: await spawnTask(
`${moonbeamBinaryPath} ${logs} ${commonParams.join(" ")} ${alithCollatorBaseParams.join(" ")} ${noDevParams.join(" ")}`
`${moonbeamBinaryPath} ${logs} ${commonParams.join(" ")} ${alithCollatorBaseParams.join(
" "
)} ${noDevParams.join(" ")}`
);
process.stdout.write(` ✓\n`);

Expand Down
Loading

0 comments on commit 4f2c444

Please sign in to comment.