bitcoinj-cash / bitcoinj

A library for working with Bitcoin
http://bitcoinj.cash
Apache License 2.0
66 stars 35 forks source link

After the wallet address receives the currency, the wallet address will change, resulting in the use of the wallet class for the transfer transaction to prompt the org.bitcoinj.core.InsufficientMoneyException: Insufficient money #36

Open fastener opened 6 years ago

fastener commented 6 years ago
     WalletAppKit kit = new WalletAppKit(params, new File("/tmp/test_address3"), "PRE_FIX_address3") {
    @Override
    protected void onSetupCompleted() {
        if (wallet().getKeyChainGroupSize() < 1) {
            String seedCode = "music luggage more pizza peasant family seminar meat solid trust uncover learn";
            DeterministicSeed seed = null;
            long creationtime = 1524600058l;
            try {
                seed = new DeterministicSeed(seedCode, null, "", creationtime);
            } catch (UnreadableWalletException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            restoreWalletFromSeed(seed);

        }
    }

    @Override
    public PeerGroup peerGroup() {
        // TODO Auto-generated method stub
        PeerGroup peerGroup = null;
        try {
            FullPrunedBlockStore store = new H2FullPrunedBlockStore(this.params, "bch_test3", 1000);
            FullPrunedBlockChain vChain = new FullPrunedBlockChain(this.params, store);
            peerGroup = new PeerGroup(this.params, vChain);
            peerGroup.setFastCatchupTimeSecs(this.wallet().getEarliestKeyCreationTime());
            peerGroup.addPeerDiscovery(new DnsDiscovery(this.params));
            // peerGroup.downloadBlockChain();
        } catch (BlockStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("peergroup error"+e);
        }

        return peerGroup;
    }

};

if (params == RegTestParams.get()) {
    kit.connectToLocalHost();
}

kit.startAsync();

kit.awaitRunning();

kit.wallet().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
    @Override
    public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) {

        Coin value = tx.getValueSentToMe(w);
        System.out.println("Received tx for " + value.toFriendlyString() + ": " + tx);
        System.out.println("Transaction will be forwarded after it confirms.");

        Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<TransactionConfidence>() {
            @Override
            public void onSuccess(TransactionConfidence result) {
                // "result" here is the same as "tx" above, but we use it anyway for clarity.
                // forwardCoins(result);
                System.out.println(result.toString());
            }

            @Override
            public void onFailure(Throwable t) {
            }
        });
    }
});
Address targetAddress = new Address(params, "mgRoeWs2CeCEuqQmNfhJjnpX8YvtPACmCX");
Script script = new ScriptBuilder().op(ScriptOpCodes.OP_RETURN).data("my message mango ".getBytes()).build();
Wallet wallet = kit.wallet();
Transaction tx = new Transaction(params);
tx.addOutput(Coin.CENT, targetAddress);
tx.addOutput(Coin.CENT, script);
System.out.println(kit.wallet().toString());
SendResult sr = wallet.sendCoins(SendRequest.forTx(tx));
Transaction trans = sr.broadcastComplete.get();// future.get();
System.out.println(trans.toString());
System.out.println("hashAsString:" + trans.getHashAsString());

The above is my code, I use TestNet to transfer to my wallet address mhCHrjrvtjnv1of8EN6ffGUe1iazeQUc7i, and then when the code is executed again, the balance of the wallet is rich, but when I turn out, the account balance is insufficient and the wallet information has several addresses. How should I make it correct Transfer your wallet with your wallet? Is there any problem with my code? Thank you very much. The following is the log:

Wallet containing 6.47998695 BCH (spendable: 0.00 BCH) in: 1 pending transactions 0 unspent transactions 1 spent transactions 0 dead transactions Last seen best block: 1228736 (2018-04-24T20:27:32Z): 00000000cd7385aa71e6a3fce425cd496bf70d3aac7d6b425945280b8cd92f63

Keys: Earliest creation time: 2018-04-24T20:00:58Z Seed birthday: 1524600058 [2018-04-24T20:00:58Z] Key to watch: tpubD8rSayx3h35cfkWLBBMv7PGNQK2PaQ6LQptDXoQD3mAtgR3WsxbKDQ1nHfNcELKAEB1qyrvZoAn6e45pHDwEDgtTRC8MWjj9i7PXGQiSbus addr:mhCHrjrvtjnv1of8EN6ffGUe1iazeQUc7i hash160:1268addaddf7b27eb9882b0e8a55ac6047dcbb28 (M/0H/0/0) addr:mxiqQTd759U8taPsEZQy8YZYz46GNvqqHF hash160:bcb960f611b0e3b92d76606d10df9efc7ea33c6d (M/0H/0/1) addr:mfiw2BdhH49DRkHYVD1duYrSMGExS2poAN hash160:0243edaa30bf9ddb8ba876c4ba8b5440432cf2d1 (M/0H/1/0) addr:mzEK14E6ZNLWGdLj5dwbucKWTrT5Nedsgv hash160:cd449a9422484f650a61dcd3dce6d2c3b71c8470 (M/0H/1/1)

