Skip to content

Commit

Permalink
Fix: Rewards have to be sent to the finality, not staking contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Lacy committed Dec 15, 2024
1 parent dd6727b commit 5a3698e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
8 changes: 4 additions & 4 deletions contracts/btc-finality/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ fn handle_update_staking(
}

fn handle_begin_block(deps: &mut DepsMut, env: Env) -> Result<Response<BabylonMsg>, ContractError> {
// Distribute rewards of the previous block
distribute_rewards(deps, env.block.height - 1)?;
// Distribute rewards
distribute_rewards(deps, &env)?;

// Compute active finality provider set
let max_active_fps = PARAMS.load(deps.storage)?.max_active_finality_providers as usize;
compute_active_finality_providers(deps, env, max_active_fps)?;
compute_active_finality_providers(deps, env.block.height, max_active_fps)?;

// TODO: Add events
Ok(Response::new())
Expand All @@ -250,7 +250,7 @@ fn handle_end_block(
let ev = finality::index_block(deps, env.block.height, &hex::decode(app_hash_hex)?)?;
res = res.add_event(ev);
// Tally all non-finalised blocks
let (msg, events) = finality::tally_blocks(deps, activated_height, env.block.height)?;
let (msg, events) = finality::tally_blocks(deps, &env, activated_height)?;
if let Some(msg) = msg {
res = res.add_message(msg);
}
Expand Down
22 changes: 13 additions & 9 deletions contracts/btc-finality/src/finality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ pub fn index_block(
/// It must be invoked only after the BTC staking protocol is activated.
pub fn tally_blocks(
deps: &mut DepsMut,
env: &Env,
activated_height: u64,
height: u64,
) -> Result<(Option<BabylonMsg>, Vec<Event>), ContractError> {
// Start finalising blocks since max(activated_height, next_height)
let next_height = NEXT_HEIGHT.may_load(deps.storage)?.unwrap_or(0);
Expand All @@ -440,7 +440,7 @@ pub fn tally_blocks(
// non-finalisable
let mut events = vec![];
let mut finalized_blocks = 0;
for h in start_height..=height {
for h in start_height..=env.block.height {
let mut indexed_block = BLOCKS.load(deps.storage, h)?;
// Get the finality provider set of this block
let fp_set = FP_SET.may_load(deps.storage, h)?;
Expand Down Expand Up @@ -492,7 +492,7 @@ pub fn tally_blocks(
// Assemble mint message
let mint_msg = BabylonMsg::MintRewards {
amount: rewards,
recipient: cfg.staking.into(),
recipient: env.contract.address.to_string(),
};
Some(mint_msg)
} else {
Expand Down Expand Up @@ -568,7 +568,7 @@ const QUERY_LIMIT: Option<u32> = Some(30);
/// power of top finality providers, and records them in the contract state
pub fn compute_active_finality_providers(
deps: &mut DepsMut,
env: Env,
height: u64,
max_active_fps: usize,
) -> Result<(), ContractError> {
let cfg = CONFIG.load(deps.storage)?;
Expand Down Expand Up @@ -602,7 +602,7 @@ pub fn compute_active_finality_providers(
// TODO: Filter out slashed / offline / jailed FPs
// Save the new set of active finality providers
// TODO: Purge old (height - finality depth) FP_SET entries to avoid bloating the storage
FP_SET.save(deps.storage, env.block.height, &finality_providers)?;
FP_SET.save(deps.storage, height, &finality_providers)?;

Ok(())
}
Expand All @@ -622,18 +622,22 @@ pub fn list_fps_by_power(
}

/// `distribute_rewards` distributes rewards to finality providers who are in the active set at `height`
pub fn distribute_rewards(deps: &mut DepsMut, height: u64) -> Result<(), ContractError> {
let active_fps = FP_SET.may_load(deps.storage, height)?;
pub fn distribute_rewards(deps: &mut DepsMut, env: &Env) -> Result<(), ContractError> {
// Try to use the finality provider set at the previous height
let active_fps = FP_SET.may_load(deps.storage, env.block.height - 1)?;
// Short-circuit if there are no active finality providers
let active_fps = match active_fps {
Some(active_fps) => active_fps,
None => return Ok(()),
};
// Get the voting power of the active FPS
let total_voting_power = active_fps.iter().map(|fp| fp.power as u128).sum::<u128>();
// Get the rewards to distribute (bank balance of the staking contract)
// Get the rewards to distribute (bank balance of the finality contract)
let cfg = CONFIG.load(deps.storage)?;
let rewards_amount = deps.querier.query_balance(cfg.staking, cfg.denom)?.amount;
let rewards_amount = deps
.querier
.query_balance(env.contract.address.clone(), cfg.denom)?
.amount;
// Compute the rewards for each active FP
let mut total_rewards = Uint128::zero();
for fp in active_fps {
Expand Down

0 comments on commit 5a3698e

Please sign in to comment.