I took a quick (re)look at the code and sendtoaddress is pretty simple. It does some parsing and error checking and then calls. SendMoneyToDestination()
string CWallet::SendMoneyToDestination(const CTxDestination& address, int64_t nValue, CWalletTx& wtxNew, bool fAskFee)
{
// Check amount
if (nValue <= 0)
return _("Invalid amount");
if (nValue + nTransactionFee > GetBalance())
return _("Insufficient funds");
// Parse Bitcoin address
CScript scriptPubKey;
scriptPubKey.SetDestination(address);
return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee);
}
https://github.com/bitcoin/bitcoin/blob/d980f9b7d687a1e60eecf3691b592d9806a30f4a/src/wallet.cpp#L1467Note the balance check calling GetBalance()
int64_t CWallet::GetBalance() const
{
int64_t nTotal = 0;
{
LOCK(cs_wallet);
for (map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
{
const CWalletTx* pcoin = &(*it).second;
if (pcoin->IsConfirmed())
nTotal += pcoin->GetAvailableCredit();
}
}
return nTotal;
}
https://github.com/bitcoin/bitcoin/blob/d980f9b7d687a1e60eecf3691b592d9806a30f4a/src/wallet.cpp#L961GetBalance gets the sum of confirmed unspent outputs.
The only thing I can think of is at the time you created the second tx the unspent output used WAS CONFIRMED. Possibly there was a short re-org in the blockchain AFTER the second tx was broadcast which made the first tx unconfirmed. Then because of high number of free tx and miners including a limited number you ended up with the first tx not confirming for a while and that led to the second tx not confirming for a while.
IMHO the default behavior for a lot of things probably need to change. I doubt the core devs will see it as a high priority but I think the entire system would be simpler and less prone to unexpected (from user standpoint) behavior if we saw the following.
1) Drop min fee to 0.02 mBTC. The last time this was done the exchange rate was ~$100 USD. The drop would put min fees back to ~$0.01 per KB.
2) Change default behavior of client to include a fee on ALL tx (not just low priority ones) and make the default fee the min fee. Users can override this but they should be warned "including no fee may result in substantial delays before confirmation".
3) Implement "parent pays child".
4) Implement "tx replacement".
Use blogs and other public forums to advocate that authors of other wallets/eWallets/exchanges/etc do the same.