Skip to content

Commit

Permalink
Merge pull request #429 from hirosystems/feat/revisit-fork-handling
Browse files Browse the repository at this point in the history
Feat: revisit fork handling
  • Loading branch information
lgalabru authored Jul 11, 2022
2 parents 13f6d2c + aa99c87 commit 293fbe1
Show file tree
Hide file tree
Showing 38 changed files with 10,171 additions and 672 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

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

9 changes: 6 additions & 3 deletions node-bindings/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const {
stacksDevnetWaitForBitcoinBlock,
stacksDevnetGetStacksNodeUrl,
} = require("../native/index.node");
import { Block } from "@hirosystems/orchestra-types";
import {
BitcoinChainUpdate,
StacksChainUpdate,
} from "@hirosystems/orchestra-types";
export * from "@hirosystems/orchestra-types";

/**
Expand Down Expand Up @@ -347,15 +350,15 @@ export class StacksDevnetOrchestrator {
* @summary Wait for the next Stacks block
* @memberof StacksDevnetOrchestrator
*/
waitForStacksBlock(): Block {
waitForStacksBlock(): StacksChainUpdate {
return stacksDevnetWaitForStacksBlock.call(this.handle);
}

/**
* @summary Wait for the next Bitcoin block
* @memberof StacksDevnetOrchestrator
*/
waitForBitcoinBlock(): Block {
waitForBitcoinBlock(): BitcoinChainUpdate {
return stacksDevnetWaitForBitcoinBlock.call(this.handle);
}

Expand Down
16 changes: 8 additions & 8 deletions node-bindings/package-lock.json

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

2 changes: 1 addition & 1 deletion node-bindings/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"version": "npm run build-release"
},
"dependencies": {
"@hirosystems/orchestra-types": "^1.0.1-beta.1",
"@hirosystems/orchestra-types": "^1.0.1-beta.3",
"@mapbox/node-pre-gyp": "^1.0.8",
"neon-cli": "^0.9.1",
"node-pre-gyp-github": "^1.4.3",
Expand Down
29 changes: 15 additions & 14 deletions node-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use clarinet_files::{
use clarinet_lib::deployments;
use clarinet_lib::integrate::{self, DevnetEvent, DevnetOrchestrator};
use orchestra_types::{
BitcoinBlockData, BitcoinChainEvent, ChainUpdatedWithBlockData, StacksChainEvent, StacksNetwork,
BitcoinChainEvent, BitcoinChainUpdatedWithBlocksData, StacksChainEvent,
StacksChainUpdatedWithBlocksData, StacksNetwork,
};

use core::panic;
Expand All @@ -27,8 +28,8 @@ type DevnetCallback = Box<dyn FnOnce(&Channel) + Send>;

struct StacksDevnet {
tx: mpsc::Sender<DevnetCommand>,
bitcoin_block_rx: mpsc::Receiver<BitcoinBlockData>,
stacks_block_rx: mpsc::Receiver<ChainUpdatedWithBlockData>,
bitcoin_block_rx: mpsc::Receiver<BitcoinChainUpdatedWithBlocksData>,
stacks_block_rx: mpsc::Receiver<StacksChainUpdatedWithBlocksData>,
node_url: String,
}

Expand Down Expand Up @@ -112,20 +113,20 @@ impl StacksDevnet {

thread::spawn(move || {
if let Ok(ref devnet_rx) = meta_rx.recv() {
while let Ok(ref event) = devnet_rx.recv() {
while let Ok(event) = devnet_rx.recv() {
match event {
DevnetEvent::BitcoinChainEvent(
BitcoinChainEvent::ChainUpdatedWithBlock(block),
BitcoinChainEvent::ChainUpdatedWithBlocks(update),
) => {
bitcoin_block_tx
.send(block.clone())
.send(update)
.expect("Unable to transmit bitcoin block");
}
DevnetEvent::StacksChainEvent(StacksChainEvent::ChainUpdatedWithBlock(
block,
)) => {
DevnetEvent::StacksChainEvent(
StacksChainEvent::ChainUpdatedWithBlocks(update),
) => {
stacks_block_tx
.send(block.clone())
.send(update)
.expect("Unable to transmit stacks block");
}
DevnetEvent::Log(log) => {
Expand Down Expand Up @@ -560,14 +561,14 @@ impl StacksDevnet {
.this()
.downcast_or_throw::<JsBox<StacksDevnet>, _>(&mut cx)?;

let block = match devnet.stacks_block_rx.recv() {
Ok(obj) => obj.new_block,
let blocks = match devnet.stacks_block_rx.recv() {
Ok(obj) => obj,
Err(err) => panic!("{:?}", err),
};

let js_block = serde::to_value(&mut cx, &block).expect("Unable to serialize block");
let js_blocks = serde::to_value(&mut cx, &blocks).expect("Unable to serialize block");

Ok(js_block)
Ok(js_blocks)
}

fn js_on_bitcoin_block(mut cx: FunctionContext) -> JsResult<JsValue> {
Expand Down
3 changes: 3 additions & 0 deletions src/chainhooks/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct ChainhookSpecificationFile {
pub struct ChainhookNetworkSpecificationFile {
start_block: Option<u64>,
end_block: Option<u64>,
expire_after_occurrence: Option<u64>,
predicate: ChainhookPredicateFile,
action: HookActionFile,
oreo_url: String,
Expand Down Expand Up @@ -147,6 +148,7 @@ impl ChainhookSpecificationFile {
network: network.clone(),
start_block: network_spec.start_block,
end_block: network_spec.end_block,
expire_after_occurrence: network_spec.expire_after_occurrence,
predicate: network_spec.predicate.to_bitcoin_predicate()?,
action: network_spec.action.to_specifications()?,
})
Expand Down Expand Up @@ -174,6 +176,7 @@ impl ChainhookSpecificationFile {
network: network.clone(),
start_block: network_spec.start_block,
end_block: network_spec.end_block,
expire_after_occurrence: network_spec.expire_after_occurrence,
predicate: network_spec.predicate.to_stacks_predicate()?,
action: network_spec.action.to_specifications()?,
})
Expand Down
52 changes: 27 additions & 25 deletions src/integrate/chains_coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,15 @@ pub async fn start_chains_coordinator(
// Contextual shortcut: Devnet is an environment under control,
// with 1 miner. As such we will ignore Reorgs handling.
let (log, status) = match &chain_update {
BitcoinChainEvent::ChainUpdatedWithBlock(block) => {
let log =
format!("Bitcoin block #{} received", block.block_identifier.index);
let status = format!(
"mining blocks (chaintip = #{})",
block.block_identifier.index
);
BitcoinChainEvent::ChainUpdatedWithBlocks(event) => {
let tip = event.new_blocks.last().unwrap();
let log = format!("Bitcoin block #{} received", tip.block_identifier.index);
let status =
format!("mining blocks (chaintip = #{})", tip.block_identifier.index);
(log, status)
}
BitcoinChainEvent::ChainUpdatedWithReorg(_old_blocks, new_blocks) => {
let tip = new_blocks.last().unwrap();
BitcoinChainEvent::ChainUpdatedWithReorg(events) => {
let tip = events.blocks_to_apply.last().unwrap();
let log = format!(
"Bitcoin reorg received (new height: {})",
tip.block_identifier.index
Expand Down Expand Up @@ -275,13 +273,18 @@ pub async fn start_chains_coordinator(
});
}

let update = match &chain_event {
StacksChainEvent::ChainUpdatedWithBlock(block) => block.clone(),
StacksChainEvent::ChainUpdatedWithMicroblock(_) => {
let known_tip = match &chain_event {
StacksChainEvent::ChainUpdatedWithBlocks(block) => {
match block.new_blocks.last() {
Some(known_tip) => known_tip.clone(),
None => unreachable!(),
}
}
StacksChainEvent::ChainUpdatedWithMicroblocks(_) => {
continue;
// TODO(lgalabru): good enough for now - code path unreachable in the context of Devnet
}
StacksChainEvent::ChainUpdatedWithMicroblockReorg(_) => {
StacksChainEvent::ChainUpdatedWithMicroblocksReorg(_) => {
unreachable!() // TODO(lgalabru): good enough for now - code path unreachable in the context of Devnet
}
StacksChainEvent::ChainUpdatedWithReorg(_) => {
Expand All @@ -299,24 +302,24 @@ pub async fn start_chains_coordinator(
name: "stacks-node".into(),
comment: format!(
"mining blocks (chaintip = #{})",
update.new_block.block_identifier.index
known_tip.block.block_identifier.index
),
}));
let _ = devnet_event_tx.send(DevnetEvent::info(format!(
"Block #{} anchored in Bitcoin block #{} includes {} transactions",
update.new_block.block_identifier.index,
update
.new_block
known_tip.block.block_identifier.index,
known_tip
.block
.metadata
.bitcoin_anchor_block_identifier
.index,
update.new_block.transactions.len(),
known_tip.block.transactions.len(),
)));

let should_submit_pox_orders = update.new_block.metadata.pox_cycle_position
== (update.new_block.metadata.pox_cycle_length - 2);
let should_submit_pox_orders = known_tip.block.metadata.pox_cycle_position
== (known_tip.block.metadata.pox_cycle_length - 2);
if should_submit_pox_orders {
let bitcoin_block_height = update.new_block.block_identifier.index;
let bitcoin_block_height = known_tip.block.block_identifier.index;
let res = publish_stacking_orders(
&config.devnet_config,
&config.accounts,
Expand Down Expand Up @@ -344,10 +347,9 @@ pub async fn start_chains_coordinator(
}
}
ObserverEvent::HookRegistered(hook) => {
let _ = devnet_event_tx.send(DevnetEvent::info(format!(
"New hook \"{}\" registered",
hook.name()
)));
let message = format!("New hook \"{}\" registered", hook.name());
info!("{}", message);
let _ = devnet_event_tx.send(DevnetEvent::info(message));
}
ObserverEvent::HookDeregistered(_hook) => {}
ObserverEvent::HooksTriggered(count) => {
Expand Down
9 changes: 5 additions & 4 deletions src/integrate/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ pub fn start_ui(
app.display_service_status_update(status);
}
DevnetEvent::StacksChainEvent(chain_event) => {
if let StacksChainEvent::ChainUpdatedWithBlock(update) = chain_event {
if let StacksChainEvent::ChainUpdatedWithBlocks(update) = chain_event {

let raw_txs = if app.mempool.items.is_empty() {
vec![]
} else {
update.new_block.transactions.iter().map(|tx| tx.metadata.raw_tx.as_str()).collect::<Vec<_>>()
update.new_blocks.iter().flat_map(|b| b.block.transactions.iter().map(|tx| tx.metadata.raw_tx.as_str())).collect::<Vec<_>>()
};

let mut indices_to_remove = vec![];
Expand All @@ -114,8 +114,9 @@ pub fn start_ui(
for i in indices_to_remove {
app.mempool.items.remove(i);
}

app.display_block(update.new_block);
for block_update in update.new_blocks.into_iter() {
app.display_block(block_update.block);
}
} else {
// TODO(lgalabru)
}
Expand Down
5 changes: 4 additions & 1 deletion vendor/orchestra-event-observer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ clap = { version = "3.1.6", features = ["derive"], optional = true }
clap_generate = { version = "3.0.3", optional = true }
toml = { version = "0.5.6", features = ["preserve_order"], optional = true }
ctrlc = { version = "3.2.2", optional = true }
slog = { version = "2.7.0" }
slog-json = { version = "2.6.1", optional = true }
slog-scope = { version = "4.4.0", optional = true }

[features]
default = ["cli"]
cli = ["clap", "clap_generate", "toml", "ctrlc"]
cli = ["clap", "clap_generate", "toml", "ctrlc", "slog-json", "slog-scope"]
Loading

0 comments on commit 293fbe1

Please sign in to comment.