Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Token amount history #498

Closed
wants to merge 10 commits into from
Closed

Token amount history #498

wants to merge 10 commits into from

Conversation

tdroxler
Copy link
Member

Resolves #497

High priority as it blocks the next release of the wallet

It might looks like a huge PR, but it's mostly some functions extracted and some other copy/pasted and adapted from our current amount-history for ALPH endpoint.

The only "real" new part is the new token_inputs table, we need it in order to compute the diff of token amount.

For example the sumAddressToken* are taken from here

@polarker
Copy link
Member

What is the query complexity of this endpoint? It looks like very expensive to support

@tdroxler
Copy link
Member Author

It's the same as the complexity as the one we already have to show the amount history for ALPH, it's used in the wallet to show the graph evolution of price.

for hourly we have a max of 7 days: 7 * 24 = 168 and daily is 365 days.

For each of those time intervals we sum the input and output amount, separately so no join. so it's a linear pass in both table. There are indexes on timestamp so don't need to pass the all tables.

So 2 tables, max 365 times a linear query.

Comment on lines +305 to +309
run(
for {
in <- sumAddressTokenInputs(address, token, from, to)
out <- sumAddressTokenOutputs(address, token, from, to)
} yield (in, out, to)
Copy link
Member

@simerplaha simerplaha Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question: The two tables being queried here are token_output and token_inputs, these tables are inserted by BlockFlowSyncService within this function, non-transactionally and also concurrently. Could there be a situation where for an address, the above two sum queries return more for the sum of token-output than token-inputs or vice versa? Or in general, could it return out of sync data for input and output for an address?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, it could have a small time interval where it could happen. I need to check if we could have insertBlocks as a DBAction rather than a Future, otherwise it will be complicated to have a transactionally query

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm it's complicated to move to full DBAction, during the insert, if we miss the parent we need to recover that and fetch the missing parent from the node, so it's a Future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is a bug right? The possibility that the explorer could display n outputs for an address with n - 1 input? Should this be addressed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I'll create an issue

(intervalType.duration - Duration.ofMillisUnsafe(1)).get
)
FlowableUtil.getAmountHistory(from, to, intervalType, paralellism) { case (fromTs, toTs) =>
getInOutAmount(address, fromTs, toTs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another question: Since this query getInOutAmount is being invoked multiple times for a single address with different timestamps ranges, would it simpler and more efficient to fetch this data in a single query? Since address and timestamps are both indexed.

For example (untested):

SELECT SUM(output_ref_amount)
      FROM inputs
WHERE ...
    AND (block_timestamp BETWEEN $fromTime1 AND $toTime1)
    OR (block_timestamp BETWEEN $fromTime2 AND $toTime2)
...

Totally understandable if reactive stream is preferred instead, for back-pressure and other fancy stuff that comes with it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very good point, as this query is limited to max 365 days, I could give a try and check performance. Backpresure and streaming is more important for the txs exporting endpoint.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this in #499 I'll change that PR once merged

@tdroxler
Copy link
Member Author

on hold until #499 is merged

@tdroxler
Copy link
Member Author

tdroxler commented Feb 5, 2024

I'm closing as we discussed with the team and we will rather download the transactions history and get the token amount history from there

@tdroxler tdroxler closed this Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to get the amount history for tokens
3 participants