PENDING: -0.02001305 BCH total value (sends 6.50 BCH and receives 6.47998695 BCH) confidence: Pending/unconfirmed. Source: SELF be7d834a0192d0ab481b075da112050ec25b5441b0c023dcfb40a3c06f818a64 updated: 2018-04-24T20:12:07Z in PUSHDATA(72)[30450221008a042127ba3a7476c70a40e4e5b2b9f7937c361d8645a124b43ac8efb8c4667602203274ea4d5ce302bda4a066379959e74904efbcb3bf6fa18f4fcf31f884d3031e01] PUSHDATA(33)[02df634709ebc833aebb71b2b12d7fd58950953f02292e1721ef15e7828f085c31] 6.50 BCH outpoint:a9d8ad9c9ec346049063c512cff98d3d86c138d2e0b5f8bbde9eb395dff450a4:0 hash160:1268addaddf7b27eb9882b0e8a55ac6047dcbb28 out RETURN PUSHDATA(23)[6d79206d657373616765206d616e676f20e4b8ade69687] 0.01 BCH out DUP HASH160 PUSHDATA(20)[0243edaa30bf9ddb8ba876c4ba8b5440432cf2d1] EQUALVERIFY CHECKSIG 6.47998695 BCH out DUP HASH160 PUSHDATA(20)[09fed3e08e624b23dbbacc77f7b2a39998351a68] EQUALVERIFY CHECKSIG 0.01 BCH fee 0.00005019 BCH/kB, 0.00001305 BCH for 260 bytes prps USER_PAYMENT

SPENT: 6.50 BCH total value (sends 0.00 BCH and receives 6.50 BCH) confidence: Appeared in best chain at height 1228735, depth 2. Source: NETWORK a9d8ad9c9ec346049063c512cff98d3d86c138d2e0b5f8bbde9eb395dff450a4 updated: 2018-04-24T20:07:15Z in PUSHDATA(72)[3045022100b46976d3427f7cc729b36aef98628ab2e731eff81574504c4b8ce53cf763a987022051870d329fda052f1d722daf4476b9f0a655395cdffd9ad7f4c9b7df66afb14b41] PUSHDATA(33)[022ced4fa5bbdfdb8b4bd3ff84822ebb69178d2622af4eb9c0300e4b164092fd5e] outpoint:5f42d029c1b6611865f3f0e0c8880c57b9f5c06323e99cdda6e87bea542f53fa:1 out DUP HASH160 PUSHDATA(20)[1268addaddf7b27eb9882b0e8a55ac6047dcbb28] EQUALVERIFY CHECKSIG 6.50 BCH Spent by be7d834a0192d0ab481b075da112050ec25b5441b0c023dcfb40a3c06f818a64 out DUP HASH160 PUSHDATA(20)[04ee9419a1c5d35402b349edeb497192c0b26fb1] EQUALVERIFY CHECKSIG 711.55342792 BCH prps UNKNOWN

Apr 25, 2018 4:32:39 AM org.bitcoinj.wallet.Wallet completeTx 信息: Completing send tx with 2 outputs totalling 0.02 BCH and a fee of 0.00005 BCH/kB Apr 25, 2018 4:32:39 AM org.bitcoinj.wallet.Wallet calculateFee 警告: Insufficient value in wallet for send: needed 0.02001 BCH more Exception in thread "main" org.bitcoinj.core.InsufficientMoneyException: Insufficient money, missing 0.02001 BCH at org.bitcoinj.wallet.Wallet.calculateFee(Wallet.java:4970) at org.bitcoinj.wallet.Wallet.completeTx(Wallet.java:3979) at org.bitcoinj.wallet.Wallet.sendCoinsOffline(Wallet.java:3767) at org.bitcoinj.wallet.Wallet.sendCoins(Wallet.java:3840) at org.bitcoinj.wallet.Wallet.sendCoins(Wallet.java:3870) at com.btc.api.test.demo.WalletAppKitTest.main(WalletAppKitTest.java:125)

This is a transfer record:https://www.blocktrail.com/tBCC/tx/a9d8ad9c9ec346049063c512cff98d3d86c138d2e0b5f8bbde9eb395dff450a4

HashEngineering commented 6 years ago

From what I can tell, you ran this app twice after sending 6.50 testnet BCH to this address: addr:mhCHrjrvtjnv1of8EN6ffGUe1iazeQUc7i hash160:1268addaddf7b27eb9882b0e8a55ac6047dcbb28 (M/0H/0/0)

The first time the app ran, it created a 0.02 BCH transaction with a fee and attempted to broadcast it to the network.

PENDING:
-0.02001305 BCH total value (sends 6.50 BCH and receives 6.47998695 BCH)
confidence: Pending/unconfirmed. Source: SELF
be7d834a0192d0ab481b075da112050ec25b5441b0c023dcfb40a3c06f818a64
updated: 2018-04-24T20:12:07Z

However, as one can see from this note, the confidence is Pending/unconfirmed and there is no text about how many peers have received this transaction, which means that most likely none have received it.

Next you ran the app again and the logs indicate that:

Apr 25, 2018 4:32:39 AM org.bitcoinj.wallet.Wallet calculateFee
警告: Insufficient value in wallet for send: needed 0.02001 BCH more

This is because there are no available BCH to spend. Why is this? Because in order to spend BCH, a transaction with coins being spent must meet several rules:

  1. The transaction has at least 1 confirmation
  2. The transaction was created by the app, was broadcast and has more than 1 peer reporting that the transaction was recieved.

Your pending transaction (above) has no peers reporting that they received it. So according to the app, the transaction of sending 0.02 BCH and receiving back the change (6.47 BCH) is stuck and the 6.47 BCH is not available to spend because it doesn't meet rule 2.

Your logs don't have enough information, but there is probably something in them that says that the app is waiting for enough peers to send the first 0.02 BCH transaction.

See issues #17 and #27 concerning broadcasting transactions.