| 524 | } |
| 525 | |
| 526 | Value getchaininfo(const Array& params, bool fHelp) { |
| 527 | if (fHelp || params.size() < 1 || params.size() > 2) |
| 528 | throw runtime_error( |
| 529 | "getchaininfo \"count\" [height]\n" |
| 530 | "\nget the chain state of the most recent blocks.\n" |
| 531 | "\nArguments:\n" |
| 532 | "1.\"count\": (numeric, required) The count of the most recent blocks to get. MAX=10000\n" |
| 533 | "2.\"height\": (numeric, optional) The tip height of blocks\n" |
| 534 | "\nResult:\n" |
| 535 | "[\n" |
| 536 | " {\n" |
| 537 | " \"height\": n, (numeric) The block height\n" |
| 538 | " \"time\": n, (numeric) The block time\n" |
| 539 | " \"tx_count\":n, (numeric) The transaction number in the block\n" |
| 540 | " \"fuel\": n, (numeric) The fuel consumed in the block\n" |
| 541 | " \"fuel_rate\":n, (numeric) The fuel rate in the block\n" |
| 542 | " \"miner\": n, (string) The miner\n" |
| 543 | " },\n" |
| 544 | " ...\n" |
| 545 | "]\n" |
| 546 | "\nExamples:\n" + |
| 547 | HelpExampleCli("getchaininfo", "5") + "\nAs json rpc call\n" + HelpExampleRpc("getchaininfo", "5")); |
| 548 | |
| 549 | int32_t count = params[0].get_int(); |
| 550 | int32_t height = chainActive.Height(); |
| 551 | if (params.size() > 1) { |
| 552 | height = params[1].get_int(); |
| 553 | if (height > chainActive.Height()) { |
| 554 | throw JSONRPCError(RPC_INVALID_PARAMS, strprintf("The height exceed the tip height! height=%d, tip_height=%d", |
| 555 | height, chainActive.Height())); |
| 556 | } |
| 557 | } |
| 558 | if (count < 1 || count > height || count > MAX_RECENT_BLOCK_COUNT) |
| 559 | throw JSONRPCError(RPC_INVALID_PARAMS, strprintf("The input count out of range! count=%d, height=%d, max_count=%d", |
| 560 | count, height, MAX_RECENT_BLOCK_COUNT)); |
| 561 | |
| 562 | CBlockIndex* pBlockIndex = chainActive[height]; |
| 563 | Array array; |
| 564 | CBlock block; |
| 565 | |
| 566 | for (int32_t i = 0; (i < count) && (pBlockIndex != nullptr); i++) { |
| 567 | Object object; |
| 568 | CDiskBlockIndex diskBlockIndex; |
| 569 | if (!pCdMan->pBlockIndexDb->GetBlockIndex(pBlockIndex->GetBlockHash(), diskBlockIndex)) { |
| 570 | throw JSONRPCError(RPC_INVALID_PARAMS, |
| 571 | strprintf("the index of block=%s not found in db", pBlockIndex->GetIdString())); |
| 572 | } |
| 573 | object.push_back(Pair("height", pBlockIndex->height)); |
| 574 | object.push_back(Pair("time", pBlockIndex->GetBlockTime())); |
| 575 | object.push_back(Pair("tx_count", (int32_t)diskBlockIndex.nTx)); |
| 576 | object.push_back(Pair("fuel_fee", (int64_t)diskBlockIndex.nFuelFee)); |
| 577 | object.push_back(Pair("fuel_rate", (int32_t)diskBlockIndex.nFuelRate)); |
| 578 | |
| 579 | block.SetNull(); |
| 580 | if (ReadBlockFromDisk(pBlockIndex, block)) { |
| 581 | object.push_back(Pair("miner", block.vptx[0]->txUid.ToString())); |
| 582 | } |
| 583 |
nothing calls this directly
no test coverage detected