Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

org.bitcoinj.core.InsufficientMoneyException: Insufficient money, missing 0.01001546 BCH #35

Open
fastener opened this issue Apr 23, 2018 · 5 comments

Comments

@fastener
Copy link

https://www.blocktrail.com/tBCC/address/mnzWQVW2iwgNccCW9VgfuW94PuK1vL17uV
Account balances are adequate, and transfers always indicate insufficient balance. The balance found through the import wallet is also correct. Why?

org.bitcoinj.core.InsufficientMoneyException: Insufficient money, missing 0.01001546 BCH

@HashEngineering
Copy link
Collaborator

To diagnose the issue, more information is needed. What do your logs say? How many peers do you have? How many confirmations are on the last transaction?

@fastener
Copy link
Author

I found seedCode unchanged, but when the current address receives a transaction, the address returned by the wallet.currentReceiveAddress will be changed, so when I use this purse to turn out, the address becomes a new address. The new address account balance is 0. How should I use the amount I receive when I receive the amount of the account? The current address is transferred out of the transaction?

@HashEngineering
Copy link
Collaborator

If you are using the Wallet class, it handles all of this for you.

If you create a Wallet with a seed (DeterministicSeed) and call wallet.currentReceiveAddress() it will return m/0'/0'/0 (the first address of the external chain of account 0). Once m/0'/0'/0 receives a transaction, wallet.currentReceiveAddress() returns the next unused address m/0'/0'/1.

At this point your wallet contains:
m/0'/0'/0 - balance of 1 BCH (let us say you received 1 BTC in the transaction).
m/0'/0'/1 - balance of 0 BCH

If your next task to to send a transaction, the wallet will determine that you will send the coins from the first address (m/0'/0'/0) and will place the change if any in m/0'/1'/0 (the first address of the internal chain of account 0).

At this point your wallet contains:
m/0'/0'/0 - balance of 0 BCH
m/0'/0'/1 - balance of 0 BCH
m/0'/1'/0 - balance of 0.5 BCH (if you sent 0.5 to the someone else).

Not sure if this helps you. There is a wallet dump function which can give more detailed information on the transactions and the addresses and their HD paths.

@fastener
Copy link
Author

fastener commented Apr 24, 2018

         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
Copy link
Collaborator

HashEngineering commented Apr 25, 2018

I answered this in #36.

The issue appears to be caused by not broadcasting transactions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants