Skip to content

Enhance performance of View endorsement chain #923

@satz07

Description

@satz07

Issue:
View endorsement chain option while verifying document is getting timed out or resulting in RPC errors

Issue Description:
When verifying a document and attempting to view the endorsement chain on the TradeTrust platform, the process often results in 'time out' or 'block range too wide' errors. This issue arises from an eth_getLogs RPC call that queries the blockchain (archive node) from the genesis block to the latest block. Due to the extensive block range, the process is resource-intensive and prone to errors.

Steps to Reproduce:
Create a document on the TradeTrust platform (e.g., eBoL) at TradeTrust Document Creator.
Verify the document on the TradeTrust platform at TradeTrust Verify.
Once the document is verified, click on the "View endorsement chain" option.
Alternatively, replicate the issue using the following command:

curl -X POST -H "Content-Type: application/json" --data '{
  "method":"eth_getLogs",
  "params":[{
    "fromBlock":"0x0",
    "toBlock":"latest",
    "address":"0xYourContractAddress",
    "topics":[
      "0xYourEventSignature",
      null,
      null,
      "0xYourDocumentHash"
    ]
  }],
  "id":59,
  "jsonrpc":"2.0"
}' <RPC_URL>

Suggested Enhancements:
To optimize the process and effectively fetch the data, it is recommended to implement an events mechanism in the smart contract functions. Logging events when actions such as issuing a document, transferring ownership, or changing ownership occur will allow for more efficient data retrieval using filter log

Example Enhancement:
Add events to the smart contract and log them during relevant actions. Below is an example of how to add and use events in a smart contract:

Smart Contract Example:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract DocumentManagement {
    event DocumentIssued(address indexed issuer, uint256 indexed documentId, string documentHash);
    event OwnershipTransferred(address indexed from, address indexed to, uint256 indexed documentId);

    function issueDocument(uint256 documentId, string memory documentHash) public {
        // Issue document logic...
        emit DocumentIssued(msg.sender, documentId, documentHash);
    }

    function transferOwnership(address to, uint256 documentId) public {
        // Transfer ownership logic...
        emit OwnershipTransferred(msg.sender, to, documentId);
    }
}

Query Logs with Filters:

async function getDocumentEvents(documentId) {
    const filter = {
        address: contractAddress,
        topics: [
            [documentIssuedEventSignature, ownershipTransferredEventSignature],
            null,
            ethers.utils.hexZeroPad(ethers.BigNumber.from(documentId).toHexString(), 32)
        ],
        fromBlock: "earliest",
        toBlock: "latest"
    };
    const logs = await provider.getLogs(filter);
    logs.forEach(log => {
        const parsedLog = contract.interface.parseLog(log);
        console.log(parsedLog);
    });
}

Related Pull Request:
A pull request was made to make the fromBlockNumber configurable for each chain, but implementing the event mechanism described above would provide a more optimized solution:
PR #921

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions