Skip to content

Commit

Permalink
Respect delegation switch in RPC calls (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidenaio authored Mar 26, 2021
1 parent b38f22b commit 8baa474
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions api/dna_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,11 @@ func (api *DnaApi) Identities() []Identity {
if addr == api.GetCoinbaseAddr() {
flipKeyWordPairs = api.ceremony.FlipKeyWordPairs()
}
identities = append(identities, convertIdentity(epoch, addr, data, flipKeyWordPairs, appState.ValidatorsCache.IsPool(addr)))
identities = append(identities, convertIdentity(epoch, addr, data, flipKeyWordPairs, appState))

return false
})

for idx := range identities {
identities[idx].Online = getIdentityOnlineStatus(api.baseApi.getReadonlyAppState(), identities[idx].Address)
}

return identities
}

Expand All @@ -317,22 +313,10 @@ func (api *DnaApi) Identity(address *common.Address) Identity {
}

appState := api.baseApi.getReadonlyAppState()
converted := convertIdentity(appState.State.Epoch(), *address, appState.State.GetIdentity(*address), flipKeyWordPairs, appState.ValidatorsCache.IsPool(*address))
converted.Online = getIdentityOnlineStatus(appState, *address)
return converted
}

func getIdentityOnlineStatus(state *appstate.AppState, addr common.Address) bool {
isOnline := state.ValidatorsCache.IsOnlineIdentity(addr)
hasPendingStatusSwitch := state.State.HasStatusSwitchAddresses(addr)
if hasPendingStatusSwitch {
return !isOnline
} else {
return isOnline
}
return convertIdentity(appState.State.Epoch(), *address, appState.State.GetIdentity(*address), flipKeyWordPairs, appState)
}

func convertIdentity(currentEpoch uint16, address common.Address, data state.Identity, flipKeyWordPairs []int, isPool bool) Identity {
func convertIdentity(currentEpoch uint16, address common.Address, data state.Identity, flipKeyWordPairs []int, appState *appstate.AppState) Identity {
var s string
switch data.State {
case state.Invite:
Expand Down Expand Up @@ -402,6 +386,22 @@ func convertIdentity(currentEpoch uint16, address common.Address, data state.Ide

totalPoints, totalFlips := common.CalculateIdentityScores(data.Scores, data.GetShortFlipPoints(), data.QualifiedFlips)

isOnline := appState.ValidatorsCache.IsOnlineIdentity(address)
hasPendingStatusSwitch := appState.State.HasStatusSwitchAddresses(address)
if hasPendingStatusSwitch {
isOnline = !isOnline
}

delegatee := data.Delegatee
switchDelegation := appState.State.DelegationSwitch(address)
if switchDelegation != nil {
if switchDelegation.Delegatee.IsEmpty() {
delegatee = nil
} else {
delegatee = &switchDelegation.Delegatee
}
}

return Identity{
Address: address,
State: s,
Expand All @@ -422,10 +422,11 @@ func convertIdentity(currentEpoch uint16, address common.Address, data state.Ide
Invitees: invitees,
Penalty: blockchain.ConvertToFloat(data.Penalty),
LastValidationFlags: flags,
Delegatee: data.Delegatee,
Delegatee: delegatee,
DelegationEpoch: data.DelegationEpoch,
DelegationNonce: data.DelegationNonce,
IsPool: isPool,
Online: isOnline,
IsPool: appState.ValidatorsCache.IsPool(address),
}
}

Expand Down

0 comments on commit 8baa474

Please sign in to comment.