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

F/rewards distribution consumer #104

Merged
merged 46 commits into from
Jan 30, 2025
Merged

Conversation

maurolacy
Copy link
Contributor

@maurolacy maurolacy commented Jan 21, 2025

Closes #102, with the variant in which we process all the distribution on the Consumer.

Implements the rewards distribution's second step (over delegators) on the Consumer.
Closely follows the mesh security impl, adapting it to our use case.

TODO:

  • Queries.
  • Unit tests.
  • Improvements / refactoring.
  • Renaming / review changes.

@maurolacy maurolacy marked this pull request as ready for review January 22, 2025 12:11
Copy link
Member

@SebastianElvis SebastianElvis left a comment

Choose a reason for hiding this comment

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

Some initial comments, still parsing the whole thing

@@ -49,4 +49,5 @@ pub struct Params {
/// expressed as a decimal (e.g. 0.5 for 50%).
#[derivative(Default(value = "String::from(\"0.1\")"))]
pub slashing_rate: String,
pub rewards_denom: String,
Copy link
Member

Choose a reason for hiding this comment

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

Can this be queried from Cosmos SDK or be retrieved here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Queried using the query_bonded_denom, which makes sense for consistency with other parts of the code.

That said, rewards denom and bonded denom are not necessarily the same. This is something we will need to address at some point.

contracts/btc-staking/src/staking.rs Outdated Show resolved Hide resolved
contracts/btc-staking/src/staking.rs Show resolved Hide resolved
.collect()
}

fn distribute_rewards(
Copy link
Member

Choose a reason for hiding this comment

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

Does this function distributes commission to FP's address, or this happens somewhere else?

Copy link
Contributor Author

@maurolacy maurolacy Jan 27, 2025

Choose a reason for hiding this comment

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

FP commissions are not implemented yet. I suggest we do it in a follow-up.

contracts/btc-staking/src/state/delegations.rs Outdated Show resolved Hide resolved
contracts/btc-staking/src/state/delegations.rs Outdated Show resolved Hide resolved
Comment on lines 23 to 28
// Delegations by finality provider's public key and staking hash.
// Last type param defines the pk deserialization type
pub rev: MultiIndex<'a, (String, Vec<u8>), Delegation, (Vec<u8>, String)>,
// Delegations by staker's (raw, canonical) address and finality provider's public key.
// Last type param defines the pk deserialization type
pub staker: MultiIndex<'a, (Vec<u8>, String), Delegation, (Vec<u8>, String)>,
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if I understand these two multi index:

  • Why would the key contain both raw and canonical addresses? Probably raw address is good enough as the reward distribution does not care about the account prefix at all?
  • What is the definition of rev?
  • Why should we use multi index and differentiate key / primary key?

Copy link
Contributor Author

@maurolacy maurolacy Jan 24, 2025

Choose a reason for hiding this comment

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

The key does not contain both, just raw. We use raw addresses because CanonicalAddr does not implement PrimaryKey (a technicality). Perhaps we can simply store the raw key in the Delegations struct instead of CanonicalAddr for clarity. It's also more compact, and so, good for storage savings.

rev is a reverse index of delegations per FP. It can be used to replace the DELEGATIONS_FPS store entirely. Same with the direct key, which can be used to replace the current FP_DELEGATIONS.

There's already a TODO in the code for that. Will address in a follow-up.

contracts/btc-staking/src/state/points_alignment.rs Outdated Show resolved Hide resolved
@maurolacy
Copy link
Contributor Author

maurolacy commented Jan 27, 2025

This is now in a good shape to merge IMO. Please take another look when you find the time.

Follow-ups to this (different PRs):

  • FP commissions.
  • Send rewards during claiming to Babylon, using ICS-020 instead of Bank send (alternative to distribute rewards on Babylon).
  • Extra refactors / simplifications:
    • Remove delegation distribution instead of reducing.
    • Use the delegations() map and indexes to replace the DELEGATION_FPS and FP_DELEGATIONS maps.
    • ...

Copy link
Member

@SebastianElvis SebastianElvis left a comment

Choose a reason for hiding this comment

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

Still parsing all the math here 👍 Some initial comments, but there does not seem any blocker.

Will take a look at ADR as well

contracts/btc-finality/src/multitest/suite.rs Outdated Show resolved Hide resolved
contracts/btc-finality/src/contract.rs Show resolved Hide resolved
contracts/btc-finality/src/contract.rs Show resolved Hide resolved
contracts/btc-staking/src/staking.rs Outdated Show resolved Hide resolved
@maurolacy maurolacy force-pushed the f/rewards-distribution-consumer branch from eba2f42 to 2497493 Compare January 29, 2025 12:35
Copy link
Member

@SebastianElvis SebastianElvis left a comment

Choose a reason for hiding this comment

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

That's quite some heavylifting, great work!

@maurolacy
Copy link
Contributor Author

Thanks for the support and comments! Will now work in the follow-ups, to put all this into a really good shape.

@maurolacy maurolacy merged commit fdf7ebc into main Jan 30, 2025
6 checks passed
@maurolacy maurolacy deleted the f/rewards-distribution-consumer branch January 30, 2025 07:34
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.

Native rewards distribution protocol
2 participants