How to catch a 'bad-txns-inputs-spent' error

1,524 views
Skip to first unread message

giulio...@gmail.com

unread,
Mar 9, 2016, 6:38:15 AM3/9/16
to bitc...@googlegroups.com
Hello,

I am submitting a transaction with code that looks like this:

request = Wallet.SendRequest.forTx(contract);
wallet.completeTx(request);
transactionBroadcast = peers.broadcastTransaction(request.tx);

From time to time I get two kinds of errors: bad-txns-inputs-spent and bad-txns-too-many-sigops.

The first error is fatal (eg the transaction is not broadcasted), while the later is printed out in the logs, but the transaction ends up in the blockchain eventually.

I am wandering what is the right way to catch these kind of errors in the code.

Do you have any sample code I could look at?

Thanks in advance for any help.

Regards,

Giulio Cesare


Jameson Lopp

unread,
Mar 9, 2016, 9:27:05 AM3/9/16
to bitc...@googlegroups.com
The /best/ thing for you to do would be to keep track of your unspents and count your sigops before broadcasting a transaction. However, you should be able to process rejection messages with a listener like so:

    public Message onPreMessageReceived(Peer peer, Message m) {

        if (m instanceof RejectMessage) {

            RejectMessage rejection = (RejectMessage)m;

            if (rejection.getRejectedMessage().equals("tx")) {

                log.error("Transaction {} was rejected by bitcoind for reason '{}'", rejection.getRejectedObjectHash().toString(), rejection.getReasonString());

                // add other logic to clean up / recreate transaction as needed here

            }

        }*/

        return m;

    }


--
You received this message because you are subscribed to the Google Groups "bitcoinj" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bitcoinj+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

giulio...@gmail.com

unread,
Mar 9, 2016, 9:45:50 AM3/9/16
to bitc...@googlegroups.com
Thanks Jameson,

I think the problem is due to my code that is building a set of transactions spending coins from the same address; and I suppose the wallet is selecting the same outputs also for the second transaction, as the amount are the same, and the first transaction had no time to provide feedback to the wallet to mark the used outputs as spent.

I will try to sort out how to register the callback you have provided and see if this helps in my case.

Thanks for the support.

Regards,

Giulio Cesare


Andreas Schildbach

unread,
Mar 9, 2016, 10:41:05 AM3/9/16
to bitc...@googlegroups.com
On 03/09/2016 03:45 PM, giulio...@gmail.com wrote:

> I think the problem is due to my code that is building a set of
> transactions spending coins from the same address; and I suppose the
> wallet is selecting the same outputs also for the second transaction, as
> the amount are the same, and the first transaction had no time to
> provide feedback to the wallet to mark the used outputs as spent.

In order to prevent that, you could commitTx your first transaction to
the wallet already, marking outputs as spent so they are not selected again.

If you don't do that, PeerGroup will hear back the transaction from the
network and commit it at that occasion (in most cases a couple of
seconds later). Both procedures have the pros and cons.


Reply all
Reply to author
Forward
0 new messages