BuildOnViction / victionchain

The Efficient Blockchain Powered By Proof Of Stake Voting Consensus
https://viction.xyz
GNU Lesser General Public License v3.0
168 stars 85 forks source link

Wrong blockhash in event logs from websocket and RPC API #429

Closed trinhdn2 closed 10 months ago

trinhdn2 commented 10 months ago

When we're trying to get a transaction receipt via eth_getTransactionReceipt API, for example

curl --location 'https://rpc.viction.xyz' \
--header 'Content-Type: application/json' \
--data '{
    "jsonrpc": "2.0",
    "method": "eth_getTransactionReceipt",
    "params": [
        "0xf71fb3d6aaa44a631f3b8af92571368f80b7e211a596ac70ddfbc8de197da38e"
    ],
    "id": 1
}'

The blockHash is correct in the transaction object but in the logs it is incorrect

{
  blockHash: "0x43b965242118e31b99a4d14a2427998088676ee03516b07dcfee0add40175355",
  blockNumber: 73736810n,
  contractAddress: null,
  cumulativeGasUsed: 1197392n,
  from: "0x0c4fab8d9dbe774708eec313bf0295278e307bcd",
  gasUsed: 691755n,
  logs: [
    {
      address: "0x69b678c8e02a23cc8f3b33cef339424245841439",
      topics: [ "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
        "0x0000000000000000000000000c4fab8d9dbe774708eec313bf0295278e307bcd", "0x0000000000000000000000005d3513eb3f889c8451bb8a1a02c23affd0ca64be"
      ],
      data: "0x0000000000000000000000000000000000000000a18effcb3ac9408644400000",
      blockNumber: 73736810n,
      transactionHash: "0xf71fb3d6aaa44a631f3b8af92571368f80b7e211a596ac70ddfbc8de197da38e",
      transactionIndex: 3,
      blockHash: "0x35a97101d4babf9b1a0299384d1ab03b2ff8ee6087017270879e3be9098ee10a",
      logIndex: 10,
      removed: false
    },
    ...
  ]
}

In the above extract, the blockHash that is a member of transaction is correct, but in the logs the blockHash is incorrect, although the blockNumber is correct. The same problem appears when subscibing websocket for new emitted event logs. There are many transaction receipts are affected from this issue.

trinhdn2 commented 10 months ago

Getting receipts via RPC API has been fixed by the merged PR https://github.com/BuildOnViction/tomochain/pull/422. Now whenever we retrieve a receipt, the node will execute the types.DeriveFields function to overwrite the correct blockHash in event logs and many other fields before returning the correct receipt to clients.

trinhdn2 commented 10 months ago

Getting receipts via RPC API can be fixed by the PR https://github.com/BuildOnViction/tomochain/pull/425 by correcting the blockHash in logs of ResultProcessBlock without affecting the consensus. This logs field contains event logs for websocket to fire to its subscribers.

type ResultProcessBlock struct {
    logs         []*types.Log
    receipts     []*types.Receipt
    state        *state.StateDB
    tradingState *tradingstate.TradingStateDB
    lendingState *lendingstate.LendingStateDB
    proctime     time.Duration
    usedGas      uint64
}