Skip to content

Commit 86298a4

Browse files
authored
refactor: move reward apis to state-transition (#8719)
**Motivation** - the reward apis tightly couple to state-transition functions like `beforeProcessEpoch() processBlock() processAttestationAltair()` so it needs to be moved there **Description** - move api type definitions to `types` package so that it can be used everywhere - move reward apis implementation to `state-transition` package Closes #8690 --------- Co-authored-by: Tuyen Nguyen <twoeths@users.noreply.github.com>
1 parent 075956b commit 86298a4

File tree

23 files changed

+175
-197
lines changed

23 files changed

+175
-197
lines changed

packages/api/src/beacon/routes/beacon/index.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ export {block, pool, state, rewards};
1313

1414
export type {BlockHeaderResponse, BlockId} from "./block.js";
1515
export {BroadcastValidation} from "./block.js";
16-
export type {
17-
AttestationsRewards,
18-
BlockRewards,
19-
IdealAttestationsReward,
20-
SyncCommitteeRewards,
21-
TotalAttestationsReward,
22-
} from "./rewards.js";
2316
// TODO: Review if re-exporting all these types is necessary
2417
export type {
2518
EpochCommitteeResponse,

packages/api/src/beacon/routes/beacon/pool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {ValueOf} from "@chainsafe/ssz";
22
import {ChainForkConfig} from "@lodestar/config";
33
import {ForkPostElectra, ForkPreElectra, isForkPostElectra} from "@lodestar/params";
44
import {
5+
ArrayOf,
56
AttesterSlashing,
67
CommitteeIndex,
78
SingleAttestation,
@@ -12,7 +13,6 @@ import {
1213
ssz,
1314
} from "@lodestar/types";
1415
import {
15-
ArrayOf,
1616
EmptyArgs,
1717
EmptyMeta,
1818
EmptyMetaCodec,

packages/api/src/beacon/routes/beacon/rewards.ts

Lines changed: 8 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,12 @@
1-
import {ContainerType, ValueOf} from "@chainsafe/ssz";
21
import {ChainForkConfig} from "@lodestar/config";
3-
import {Epoch, ssz} from "@lodestar/types";
4-
import {ArrayOf, JsonOnlyReq} from "../../../utils/codecs.js";
2+
import {Epoch, rewards} from "@lodestar/types";
3+
import {JsonOnlyReq} from "../../../utils/codecs.js";
54
import {Endpoint, RouteDefinitions, Schema} from "../../../utils/index.js";
65
import {ExecutionOptimisticAndFinalizedCodec, ExecutionOptimisticAndFinalizedMeta} from "../../../utils/metadata.js";
76
import {fromValidatorIdsStr, toValidatorIdsStr} from "../../../utils/serdes.js";
87
import {BlockArgs} from "./block.js";
98
import {ValidatorId} from "./state.js";
109

11-
const BlockRewardsType = new ContainerType(
12-
{
13-
/** Proposer of the block, the proposer index who receives these rewards */
14-
proposerIndex: ssz.ValidatorIndex,
15-
/** Total block reward, equal to attestations + sync_aggregate + proposer_slashings + attester_slashings */
16-
total: ssz.UintNum64,
17-
/** Block reward component due to included attestations */
18-
attestations: ssz.UintNum64,
19-
/** Block reward component due to included sync_aggregate */
20-
syncAggregate: ssz.UintNum64,
21-
/** Block reward component due to included proposer_slashings */
22-
proposerSlashings: ssz.UintNum64,
23-
/** Block reward component due to included attester_slashings */
24-
attesterSlashings: ssz.UintNum64,
25-
},
26-
{jsonCase: "eth2"}
27-
);
28-
29-
const AttestationsRewardType = new ContainerType(
30-
{
31-
/** Reward for head vote. Could be negative to indicate penalty */
32-
head: ssz.UintNum64,
33-
/** Reward for target vote. Could be negative to indicate penalty */
34-
target: ssz.UintNum64,
35-
/** Reward for source vote. Could be negative to indicate penalty */
36-
source: ssz.UintNum64,
37-
/** Inclusion delay reward (phase0 only) */
38-
inclusionDelay: ssz.UintNum64,
39-
/** Inactivity penalty. Should be a negative number to indicate penalty */
40-
inactivity: ssz.UintNum64,
41-
},
42-
{jsonCase: "eth2"}
43-
);
44-
45-
const IdealAttestationsRewardsType = new ContainerType(
46-
{
47-
...AttestationsRewardType.fields,
48-
effectiveBalance: ssz.UintNum64,
49-
},
50-
{jsonCase: "eth2"}
51-
);
52-
53-
const TotalAttestationsRewardsType = new ContainerType(
54-
{
55-
...AttestationsRewardType.fields,
56-
validatorIndex: ssz.ValidatorIndex,
57-
},
58-
{jsonCase: "eth2"}
59-
);
60-
61-
const AttestationsRewardsType = new ContainerType(
62-
{
63-
idealRewards: ArrayOf(IdealAttestationsRewardsType),
64-
totalRewards: ArrayOf(TotalAttestationsRewardsType),
65-
},
66-
{jsonCase: "eth2"}
67-
);
68-
69-
const SyncCommitteeRewardsType = ArrayOf(
70-
new ContainerType(
71-
{
72-
validatorIndex: ssz.ValidatorIndex,
73-
reward: ssz.UintNum64,
74-
},
75-
{jsonCase: "eth2"}
76-
)
77-
);
78-
79-
/**
80-
* Rewards info for a single block. Every reward value is in Gwei.
81-
*/
82-
export type BlockRewards = ValueOf<typeof BlockRewardsType>;
83-
84-
/**
85-
* Rewards for a single set of (ideal or actual depending on usage) attestations. Reward value is in Gwei
86-
*/
87-
export type AttestationsReward = ValueOf<typeof AttestationsRewardType>;
88-
89-
/**
90-
* Rewards info for ideal attestations ie. Maximum rewards could be earned by making timely head, target and source vote.
91-
* `effectiveBalance` is in Gwei
92-
*/
93-
export type IdealAttestationsReward = ValueOf<typeof IdealAttestationsRewardsType>;
94-
95-
/**
96-
* Rewards info for actual attestations
97-
*/
98-
export type TotalAttestationsReward = ValueOf<typeof TotalAttestationsRewardsType>;
99-
100-
export type AttestationsRewards = ValueOf<typeof AttestationsRewardsType>;
101-
102-
/**
103-
* Rewards info for sync committee participation. Every reward value is in Gwei.
104-
* Note: In the case that block proposer is present in `SyncCommitteeRewards`, the reward value only reflects rewards for
105-
* participating in sync committee. Please refer to `BlockRewards.syncAggregate` for rewards of proposer including sync committee
106-
* outputs into their block
107-
*/
108-
export type SyncCommitteeRewards = ValueOf<typeof SyncCommitteeRewardsType>;
109-
11010
export type Endpoints = {
11111
/**
11212
* Get block rewards
@@ -116,7 +16,7 @@ export type Endpoints = {
11616
"GET",
11717
BlockArgs,
11818
{params: {block_id: string}},
119-
BlockRewards,
19+
rewards.BlockRewards,
12020
ExecutionOptimisticAndFinalizedMeta
12121
>;
12222

@@ -133,7 +33,7 @@ export type Endpoints = {
13333
validatorIds?: ValidatorId[];
13434
},
13535
{params: {epoch: number}; body: string[]},
136-
AttestationsRewards,
36+
rewards.AttestationsRewards,
13737
ExecutionOptimisticAndFinalizedMeta
13838
>;
13939

@@ -148,7 +48,7 @@ export type Endpoints = {
14848
validatorIds?: ValidatorId[];
14949
},
15050
{params: {block_id: string}; body: string[]},
151-
SyncCommitteeRewards,
51+
rewards.SyncCommitteeRewards,
15252
ExecutionOptimisticAndFinalizedMeta
15353
>;
15454
};
@@ -164,7 +64,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
16464
schema: {params: {block_id: Schema.StringRequired}},
16565
},
16666
resp: {
167-
data: BlockRewardsType,
67+
data: rewards.BlockRewardsType,
16868
meta: ExecutionOptimisticAndFinalizedCodec,
16969
},
17070
},
@@ -186,7 +86,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
18686
},
18787
}),
18888
resp: {
189-
data: AttestationsRewardsType,
89+
data: rewards.AttestationsRewardsType,
19090
meta: ExecutionOptimisticAndFinalizedCodec,
19191
},
19292
},
@@ -208,7 +108,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
208108
},
209109
}),
210110
resp: {
211-
data: SyncCommitteeRewardsType,
111+
data: rewards.SyncCommitteeRewardsType,
212112
meta: ExecutionOptimisticAndFinalizedCodec,
213113
},
214114
},

packages/api/src/beacon/routes/beacon/state.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {ContainerType, ValueOf} from "@chainsafe/ssz";
22
import {ChainForkConfig} from "@lodestar/config";
33
import {MAX_VALIDATORS_PER_COMMITTEE} from "@lodestar/params";
44
import {
5+
ArrayOf,
56
CommitteeIndex,
67
Epoch,
78
RootHex,
@@ -13,7 +14,7 @@ import {
1314
phase0,
1415
ssz,
1516
} from "@lodestar/types";
16-
import {ArrayOf, JsonOnlyReq} from "../../../utils/codecs.js";
17+
import {JsonOnlyReq} from "../../../utils/codecs.js";
1718
import {Endpoint, RequestCodec, RouteDefinitions, Schema} from "../../../utils/index.js";
1819
import {
1920
ExecutionOptimisticAndFinalizedCodec,

packages/api/src/beacon/routes/config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {ContainerType, ValueOf} from "@chainsafe/ssz";
22
import {ChainForkConfig, SpecJson} from "@lodestar/config";
3-
import {ssz} from "@lodestar/types";
3+
import {ArrayOf, ssz} from "@lodestar/types";
44
import {
5-
ArrayOf,
65
EmptyArgs,
76
EmptyMeta,
87
EmptyMetaCodec,

packages/api/src/beacon/routes/debug.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {ContainerType, Type, ValueOf} from "@chainsafe/ssz";
22
import {ChainForkConfig} from "@lodestar/config";
3-
import {BeaconState, StringType, fulu, ssz} from "@lodestar/types";
3+
import {ArrayOf, BeaconState, StringType, fulu, ssz} from "@lodestar/types";
44
import {
5-
ArrayOf,
65
EmptyArgs,
76
EmptyMeta,
87
EmptyMetaCodec,

packages/api/src/beacon/routes/lodestar.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {ContainerType, Type, ValueOf} from "@chainsafe/ssz";
22
import {ChainForkConfig} from "@lodestar/config";
3-
import {BeaconState, Epoch, RootHex, Slot, ssz} from "@lodestar/types";
3+
import {ArrayOf, BeaconState, Epoch, RootHex, Slot, ssz} from "@lodestar/types";
44
import {
5-
ArrayOf,
65
EmptyArgs,
76
EmptyMeta,
87
EmptyRequest,

packages/api/src/beacon/routes/node.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {ContainerType, OptionalType, ValueOf} from "@chainsafe/ssz";
22
import {ChainForkConfig} from "@lodestar/config";
3-
import {StringType, fulu, ssz, stringType} from "@lodestar/types";
3+
import {ArrayOf, StringType, fulu, ssz, stringType} from "@lodestar/types";
44
import {
5-
ArrayOf,
65
EmptyArgs,
76
EmptyMeta,
87
EmptyMetaCodec,

packages/api/src/beacon/routes/proof.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {CompactMultiProof, ProofType} from "@chainsafe/persistent-merkle-tree";
22
import {ByteListType, ContainerType} from "@chainsafe/ssz";
33
import {ChainForkConfig} from "@lodestar/config";
4-
import {ssz} from "@lodestar/types";
4+
import {ArrayOf, ssz} from "@lodestar/types";
55
import {fromHex, toHex} from "@lodestar/utils";
6-
import {ArrayOf} from "../../utils/codecs.js";
76
import {Endpoint, RouteDefinitions, Schema} from "../../utils/index.js";
87
import {VersionCodec, VersionMeta} from "../../utils/metadata.js";
98

packages/api/src/beacon/routes/validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
isForkPostElectra,
99
} from "@lodestar/params";
1010
import {
11+
ArrayOf,
1112
Attestation,
1213
BLSSignature,
1314
BeaconBlock,
@@ -28,7 +29,6 @@ import {
2829
} from "@lodestar/types";
2930
import {fromHex, toHex, toRootHex} from "@lodestar/utils";
3031
import {
31-
ArrayOf,
3232
EmptyMeta,
3333
EmptyMetaCodec,
3434
EmptyResponseCodec,

0 commit comments

Comments
 (0)