Skip to content

Commit

Permalink
Merge pull request #82 from buck54321/rpc-mediantime-txsize
Browse files Browse the repository at this point in the history
add median block time and tx size fields to RPC responses
  • Loading branch information
RhettCreighton authored Sep 15, 2023
2 parents c130260 + 3fcc026 commit f7bff4f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,10 +811,11 @@ bool CheckFinalTx(const CTransaction &tx, int flags)
// IsFinalTx() with one more than chainActive.Height().
const int nBlockHeight = chainActive.Height() + 1;

// Timestamps on the other hand don't get any special treatment,
// because we can't know what timestamp the next block will have,
// and there aren't timestamp applications where it matters.
// However this changes once median past time-locks are enforced:
// Require that time-locked transactions have nLockTime set to less than the
// median time of the previous block they're contained in. When the next
// block is created its previous block will be the current chain tip, so we
// use that to calculate the median time passed to IsFinalTx() if
// LOCKTIME_MEDIAN_TIME_PAST is set.
const int64_t nBlockTime = (flags & LOCKTIME_MEDIAN_TIME_PAST)
? chainActive.Tip()->GetMedianTimePast()
: GetAdjustedTime();
Expand Down
5 changes: 5 additions & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
}
result.push_back(Pair("tx", txs));
result.push_back(Pair("time", block.GetBlockTime()));
result.push_back(Pair("mediantime", (int64_t)blockindex->GetMedianTimePast()));
result.push_back(Pair("nonce", block.nNonce.GetHex()));
result.push_back(Pair("solution", HexStr(block.nSolution)));
result.push_back(Pair("bits", strprintf("%08x", block.nBits)));
Expand Down Expand Up @@ -374,6 +375,7 @@ UniValue getblockheader(const UniValue& params, bool fHelp)
" \"merkleroot\" : \"xxxx\", (string) The merkle root\n"
" \"finalsaplingroot\" : \"xxxx\", (string) The root of the Sapling commitment tree after applying this block\n"
" \"time\" : ttt, (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n"
" \"mediantime\" : ttt, (numeric) The median block time expressed in seconds since epoch (Jan 1 1970 GMT)\n"
" \"nonce\" : n, (numeric) The nonce\n"
" \"bits\" : \"1d00ffff\", (string) The bits\n"
" \"difficulty\" : x.xxx, (numeric) The difficulty\n"
Expand Down Expand Up @@ -438,6 +440,7 @@ UniValue getblock(const UniValue& params, bool fHelp)
" \"transactionid\" (string) The transaction id\n"
" ,...\n"
" ],\n"
" \"mediantime\" : ttt, (numeric) The median block time expressed in seconds since epoch (Jan 1 1970 GMT)\n"
" \"time\" : ttt, (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n"
" \"nonce\" : n, (numeric) The nonce\n"
" \"bits\" : \"1d00ffff\", (string) The bits\n"
Expand Down Expand Up @@ -746,6 +749,7 @@ UniValue getblockchaininfo(const UniValue& params, bool fHelp)
" \"blocks\": xxxxxx, (numeric) the current number of blocks processed in the server\n"
" \"headers\": xxxxxx, (numeric) the current number of headers we have validated\n"
" \"bestblockhash\": \"...\", (string) the hash of the currently best block\n"
" \"mediantime\" : ttt, (numeric) The median block time of the current best block\n"
" \"difficulty\": xxxxxx, (numeric) the current difficulty\n"
" \"verificationprogress\": xxxx, (numeric) estimate of verification progress [0..1]\n"
" \"chainwork\": \"xxxx\" (string) total amount of work in active chain, in hexadecimal\n"
Expand Down Expand Up @@ -789,6 +793,7 @@ UniValue getblockchaininfo(const UniValue& params, bool fHelp)
obj.push_back(Pair("blocks", (int)chainActive.Height()));
obj.push_back(Pair("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1));
obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()));
obj.push_back(Pair("mediantime", (int64_t)chainActive.Tip()->GetMedianTimePast()));
obj.push_back(Pair("difficulty", (double)GetNetworkDifficulty()));
obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(Params().Checkpoints(), chainActive.Tip())));
obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
Expand Down
3 changes: 3 additions & 0 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
entry.push_back(Pair("txid", tx.GetHash().GetHex()));
entry.push_back(Pair("overwintered", tx.fOverwintered));
entry.push_back(Pair("version", tx.nVersion));
entry.push_back(Pair("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION)));
if (tx.fOverwintered) {
entry.push_back(Pair("versiongroupid", HexInt(tx.nVersionGroupId)));
}
Expand Down Expand Up @@ -242,6 +243,7 @@ UniValue getrawtransaction(const UniValue& params, bool fHelp)
" \"hex\" : \"data\", (string) The serialized, hex-encoded data for 'txid'\n"
" \"txid\" : \"id\", (string) The transaction id (same as provided)\n"
" \"version\" : n, (numeric) The version\n"
" \"size\" : n, (numeric) The size of the transaction in bytes\n"
" \"locktime\" : ttt, (numeric) The lock time\n"
" \"expiryheight\" : ttt, (numeric, optional) The block height after which the transaction expires\n"
" \"vin\" : [ (array of json objects)\n"
Expand Down Expand Up @@ -587,6 +589,7 @@ UniValue decoderawtransaction(const UniValue& params, bool fHelp)
" \"overwintered\" : bool (boolean) The Overwintered flag\n"
" \"version\" : n, (numeric) The version\n"
" \"versiongroupid\": \"hex\" (string, optional) The version group id (Overwintered txs)\n"
" \"size\" : n, (numeric) The size of the transaction in bytes\n"
" \"locktime\" : ttt, (numeric) The lock time\n"
" \"expiryheight\" : n, (numeric, optional) Last valid block height for mining transaction (Overwintered txs)\n"
" \"vin\" : [ (array of json objects)\n"
Expand Down

0 comments on commit f7bff4f

Please sign in to comment.