diff --git a/src/core_io.h b/src/core_io.h index 0bcae456c8..faaaa38152 100644 --- a/src/core_io.h +++ b/src/core_io.h @@ -15,6 +15,8 @@ class CBlock; class CBlockHeader; class CScript; class CTransaction; +class CTxIn; +class CTxOut; struct CMutableTransaction; class uint256; class UniValue; @@ -46,6 +48,8 @@ std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0); std::string SighashToStr(unsigned char sighash_type); void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex); void ScriptToUniv(const CScript& script, UniValue& out, bool include_address); +UniValue TxInToUniv(const CTransaction& tx, const CTxIn& txin, const unsigned int index, bool include_hex, int version); +UniValue TxOutToUniv(const CTransaction& tx, const CTxOut& txout, const unsigned int index, bool include_hex, int version); void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex = true, int serialize_flags = 0, int verbosity = 0); #endif // DEFI_CORE_IO_H diff --git a/src/core_write.cpp b/src/core_write.cpp index 8f690047f4..b234fb3e34 100644 --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -183,48 +183,48 @@ void ScriptPubKeyToUniv(const CScript& scriptPubKey, out.pushKV("addresses", a); } -void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex, int serialize_flags, int version) -{ - const auto txInToUniValue = [](const CTransaction& tx, const CTxIn& txin, const unsigned int index, bool include_hex, int version) { - UniValue in(UniValue::VOBJ); - if (tx.IsCoinBase()) - in.pushKV("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())); - else { - in.pushKV("txid", txin.prevout.hash.GetHex()); - in.pushKV("vout", (int64_t)txin.prevout.n); - UniValue o(UniValue::VOBJ); - o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true)); - if (include_hex || version <= 2) { - o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())); - } - in.pushKV("scriptSig", o); - if (!tx.vin[index].scriptWitness.IsNull()) { - UniValue txinwitness(UniValue::VARR); - for (const auto& item : tx.vin[index].scriptWitness.stack) { - txinwitness.push_back(HexStr(item.begin(), item.end())); - } - in.pushKV("txinwitness", txinwitness); - } - } - in.pushKV("sequence", (int64_t)txin.nSequence); - return in; - }; - - const auto txOutToUniValue = [](const CTransaction& tx, const CTxOut& txout, const unsigned int index, bool include_hex, int version) { - UniValue out(UniValue::VOBJ); - out.pushKV("value", ValueFromAmount(txout.nValue)); - out.pushKV("n", (int64_t)index); +UniValue TxInToUniv(const CTransaction& tx, const CTxIn& txin, const unsigned int index, bool include_hex, int version) { + UniValue in(UniValue::VOBJ); + if (tx.IsCoinBase()) + in.pushKV("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())); + else { + in.pushKV("txid", txin.prevout.hash.GetHex()); + in.pushKV("vout", (int64_t)txin.prevout.n); UniValue o(UniValue::VOBJ); - ScriptPubKeyToUniv(txout.scriptPubKey, o, include_hex || version <= 2); - out.pushKV("scriptPubKey", o); - // We skip this for v3+ as we tokenId field is unused. - if (version <= 2 && tx.nVersion >= CTransaction::TOKENS_MIN_VERSION) { - // Start to print tokenId start from version TOKENS_MIN_VERSION - out.pushKV("tokenId", (uint64_t)txout.nTokenId.v); + o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true)); + if (include_hex || version != 3) { + o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())); + } + in.pushKV("scriptSig", o); + if (!tx.vin[index].scriptWitness.IsNull()) { + UniValue txinwitness(UniValue::VARR); + for (const auto& item : tx.vin[index].scriptWitness.stack) { + txinwitness.push_back(HexStr(item.begin(), item.end())); + } + in.pushKV("txinwitness", txinwitness); } - return out; - }; + } + in.pushKV("sequence", (int64_t)txin.nSequence); + return in; +} +UniValue TxOutToUniv(const CTransaction& tx, const CTxOut& txout, const unsigned int index, bool include_hex, int version) { + UniValue out(UniValue::VOBJ); + out.pushKV("value", ValueFromAmount(txout.nValue)); + out.pushKV("n", (int64_t)index); + UniValue o(UniValue::VOBJ); + ScriptPubKeyToUniv(txout.scriptPubKey, o, include_hex || version != 3); + out.pushKV("scriptPubKey", o); + // We skip this for v3+ as we tokenId field is unused. + if (version <= 2 && tx.nVersion >= CTransaction::TOKENS_MIN_VERSION) { + // Start to print tokenId start from version TOKENS_MIN_VERSION + out.pushKV("tokenId", (uint64_t)txout.nTokenId.v); + } + return out; +} + +void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex, int serialize_flags, int version) +{ entry.pushKV("txid", tx.GetHash().GetHex()); entry.pushKV("hash", tx.GetWitnessHash().GetHex()); entry.pushKV("version", tx.nVersion); @@ -235,13 +235,13 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, UniValue vin(UniValue::VARR); for (unsigned int i = 0; i < tx.vin.size(); i++) { - vin.push_back(txInToUniValue(tx, tx.vin[i], i, include_hex, version)); + vin.push_back(TxInToUniv(tx, tx.vin[i], i, include_hex, version)); } entry.pushKV("vin", vin); UniValue vout(UniValue::VARR); for (unsigned int i = 0; i < tx.vout.size(); i++) { - vout.push_back(txOutToUniValue(tx, tx.vout[i], i, include_hex, version)); + vout.push_back(TxOutToUniv(tx, tx.vout[i], i, include_hex, version)); } entry.pushKV("vout", vout); diff --git a/src/logging.h b/src/logging.h index 4835f86f5d..4988aa102e 100644 --- a/src/logging.h +++ b/src/logging.h @@ -65,6 +65,8 @@ namespace BCLog { TOKENSPLIT = (1UL << 28), RPCCACHE = (1UL << 29), CUSTOMTXBENCH = (1UL << 30), + CONNECT = (1UL << 31), + SIGN = (1UL << 32), ALL = ~(0UL), }; @@ -84,7 +86,7 @@ namespace BCLog { std::atomic_bool m_started_new_line{true}; /** Log categories bitfield. */ - std::atomic m_categories{0}; + std::atomic m_categories{0}; std::string LogTimestampStr(const std::string& str); diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 96348adeaa..de2b3ed484 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -268,7 +268,6 @@ struct RewardInfo { std::optional VmInfoUniv(const CTransaction& tx) { - auto evmBlockHeaderToUniValue = [](const EVMBlockHeader& header) { UniValue r(UniValue::VOBJ); r.pushKV("parenthash", uint256::FromByteArray(header.parent_hash).ToString()); @@ -284,45 +283,44 @@ std::optional VmInfoUniv(const CTransaction& tx) { return r; }; - CustomTxType guess; - UniValue txResults(UniValue::VOBJ); - if (tx.IsCoinBase()) { - if (tx.vout.size() < 2) { - // TODO: Decode vout 0 to dvm - return {}; - } - auto tx1ScriptPubKey = tx.vout[1].scriptPubKey; - if (tx1ScriptPubKey.size() == 0) return {}; - auto xvm = XVM::TryFrom(tx1ScriptPubKey); - if (!xvm) return {}; - UniValue result(UniValue::VOBJ); - result.pushKV("vmtype", "coinbase"); - result.pushKV("txtype", "coinbase"); - result.pushKV("msg", xvm->ToUniValue()); - CrossBoundaryResult res; - auto evmBlockHeader = evm_try_get_block_header_by_hash(res, xvm->evm.blockHash.GetByteArray()); - if (!res.ok) return {}; - result.pushKV("xvmHeader", evmBlockHeaderToUniValue(evmBlockHeader)); - return result; - } - auto res = RpcInfo(tx, std::numeric_limits::max(), guess, txResults); - if (guess == CustomTxType::None) { + CustomTxType guess; + UniValue txResults(UniValue::VOBJ); + if (tx.IsCoinBase()) { + if (tx.vout.size() < 2) { + // TODO: Decode vout 0 to dvm return {}; } + auto tx1ScriptPubKey = tx.vout[1].scriptPubKey; + if (tx1ScriptPubKey.size() == 0) return {}; + auto xvm = XVM::TryFrom(tx1ScriptPubKey); + if (!xvm) return {}; UniValue result(UniValue::VOBJ); - result.pushKV("vmtype", guess == CustomTxType::EvmTx ? "evm" : "dvm"); - result.pushKV("txtype", ToString(guess)); - if (!res.ok) { - result.pushKV("error", res.msg); - } else { - result.pushKV("msg", txResults); - } + result.pushKV("vmtype", "coinbase"); + result.pushKV("txtype", "coinbase"); + result.pushKV("msg", xvm->ToUniValue()); + CrossBoundaryResult res; + auto evmBlockHeader = evm_try_get_block_header_by_hash(res, xvm->evm.blockHash.GetByteArray()); + if (!res.ok) return {}; + result.pushKV("xvmHeader", evmBlockHeaderToUniValue(evmBlockHeader)); return result; + } + auto res = RpcInfo(tx, std::numeric_limits::max(), guess, txResults); + if (guess == CustomTxType::None) { + return {}; + } + UniValue result(UniValue::VOBJ); + result.pushKV("vmtype", guess == CustomTxType::EvmTx ? "evm" : "dvm"); + result.pushKV("txtype", ToString(guess)); + if (!res.ok) { + result.pushKV("error", res.msg); + } else { + result.pushKV("msg", txResults); + } + return result; } UniValue ExtendedTxToUniv(const CTransaction& tx, bool include_hex, int serialize_flags, int version, bool txDetails) { - - if (txDetails) { + if (txDetails) { UniValue objTx(UniValue::VOBJ); TxToUniv(tx, uint256(), objTx, version != 3, RPCSerializationFlags(), version); if (version > 2) { @@ -344,8 +342,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIn auto txsToUniValue = [](const CBlock& block, bool txDetails, int version) { UniValue txs(UniValue::VARR); - for(const auto& tx : block.vtx) - { + for(const auto& tx : block.vtx) { txs.push_back(ExtendedTxToUniv(*tx, txDetails, RPCSerializationFlags(), version, txDetails)); } return txs; diff --git a/src/rpc/rawtransaction_util.cpp b/src/rpc/rawtransaction_util.cpp index 6fd9fccf3a..06f660a211 100644 --- a/src/rpc/rawtransaction_util.cpp +++ b/src/rpc/rawtransaction_util.cpp @@ -296,8 +296,14 @@ static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std:: vErrorsRet.push_back(entry); } +int RPCSerializationFlags(); +UniValue ExtendedTxToUniv(const CTransaction& tx, bool include_hex, int serialize_flags, int version, bool txDetails); + UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxsUnival, FillableSigningProvider* keystore, std::map& coins, bool is_temp_keystore, const UniValue& hashType) { + if (LogAcceptCategory(BCLog::SIGN)) { + LogPrintf("SignTransaction::Pre: %s\n", ExtendedTxToUniv(CTransaction(mtx), true, RPCSerializationFlags(), 4, true).write(2)); + } // Add previous txouts given in the RPC call: if (!prevTxsUnival.isNull()) { UniValue prevTxs = prevTxsUnival.get_array(); @@ -425,6 +431,10 @@ UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxsUnival } bool fComplete = vErrors.empty(); + if (LogAcceptCategory(BCLog::SIGN)) { + LogPrintf("SignTransaction::Post: %s\n", ExtendedTxToUniv(CTransaction(mtx), true, RPCSerializationFlags(), 4, true).write(2)); + } + UniValue result(UniValue::VOBJ); result.pushKV("hex", EncodeHexTx(CTransaction(mtx))); result.pushKV("complete", fComplete); diff --git a/src/validation.cpp b/src/validation.cpp index 63d6f8547d..6df3252177 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -72,6 +72,8 @@ #define MICRO 0.000001 #define MILLI 0.001 +UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails, int version); + bool CBlockIndexWorkComparator::operator()(const CBlockIndex *pa, const CBlockIndex *pb) const { // First sort by most total work, ... if (pa->nChainWork > pb->nChainWork) return false; @@ -3406,6 +3408,9 @@ bool CChainState::ConnectTip(CValidationState& state, const CChainParams& chainp LogPrint(BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime6 - nTime5) * MILLI, nTimePostConnect * MICRO, nTimePostConnect * MILLI / nBlocksTotal); LogPrint(BCLog::BENCH, "- Connect block: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime6 - nTime1) * MILLI, nTimeTotal * MICRO, nTimeTotal * MILLI / nBlocksTotal); + if (LogAcceptCategory(BCLog::CONNECT)) { + LogPrintf("ConnectTip: %s\n", blockToJSON(*pthisBlock, pindexNew, pindexNew, true, 4).write(2)); + } connectTrace.BlockConnected(pindexNew, std::move(pthisBlock)); return true; } @@ -3500,6 +3505,7 @@ static CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOC return nullptr; } + /** * Try to make some progress towards making pindexMostWork the active block. * pblock is either nullptr or a pointer to a CBlock corresponding to pindexMostWork.