vechain / vechain-sdk-js

The official JavaScript SDK for VeChain.
19 stars 6 forks source link

🐛 [BUG] - RPC: Potential Bug in transaction formatter: `logIndex` can not be bigger than first clause event count #1018

Open ifavo opened 4 days ago

ifavo commented 4 days ago

Description

The logIndex in the transaction conversion can only be as big as the first clause number of events. I don't have an example transaction to show this situation but from the code it seems there is a bug in the conversion

This issue relates to this part: https://github.com/vechain/vechain-sdk-js/blob/10787514e01b3b8bfb7bd3256e47ad27401382c3/packages/network/src/provider/utils/formatter/transactions/formatter.ts#L149-L171

When looking at these two lines:

 const n = receipt.outputs.length > 0 ? receipt.outputs[0].events.length : 0; 
 const filledLogIndexes = new Array<number>(n) 

The list of potential indexes is equal to the number of events in the first clause output.

Reproduction URL

No response

Reproduction steps

Given this imaginary transaction:

Then:

A different approach & solution can be:

 const logs: TransactionReceiptLogsRPC[] = []; 
 let logIndex = logIndexOffset;
 receipt.outputs.forEach((output) => { 
     output.events.forEach((event, index) => { 
         logs.push({ 
             blockHash: receipt.meta.blockID, 
             blockNumber: Quantity.of(receipt.meta.blockNumber), 
             transactionHash: receipt.meta.txID as string, 
             address: event.address, 
             topics: event.topics.map((topic) => topic), 
             data: event.data, 
             removed: false, 
             transactionIndex: Quantity.of(transactionIndex), 

             // convert to hex on-demand
             logIndex: Quantity.of(logIndex)
         }); 

        // increase logIndex after each event
        logIndex++
     }); 
 });

Screenshots

![DESCRIPTION](LINK.png)

Logs

No response

OS

No response