cosmos / cosmjs

The Swiss Army knife to power JavaScript based client solutions ranging from Web apps/explorers over browser extensions to server-side clients like faucets/scrapers.
https://cosmos.github.io/cosmjs/
Apache License 2.0
636 stars 324 forks source link

Failed to get block time #1578

Closed ljunb closed 2 months ago

ljunb commented 3 months ago

Hey, I am using the cosmjs to connect to the Nibiru chain, and there is a transaction histories page in our app. When the list data is loaded successfully, I will request the block information of each transaction in order to get the timestamp. The following is a sample code:

async getTxHistories(): Promise<TransactionHistoryItem[]> {
  const originalTxs = await this.getOriginalTxHistories()
  return await Promise.all(
    originalTxs.map(async (transaction) => {
      return await this.parsedCosmosTxInfo(transaction);
    })
  );
}

async parsedCosmosTxInfo(transaction: TxResponse): Promise<TransactionHistoryItem> {
  const timestamp = await this.aCosmosSDK.getBlockTime(transaction.height);
  // ...
}

async getBlockTime(height: number): Promise<number> {
  const cometClient = await this.getCometClient();
  try {
    const blockRes = await cometClient.block(height);
    return blockRes.block.header.time.getTime();
  } catch (e) {
    console.warn("cometClient.block error: ", e);
    return 0;
  }
}

Everything works fine when connecting to other chains, but I got the following error message in the terminal when connecting to Nibiru:

 WARN  cometClient.block error:  [Error: {"code":-32603,"message":"Internal error","data":"height 4325885 is not available, lowest height is 4455515"}]
 WARN  cometClient.block error:  [Error: {"code":-32603,"message":"Internal error","data":"height 4349149 is not available, lowest height is 4455515"}]
 WARN  cometClient.block error:  [Error: {"code":-32603,"message":"Internal error","data":"height 4316168 is not available, lowest height is 4772001"}]

How should I deal with this problem? Is it because I am using the cosmjs in wrong way, or does the Nibiru chain need special processing?

webmaster128 commented 3 months ago

The node does not store all blocks. Which block it has is a matter of node configuration and when/how you started syncing. The important part of the error message (coming from the node) is:

[...] lowest height is 4455515 [...] lowest height is 4772001

Blocks before those heights are not stored in the node.

ljunb commented 2 months ago

Thank you for your response. I'm not certain whether this issue is due to too many failed records during the previous test. The transfer function has been completed, and when I tried transferring to a new account today, I was able to successfully obtain the block time. I will close this issue and continue monitoring the situation for a while.