From 432e9184cb10a085efc7ab195260d8f7a1fc33ea Mon Sep 17 00:00:00 2001 From: Aaron Blankstein Date: Fri, 1 Mar 2024 09:38:16 -0600 Subject: [PATCH 1/2] fix: manually calculate current reward cycle ID in /v2/pox_info --- stackslib/src/net/api/getpoxinfo.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/stackslib/src/net/api/getpoxinfo.rs b/stackslib/src/net/api/getpoxinfo.rs index f7475c9cde..c9e59b6519 100644 --- a/stackslib/src/net/api/getpoxinfo.rs +++ b/stackslib/src/net/api/getpoxinfo.rs @@ -226,12 +226,6 @@ impl RPCPoxInfoData { .to_owned() .expect_u128()? as u64; - let reward_cycle_id = res - .get("reward-cycle-id") - .unwrap_or_else(|_| panic!("FATAL: no 'reward-cycle-id'")) - .to_owned() - .expect_u128()? as u64; - let reward_cycle_length = res .get("reward-cycle-length") .unwrap_or_else(|_| panic!("FATAL: no 'reward-cycle-length'")) @@ -292,7 +286,13 @@ impl RPCPoxInfoData { return Err(NetError::DBError(DBError::Corruption)); } + let reward_cycle_id = burnchain + .block_height_to_reward_cycle(burnchain_tip.block_height) + .ok_or_else(|| { + NetError::ChainstateError("Current burn block height is before stacks start".into()) + })?; let effective_height = burnchain_tip.block_height - first_burnchain_block_height; + let next_reward_cycle_in = reward_cycle_length - (effective_height % reward_cycle_length); let next_rewards_start = burnchain_tip.block_height + next_reward_cycle_in; From d5af26e0ce2f5b3b032f430d8adb1081516074c9 Mon Sep 17 00:00:00 2001 From: Aaron Blankstein Date: Mon, 4 Mar 2024 16:04:38 -0600 Subject: [PATCH 2/2] chore: add comment to the manual calculation of reward_cycle_id --- stackslib/src/net/api/getpoxinfo.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stackslib/src/net/api/getpoxinfo.rs b/stackslib/src/net/api/getpoxinfo.rs index c9e59b6519..b463609849 100644 --- a/stackslib/src/net/api/getpoxinfo.rs +++ b/stackslib/src/net/api/getpoxinfo.rs @@ -286,6 +286,9 @@ impl RPCPoxInfoData { return Err(NetError::DBError(DBError::Corruption)); } + // Manually calculate `reward_cycle_id` so that clients don't get an "off by one" view at + // reward cycle boundaries (because if the reward cycle is loaded from clarity, its + // evaluated in the last mined Stacks block, not the most recent burn block). let reward_cycle_id = burnchain .block_height_to_reward_cycle(burnchain_tip.block_height) .ok_or_else(|| {