Skip to content

Commit

Permalink
feat: bitcoin v24 support, cascade updates from chainhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludo Galabru committed Feb 10, 2023
1 parent 6904055 commit b1264e6
Show file tree
Hide file tree
Showing 13 changed files with 333 additions and 67 deletions.
4 changes: 3 additions & 1 deletion components/clarinet-cli/src/chainhooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::path::PathBuf;
pub mod types;
use crate::chainhooks::types::ChainhookSpecificationFile;

use chainhook_event_observer::chainhooks::types::{ChainhookConfig, ChainhookSpecification};
use stacks_network::chainhook_event_observer::chainhooks::types::{
ChainhookConfig, ChainhookSpecification,
};

use chainhook_types::{BitcoinNetwork, StacksNetwork};

Expand Down
46 changes: 29 additions & 17 deletions components/clarinet-cli/src/chainhooks/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chainhook_event_observer::chainhooks::types::*;
use chainhook_types::{BitcoinNetwork, StacksNetwork};
use serde::{Deserialize, Serialize};
use stacks_network::chainhook_event_observer::chainhooks::types::*;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{BufReader, Read};
Expand Down Expand Up @@ -42,6 +42,8 @@ pub struct ChainhookPredicateFile {
p2wsh: Option<BTreeMap<String, String>>,
script: Option<BTreeMap<String, String>>,
scope: Option<String>,
protocol: Option<String>,
operation: Option<String>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -229,27 +231,37 @@ impl HookActionFile {
}

impl ChainhookPredicateFile {
pub fn to_bitcoin_predicate(&self) -> Result<BitcoinTransactionFilterPredicate, String> {
pub fn to_bitcoin_predicate(&self) -> Result<BitcoinPredicateType, String> {
if let Some(ref specs) = self.op_return {
let rule = BitcoinPredicateType::OpReturn(self.extract_matching_rule(specs)?);
let scope = self.extract_scope()?;
return Ok(BitcoinTransactionFilterPredicate::new(scope, rule));
let predicate = BitcoinPredicateType::Scope(Scopes::Outputs(
OutputPredicate::OpReturn(self.extract_matching_rule(specs)?),
));
return Ok(predicate);
} else if let Some(ref specs) = self.p2pkh {
let rule = BitcoinPredicateType::P2pkh(self.extract_exact_matching_rule(specs)?);
let scope = self.extract_scope()?;
return Ok(BitcoinTransactionFilterPredicate::new(scope, rule));
let predicate = BitcoinPredicateType::Scope(Scopes::Outputs(OutputPredicate::P2pkh(
self.extract_exact_matching_rule(specs)?,
)));
return Ok(predicate);
} else if let Some(ref specs) = self.p2sh {
let rule = BitcoinPredicateType::P2sh(self.extract_exact_matching_rule(specs)?);
let scope = self.extract_scope()?;
return Ok(BitcoinTransactionFilterPredicate::new(scope, rule));
let predicate = BitcoinPredicateType::Scope(Scopes::Outputs(OutputPredicate::P2sh(
self.extract_exact_matching_rule(specs)?,
)));
return Ok(predicate);
} else if let Some(ref specs) = self.p2wpkh {
let rule = BitcoinPredicateType::P2wpkh(self.extract_exact_matching_rule(specs)?);
let scope = self.extract_scope()?;
return Ok(BitcoinTransactionFilterPredicate::new(scope, rule));
let predicate = BitcoinPredicateType::Scope(Scopes::Outputs(OutputPredicate::P2wpkh(
self.extract_exact_matching_rule(specs)?,
)));
return Ok(predicate);
} else if let Some(ref specs) = self.p2wsh {
let rule = BitcoinPredicateType::P2wsh(self.extract_exact_matching_rule(specs)?);
let scope = self.extract_scope()?;
return Ok(BitcoinTransactionFilterPredicate::new(scope, rule));
let predicate = BitcoinPredicateType::Scope(Scopes::Outputs(OutputPredicate::P2wsh(
self.extract_exact_matching_rule(specs)?,
)));
return Ok(predicate);
} else if let Some(ref specs) = self.protocol {
let predicate = BitcoinPredicateType::Protocol(Protocols::Ordinal(
OrdinalOperations::InscriptionRevealed,
));
return Ok(predicate);
}
return Err(format!(
"trigger not specified (op-return, p2pkh, p2sh, p2wpkh, p2wsh)"
Expand Down
2 changes: 1 addition & 1 deletion components/clarinet-cli/src/frontend/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::integrate;
use crate::lsp::run_lsp;
use crate::runner::run_scripts;
use crate::runner::DeploymentCache;
use chainhook_event_observer::chainhooks::types::ChainhookSpecification;
use chainhook_types::StacksNetwork;
use chainhook_types::{BitcoinNetwork, Chain};
use clarinet_deployments::onchain::{
Expand All @@ -36,6 +35,7 @@ use clarity_repl::clarity::ClarityVersion;
use clarity_repl::repl::diagnostic::{output_code, output_diagnostic};
use clarity_repl::repl::{ClarityCodeSource, ClarityContract, ContractDeployer, DEFAULT_EPOCH};
use clarity_repl::{analysis, repl, Terminal};
use stacks_network::chainhook_event_observer::chainhooks::types::ChainhookSpecification;
use stacks_network::{self, DevnetOrchestrator};
use std::collections::HashMap;
use std::fs::{self, File};
Expand Down
2 changes: 1 addition & 1 deletion components/clarinet-cli/src/integrate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use std::{
};

use crate::chainhooks::load_chainhooks;
use chainhook_event_observer::utils::Context;
use chainhook_types::{BitcoinNetwork, StacksNetwork};
use clarinet_deployments::types::DeploymentSpecification;
use hiro_system_kit::Drain;
use hiro_system_kit::{slog, slog_async, slog_term};
use stacks_network::chainhook_event_observer::utils::Context;
use stacks_network::{
do_run_devnet, ChainsCoordinatorCommand, DevnetEvent, DevnetOrchestrator, LogData,
};
Expand Down
14 changes: 7 additions & 7 deletions components/clarinet-cli/src/runner/api_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ use super::ChainhookEvent;
use super::DeploymentCache;
use super::SessionArtifacts;
use crate::runner::api_v1::utils::serialize_event;
use chainhook_event_observer::chainhooks::stacks::evaluate_stacks_transaction_predicate_on_transaction;
use chainhook_event_observer::chainhooks::stacks::handle_stacks_hook_action;
use chainhook_event_observer::chainhooks::stacks::StacksChainhookOccurrence;
use chainhook_event_observer::chainhooks::stacks::StacksTriggerChainhook;
use chainhook_event_observer::chainhooks::types::StacksChainhookSpecification;
use chainhook_event_observer::indexer::stacks::get_standardized_stacks_receipt;
use chainhook_event_observer::utils::Context;
use chainhook_types::BlockIdentifier;
use chainhook_types::StacksBlockData;
use chainhook_types::StacksBlockMetadata;
Expand Down Expand Up @@ -50,6 +43,13 @@ use deno_core::serde_json::{json, Value};
use deno_core::{op, Extension};
use deno_core::{ModuleSpecifier, OpState};
use sha2::{Digest, Sha256};
use stacks_network::chainhook_event_observer::chainhooks::stacks::evaluate_stacks_transaction_predicate_on_transaction;
use stacks_network::chainhook_event_observer::chainhooks::stacks::handle_stacks_hook_action;
use stacks_network::chainhook_event_observer::chainhooks::stacks::StacksChainhookOccurrence;
use stacks_network::chainhook_event_observer::chainhooks::stacks::StacksTriggerChainhook;
use stacks_network::chainhook_event_observer::chainhooks::types::StacksChainhookSpecification;
use stacks_network::chainhook_event_observer::indexer::stacks::get_standardized_stacks_receipt;
use stacks_network::chainhook_event_observer::utils::Context;
use std::collections::{BTreeMap, HashMap};
use std::fs::OpenOptions;
use std::io::Write;
Expand Down
2 changes: 1 addition & 1 deletion components/clarinet-cli/src/runner/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use super::vendor::deno_cli::tools::test::{
use super::vendor::deno_runtime::permissions::Permissions;
use super::vendor::deno_runtime::tokio_util::run_local;
use super::{api_v1, costs, ChainhookEvent, DeploymentCache, SessionArtifacts};
use chainhook_event_observer::chainhooks::types::StacksChainhookSpecification;
use clarinet_files::{FileLocation, ProjectManifest};
use clarity_repl::analysis::coverage::CoverageReporter;
use deno_ast::swc::common::comments::CommentKind;
Expand All @@ -43,6 +42,7 @@ use rand::rngs::SmallRng;
use rand::seq::SliceRandom;
use rand::SeedableRng;
use regex::Regex;
use stacks_network::chainhook_event_observer::chainhooks::types::StacksChainhookSpecification;
use std::collections::HashSet;
use std::fmt::Write as _;
use std::num::NonZeroUsize;
Expand Down
2 changes: 1 addition & 1 deletion components/clarinet-cli/src/runner/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use chainhook_event_observer::chainhooks::types::StacksChainhookSpecification;
use clarinet_deployments::types::DeploymentGenerationArtifacts;
use clarinet_deployments::{
initiate_session_from_deployment, update_session_with_contracts_executions,
Expand All @@ -14,6 +13,7 @@ use clarity_repl::clarity::vm::types::QualifiedContractIdentifier;
use clarity_repl::clarity::vm::EvaluationResult;
use clarity_repl::repl::{session::CostsReport, Session};
use deno_core::error::AnyError;
use stacks_network::chainhook_event_observer::chainhooks::types::StacksChainhookSpecification;
use std::collections::HashMap;

use clarinet_deployments::types::DeploymentSpecification;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub fn sign_transaction(

pub fn send_transaction_spec(
bitcoin_rpc: &Client,
bitcoin_wallet_rpc: &Client,
tx_spec: &BtcTransferSpecification,
signer: &SecretKey,
) -> Result<bitcoincore_rpc::bitcoin::Txid, String> {
Expand All @@ -135,7 +136,7 @@ pub fn send_transaction_spec(
Address::from_str(&tx_spec.expected_sender).expect("Unable to parse address");
let addresses = vec![&sender_address];

let mut utxos = bitcoin_rpc
let mut utxos = bitcoin_wallet_rpc
.list_unspent(None, None, Some(&addresses), None, None)
.expect("Unable to retrieve UTXOs");

Expand Down
20 changes: 17 additions & 3 deletions components/clarinet-deployments/src/onchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,25 @@ pub fn apply_on_chain_deployment(
url.host().expect("Host unknown"),
url.port_or_known_default().expect("Protocol unknown")
);
let bitcoin_rpc = Client::new(&bitcoin_node_rpc_url, auth).unwrap();
let bitcoin_rpc = Client::new(&bitcoin_node_rpc_url, auth.clone()).unwrap();

let bitcoin_node_wallet_rpc_url = format!(
"{}://{}:{}/wallet/",
url.scheme(),
url.host().expect("Host unknown"),
url.port_or_known_default().expect("Protocol unknown")
);
let bitcoin_node_wallet_rpc =
Client::new(&bitcoin_node_wallet_rpc_url, auth).unwrap();

let account = btc_accounts_lookup.get(&tx.expected_sender).unwrap();
let (secret_key, _public_key) = get_btc_keypair(account);
let _ =
bitcoin_deployment::send_transaction_spec(&bitcoin_rpc, tx, &secret_key);
let _ = bitcoin_deployment::send_transaction_spec(
&bitcoin_rpc,
&bitcoin_node_wallet_rpc,
tx,
&secret_key,
);
continue;
}
TransactionSpecification::ContractCall(tx) => {
Expand Down
1 change: 0 additions & 1 deletion components/stacks-network/src/chains_coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ pub async fn start_chains_coordinator(
}
ObserverEvent::HookRegistered(hook) => {
let message = format!("New hook \"{}\" registered", hook.name());
ctx.try_log(|logger| slog::info!(logger, "{}", message));
let _ = devnet_event_tx.send(DevnetEvent::info(message));
}
ObserverEvent::HookDeregistered(_hook) => {}
Expand Down
2 changes: 1 addition & 1 deletion components/stacks-network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod chains_coordinator;
mod orchestrator;
mod ui;

pub use chainhook_event_observer::utils::Context;
pub use chainhook_event_observer::{self, utils::Context};
pub use orchestrator::DevnetOrchestrator;

use std::{
Expand Down
Loading

0 comments on commit b1264e6

Please sign in to comment.