archethic-foundation / archethic-node

Official Archethic Blockchain node, written in Elixir
GNU Affero General Public License v3.0
72 stars 22 forks source link

Get List of transactions in a transaction from the newest to the oldest with paging management #373

Closed redDwarf03 closed 1 year ago

redDwarf03 commented 2 years ago

Problem to solve

The PR https://github.com/archethic-foundation/archethic-node/pull/314 allowed to get a list of transactions order by index in the chain ascendant So if we want to obtain the last 10 transactions for example in a transaction chain with 1000 transactions, we should browse 100 lists to find the 10 last transactions.

This need arise from the wallet that should display the last transactions to a user and i don't think it's a good way to loop the getTransactionChain API to obtain the last tx

Solution

Provide an API to get the n last transactions with a paging management to allow a dynamic loading

ghost commented 2 years ago

To provide this kind of mechanism we have to think of a way to scan file in reverse order with efficiency. This is not the best usage of files as sequential reads are more efficient than random ones.

A way of doing it could be to use a magic byte as delimiter in the chain file so we can seek at the end of the filez read back and reverse bytes to get the binary in the right order.

Another way would be to scan the entire file from the beginning and using some cycle buffer to eject the first in if it's full (i.e > 10txs)

Another approach would be to use an index(existing or new) to do sequential reads until a limit.

Neylix commented 2 years ago

Actually a good way to get the 10 last transactions is to get the last index of the chain (via API) substract 11 and derive address at this index. So you have the pagination state to get the last 10 transactions.

pagination_address = derive_address(seed, last_index - 11 )
transactions = get_transaction_chain(genesis_address, pagination_address)

But this works only if you know the seed

samuelmanzanera commented 1 year ago

Closed by #452