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

add blocks counter and logs #66

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion app/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ let app: App;
resolve_min_success_count: process.env.NETWORK_MIN_SUCCESS_RESOLVE
? parseInt(process.env.NETWORK_MIN_SUCCESS_RESOLVE)
: undefined,
block_logs_mode: process.env.NETWORK_BLOCK_LOG_MODE === 'true' || process.env.NETWORK_BLOCK_LOG_MODE === '1',
block_logs_mode: process.env.NETWORK_BLOCK_LOGS_MODE === 'true' || process.env.NETWORK_BLOCK_LOGS_MODE === '1',
agents: {
[agentAddress]: agentConfig,
},
Expand Down
9 changes: 2 additions & 7 deletions app/ConfigGetters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,12 @@ export function getDefaultExecutorConfig() {
gas_price_priority_add_gwei: 2,
};
}
//TODO: use AVERAGE_BLOCK_TIME_SECONDS
const maxNewBlockDelayByNetwork = {
gnosis: 10,
sepolia: 20,
mainnet: 20,
};

export function getDefaultNetworkConfig(name) {
const avgBlockTime = AVERAGE_BLOCK_TIME_SECONDS[name];
return {
max_block_delay: 60,
max_new_block_delay: maxNewBlockDelayByNetwork[name] || 10,
max_new_block_delay: avgBlockTime ? avgBlockTime * 3 : 10,
resolve_min_success_count: 3,
block_logs_mode: false,
};
Expand Down
3 changes: 3 additions & 0 deletions app/agents/AbstractAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ export abstract class AbstractAgent implements IAgent {
const keeperConfig = await this.queryKeeperDetails(this.keeperId);
this.myStake = keeperConfig.currentStake;
this.myKeeperIsActive = keeperConfig.isActive;
if (!this.myKeeperIsActive) {
this.clog('warn', `Your keeper(${this.keeperId}) is not active in agent contract(${this.contract.address})`);
}

if (toChecksummedAddress(this.workerSigner.address) !== toChecksummedAddress(keeperConfig.worker)) {
throw this.err(
Expand Down
34 changes: 32 additions & 2 deletions app/services/ContractEventsEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export default class ContractEventsEmitter {
contractByAddress = {};
eventByContractTopic = {};

contractEmitterCount = {};
emitByBlockCount = {};

constructor(_blockLogsMode) {
this.blockLogsMode = _blockLogsMode;
}
Expand All @@ -25,18 +28,38 @@ export default class ContractEventsEmitter {
if (!this.blockLogsMode && !forceEmit) {
return;
}
console.log('emitByBlockLogs logs.length', logs.length);
let address, blockNumber;
logs.forEach(l => {
const address = l.address.toLowerCase();
address = l.address.toLowerCase();
if (!this.contractEmitterByAddress[address]) {
return;
}
if (!this.emitByBlockCount[address]) {
this.emitByBlockCount[address] = {};
}
const eventName = this.eventByContractTopic[address][l.topics[0]];
if (!eventName) {
return;
}
blockNumber = l.blockNumber;
this.emitByBlockCount[address][blockNumber] = (this.emitByBlockCount[address][blockNumber] || 0) + 1;
this.emitByContractAddress(address, eventName, this.contractByAddress[address].parseLog(l));
});
if (blockNumber && address) {
const diff = this.emitByBlockCount[address][blockNumber] - this.contractEmitterCount[address][blockNumber];
console.log(
blockNumber + ' block logs count( query:',
this.emitByBlockCount[address][blockNumber],
'websocket:',
this.contractEmitterCount[address][blockNumber],
')',
);
if (diff != 0) {
console.log(`❗️ ${blockNumber} Block Events Mismatch Error! Diff: ${diff}`);
}
delete this.emitByBlockCount[address][blockNumber];
delete this.contractEmitterCount[address][blockNumber];
}
}

on(contract: ContractWrapper, eventName, callback) {
Expand All @@ -51,6 +74,9 @@ export default class ContractEventsEmitter {
if (!this.eventByContractTopic[address][eventTopic]) {
this.eventByContractTopic[address][eventTopic] = eventName;
contract.on(eventName, value => {
const { blockNumber } = value;
this.contractEmitterCount[address][blockNumber] = (this.contractEmitterCount[address][blockNumber] || 0) + 1;
delete this.contractEmitterCount[address][blockNumber - 1];
if (this.blockLogsMode) {
return;
}
Expand All @@ -61,6 +87,10 @@ export default class ContractEventsEmitter {
}

contractEmitter(contract): EventEmitter {
const address = contract.address.toLowerCase();
if (!this.contractEmitterCount[address]) {
this.contractEmitterCount[address] = {};
}
return {
on: (eventName, callback) => {
this.on(contract, eventName, callback);
Expand Down
Loading