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

Implement account rewards #335

Open
wants to merge 47 commits into
base: main
Choose a base branch
from

Conversation

lassemand
Copy link
Contributor

@lassemand lassemand commented Dec 19, 2024

Tasks:

https://linear.app/concordium/issue/CCD-79/implement-blockspecial-events
https://linear.app/concordium/issue/CCD-74/implement-accountrewards
https://linear.app/concordium/issue/CCD-85/implement-accountaccount-statement

Purpose

Index special events
Query account statements
Query rewards

We are not including the account statements generated from transactions as part of this PR. Leaving that for the next PR to limit scope of this PR.

Checklist

  • My code follows the style of this project.
  • The code compiles without warnings.
  • I have performed a self-review of the changes.
  • I have documented my code, in particular the intent of the
    hard-to-understand areas.
  • (If necessary) I have updated the CHANGELOG.

@lassemand lassemand changed the title Lma/ccd 74/implement accountrewards [DRAFT] Dec 19, 2024
@lassemand lassemand marked this pull request as draft December 19, 2024 08:12
@lassemand lassemand changed the title [DRAFT] [DRAFT] Implement account rewards Dec 19, 2024
@lassemand lassemand changed the base branch from main to index-scheduled-transfer December 19, 2024 09:24
Base automatically changed from index-scheduled-transfer to main December 19, 2024 12:44
@lassemand lassemand marked this pull request as ready for review December 30, 2024 17:03
@lassemand lassemand marked this pull request as draft December 30, 2024 17:44
@lassemand lassemand changed the title [DRAFT] Implement account rewards Implement account rewards Dec 31, 2024
@lassemand lassemand marked this pull request as ready for review December 31, 2024 09:50
backend-rust/migrations/0001_initialize.up.sql Outdated Show resolved Hide resolved
backend-rust/migrations/0001_initialize.up.sql Outdated Show resolved Hide resolved
backend-rust/migrations/0001_initialize.up.sql Outdated Show resolved Hide resolved
backend-rust/migrations/0001_initialize.up.sql Outdated Show resolved Hide resolved
backend-rust/src/graphql_api.rs Show resolved Hide resolved
backend-rust/src/indexer.rs Show resolved Hide resolved
backend-rust/src/indexer.rs Show resolved Hide resolved
Comment on lines +1034 to +1039
SpecialTransactionOutcome::BlockAccrueReward {
..
}
| SpecialTransactionOutcome::PaydayPoolReward {
..
} => Vec::new(),
Copy link
Contributor

Choose a reason for hiding this comment

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

I am surprised these are not implemented as these also include rewards for accounts

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lets implement that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As this deviates from the existing flow I would like to wait with this. I added a TODO, and will do that after I have validated correctness between the two instances of CCDScan.

Copy link
Contributor

Choose a reason for hiding this comment

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

Pretty sure these outcomes were introduced in a later protocol version, so this might be why the code for handling it is scattered in the old backend.

backend-rust/src/indexer.rs Show resolved Hide resolved
@lassemand lassemand requested a review from limemloh January 3, 2025 10:47
amount,
block_height
FROM account_statements
WHERE entry_type::TEXT IN (
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't think that we need to cast it to TEXT here, comparing enums values seems better.

Suggested change
WHERE entry_type::TEXT IN (
WHERE entry_type IN (


CREATE INDEX account_statements_entry_type_idx ON account_statements (entry_type);

CREATE VIEW account_rewards AS
Copy link
Contributor

@limemloh limemloh Jan 3, 2025

Choose a reason for hiding this comment

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

Not sure how this performs, since the query we need is the rewards for some account_index and this view is first filtering rewards across every account, and then we filter the ones related to the account (scanning potentially). Maybe postgres is smart enough to rearrange to avoid scanning, but a more direct approach would be to have the index:

account_statements (account_index, entry_type)

And then query directly:

SELECT ... FROM account_statements WHERE account_index = $1 AND entry_type IN (...)

Which I expect to scale better

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A view is essentially a stored query that you can treat almost like a table. So performance will be the same.

Even if performance would be an issue this is to be used on a single account. This is by no means a bottleneck of anything, lets not waste time on pseudo optimisations.

let mut account_statements = sqlx::query_as!(
AccountStatementEntry,
r#"
SELECT
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we are missing the subquery for handling the last query parameter, currently it seems that it will behave identically to providing first as we are not ordering a subquery according to query.desc (which is true when last is provided, the bad naming of query.desc is on me)

config.reward_connection_limit,
)?;
let mut rewards = sqlx::query_as!(
AccountReward,
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to above, I think we are missing handling of last query parameter

Comment on lines +1034 to +1039
SpecialTransactionOutcome::BlockAccrueReward {
..
}
| SpecialTransactionOutcome::PaydayPoolReward {
..
} => Vec::new(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Pretty sure these outcomes were introduced in a later protocol version, so this might be why the code for handling it is scattered in the old backend.

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.

2 participants