From 47a7ca569e45788291d1c45877440dfd94607f23 Mon Sep 17 00:00:00 2001 From: andhikayuana Date: Thu, 22 May 2025 12:21:29 +0700 Subject: [PATCH 1/2] chore: add log for debugging --- BRPeer.c | 89 ++++++++++++++++++++++++++++++++++--------------- BRPeerManager.c | 37 +++++++++++++++++--- 2 files changed, 95 insertions(+), 31 deletions(-) diff --git a/BRPeer.c b/BRPeer.c index 27af838..8155733 100644 --- a/BRPeer.c +++ b/BRPeer.c @@ -419,34 +419,40 @@ static int _BRPeerAcceptTxMessage(BRPeer *peer, const uint8_t *msg, size_t msgLe uint64_t feeAmount = BRTransactionStandardFee(tx); //DEV: uncomment for debugging -// peer_log(peer, "tx details: hash=%s, version=%d, blockHeight=%d, timestamp=%d, lockTime=%u", -// u256hex(tx->txHash), tx->version, tx->blockHeight, tx->timestamp, tx->lockTime); -// -// peer_log(peer, "tx inputs: count=%zu", tx->inCount); -// for (size_t i = 0; i < tx->inCount; i++) { -// peer_log(peer, " input[%zu]: txHash=%s, index=%u, signature length=%zu, sequence=%u", -// i, u256hex(tx->inputs[i].txHash), tx->inputs[i].index, -// tx->inputs[i].sigLen, tx->inputs[i].sequence); -// } -// -// peer_log(peer, "tx outputs: count=%zu", tx->outCount); -// for (size_t j = 0; j < tx->outCount; j++) { -// char addr[75] = ""; -// BRAddressFromScriptPubKey(addr, sizeof(addr), tx->outputs[j].script, tx->outputs[j].scriptLen); -// peer_log(peer, " output[%zu]: amount=%llu, scriptLen=%zu, address=%s", -// j, tx->outputs[j].amount, tx->outputs[j].scriptLen, addr); -// } -// -// peer_log(peer, "tx size=%zu bytes, fee=%llu", txSize, feeAmount); - - if (txSize > 0 && tx->blockHeight == TX_UNCONFIRMED && feeAmount/txSize < 10) { - //don't relay to wallet - peer_log(peer, "skipping stuck transaction: %s, (low fee: %llu litoshis/byte, min. is 10 litoshis/byte)", u256hex(txHash), feeAmount/txSize); - BRTransactionFree(tx); - if (ctx->rejectedTx) ctx->rejectedTx(ctx->info, txHash, REJECT_LOWFEE); - r = 1; + peer_log(peer, "from _BRPeerAcceptTxMessage"); + peer_log(peer, "tx details: hash=%s, version=%d, blockHeight=%d, timestamp=%d, lockTime=%u", + u256hex(tx->txHash), tx->version, tx->blockHeight, tx->timestamp, tx->lockTime); + + peer_log(peer, "tx inputs: count=%zu", tx->inCount); + for (size_t i = 0; i < tx->inCount; i++) { + peer_log(peer, " input[%zu]: txHash=%s, index=%u, signature length=%zu, sequence=%u", + i, u256hex(tx->inputs[i].txHash), tx->inputs[i].index, + tx->inputs[i].sigLen, tx->inputs[i].sequence); } - else if (ctx->relayedTx) { + + peer_log(peer, "tx outputs: count=%zu", tx->outCount); + for (size_t j = 0; j < tx->outCount; j++) { + char addr[75] = ""; + BRAddressFromScriptPubKey(addr, sizeof(addr), tx->outputs[j].script, tx->outputs[j].scriptLen); + peer_log(peer, " output[%zu]: amount=%llu, scriptLen=%zu, address=%s", + j, tx->outputs[j].amount, tx->outputs[j].scriptLen, addr); + } + + peer_log(peer, "tx size=%zu bytes, fee=%llu", txSize, feeAmount); + + if (feeAmount/txSize < 10) { + peer_log(peer, "stuck tx: %s, (low fee: %llu lit/byte)", u256hex(txHash), feeAmount/txSize); + } + +// if (txSize > 0 && tx->blockHeight == TX_UNCONFIRMED && feeAmount/txSize < 10) { +// //don't relay to wallet +// peer_log(peer, "skipping stuck transaction: %s, (low fee: %llu sat/byte)", u256hex(txHash), feeAmount/txSize); +// BRTransactionFree(tx); +// if (ctx->rejectedTx) ctx->rejectedTx(ctx->info, txHash, REJECT_LOWFEE); +// return 1; +// } +// else + if (ctx->relayedTx) { ctx->relayedTx(ctx->info, tx); } else BRTransactionFree(tx); @@ -577,6 +583,35 @@ static int _BRPeerAcceptGetdataMessage(BRPeer *peer, const uint8_t *msg, size_t } peer_log(peer, "publishing tx: %s", txHex); + + size_t txSize = BRTransactionSize(tx); + uint64_t feeAmount = BRTransactionStandardFee(tx); + + //DEV: uncomment for debugging + peer_log(peer, "from _BRPeerAcceptGetdataMessage"); + peer_log(peer, "tx details: hash=%s, version=%d, blockHeight=%d, timestamp=%d, lockTime=%u", + u256hex(tx->txHash), tx->version, tx->blockHeight, tx->timestamp, tx->lockTime); + + peer_log(peer, "tx inputs: count=%zu", tx->inCount); + for (size_t i = 0; i < tx->inCount; i++) { + peer_log(peer, " input[%zu]: txHash=%s, index=%u, signature length=%zu, sequence=%u", + i, u256hex(tx->inputs[i].txHash), tx->inputs[i].index, + tx->inputs[i].sigLen, tx->inputs[i].sequence); + } + + peer_log(peer, "tx outputs: count=%zu", tx->outCount); + for (size_t j = 0; j < tx->outCount; j++) { + char addr[75] = ""; + BRAddressFromScriptPubKey(addr, sizeof(addr), tx->outputs[j].script, tx->outputs[j].scriptLen); + peer_log(peer, " output[%zu]: amount=%llu, scriptLen=%zu, address=%s", + j, tx->outputs[j].amount, tx->outputs[j].scriptLen, addr); + } + + peer_log(peer, "tx size=%zu bytes, fee=%llu", txSize, feeAmount); + if (feeAmount/txSize < 10) { + peer_log(peer, "stuck tx: %s, (low fee: %llu lit/byte)", u256hex(tx->txHash), feeAmount/txSize); + } + BRPeerSendMessage(peer, buf, bufLen, MSG_TX); break; } diff --git a/BRPeerManager.c b/BRPeerManager.c index 4af405b..db54ee1 100644 --- a/BRPeerManager.c +++ b/BRPeerManager.c @@ -1222,6 +1222,35 @@ static void _peerHasTx(void *info, UInt256 txHash) } if (tx) { + + //DEV: uncomment for debugging + peer_log(peer, "from _peer_hasTx"); + size_t txSize = BRTransactionSize(tx); + uint64_t feeAmount = BRTransactionStandardFee(tx); + peer_log(peer, "tx details: hash=%s, version=%d, blockHeight=%d, timestamp=%d, lockTime=%u", + u256hex(tx->txHash), tx->version, tx->blockHeight, tx->timestamp, tx->lockTime); + + peer_log(peer, "tx inputs: count=%zu", tx->inCount); + for (size_t i = 0; i < tx->inCount; i++) { + peer_log(peer, " input[%zu]: txHash=%s, index=%u, signature length=%zu, sequence=%u", + i, u256hex(tx->inputs[i].txHash), tx->inputs[i].index, + tx->inputs[i].sigLen, tx->inputs[i].sequence); + } + + peer_log(peer, "tx outputs: count=%zu", tx->outCount); + for (size_t j = 0; j < tx->outCount; j++) { + char addr[75] = ""; + BRAddressFromScriptPubKey(addr, sizeof(addr), tx->outputs[j].script, tx->outputs[j].scriptLen); + peer_log(peer, " output[%zu]: amount=%llu, scriptLen=%zu, address=%s", + j, tx->outputs[j].amount, tx->outputs[j].scriptLen, addr); + } + + peer_log(peer, "tx size=%zu bytes, fee=%llu", txSize, feeAmount); + if (feeAmount/txSize < 10) { + peer_log(peer, "stuck tx: %s, (low fee: %llu lit/byte)", u256hex(tx->txHash), feeAmount/txSize); + } + + isWalletTx = BRWalletRegisterTransaction(manager->wallet, tx); if (isWalletTx) tx = BRWalletTransactionForHash(manager->wallet, tx->txHash); @@ -1260,10 +1289,10 @@ static void _peerRejectedTx(void *info, UInt256 txHash, uint8_t code) if (tx) { // Handle tx rejection - if (tx->blockHeight == TX_UNCONFIRMED && (code == REJECT_DUST || code == REJECT_LOWFEE || code == REJECT_NONSTANDARD)) { - peer_log(peer, "transaction rejected as dust/lowfee/nonstandard, removing: %s", u256hex(txHash)); - BRWalletRemoveTransaction(manager->wallet, txHash); - } +// if (tx->blockHeight == TX_UNCONFIRMED && (code == REJECT_DUST || code == REJECT_LOWFEE || code == REJECT_NONSTANDARD)) { +// peer_log(peer, "transaction rejected as dust/lowfee/nonstandard, removing: %s", u256hex(txHash)); +// BRWalletRemoveTransaction(manager->wallet, txHash); +// } if (_BRTxPeerListRemovePeer(manager->txRelays, txHash, peer) && tx->blockHeight == TX_UNCONFIRMED) { // set timestamp 0 to mark tx as unverified From 25c9e0584825294e741c176f02ed48e523bc9f42 Mon Sep 17 00:00:00 2001 From: andhikayuana Date: Thu, 22 May 2025 14:46:12 +0700 Subject: [PATCH 2/2] fix: filter out using rejectedTx callback --- BRPeer.c | 111 ++++++++++++++++++++++++------------------------ BRPeerManager.c | 57 +++++++++++-------------- 2 files changed, 80 insertions(+), 88 deletions(-) diff --git a/BRPeer.c b/BRPeer.c index 8155733..0d1e313 100644 --- a/BRPeer.c +++ b/BRPeer.c @@ -419,40 +419,39 @@ static int _BRPeerAcceptTxMessage(BRPeer *peer, const uint8_t *msg, size_t msgLe uint64_t feeAmount = BRTransactionStandardFee(tx); //DEV: uncomment for debugging - peer_log(peer, "from _BRPeerAcceptTxMessage"); - peer_log(peer, "tx details: hash=%s, version=%d, blockHeight=%d, timestamp=%d, lockTime=%u", - u256hex(tx->txHash), tx->version, tx->blockHeight, tx->timestamp, tx->lockTime); - - peer_log(peer, "tx inputs: count=%zu", tx->inCount); - for (size_t i = 0; i < tx->inCount; i++) { - peer_log(peer, " input[%zu]: txHash=%s, index=%u, signature length=%zu, sequence=%u", - i, u256hex(tx->inputs[i].txHash), tx->inputs[i].index, - tx->inputs[i].sigLen, tx->inputs[i].sequence); - } - - peer_log(peer, "tx outputs: count=%zu", tx->outCount); - for (size_t j = 0; j < tx->outCount; j++) { - char addr[75] = ""; - BRAddressFromScriptPubKey(addr, sizeof(addr), tx->outputs[j].script, tx->outputs[j].scriptLen); - peer_log(peer, " output[%zu]: amount=%llu, scriptLen=%zu, address=%s", - j, tx->outputs[j].amount, tx->outputs[j].scriptLen, addr); - } - - peer_log(peer, "tx size=%zu bytes, fee=%llu", txSize, feeAmount); - - if (feeAmount/txSize < 10) { - peer_log(peer, "stuck tx: %s, (low fee: %llu lit/byte)", u256hex(txHash), feeAmount/txSize); - } - -// if (txSize > 0 && tx->blockHeight == TX_UNCONFIRMED && feeAmount/txSize < 10) { -// //don't relay to wallet -// peer_log(peer, "skipping stuck transaction: %s, (low fee: %llu sat/byte)", u256hex(txHash), feeAmount/txSize); -// BRTransactionFree(tx); -// if (ctx->rejectedTx) ctx->rejectedTx(ctx->info, txHash, REJECT_LOWFEE); -// return 1; +// peer_log(peer, "from _BRPeerAcceptTxMessage"); +// peer_log(peer, "tx details: hash=%s, version=%d, blockHeight=%d, timestamp=%d, lockTime=%u", +// u256hex(tx->txHash), tx->version, tx->blockHeight, tx->timestamp, tx->lockTime); +// +// peer_log(peer, "tx inputs: count=%zu", tx->inCount); +// for (size_t i = 0; i < tx->inCount; i++) { +// peer_log(peer, " input[%zu]: txHash=%s, index=%u, signature length=%zu, sequence=%u", +// i, u256hex(tx->inputs[i].txHash), tx->inputs[i].index, +// tx->inputs[i].sigLen, tx->inputs[i].sequence); +// } +// +// peer_log(peer, "tx outputs: count=%zu", tx->outCount); +// for (size_t j = 0; j < tx->outCount; j++) { +// char addr[75] = ""; +// BRAddressFromScriptPubKey(addr, sizeof(addr), tx->outputs[j].script, tx->outputs[j].scriptLen); +// peer_log(peer, " output[%zu]: amount=%llu, scriptLen=%zu, address=%s", +// j, tx->outputs[j].amount, tx->outputs[j].scriptLen, addr); // } -// else - if (ctx->relayedTx) { +// +// peer_log(peer, "tx size=%zu bytes, fee=%llu", txSize, feeAmount); +// +// if (feeAmount/txSize < 10) { +// peer_log(peer, "stuck tx: %s, (low fee: %llu lit/byte)", u256hex(txHash), feeAmount/txSize); +// } + + // Check if the transaction has a low fee rate + if (txSize > 0 && tx->blockHeight == TX_UNCONFIRMED && tx->timestamp == 0 && feeAmount/txSize < 10) { + // Don't relay to wallet + peer_log(peer, "skipping stuck transaction: %s, (low fee: %llu sat/byte)", u256hex(txHash), feeAmount/txSize); + BRTransactionFree(tx); + if (ctx->rejectedTx) ctx->rejectedTx(ctx->info, txHash, REJECT_LOWFEE); + r = 0; + } else if (ctx->relayedTx) { ctx->relayedTx(ctx->info, tx); } else BRTransactionFree(tx); @@ -588,29 +587,29 @@ static int _BRPeerAcceptGetdataMessage(BRPeer *peer, const uint8_t *msg, size_t uint64_t feeAmount = BRTransactionStandardFee(tx); //DEV: uncomment for debugging - peer_log(peer, "from _BRPeerAcceptGetdataMessage"); - peer_log(peer, "tx details: hash=%s, version=%d, blockHeight=%d, timestamp=%d, lockTime=%u", - u256hex(tx->txHash), tx->version, tx->blockHeight, tx->timestamp, tx->lockTime); - - peer_log(peer, "tx inputs: count=%zu", tx->inCount); - for (size_t i = 0; i < tx->inCount; i++) { - peer_log(peer, " input[%zu]: txHash=%s, index=%u, signature length=%zu, sequence=%u", - i, u256hex(tx->inputs[i].txHash), tx->inputs[i].index, - tx->inputs[i].sigLen, tx->inputs[i].sequence); - } - - peer_log(peer, "tx outputs: count=%zu", tx->outCount); - for (size_t j = 0; j < tx->outCount; j++) { - char addr[75] = ""; - BRAddressFromScriptPubKey(addr, sizeof(addr), tx->outputs[j].script, tx->outputs[j].scriptLen); - peer_log(peer, " output[%zu]: amount=%llu, scriptLen=%zu, address=%s", - j, tx->outputs[j].amount, tx->outputs[j].scriptLen, addr); - } - - peer_log(peer, "tx size=%zu bytes, fee=%llu", txSize, feeAmount); - if (feeAmount/txSize < 10) { - peer_log(peer, "stuck tx: %s, (low fee: %llu lit/byte)", u256hex(tx->txHash), feeAmount/txSize); - } +// peer_log(peer, "from _BRPeerAcceptGetdataMessage"); +// peer_log(peer, "tx details: hash=%s, version=%d, blockHeight=%d, timestamp=%d, lockTime=%u", +// u256hex(tx->txHash), tx->version, tx->blockHeight, tx->timestamp, tx->lockTime); +// +// peer_log(peer, "tx inputs: count=%zu", tx->inCount); +// for (size_t i = 0; i < tx->inCount; i++) { +// peer_log(peer, " input[%zu]: txHash=%s, index=%u, signature length=%zu, sequence=%u", +// i, u256hex(tx->inputs[i].txHash), tx->inputs[i].index, +// tx->inputs[i].sigLen, tx->inputs[i].sequence); +// } +// +// peer_log(peer, "tx outputs: count=%zu", tx->outCount); +// for (size_t j = 0; j < tx->outCount; j++) { +// char addr[75] = ""; +// BRAddressFromScriptPubKey(addr, sizeof(addr), tx->outputs[j].script, tx->outputs[j].scriptLen); +// peer_log(peer, " output[%zu]: amount=%llu, scriptLen=%zu, address=%s", +// j, tx->outputs[j].amount, tx->outputs[j].scriptLen, addr); +// } +// +// peer_log(peer, "tx size=%zu bytes, fee=%llu", txSize, feeAmount); +// if (feeAmount/txSize < 10) { +// peer_log(peer, "stuck tx: %s, (low fee: %llu lit/byte)", u256hex(tx->txHash), feeAmount/txSize); +// } BRPeerSendMessage(peer, buf, bufLen, MSG_TX); break; diff --git a/BRPeerManager.c b/BRPeerManager.c index db54ee1..1375c56 100644 --- a/BRPeerManager.c +++ b/BRPeerManager.c @@ -1224,31 +1224,31 @@ static void _peerHasTx(void *info, UInt256 txHash) if (tx) { //DEV: uncomment for debugging - peer_log(peer, "from _peer_hasTx"); - size_t txSize = BRTransactionSize(tx); - uint64_t feeAmount = BRTransactionStandardFee(tx); - peer_log(peer, "tx details: hash=%s, version=%d, blockHeight=%d, timestamp=%d, lockTime=%u", - u256hex(tx->txHash), tx->version, tx->blockHeight, tx->timestamp, tx->lockTime); - - peer_log(peer, "tx inputs: count=%zu", tx->inCount); - for (size_t i = 0; i < tx->inCount; i++) { - peer_log(peer, " input[%zu]: txHash=%s, index=%u, signature length=%zu, sequence=%u", - i, u256hex(tx->inputs[i].txHash), tx->inputs[i].index, - tx->inputs[i].sigLen, tx->inputs[i].sequence); - } - - peer_log(peer, "tx outputs: count=%zu", tx->outCount); - for (size_t j = 0; j < tx->outCount; j++) { - char addr[75] = ""; - BRAddressFromScriptPubKey(addr, sizeof(addr), tx->outputs[j].script, tx->outputs[j].scriptLen); - peer_log(peer, " output[%zu]: amount=%llu, scriptLen=%zu, address=%s", - j, tx->outputs[j].amount, tx->outputs[j].scriptLen, addr); - } - - peer_log(peer, "tx size=%zu bytes, fee=%llu", txSize, feeAmount); - if (feeAmount/txSize < 10) { - peer_log(peer, "stuck tx: %s, (low fee: %llu lit/byte)", u256hex(tx->txHash), feeAmount/txSize); - } +// peer_log(peer, "from _peerHasTx"); +// size_t txSize = BRTransactionSize(tx); +// uint64_t feeAmount = BRTransactionStandardFee(tx); +// peer_log(peer, "tx details: hash=%s, version=%d, blockHeight=%d, timestamp=%d, lockTime=%u", +// u256hex(tx->txHash), tx->version, tx->blockHeight, tx->timestamp, tx->lockTime); +// +// peer_log(peer, "tx inputs: count=%zu", tx->inCount); +// for (size_t i = 0; i < tx->inCount; i++) { +// peer_log(peer, " input[%zu]: txHash=%s, index=%u, signature length=%zu, sequence=%u", +// i, u256hex(tx->inputs[i].txHash), tx->inputs[i].index, +// tx->inputs[i].sigLen, tx->inputs[i].sequence); +// } +// +// peer_log(peer, "tx outputs: count=%zu", tx->outCount); +// for (size_t j = 0; j < tx->outCount; j++) { +// char addr[75] = ""; +// BRAddressFromScriptPubKey(addr, sizeof(addr), tx->outputs[j].script, tx->outputs[j].scriptLen); +// peer_log(peer, " output[%zu]: amount=%llu, scriptLen=%zu, address=%s", +// j, tx->outputs[j].amount, tx->outputs[j].scriptLen, addr); +// } +// +// peer_log(peer, "tx size=%zu bytes, fee=%llu", txSize, feeAmount); +// if (feeAmount/txSize < 10) { +// peer_log(peer, "stuck tx: %s, (low fee: %llu lit/byte)", u256hex(tx->txHash), feeAmount/txSize); +// } isWalletTx = BRWalletRegisterTransaction(manager->wallet, tx); @@ -1287,13 +1287,6 @@ static void _peerRejectedTx(void *info, UInt256 txHash, uint8_t code) _BRTxPeerListRemovePeer(manager->txRequests, txHash, peer); if (tx) { - - // Handle tx rejection -// if (tx->blockHeight == TX_UNCONFIRMED && (code == REJECT_DUST || code == REJECT_LOWFEE || code == REJECT_NONSTANDARD)) { -// peer_log(peer, "transaction rejected as dust/lowfee/nonstandard, removing: %s", u256hex(txHash)); -// BRWalletRemoveTransaction(manager->wallet, txHash); -// } - if (_BRTxPeerListRemovePeer(manager->txRelays, txHash, peer) && tx->blockHeight == TX_UNCONFIRMED) { // set timestamp 0 to mark tx as unverified _BRPeerManagerUpdateTx(manager, &txHash, 1, TX_UNCONFIRMED, 0);