How do I get number of pending transactions (queued and waiting for confirmation) for an arbitrary blockchain? Do I need to download that blockchain? There should be a more adequate way.
1 Answer
Due to the decentralized nature of blockchains, the state of transactions is relative. A pending transaction is one that your node has seen and verified as valid, yet your node has not seen a block that includes it. This might not be the same state that other nodes see.
For example, one-block chain forks are a common occurrence, where there is a disagreement on the network as to which block is the next valid block. This will usually resolve itself when a block is found on top of one of these two blocks, but in the meantime, there is disagreement about which transactions have been included in a block.
There is also the possibility that your node has not seen a transaction, whereas other nodes (including mining nodes) have. You wouldn't have this transaction in your list of pending transactions, even though there are miners actively attempting to include it in a block. The opposite is also true, where you have seen a transaction, but miners are ignoring it for whatever reason. Bitcoin is actually pretty good about these scenarios, so it normally isn't an issue, but it is a possibility.
In addition, there is the question of a transaction's validity. Even though you can be fairly certain that a transaction is valid without independently verifying it with a downloaded copy of the blockchain, you can only be completely sure if you do independently verify it. If your node is ever wrong or in disagreement with the network about a transaction's validity, then you may consider it pending whereas mining nodes will never include it in a block. Again, the opposite is also possible.
In order to see which transactions your node (assuming Bitcoin or Bitcoin-based) has "pending", you can use the getrawmempool
RPC command.
Further reading:
bitcoind getrawmempool without downloading the full blockchain
Is it possible to view unconfirmed transactions with the bitcoin client?