Skip to content

Commit

Permalink
Merge pull request #38 from PowerLoom/37-last-finalized-epoch-id-from…
Browse files Browse the repository at this point in the history
…-protocol-state

fix: fetch last finalized epoch ID for project from protocol state contract
  • Loading branch information
SwaroopH authored Jun 21, 2023
2 parents d5f00b9 + 21ccba0 commit 762fa59
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions pooler/core_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,36 @@ async def get_project_last_finalized_epoch_info(
)

if project_last_finalized_epoch is None:
response.status_code = 404
return {
'status': 'error',
'message': f'Unable to find last finalized epoch for project {project_id}',
}

project_last_finalized_epoch = int(project_last_finalized_epoch.decode('utf-8'))

# find from contract
epoch_finalized = False
[cur_epoch] = await request.app.state.anchor_rpc_helper.web3_call(
[request.app.state.protocol_state_contract.functions.currentEpoch()],
redis_conn=request.app.state.redis_pool,
)
epoch_id = int(cur_epoch[2])
while not epoch_finalized and epoch_id >= 0:
# get finalization status
[epoch_finalized_contract] = await request.app.state.anchor_rpc_helper.web3_call(
[request.app.state.protocol_state_contract.functions.snapshotStatus(project_id, epoch_id)],
redis_conn=request.app.state.redis_pool,
)
if epoch_finalized_contract[0]:
epoch_finalized = True
project_last_finalized_epoch = epoch_id
await request.app.state.redis_pool.set(
project_last_finalized_epoch_key(project_id),
project_last_finalized_epoch,
)
else:
epoch_id -= 1
if epoch_id < 0:
response.status_code = 404
return {
'status': 'error',
'message': f'Unable to find last finalized epoch for project {project_id}',
}
else:
project_last_finalized_epoch = int(project_last_finalized_epoch.decode('utf-8'))
[epoch_info_data] = await request.app.state.anchor_rpc_helper.web3_call(
[request.app.state.protocol_state_contract.functions.epochInfo(project_last_finalized_epoch)],
redis_conn=request.app.state.redis_pool,
Expand Down

0 comments on commit 762fa59

Please sign in to comment.