Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F/rewards generation #80

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion contracts/btc-finality/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ 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 events = finality::tally_blocks(deps, activated_height, env.block.height)?;
let (msgs, events) = finality::tally_blocks(deps, activated_height, env.block.height)?;
res = res.add_messages(msgs);
res = res.add_events(events);
}
Ok(res)
Expand Down
16 changes: 11 additions & 5 deletions contracts/btc-finality/src/finality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ pub fn tally_blocks(
deps: &mut DepsMut,
activated_height: u64,
height: u64,
) -> Result<Vec<Event>, ContractError> {
) -> Result<(Vec<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);
let start_height = max(activated_height, next_height);
Expand All @@ -436,6 +436,7 @@ pub fn tally_blocks(
// - Does not have finality providers, finalised: Impossible, panic.
// After this for loop, the blocks since the earliest activated height are either finalised or
// non-finalisable
let mut msgs = vec![];
let mut events = vec![];
for h in start_height..=height {
let mut indexed_block = BLOCKS.load(deps.storage, h)?;
Expand All @@ -451,7 +452,9 @@ pub fn tally_blocks(
.collect::<StdResult<Vec<_>>>()?;
if tally(&fp_set, &voter_btc_pks) {
// If this block gets >2/3 votes, finalise it
let ev = finalize_block(deps.storage, &mut indexed_block, &voter_btc_pks)?;
let (msg, ev) =
finalize_block(deps.storage, &mut indexed_block, &voter_btc_pks)?;
msgs.push(msg);
events.push(ev);
} else {
// If not, then this block and all subsequent blocks should not be finalised.
Expand Down Expand Up @@ -480,7 +483,7 @@ pub fn tally_blocks(
}
}
}
Ok(events)
Ok((msgs, events))
}

/// `tally` checks whether a block with the given finality provider set and votes reaches a quorum
Expand All @@ -504,21 +507,24 @@ fn finalize_block(
store: &mut dyn Storage,
block: &mut IndexedBlock,
_voters: &[String],
) -> Result<Event, ContractError> {
) -> Result<(BabylonMsg, Event), ContractError> {
// Set block to be finalised
block.finalized = true;
BLOCKS.save(store, block.height, block)?;

// Set the next height to finalise as height+1
NEXT_HEIGHT.save(store, &(block.height + 1))?;

// Generate block rewards for BTC staking delegators
let msg = BabylonMsg::MintRewards {};

// TODO: Distribute rewards to BTC staking delegators

// Record the last finalized height metric
let ev = Event::new("finalize_block")
.add_attribute("module", "finality")
.add_attribute("finalized_height", block.height.to_string());
Ok(ev)
Ok((msg, ev))
}

const QUERY_LIMIT: Option<u32> = Some(30);
Expand Down
4 changes: 4 additions & 0 deletions packages/bindings-test/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ impl Module for BabylonModule {
// FIXME? We don't do anything here
Ok(AppResponse::default())
}
BabylonMsg::MintRewards {} => {
// FIXME? We don't do anything here
Ok(AppResponse::default())
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/bindings/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ pub enum BabylonMsg {
height: i64,
time: i64, // NOTE: UNIX timestamp is in i64
},
/// MintRewards mints the block rewards for the finality providers.
/// It can only be sent from the finality contract.
/// The rewards are minted to the staking contract address, so that they
/// can be distributed across the active finality provider set
MintRewards {},
}

pub type BabylonSudoMsg = Empty;
Expand Down