Skip to content

Commit ddcd205

Browse files
authored
Merge pull request #115 from voteagora/jeff/add-vpc-properly
Add VPC to /delegate search properly
2 parents 8e1f63a + c26ebb1 commit ddcd205

1 file changed

Lines changed: 80 additions & 19 deletions

File tree

app/server.py

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def secret_text(t, n):
153153
NORMAL_STYLE = public_config['token_spec'].get('style', 'normal') == 'normal'
154154
INCLUDE_BALANCES = ERC20 and NORMAL_STYLE and ENABLE_BALANCES
155155
INCLUDE_NON_IVOTES_VP = bool(config['features'].get('non_ivotes_vp', False))
156-
logr.info(f"{INCLUDE_NON_IVOTES_VP=} ({type(INCLUDE_NON_IVOTES_VP)})")
156+
glogr.info(f"{INCLUDE_NON_IVOTES_VP=} ({type(INCLUDE_NON_IVOTES_VP)})")
157157

158158
########################################################################
159159

@@ -944,34 +944,89 @@ async def delegates_handler(app, request):
944944
# Get the initial list based on sort criteria
945945
else:
946946
if sort_by_vp:
947-
# Include staking VP in total VP calculation
947+
948948
if INCLUDE_NON_IVOTES_VP:
949-
non_ivotes = app.ctx.non_ivotes_vp
950-
vp_dict = dict(app.ctx.delegations.delegatee_vp)
951-
latest = non_ivotes.latest
949+
vp_dict = deepcopy(app.ctx.delegations.delegatee_vp)
950+
latest = app.ctx.non_ivotes_vp.latest
952951
for addr, nivvp in latest.items():
953952
vp_dict[addr] = vp_dict.get(addr, 0) + int(nivvp)
954953
out = [(addr, vp) for addr, vp in vp_dict.items() if vp > 0]
955954
else:
956955
out = list(app.ctx.delegations.delegatee_vp.items())
956+
957957
elif sort_by_mrd:
958-
out = [(addr, int(event['block_number']))
959-
for addr, event in app.ctx.delegations.delegatee_latest_event.items()]
958+
959+
if INCLUDE_NON_IVOTES_VP:
960+
mrd_dict = {addr : int(event['block_number'])
961+
for addr, event in app.ctx.delegations.delegatee_latest_event.items()}
962+
963+
latest = app.ctx.non_ivotes_vp.latest
964+
for addr, _ in latest.items():
965+
mrd_dict[addr] = mrd_dict.get(addr, 0)
966+
967+
out = list(mrd_dict.items())
968+
else:
969+
out = [(addr, int(event['block_number']))
970+
for addr, event in app.ctx.delegations.delegatee_latest_event.items()]
971+
960972
elif sort_by_pr:
961-
out = list(app.ctx.participation_rate_model.rates())
973+
974+
if INCLUDE_NON_IVOTES_VP:
975+
pr_dict = deepcopy(app.ctx.participation_rate_model.rates())
976+
977+
latest = app.ctx.non_ivotes_vp.latest
978+
for addr, _ in latest.items():
979+
pr_dict[addr] = pr_dict.get(addr, 0)
980+
981+
out = list(pr_dict.items())
982+
else:
983+
out = list(app.ctx.participation_rate_model.rates())
962984
elif sort_by_old:
963-
out = [(addr, int(event['block_number']))
964-
for addr, event in app.ctx.delegations.delegatee_oldest_event.items()]
985+
986+
if INCLUDE_NON_IVOTES_VP:
987+
old_dict = {addr : int(event['block_number'])
988+
for addr, event in app.ctx.delegations.delegatee_oldest_event.items()}
989+
990+
latest = app.ctx.non_ivotes_vp.latest
991+
for addr, _ in latest.items():
992+
old_dict[addr] = old_dict.get(addr, 0)
993+
994+
out = list(old_dict.items())
995+
else:
996+
out = [(addr, int(event['block_number']))
997+
for addr, event in app.ctx.delegations.delegatee_oldest_event.items()]
998+
965999
elif sort_by_dc:
966-
out = list(app.ctx.delegations.delegatee_cnt.items())
1000+
1001+
if INCLUDE_NON_IVOTES_VP:
1002+
dc_dict = deepcopy(app.ctx.delegations.delegatee_cnt)
1003+
1004+
latest = app.ctx.non_ivotes_vp.latest
1005+
for addr, _ in latest.items():
1006+
dc_dict[addr] = dc_dict.get(addr, 0) + 1
1007+
1008+
out = list(dc_dict.items())
1009+
else:
1010+
out = list(app.ctx.delegations.delegatee_cnt.items())
1011+
9671012
elif sort_by_lvb:
1013+
1014+
delegates = app.ctx.delegations.delegatee_vp.keys()
1015+
1016+
if INCLUDE_NON_IVOTES_VP:
1017+
nonivotes = app.ctx.non_ivotes_vp.latest.keys()
1018+
1019+
addresses = set(delegates) | set(nonivotes)
1020+
else:
1021+
addresses = set(delegates)
1022+
9681023
out = [(addr, int(app.ctx.votes.latest_vote_block.get(addr, 0)))
969-
for addr in app.ctx.delegations.delegatee_vp.keys()]
1024+
for addr in addresses]
9701025
out = [obj for obj in out if obj[1] > 0] # TODO This should not be necessary. The data model should prune zeros.
1026+
9711027
elif sort_by_vpc:
1028+
9721029
if INCLUDE_NON_IVOTES_VP:
973-
non_ivotes = app.ctx.non_ivotes_vp
974-
9751030
vp_dict = {}
9761031
for addr in app.ctx.delegations.delegatee_vp.keys():
9771032
vp_dict[addr] = app.ctx.delegations.delegate_seven_day_vp_change(addr)
@@ -1007,13 +1062,22 @@ def voting_power_func(addr, _sort_val):
10071062
return str(app.ctx.delegations.delegatee_vp.get(addr, 0) + int(app.ctx.non_ivotes_vp.latest.get(addr, 0)))
10081063
else:
10091064
voting_power_func = lambda addr, _sort_val: str(app.ctx.delegations.delegatee_vp[addr])
1065+
1066+
if sort_by_vpc:
1067+
voting_power_change_func = lambda addr, sort_val: str(sort_val)
1068+
else:
1069+
if INCLUDE_NON_IVOTES_VP:
1070+
def voting_power_change_func(addr, _sort_val):
1071+
return str(app.ctx.delegations.delegate_seven_day_vp_change(addr) + int(app.ctx.non_ivotes_vp.change.get(addr, 0)))
1072+
else:
1073+
voting_power_change_func = lambda x, y: str(app.ctx.delegations.delegate_seven_day_vp_change(x))
10101074

