neo-node icon indicating copy to clipboard operation
neo-node copied to clipboard

getbalance lags behind getaccountstate

Open efpies opened this issue 6 years ago • 5 comments

I need to wait until the initial NEO is claimed (sent from the multisig address to AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y) when the private net is started.

{
  "jsonrpc": "2.0",
  "method": "getaccountstate",
  "params": [
    "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"
  ],
  "id": 1
}

On the surface, it seems fine:

{
  "txid": "0xdc44739e2f97743f2ed258988327560e2185ed13eec0097938eef4aea584bf04",
  "size": 436,
  "type": "ContractTransaction",
  "version": 0,
  "attributes": [],
  "vin": [
    {
      "txid": "0x7aadf91ca8ac1e2c323c025a7e492bee2dd90c783b86ebfc3b18db66b530a76d",
      "vout": 0,
    }
  ],
  "vout": [
    {
      "n": 0,
      "asset": "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
      "value": 100000000,
      "address": "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"
    }
  ],
  "sys_fee": 0,
  "net_fee": 0,
  "scripts": [
    {
      "invocation": "40dab55e94aab0949cff0ece9f4988828bdcd9ffc47caaa1ee99cae27d32bbc3700eb01789f48197ee7cb793307530bda1e58482465ef2f74257f0874ce84ed19440cfb9fddf05bf1594f5d634d609bd988149685c09b8804dcf537c854ce4ffe71b785443e300745d12146cd58927bc73d28e4b2af5d3a786b32cbd080176f39044404cbd9dfb913a04de7d26f37f0d0333e74846c6ab26fe5c3ad7cbb6020c58685f1c2b89649b77fc40420553a4af91cba9e2c0ada1a1e368a42e7636c660f70334",
      "verification": "532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae"
    }
  ],
  "blockhash": "0x9a1fa4f637e28e1b538573b0dd4f59a5064233c370ea5ec292eab77883bd0623",
  "blocktime": 1557864100,
  "confirmations": 1
}
{
  "version": 0,
  "script_hash": "0xe9eed8dc39332032dc22e5d6e86332c50327ba23",
  "frozen": false,
  "balances": [
    {
      "asset": "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
      "value": 100000000
    }
  ]
}

But when I send funds from that node, in 50% of cases I get the Insufficient funds error. So I tried to query balance at the moment I'm querying the account state.

{
  "jsonrpc": "2.0",
  "method": "getbalance",
  "params": [
    "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b"
  ],
  "id": 1
}

And in 50% of cases it's 0.

{
  "balance": 0,
  "confirmed": 0
}

How come? If the account state is updated, which means the tx is apparently mined and known to the node, why wallet balances aren't updated immediately?

That also means that I can't rely on checking if a tx is mined and known to the node to be sure the balances are up-to-date.

efpies avatar May 14 '19 20:05 efpies

Hi @efpies, here is the documentation of this call: https://docs.neo.org/en-us/node/cli/2.9.4/api/getbalance.html

The wallet needs to be open and the specific asset should be queried.

I will close this issue, but fell free to reopen in case you find something strange.

vncoelho avatar May 14 '19 23:05 vncoelho

Wallet is open and the specific asset is queried:

{
  "jsonrpc": "2.0",
  "method": "getbalance",
  "params": [
    "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b"
  ],
  "id": 1
}

I'm not saying it's always zero. If it always was zero, opening a wallet would help, but that's not the case.

I'm saying that it works, but not immediately. I have to wait a few seconds before getbalance values are updated after the client has got a confirmed transaction (which means the blockchain is fully synchronized), though getaccountstate returns the correct value immediately. So far it seems that I'm following the instruction correctly but getting unexpected results.

If getaccountstate returns even unconfirmed balances, it would be great to describe it in the docs. But that would not explain why even having a confirmed tx doesn't mean the balance is already updated. I guess, this side-effect might as well be documented. If it's not a bug, of course.


Also, I can't reopen issues that were closed by repo collaborators.

efpies avatar May 15 '19 10:05 efpies

To be more clear, unconfirmed balances don't affect getaccountstate:

[20:22:37.955 INF] NeoController >> Balance: {}
[20:22:37.960 INF] NeoController >> AccountState: {
  "script_hash": "0x874bbe9d60e332f5b6801c97c5ce3731f025f53f",
  "balances": []
}
[20:22:37.975 INF] NeoController >> Tx: {
  "txid": "0x87084d777fbe18b2ae23ae4ad5e25a7000c6d7e2a9d1c334f5d37f2078385b5d",
  "size": 262,
  "type": "ContractTransaction",
  "attributes": [],
  "vin": [
    {
      "txid": "0xdc44739e2f97743f2ed258988327560e2185ed13eec0097938eef4aea584bf04"
    }
  ],
  "vout": [
    {
      "asset": "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
      "value": 2.0,
      "address": "AMc3tn8b6QhPh9ARn72DcCfgxRZXzEKXEx"
    },
    {
      "n": 1,
      "asset": "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
      "value": 99999998.0,
      "address": "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"
    }
  ],
  "scripts": [
    {
      "invocation": "401ba938cc732b37e1a079729a08f7862f859756d31e6db1010286ce0d475cdc2db001d8c3942f9c5326ef1d0962c6fb9bafded48426c7b8a5f897174fa8da25a8",
      "verification": "21031a6c6fbbdf02ca351745fa86b9ba5a9452d785ac4f7fc2b7548ca2a46c4fcf4aac"
    }
  ]
}

