Skip to content

Commit

Permalink
fix: devnet pox and stacking (#1350)
Browse files Browse the repository at this point in the history
* fix: devnet pox and stacking

* chore: update chainhook

* chore: update default nakamoto stacks api image
  • Loading branch information
hugocaillard authored Feb 8, 2024
1 parent 6bc06ab commit bc74e5a
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 35 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/clarinet-files/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
serde = "1"
serde_derive = "1"
# chainhook-types = "1.2"
chainhook-types = { version = "1.2", git = "https://github.com/hirosystems/chainhook.git", rev="efba5b0" }
chainhook-types = { version = "1.2", git = "https://github.com/hirosystems/chainhook.git", rev="6c8a834" }
bip39 = { version = "1.0.1", default-features = false }
libsecp256k1 = "0.7.0"
toml = { version = "0.5.6", features = ["preserve_order"] }
Expand Down
3 changes: 2 additions & 1 deletion components/clarinet-files/src/network_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pub const DEFAULT_STACKS_NODE_IMAGE: &str = "quay.io/hirosystems/stacks-node:dev
pub const DEFAULT_STACKS_API_IMAGE: &str = "hirosystems/stacks-blockchain-api:latest";
// Nakamoto images overrides
pub const DEFAULT_STACKS_NODE_IMAGE_NAKA: &str = "blockstack/stacks-blockchain:devnet-next-stable";
pub const DEFAULT_STACKS_API_IMAGE_NAKA: &str = "hirosystems/stacks-blockchain-api:nakamoto";
pub const DEFAULT_STACKS_API_IMAGE_NAKA: &str =
"hirosystems/stacks-blockchain-api:7.9.0-nakamoto.1";

pub const DEFAULT_BITCOIN_NODE_IMAGE: &str = "quay.io/hirosystems/bitcoind:26.0";
pub const DEFAULT_BITCOIN_EXPLORER_IMAGE: &str = "quay.io/hirosystems/bitcoin-explorer:devnet";
Expand Down
2 changes: 1 addition & 1 deletion components/stacks-network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ futures = "0.3.12"
base58 = "0.2.0"
tokio = { version = "1.35.1", features = ["full"] }

chainhook-sdk = { default-features = true, git = "https://github.com/hirosystems/chainhook.git", rev = "efba5b0" }
chainhook-sdk = { default-features = true, git = "https://github.com/hirosystems/chainhook.git", rev = "6c8a834" }
# chainhook-sdk = { version = "=0.11", default-features = true }
stacks-rpc-client = { path = "../stacks-rpc-client" }
clarinet-files = { path = "../clarinet-files", features = ["cli"] }
Expand Down
137 changes: 125 additions & 12 deletions components/stacks-network/src/chains_coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,119 @@ pub fn relay_devnet_protocol_deployment(
});
}

fn should_publish_stacking_orders(
current_cycle: &u32,
pox_stacking_order: &PoxStackingOrder,
) -> bool {
let PoxStackingOrder {
duration,
start_at_cycle,
..
} = pox_stacking_order;

let is_higher_than_start_cycle = *current_cycle >= (start_at_cycle - 1);
if !is_higher_than_start_cycle {
return false;
}

let offset = (current_cycle + duration).saturating_sub(*start_at_cycle);
let should_stack = (offset % duration) == (duration - 1);
if !should_stack {
return false;
}

true
}

#[cfg(test)]
mod tests {
use super::*;

fn build_pox_stacking_order(duration: u32, start_at_cycle: u32) -> PoxStackingOrder {
PoxStackingOrder {
duration,
start_at_cycle,
wallet: "wallet_1".to_string(),
slots: 1,
btc_address: "address_1".to_string(),
auto_extend: Some(true),
}
}

#[test]
fn test_should_publish_stacking_orders_basic() {
let pox_stacking_order = build_pox_stacking_order(12, 6);

// cycle just before start_at_cycle
assert_eq!(
should_publish_stacking_orders(&5, &pox_stacking_order),
true
);

// cycle before start_at_cycle + duration
assert_eq!(
should_publish_stacking_orders(&17, &pox_stacking_order),
true
);

// cycle before start_at_cycle + duration * 42
assert_eq!(
should_publish_stacking_orders(&509, &pox_stacking_order),
true
);

// cycle equal to start_at_cycle
assert_eq!(
should_publish_stacking_orders(&6, &pox_stacking_order),
false
);

// cycle after to start_at_cycle
assert_eq!(
should_publish_stacking_orders(&8, &pox_stacking_order),
false
);
}

#[test]
fn test_should_publish_stacking_orders_edge_cases() {
// duration is one cycle
let pox_stacking_order = build_pox_stacking_order(1, 4);
assert_eq!(
should_publish_stacking_orders(&2, &pox_stacking_order),
false
);

for i in 3..=20 {
assert_eq!(
should_publish_stacking_orders(&i, &pox_stacking_order),
true
);
}

// duration is low and start_at_cycle is high
let pox_stacking_order = build_pox_stacking_order(2, 100);
for i in 0..=98 {
assert_eq!(
should_publish_stacking_orders(&i, &pox_stacking_order),
false
);
}
assert_eq!(
should_publish_stacking_orders(&99, &pox_stacking_order),
true
);
assert_eq!(
should_publish_stacking_orders(&100, &pox_stacking_order),
false
);
assert_eq!(
should_publish_stacking_orders(&101, &pox_stacking_order),
true
);
}
}

