Skip to content

Commit

Permalink
Fix negative reward values.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheatfate committed Sep 17, 2024
1 parent ac8f5bf commit f4d1bd5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions beacon_chain/rpc/rest_constants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,5 @@ const
"Error reading file"
ParentBlockMissingStateError* =
"Unable to load state for parent block, database corrupt?"
RewardOverflowError* =
"Reward value overflow"
14 changes: 12 additions & 2 deletions beacon_chain/rpc/rest_rewards_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,22 @@ proc installRewardsApiHandlers*(router: var RestRouter, node: BeaconNode) =
let
pubkey = forkyState.data.current_sync_committee.pubkeys.data[i]
vindex = pubkeyIndices.getOrDefault(pubkey)
reward =
block:
let res = uint64(get_participant_reward(total_active_balance))
if res > uint64(high(int64)):
return RestApiResponse.jsonError(
Http500, RewardOverflowError)
if sync_aggregate.sync_committee_bits[i]:
cast[int64](res)
else:
-cast[int64](res)

if (len(idents) == 0) or (pubkey in keys):
resp.add(RestSyncCommitteeReward(
validator_index: RestValidatorIndex(vindex),
reward: uint64(get_participant_reward(total_active_balance))
))
reward: RestReward(reward)))

resp

RestApiResponse.jsonResponseFinalized(
Expand Down
23 changes: 23 additions & 0 deletions beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,29 @@ proc readValue*(reader: var JsonReader[RestJson], value: var uint64) {.
else:
reader.raiseUnexpectedValue($res.error() & ": " & svalue)

## RestReward
proc writeValue*(
w: var JsonWriter[RestJson], value: RestReward) {.raises: [IOError].} =
writeValue(w, $int64(value))

proc readValue*(reader: var JsonReader[RestJson], value: var RestReward) {.
raises: [IOError, SerializationError].} =
let svalue = reader.readValue(string)
if svalue.startsWith("-"):
let res =
Base10.decode(uint64, svalue.toOpenArray(1, len(svalue) - 1)).valueOr:
reader.raiseUnexpectedValue($error & ": " & svalue)
if res > uint64(high(int64)):
reader.raiseUnexpectedValue("Integer value overflow " & svalue)
value = RestReward(-int64(res))
else:
let res =
Base10.decode(uint64, svalue).valueOr:
reader.raiseUnexpectedValue($error & ": " & svalue)
if res > uint64(high(int64)):
reader.raiseUnexpectedValue("Integer value overflow " & svalue)
value = RestReward(int64(res))

## uint8
proc writeValue*(
w: var JsonWriter[RestJson], value: uint8) {.raises: [IOError].} =
Expand Down
4 changes: 3 additions & 1 deletion beacon_chain/spec/eth2_apis/rest_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,11 @@ type
subcommittee_index*: uint64
selection_proof*: ValidatorSig

RestReward* = distinct int64

RestSyncCommitteeReward* = object
validator_index*: RestValidatorIndex
reward*: uint64
reward*: RestReward

# Types based on the OAPI yaml file - used in responses to requests
GetBeaconHeadResponse* = DataEnclosedObject[Slot]
Expand Down

0 comments on commit f4d1bd5

Please sign in to comment.