Skip to content

Commit

Permalink
put logs as property of block
Browse files Browse the repository at this point in the history
  • Loading branch information
taokayan committed Nov 15, 2023
1 parent f1c4e5c commit a4db0e1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
28 changes: 10 additions & 18 deletions peripherals/eos-evm-ws-proxy/block-monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class BlockMonitor extends EventEmitter {
return this.reversible_blocks.peekBack();
}

append_new_block(block, logs) {
append_new_block(block) {
this.reversible_blocks.add(block);
this.emit('block_appended', {block, logs});
this.emit('block_appended', {block});
}

async getBlockWithLogs(number_) {
Expand All @@ -58,52 +58,44 @@ class BlockMonitor extends EventEmitter {
const block = results.data[0].result;
const logs = results.data[1].result;

block.logs = logs;
//console.log("RPC batch result:" + JSON.stringify(block));
return {block, logs};
return block;
}

async poll() {
let res = null;
let next_block = null;
let next_logs = null;
try {
// need to be conservative, sometimes getLogs return empty result for head block
let head_block = await this.web3.eth.getBlock("latest", true);
let max_block_num = Number(head_block.number) - 1;

let last = this.reversible_blocks.peekBack();
if( last == undefined || last == null) {
res = await this.getBlockWithLogs(max_block_num);
last = res.block;
if( last == undefined ) {
last = await this.getBlockWithLogs(max_block_num);
if (last != null) {
this.append_new_block(last, res.logs);
this.append_new_block(last);
}
}

if (last != null && Number(last.number) + 1 < max_block_num) {
res = await this.getBlockWithLogs(Number(last.number) + 1);
next_block = res.block;
next_logs = res.logs;
next_block = await this.getBlockWithLogs(Number(last.number) + 1);
} else {
next_block = null;
next_logs = null;
}

let found_next_block = false;

while(last != null && next_block != null) {
found_next_block = true;
if(next_block.parentHash == last.hash) {
this.append_new_block(next_block, next_logs);
this.append_new_block(next_block);
last = next_block;

if (Number(last.number) + 1 < max_block_num) {
res = await this.getBlockWithLogs(Number(last.number) + 1);
next_block = res.block;
next_logs = res.logs;
next_block = await this.getBlockWithLogs(Number(last.number) + 1);
} else {
next_block = null;
next_logs = null;
}

} else {
Expand Down
14 changes: 7 additions & 7 deletions peripherals/eos-evm-ws-proxy/subscription-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class SubscriptionServer extends EventEmitter {
await this.handle_block_forked({block});
});

this.block_monitor.on('block_appended', async ({block, logs}) => {
await this.handle_block_appended({block, logs});
this.block_monitor.on('block_appended', async ({block}) => {
await this.handle_block_appended({block});
});
}

Expand Down Expand Up @@ -140,12 +140,12 @@ class SubscriptionServer extends EventEmitter {
client.ws.send(JSON.stringify(msg, bigint_replacer));
}

async process_logs_subscriptions(block, logs) {
async process_logs_subscriptions(block) {
// Process all `logs` subscriptions
// Get logs from the recently appended block
if(this.logs_subs.size > 0) {
this.logger.debug("LOG => ", JSON.stringify(logs, bigint_replacer));
for(const log of logs) {
this.logger.debug("LOG => ", JSON.stringify(block.logs, bigint_replacer));
for(const log of block.logs) {
for(const [subid, client] of this.logs_subs) {
if(this.logs_filter_match(client.filter, log)) {
this.send_logs_response_and_save(block, client, subid, log);
Expand All @@ -170,10 +170,10 @@ class SubscriptionServer extends EventEmitter {
}
}

async handle_block_appended({block, logs}) {
async handle_block_appended({block}) {
this.logger.debug(`handle_block_appended: ${block.number}`);
await this.process_new_heads_subscriptions(block);
await this.process_logs_subscriptions(block, logs);
await this.process_logs_subscriptions(block);
await this.process_mined_transactions_subscriptions(block, false);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/nodeos_eos_evm_ws_test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,8 @@ def makeReservedEvmAddress(account):
time.sleep(0.5)
recevied_msg=ws.recv()
res=json.loads(recevied_msg)
if block_count == 0:
Utils.Print("recevied block message from websocket:" + recevied_msg)
block_json=res["params"]["result"]
num=block_json["number"] # number can be decimal or hex (with 0x prefix)
hash=block_json["hash"]
Expand Down

0 comments on commit a4db0e1

Please sign in to comment.