pub async fn publish_stacking_orders(
devnet_config: &DevnetConfig,
devnet_event_tx: &Sender<DevnetEvent>,
Expand Down Expand Up @@ -555,15 +668,12 @@ pub async fn publish_stacking_orders(
}
}?;

let effective_height = u64::saturating_sub(
bitcoin_block_height.into(),
pox_info.first_burnchain_block_height,
);
let pox_cycle_length: u64 =
(pox_info.prepare_phase_block_length + pox_info.reward_phase_block_length).into();
let reward_cycle_id = effective_height / pox_cycle_length;
let effective_height =
u32::saturating_sub(bitcoin_block_height, pox_info.first_burnchain_block_height);

let pox_cycle_position = (effective_height % pox_cycle_length) as u32;
let current_cycle = effective_height / pox_info.reward_cycle_length;
let pox_cycle_length = pox_info.reward_cycle_length;
let pox_cycle_position = effective_height % pox_cycle_length;

let should_submit_pox_orders = pox_cycle_position == 1;
if !should_submit_pox_orders {
Expand All @@ -572,19 +682,22 @@ pub async fn publish_stacking_orders(

let mut transactions = 0;
for (i, pox_stacking_order) in devnet_config.pox_stacking_orders.iter().enumerate() {
if !should_publish_stacking_orders(&current_cycle, pox_stacking_order) {
continue;
}

let PoxStackingOrder {
duration,
start_at_cycle,
..
} = pox_stacking_order;

if ((reward_cycle_id as u32) % duration) != (start_at_cycle - 1) {
continue;
}
let extend_stacking = reward_cycle_id as u32 != start_at_cycle - 1;
// if the is not the first cycle of this stacker, then stacking order will be extended
let extend_stacking = current_cycle != start_at_cycle - 1;
if extend_stacking && !pox_stacking_order.auto_extend.unwrap_or_default() {
continue;
}

let account = accounts
.iter()
.find(|e| e.label == pox_stacking_order.wallet);
Expand Down
19 changes: 10 additions & 9 deletions components/stacks-network/src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,6 @@ impl<'a> App<'a> {
}

pub fn display_block(&mut self, block: StacksBlockData) {
let (start, end) =
if block.metadata.pox_cycle_position == (block.metadata.pox_cycle_length - 1) {
("", "<")
} else if block.metadata.pox_cycle_position == 0 {
(">", "")
} else {
("", "")
};

let has_tenure_change_tx = block
.transactions
.iter()
Expand All @@ -122,6 +113,16 @@ impl<'a> App<'a> {
.iter()
.any(|tx| tx.metadata.kind == StacksTransactionKind::Coinbase);

let (start, end) = if !has_coinbase_tx {
("", "")
} else if block.metadata.pox_cycle_position == (block.metadata.pox_cycle_length - 1) {
("", "<")
} else if block.metadata.pox_cycle_position == 0 {
(">", "")
} else {
("", "")
};

let has_tx = if (block.transactions.len()
- has_coinbase_tx as usize
- has_tenure_change_tx as usize)
Expand Down
8 changes: 5 additions & 3 deletions components/stacks-rpc-client/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ pub struct NodeInfo {
pub struct PoxInfo {
pub contract_id: String,
pub pox_activation_threshold_ustx: u64,
pub first_burnchain_block_height: u64,
pub current_burnchain_block_height: u64,
pub first_burnchain_block_height: u32,
pub current_burnchain_block_height: u32,
pub prepare_phase_block_length: u32,
pub reward_phase_block_length: u32,
pub reward_slots: u32,
pub reward_cycle_id: u32,
pub reward_cycle_length: u32,
pub total_liquid_supply_ustx: u64,
pub current_cycle: CurrentPoxCycle,
pub next_cycle: NextPoxCycle,
Expand Down Expand Up @@ -112,7 +113,8 @@ impl Default for PoxInfo {
current_burnchain_block_height: 100,
first_burnchain_block_height: 100,
prepare_phase_block_length: 4,
reward_phase_block_length: 10,
reward_phase_block_length: 6,
reward_cycle_length: 10,
reward_slots: 10,
total_liquid_supply_ustx: 1000000000000000,
reward_cycle_id: 0,
Expand Down

0 comments on commit bc74e5a

Please sign in to comment.