Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #553 from godwokenrises/fix-log-index
Browse files Browse the repository at this point in the history
Fix log index
  • Loading branch information
RetricSu authored Oct 18, 2022
2 parents 29fd632 + d437f83 commit 3ba3346
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/indexer/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ impl Web3Indexer {

let web3_logs = {
let mut logs: Vec<Web3Log> = vec![];
// log_index is a log's index in block, not transaction, should update later.
let mut log_index = 0;
for log_item in log_item_vec {
let log = parse_log(&log_item, &gw_tx_hash)?;
Expand Down Expand Up @@ -465,6 +466,7 @@ impl Web3Indexer {
let mut pg_tx = pool.begin().await?;

let mut tx_index_cursor: u32 = 0;
let mut log_index_cursor: u32 = 0;

let mut cumulative_gas_used: u128 = 0;
let mut total_gas_limit: u128 = 0;
Expand All @@ -484,18 +486,21 @@ impl Web3Indexer {
let transaction_index = tx_index as u32 + tx_index_cursor;
tx.tx.transaction_index = transaction_index;
// update log.transaction_index too
// Update tx.log.index
tx.logs = tx
.logs
.into_iter()
.map(|mut log| {
log.transaction_index = transaction_index;
log.log_index += log_index_cursor;
log
})
.collect();
cumulative_gas_used += tx.tx.gas_used;
tx.tx.cumulative_gas_used = cumulative_gas_used;

total_gas_limit += tx.tx.gas_limit;
log_index_cursor += tx.logs.len() as u32;

tx
})
Expand Down
10 changes: 10 additions & 0 deletions packages/api-server/migrations/20221018014620_fix_log_index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Knex } from "knex";

// Fix for wrong logs.log_index
export async function up(knex: Knex): Promise<void> {
await knex.raw(
"with cte as(select block_number, id, log_index, row_number() over (partition by block_number order by id) rn from logs) update logs set log_index=cte.rn - 1 from cte where logs.block_number=cte.block_number and logs.id=cte.id;"
);
}

export async function down(knex: Knex): Promise<void> {}

0 comments on commit 3ba3346

Please sign in to comment.