sigp / lighthouse

Ethereum consensus client in Rust
https://lighthouse.sigmaprime.io/
Apache License 2.0
2.92k stars 743 forks source link

Implement per-validator reward APIs #3661

Open michaelsproul opened 2 years ago

michaelsproul commented 2 years ago

Description

For the protocol fellowship I've proposed a project to add reward calculation APIs to the beacon-APIs standard (https://github.com/eth-protocol-fellows/cohort-three/pull/51). This issue contemplates the design and implementation on the Lighthouse side.

Present Behaviour

Currently we have a few APIs for doing things with rewards, but none are fully satisfactory:

Design Questions

Some main design questions (DQs) that I think we should iterate on are:

DQ1: Bulk or single block/epoch

Should the new reward APIs operate on a single block/epoch or a range? Most of our existing APIs operate on a range because that's substantially more efficient with Lighthouse's database design. However most of the standard beacon-APIs operate only on a single block or epoch at a time, so perhaps we should keep things consistent.

Another point in favour of the single block/single epoch APIs is that we can likely optimise behind the scenes to make them efficient. The ongoing tree-states work will help with this by drastically reducing the size of archive nodes (4TB -> 250GB) and enabling caching of more states in memory (see: https://github.com/sigp/lighthouse/pull/3206).

DQ2: Reward granularity

There's a trade-off between implementation complexity and detail when it comes to the granularity of reward data:

We should also be mindful of the fact that rewards for attestations and sync committee messages can be negative. We should specify the rewards as signed integers.

Implementation Considerations

All of the reward APIs should probably use the BlockReplayer, like the existing reward APIs do.

Block Rewards

One way to calculate a coarse block reward is:

Alternatively we can calculate fine-grained rewards:

Attestation Rewards

I think attestation rewards are a little easier than block rewards. We can compute them for a given epoch by diffing the current_epoch_participation which contains a ParticipationFlags bitfield for each validator. For each bit set (or not set) we can then apply the appropriate reward/penalty based on get_base_reward and the inactivity penalty. See:

https://github.com/sigp/lighthouse/blob/fcfd02aeec435203269b03865e3ccc23e5f51e6d/consensus/state_processing/src/per_epoch_processing/altair/rewards_and_penalties.rs#L75-L90

We may not even want a block replayer for this, we might just want to load the state at the start of epoch n + 1 and diff it against the state at epoch n.

Sync Committee Rewards

Require more thought, TODO.

michaelsproul commented 2 years ago

CC @kevinbogner who is interested in working on this. Feel free to post questions/findings on this issue. Or hit me up on Discord.

Some related reading which might be useful:

ensi321 commented 2 years ago

After getting inputs from @michaelsproul and @potuz, @kevinbogner and I are attempting to answer some of the design questions so we can push forward the discussion: DQ1: I think the reward API should operate on a single block/epoch for the sake of consistency with the rest of the Beacon API. Operating on a range may be more efficient on Lighthouse side but it might not be the case for other clients. DQ2.1: For granularity of the data returned from the api, I think a breakdown of source/target/head vote reward is a yes. And I believe the technical difficulty to implement each of the three should be similar. DQ2.2: For block reward data (ie. sync committee and proposer) it is desirable to provide such breakdown. From Beacon API’s perspective, I think including the block reward is ideal for the sake of completeness. However if we believe such data is not too useful that it’s not worth the development effort we can forget about it

michaelsproul commented 1 year ago

Perfect, I agree with those decisions. If you (@naviechan) and @kevinbogner can put together a draft PR to the beacon-APIs repo then I can review it and we can start implementing! Let me know if you need any assistance.

Thanks!

jimmygchen commented 1 year ago

Last piece of remaining work here: https://github.com/sigp/lighthouse/issues/4512