Skip to content

Commit

Permalink
Adding logging category CONNECT, SIGN (#2340)
Browse files Browse the repository at this point in the history
* Isolate linux specific helpers

* Reduce transfer domain noise

* Add ExtendedTxToUniv

* Add log category CONNECT and SIGN

* Split more
  • Loading branch information
prasannavl authored Aug 17, 2023
1 parent 1ce2335 commit 7fda957
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 77 deletions.
4 changes: 4 additions & 0 deletions src/core_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class CBlock;
class CBlockHeader;
class CScript;
class CTransaction;
class CTxIn;
class CTxOut;
struct CMutableTransaction;
class uint256;
class UniValue;
Expand Down Expand Up @@ -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
82 changes: 41 additions & 41 deletions src/core_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ namespace BCLog {
TOKENSPLIT = (1UL << 28),
RPCCACHE = (1UL << 29),
CUSTOMTXBENCH = (1UL << 30),
CONNECT = (1UL << 31),
SIGN = (1UL << 32),
ALL = ~(0UL),
};

Expand All @@ -84,7 +86,7 @@ namespace BCLog {
std::atomic_bool m_started_new_line{true};

/** Log categories bitfield. */
std::atomic<uint32_t> m_categories{0};
std::atomic<uint64_t> m_categories{0};

std::string LogTimestampStr(const std::string& str);

Expand Down
67 changes: 32 additions & 35 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ struct RewardInfo {


std::optional<UniValue> VmInfoUniv(const CTransaction& tx) {

auto evmBlockHeaderToUniValue = [](const EVMBlockHeader& header) {
UniValue r(UniValue::VOBJ);
r.pushKV("parenthash", uint256::FromByteArray(header.parent_hash).ToString());
Expand All @@ -284,45 +283,44 @@ std::optional<UniValue> 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<int>::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<int>::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) {
Expand All @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/rpc/rawtransaction_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<COutPoint, Coin>& 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();
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 7fda957

Please sign in to comment.