ElementsProject / elements

Open Source implementation of advanced blockchain features extending the Bitcoin protocol
MIT License
1.06k stars 380 forks source link

Missing the asset balance after the transactions is trusted false #884

Open rahuldotar opened 4 years ago

rahuldotar commented 4 years ago
{
    "address": "xxxxxxxx",
    "category": "send",
    "amount": -100000.00000000,
    "amountblinder": "0000000000000000000000000000000000000000000000000000000000000000",
    "asset": "xxxxxxxx",
    "assetblinder": "0000000000000000000000000000000000000000000000000000000000000000",
    "vout": 0,
    "fee": 0.00026420,
    "confirmations": 0,
    "trusted": "false",
    "txid": "xxxxxx",
    "walletconflicts": [

    ],
    "time": xxxx,
    "timereceived": xxxx,
    "bip125-replaceable": "no",
    "abandoned": "false"
  }

After this transaction, the asset balance is not showing as expected. When I check the transaction history using listtransaction function and manually calculate the balance it is not matching with the getbalance function.

stevenroose commented 4 years ago

I don't really understand what situation you're in. Could you give a bit more information?

rahuldotar commented 4 years ago

I created the Elements blockchain network and implemented the Block signing federation using Kafka. Then I issued an asset and did the multiple transactions. After 2 multiple consecutive transactions with the same amount from the same wallet and one of that transaction failed. And I noticed that in the transaction details the param trusted is return as false and I lost the remaining balance in that wallet.

stevenroose commented 4 years ago

This is how the "trusted" field works:

bool CWalletTx::IsTrusted(interfaces::Chain::Lock& locked_chain) const
{
    LockAnnotation lock(::cs_main); // Temporary, for CheckFinalTx below. Removed in upcoming commit.

    // Quick answer in most cases
    if (!CheckFinalTx(*tx))
        return false;
    int nDepth = GetDepthInMainChain(locked_chain);
    if (nDepth >= 1)
        return true;
    if (nDepth < 0)
        return false;
    if (!pwallet->m_spend_zero_conf_change || !IsFromMe(ISMINE_ALL)) // using wtx's cached debit
        return false;

    // Don't trust unconfirmed transactions from us unless they are in the mempool.
    if (!InMempool())
        return false;

    // Trusted if all inputs are from us and are in the mempool:
    for (const CTxIn& txin : tx->vin)
    {
        // Transactions not sent by us: not trusted
        const CWalletTx* parent = pwallet->GetWalletTx(txin.prevout.hash);
        if (parent == nullptr)
            return false;
        const CTxOut& parentOut = parent->tx->vout[txin.prevout.n];
        if (pwallet->IsMine(parentOut) != ISMINE_SPENDABLE)
            return false;
    }
    return true;
}

So it can be that the tx is dropped from the mempool for some reason. Did you accidentally double-spend the same coins?