Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update target address cache_contested_tasks #27

Open
wants to merge 1 commit into
base: miner-stage
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 63 additions & 32 deletions miner/src/cache_contested_tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ethers } from "ethers";
import { initializeLogger, log } from "./log";
import { initializeMiningConfig, c } from "./mc";
import { initializeBlockchain, wallet, arbius } from "./blockchain";
import EngineABI from "./artifacts/contracts/EngineV2.sol/EngineV2.json";
import config from "./config.json";

const maxBlocks = 10_000;

Expand All @@ -18,41 +20,52 @@ type ContestationVote = {
yea: boolean;
}

const getLogs = async (startBlock: number, endBlock: number) => {
const getLogs = async (
arbiusContract: any,
targetAddress: string,
startBlock: number,
endBlock: number
) => {
const contestations: Contestation[] = [];
const contestationVotes: ContestationVote[] = [];

let fromBlock = startBlock;
let toBlock = endBlock - fromBlock + 1 > maxBlocks ? fromBlock + maxBlocks - 1 : endBlock;
let toBlock =
endBlock - fromBlock + 1 > maxBlocks ? fromBlock + maxBlocks - 1 : endBlock;

while (toBlock <= endBlock) {
log.debug(`Processing block [${fromBlock.toString()} to ${toBlock.toString()}]`);
log.debug(
`Processing block [${fromBlock.toString()} to ${toBlock.toString()}]`
);

const events = await arbius.provider.getLogs({
address: arbius.address,
topics: [
[
arbius.interface.getEventTopic("ContestationSubmitted"),
arbius.interface.getEventTopic("ContestationVote"),
arbius.interface.getEventTopic('ContestationSubmitted'),
arbius.interface.getEventTopic('ContestationVote'),
],
],
fromBlock,
toBlock,
});

events.map((event) => {
events.map(async (event) => {
const parsedLog = arbius.interface.parseLog(event);
switch (parsedLog.name) {
case "ContestationSubmitted":
log.debug(`Found contestation submitted: ${parsedLog.args.task}`);
contestations.push({
address: parsedLog.args.addr,
task: parsedLog.args.task,
fromBlock,
toBlock,
});
case 'ContestationSubmitted':
const taskData = await arbiusContract.tasks(parsedLog.args.task);
if (taskData[2] == targetAddress) {
log.debug(`Found contestation submitted: ${parsedLog.args.task}`);
contestations.push({
address: parsedLog.args.addr,
task: parsedLog.args.task,
fromBlock,
toBlock,
});
}
break;
case "ContestationVote":
case 'ContestationVote':
log.debug(`Found contestation vote: ${parsedLog.args.task}`);
contestationVotes.push({
address: parsedLog.args.addr,
Expand All @@ -67,7 +80,10 @@ const getLogs = async (startBlock: number, endBlock: number) => {

if (toBlock === endBlock) break;
fromBlock = toBlock + 1;
toBlock = endBlock - fromBlock + 1 > maxBlocks ? fromBlock + maxBlocks - 1 : endBlock;
toBlock =
endBlock - fromBlock + 1 > maxBlocks
? fromBlock + maxBlocks - 1
: endBlock;
}

return {
Expand All @@ -76,9 +92,14 @@ const getLogs = async (startBlock: number, endBlock: number) => {
};
};

async function main(configPath: string, startBlock?: string, endBlock?: string) {
async function main(
configPath: string,
targetAddress: string,
startBlock?: string,
endBlock?: string,
) {
try {
const mconf = JSON.parse(readFileSync(configPath, "utf8"));
const mconf = JSON.parse(readFileSync(configPath, 'utf8'));
initializeMiningConfig(mconf);
} catch (e) {
console.error(`unable to parse ${configPath}`);
Expand All @@ -88,27 +109,37 @@ async function main(configPath: string, startBlock?: string, endBlock?: string)
initializeLogger(null);

await initializeBlockchain();

if (! startBlock) {
const rpc = JSON.parse(JSON.stringify(arbius.provider))?.connection?.url;
const provider = new ethers.providers.JsonRpcProvider(rpc);
const arbiusContract = new ethers.Contract(
config.v2_engineAddress,
EngineABI.abi,
provider
);
if (!startBlock) {
startBlock = '51380392';
}
if (! endBlock) {
endBlock = ""+(await wallet.provider.getBlockNumber());
if (!endBlock) {
endBlock = '' + (await wallet.provider.getBlockNumber());
}
const {
contestations,
contestationVotes,

}= await getLogs(Number(startBlock), Number(endBlock));
const { contestations, contestationVotes } = await getLogs(
arbiusContract,
targetAddress,
Number(startBlock),
Number(endBlock)
);

log.debug(`${contestations.length} contested tasks found}`);
writeFileSync("contestations.json", JSON.stringify(contestations, null, 2));
writeFileSync("contestationVotes.json", JSON.stringify(contestationVotes, null, 2));
writeFileSync('contestations.json', JSON.stringify(contestations, null, 2));
writeFileSync(
'contestationVotes.json',
JSON.stringify(contestationVotes, null, 2)
);
}

if (process.argv.length < 3) {
log.error("usage: yarn scan:contested MiningConfig.json [startBlock] [endBlock]");
if (process.argv.length < 4) {
log.error("usage: yarn scan:contested MiningConfig.json <targetAddress> [startBlock] [endBlock]");
process.exit(1);
}

main(process.argv[2], process.argv[3], process.argv[4]);
main(process.argv[2], process.argv[3], process.argv[4], process.argv[5]);