HathorNetwork / hathor-core

Hathor core is the official and reference client for operating a full node in Hathor Network.
https://hathor.network
Apache License 2.0
82 stars 25 forks source link

How to get all txs in one block? #357

Closed Wangmmx closed 2 years ago

Wangmmx commented 2 years ago

Hello, I recently found that there are some transactions that cannot be obtained through blocks. Because of the dag structure, there will be cases where previous transactions are missed. I want to ask if there is a way to get all transactions through blocks. Only two txs and one parent block can be obtained now

like tx 00000f49eb03c1ee62a48a74a48eef9a98a9e46446648eb165596ff71cdbc741 should be in block 2094004, but when I got block details, I can not found the tx

luislhl commented 2 years ago

There is no such thing as txs in a block in Hathor, because txs are not inside the blocks, blocks just point to txs (their parents) in the DAG and confirm them.

However, a tx could have no block as a child, only other txs. This means it would be confirmed by a block only indirectly.

If you check the nice image at the end of https://s3.amazonaws.com/hathor-public-files/hathor-executive-summary.pdf, you will see a visual representation of this.

The tx you mentioned, which is https://explorer.hathor.network/transaction/00000f49eb03c1ee62a48a74a48eef9a98a9e46446648eb165596ff71cdbc741, should not be in block 2094004, because txs are not in blocks.

But the Explorer does states this blocks as the First Block, which just means that this is the first block confirming this tx, not that it confirms the tx directly.

Wangmmx commented 2 years ago

There is no such thing as txs in a block in Hathor, because txs are not inside the blocks, blocks just point to txs (their parents) in the DAG and confirm them.

However, a tx could have no block as a child, only other txs. This means it would be confirmed by a block only indirectly.

If you check the nice image at the end of https://s3.amazonaws.com/hathor-public-files/hathor-executive-summary.pdf, you will see a visual representation of this.

The tx you mentioned, which is https://explorer.hathor.network/transaction/00000f49eb03c1ee62a48a74a48eef9a98a9e46446648eb165596ff71cdbc741, should not be in block 2094004, because txs are not in blocks.

But the Explorer does states this blocks as the First Block, which just means that this is the first block confirming this tx, not that it confirms the tx directly.

Thanks for your patient reply.

Well as you said, the txs are not in a block, is there any way to get all the transactions? There are many missing transactions. Is there any way to obtain it? Or is there any other structure to store all transactions?

luislhl commented 2 years ago

If you need to get all transactions that were first confirmed by a specific block, you will need to walk the DAG, beginning at the block and perform a search in the DAG to find all txs confirmed by it.

Your stop conditions in the search would be:

I'll try to show part of this process using curl, as an example, but the same idea could be coded as well.

If you wanted all txs of block 2094004, for example:

First get the block parents:

curl https://node1.mainnet.hathor.network/v1a/transaction\?id\=00000000000000000e0bdaf9cb0ed2a171b847d26c1dc6f67bd02944d6d9dead

[...]
    "tx": {
        "hash": "00000000000000000e0bdaf9cb0ed2a171b847d26c1dc6f67bd02944d6d9dead",
        "timestamp": 1641337318,
        "version": 3,
        "weight": 67.9170140913311,
        "parents": [
            "00000000000000000a0bec2eb29e7083c0824ea56c815667a5c0ad104611d09f",
            "00005224326b010afd6e2888e713e52093e34d0d71440cc15041cc46ca9e1139",
            "000039fd09214523cbc7fc7b23ae64e6ac49df5cb806d6504f8e6ccd2bc4eeb6"
        ],
[...]

Then you will need to get the 3 parents using the same curl request and check their metadata.

One of them will be another block, and you will stop walking it. You will know it's a block if it has more than 2 parents.

The other 2 will be transactions. You will need to check if their meta.first_block is equal to 00000000000000000e0bdaf9cb0ed2a171b847d26c1dc6f67bd02944d6d9dead and that meta.voided_by is empty.

If the conditions are true, them you store this tx in your results and continue walking in the DAG by getting their parents, until you get one tx that doesn't pass our 2 checks.

There is an example of this algorithm in one of our services here: https://github.com/HathorNetwork/hathor-wallet-service/blob/v1.4.2-beta/src/utils.ts#L116 (This is pinned to an old version of the file, because this project went through a major refactor and newer versions do not contain it anymore)

Wangmmx commented 2 years ago

Thank you so much, thank you for answering in such detail, I'll try it now

luislhl commented 2 years ago

Closing, but if needed we can reopen it.

Wangmmx commented 2 years ago

Thanks a lot! I Sorry I saw the reply a few days late, I re-optimized my code in the way you said, it seems to meet the needs, no missing transactions, thanks again