The balance is updated roughly after 1.5 s after the wallet has been synchronized.

This is how it looks like after the tx was mined and the wallet has synchronized with the blockchain. getaccountstate displays the new balance immediately:

[20:22:38.494 INF] NeoController >> Balance: {}
[20:22:38.498 INF] NeoController >> AccountState: {
  "script_hash": "0x874bbe9d60e332f5b6801c97c5ce3731f025f53f",
  "balances": [
    {
      "asset": "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
      "value": 2.0
    }
  ]
}
[20:22:38.507 INF] NeoController >> Tx: {
  "blockhash": "0xaa49105ec0d8fafe3b24ac2a20a753889f0bd13253e474a6c3bb38cf98119d49",
  "blocktime": 1557940942,
  "confirmations": 1,
  "txid": "0x87084d777fbe18b2ae23ae4ad5e25a7000c6d7e2a9d1c334f5d37f2078385b5d",
  "size": 262,
  "type": "ContractTransaction",
  "attributes": [],
  "vin": [
    {
      "txid": "0xdc44739e2f97743f2ed258988327560e2185ed13eec0097938eef4aea584bf04"
    }
  ],
  "vout": [
    {
      "asset": "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
      "value": 2.0,
      "address": "AMc3tn8b6QhPh9ARn72DcCfgxRZXzEKXEx"
    },
    {
      "n": 1,
      "asset": "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
      "value": 99999998.0,
      "address": "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"
    }
  ],
  "scripts": [
    {
      "invocation": "401ba938cc732b37e1a079729a08f7862f859756d31e6db1010286ce0d475cdc2db001d8c3942f9c5326ef1d0962c6fb9bafded48426c7b8a5f897174fa8da25a8",
      "verification": "21031a6c6fbbdf02ca351745fa86b9ba5a9452d785ac4f7fc2b7548ca2a46c4fcf4aac"
    }
  ]
}

But getbalance is out of sync for 1.5 s more after that:

[20:22:40.076 INF] NeoController >> Balance: {
  "balance": 2.0,
  "confirmed": 2.0
}
[20:22:40.080 INF] NeoController >> AccountState: {
  "script_hash": "0x874bbe9d60e332f5b6801c97c5ce3731f025f53f",
  "balances": [
    {
      "asset": "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
      "value": 2.0
    }
  ]
}
[20:22:40.091 INF] NeoController >> Tx: {
  "blockhash": "0xaa49105ec0d8fafe3b24ac2a20a753889f0bd13253e474a6c3bb38cf98119d49",
  "blocktime": 1557940942,
  "confirmations": 1,
  "txid": "0x87084d777fbe18b2ae23ae4ad5e25a7000c6d7e2a9d1c334f5d37f2078385b5d",
  "size": 262,
  "type": "ContractTransaction",
  "attributes": [],
  "vin": [
    {
      "txid": "0xdc44739e2f97743f2ed258988327560e2185ed13eec0097938eef4aea584bf04"
    }
  ],
  "vout": [
    {
      "asset": "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
      "value": 2.0,
      "address": "AMc3tn8b6QhPh9ARn72DcCfgxRZXzEKXEx"
    },
    {
      "n": 1,
      "asset": "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
      "value": 99999998.0,
      "address": "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"
    }
  ],
  "scripts": [
    {
      "invocation": "401ba938cc732b37e1a079729a08f7862f859756d31e6db1010286ce0d475cdc2db001d8c3942f9c5326ef1d0962c6fb9bafded48426c7b8a5f897174fa8da25a8",
      "verification": "21031a6c6fbbdf02ca351745fa86b9ba5a9452d785ac4f7fc2b7548ca2a46c4fcf4aac"
    }
  ]
}

efpies avatar May 15 '19 17:05 efpies

@shargon, can you take a look at this issue and see if a fix is really necessary?

vncoelho avatar Aug 07 '19 14:08 vncoelho

This is because the utxo need to be confirmed by the chain, will be fixed in neo3 because we don't have utxo

shargon avatar Aug 07 '19 20:08 shargon

Old, if remains, please re-open

shargon avatar Dec 05 '23 13:12 shargon