Skip to content

Commit acac57b

Browse files
committed
Merge pull request #3730
1d46fe3 'sendrawtransaction' improvements (Wladimir J. van der Laan)
2 parents e33cc87 + 1d46fe3 commit acac57b

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/rpcprotocol.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ enum RPCErrorCode
4949
RPC_INVALID_PARAMETER = -8, // Invalid, missing or duplicate parameter
5050
RPC_DATABASE_ERROR = -20, // Database error
5151
RPC_DESERIALIZATION_ERROR = -22, // Error parsing or validating structure in raw format
52+
RPC_TRANSACTION_ERROR = -25, // General error during transaction submission
53+
RPC_TRANSACTION_REJECTED = -26, // Transaction was rejected by network rules
54+
RPC_TRANSACTION_ALREADY_IN_CHAIN= -27, // Transaction already in chain
5255

5356
// P2P client errors
5457
RPC_CLIENT_NOT_CONNECTED = -9, // Bitcoin is not connected

src/rpcrawtransaction.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -777,25 +777,23 @@ Value sendrawtransaction(const Array& params, bool fHelp)
777777
}
778778
uint256 hashTx = tx.GetHash();
779779

780-
bool fHave = false;
781780
CCoinsViewCache &view = *pcoinsTip;
782781
CCoins existingCoins;
783-
{
784-
fHave = view.GetCoins(hashTx, existingCoins);
785-
if (!fHave) {
786-
// push to local node
787-
CValidationState state;
788-
if (!AcceptToMemoryPool(mempool, state, tx, false, NULL, !fOverrideFees))
789-
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX rejected"); // TODO: report validation state
782+
bool fHaveMempool = mempool.exists(hashTx);
783+
bool fHaveChain = view.GetCoins(hashTx, existingCoins) && existingCoins.nHeight < 1000000000;
784+
if (!fHaveMempool && !fHaveChain) {
785+
// push to local node and sync with wallets
786+
CValidationState state;
787+
if (AcceptToMemoryPool(mempool, state, tx, false, NULL, !fOverrideFees))
788+
SyncWithWallets(hashTx, tx, NULL);
789+
else {
790+
if(state.IsInvalid())
791+
throw JSONRPCError(RPC_TRANSACTION_REJECTED, strprintf("%i: %s", state.GetRejectCode(), state.GetRejectReason()));
792+
else
793+
throw JSONRPCError(RPC_TRANSACTION_ERROR, state.GetRejectReason());
790794
}
791-
}
792-
if (fHave) {
793-
if (existingCoins.nHeight < 1000000000)
794-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "transaction already in block chain");
795-
// Not in block, but already in the memory pool; will drop
796-
// through to re-relay it.
797-
} else {
798-
SyncWithWallets(hashTx, tx, NULL);
795+
} else if (fHaveChain) {
796+
throw JSONRPCError(RPC_TRANSACTION_ALREADY_IN_CHAIN, "transaction already in block chain");
799797
}
800798
RelayTransaction(tx, hashTx);
801799

0 commit comments

Comments
 (0)