10111075
from_cnt_func = lambda x, y: app.ctx.delegations.delegatee_cnt.get(x, 0)
10121076
participation_func = lambda x, y: app.ctx.participation_rate_model.get_rate(x)
10131077
last_vote_block_func = lambda x, y: app.ctx.votes.latest_vote_block.get(x, 0)
10141078
most_recent_delegation_func = lambda x, y: app.ctx.delegations.delegatee_latest_event.get(x, {}).get('block_number', 0) # TODO: this .get() is a bit of a hack, it should be able to be strict. I think there is an integrity issue somewhere, that causes a delegate_address to be missing, and at which point break the entire endpoint.
10151079
oldest_delegation_func = lambda x, y: app.ctx.delegations.delegatee_oldest_event.get(x, {}).get('block_number', 100000000000000000000000000) # TODO: this .get() is a bit of a hack, it should be able to be strict. I think there is an integrity issue somewhere, that causes a delegate_address to be missing, and at which point break the entire endpoint.
1016-
seven_day_vp_change_func = lambda x, y: str(app.ctx.delegations.delegate_seven_day_vp_change(x))
1080+
10171081

10181082
use_sort_key = lambda x, y: y
10191083
addr_func = lambda x, y: x
@@ -1033,7 +1097,7 @@ def voting_power_func(addr, _sort_val):
10331097
if add_oldest_delegation:
10341098
transformers.append(('OLD', use_sort_key if sort_by_old else oldest_delegation_func))
10351099
if add_seven_day_vp_change:
1036-
transformers.append(('VPC', use_sort_key if sort_by_vpc else seven_day_vp_change_func))
1100+
transformers.append(('VPC', use_sort_key if sort_by_vpc else voting_power_change_func))
10371101

10381102
# TODO - the zero address shouldn't appear here. But, if it does, remove it.
10391103
out = [dict([(k, func(addr, sort_val)) for k, func in transformers]) for addr, sort_val in out if addr != '0x0000000000000000000000000000000000000000']
@@ -1651,12 +1715,9 @@ async def read_realtime(app, rt_client_num):
16511715

16521716
async def read_naive_socket(app, ws_client):
16531717

1654-
logr.info(f"read_naive_socket invoked")
16551718
async for event in ws_client.read():
16561719
event['signal'] = 'non_ivotes_vp' # its the only one for now.
1657-
logr.info(f"reading an event with keys: {event.keys()}.")
16581720
await app.ctx.dispatch_from_realtime(event)
1659-
logr.info(f"read_naive_socket done")
16601721

16611722
async def read_polling(app, polling_client_num):
16621723
"""

0 commit comments

Comments
 (0)