Qortal / qortal

Qortal Project Core - decentralize the world - Data storage, communications, web hosting, decentralized trading, complete infrastructure for the future blockchain based Internet
https://qortal.org
71 stars 59 forks source link

'Dust' error when redeeming P2SH-B #14

Open archived-2 opened 4 years ago

archived-2 commented 4 years ago

It looks like there is a repeatable issue with trades failing to complete. I have encountered this twice now in my two trade attempts.

Problem: Trades get stuck in the BOB_WAITING_FOR_P2SH_B state for the seller, and the ALICE_WATCH_P2SH_B state for the buyer, when performed on the main BTC network.

Details: I have tracked this down to an issue with BTC.getInstance().broadcastTransaction() in TradeBot.handleBobWaitingForP2shB(). This is the transaction that is attempting to redeem the P2SH-B. Electrum nodes respond with the error:

ElectrumX:461 - Response: {"error":"sendrawtransaction RPC error: {\"code\":-26,\"message\":\"dust\"}","id":30,"jsonrpc":"2.0"}

or alternatively:

Response: {"jsonrpc": "2.0", "error": {"code": 1, "message": "the transaction was rejected by network rules.\n\ndust\n

The redeem transaction is broadcast over and over, but never succeeds. To me, it looks as though the amount held in the P2SH-B is too small for the bitcoin RPC on the Electrum nodes to allow it to be broadcast. As a result, the trade is blocked from this point onwards.

Solution: I think I will need some input from cat on how to move forward with this, but I suspect to amount sent to P2SH-B will need to be increased so that it is above the minimum value.

The amount funded to PS2H-B is defined by: private static final long FEE_AMOUNT = 5000L;

However, it appears that the 'dust' threshold varies per Electrum client and isn't a universally known value. Simply adding more Electrum nodes may be enough, however I think we may need to increase the value to ensure the transactions to be broadcast reliably.

archived-2 commented 4 years ago

I'm making some progress. It seems that the 'dust' amount isn't the 5000 sats that the transaction was funded with. It's actually the zero sats that the trade bot is using as the value to redeem.

Coin redeemAmount = Coin.ZERO; // The real funds are in P2SH-A

The actual dust threshold is 3000 sats per kB, and my P2SH-B transaction is 224 bytes. So I'd expect the dust threshold to always be below 1000.

As a test, I tried with Coin redeemAmount = Coin.valueOf(1000); This allowed the transaction to be broadcast, and the trade proceeded to complete.

So the problem is that we are not allowed to redeem zero sats from P2SH-B. In my case it had to be a minimum of 657 sats.

archived-2 commented 4 years ago

One downside of the above approach of redeeming 1000 sats, is that the seller obviously receives 1000 sats more than they are expecting. See the example below - the original agreed sale amount was 0.00165000 BTC, but they received 0.00166000 BTC:

https://www.blockchain.com/btc/address/13aefhjgRcF75ef7b7BUFAHvPEz6fnbSuf

If this is a problem, we could reduce the amount funded into P2SH-A by the same amount.