Skip to content
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ jobs:
- nightly
steps:
- uses: actions/checkout@v2
- run: GOAT_CHAIN_URL=https://rpc.testnet3.goat.network GOAT_GATEWAY_CONTRACT_ADDRESS=0xeD8AeeD334fA446FA03Aa00B28aFf02FA8aC02df GOAT_GATEWAY_CONTRACT_CREATION=0 GOAT_CHAIN_ID=48816 cargo test -r
- run: cd scripts && docker compose up -d
- run: cargo test -r --features tests
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ libp2p-metrics = "0.16.0"
libp2p-core = "0.43.0"
prometheus-client = "0.22.3"
once_cell = "1.5"
log = "0.4.0"
bitcoin-script = { git = "https://github.com/BitVM/rust-bitcoin-script" }
bitcoin = { version = "0.32.5", features = ["rand-std"] }
strum = "0.27"
Expand Down Expand Up @@ -79,4 +78,4 @@ bitvm2-lib = { path = "crates/bitvm2" }
identity = { path = "crates/identity" }
store = { path = "crates/store" }
spv = { path = "crates/spv" }
client = { path = "crates/client" }
client = { path = "crates/client" }
1 change: 1 addition & 0 deletions crates/bitvm2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ bitcoin-script = { workspace = true }
serde_json = { workspace = true }
bincode = "1.3.3"
uuid = { workspace = true }
clap = { version = "4.5.37", features = ["derive"] }
13 changes: 6 additions & 7 deletions crates/bitvm2/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::committee::{COMMITTEE_PRE_SIGN_NUM, push_committee_pre_signatures};
use crate::operator::push_operator_pre_signature;
use anyhow::Result;
use bitcoin::{Address, Amount, Network, PrivateKey, PublicKey, XOnlyPublicKey, key::Keypair};
use bitcoin::{OutPoint, TapNodeHash, Witness};
Expand Down Expand Up @@ -28,9 +30,6 @@ use rand::{Rng, distributions::Alphanumeric};
use secp256k1::SECP256K1;
use serde::{Deserialize, Serialize};

use crate::committee::{COMMITTEE_PRE_SIGN_NUM, push_committee_pre_signatures};
use crate::operator::push_operator_pre_signature;

pub type VerifyingKey = ark_groth16::VerifyingKey<ark_bn254::Bn254>;
pub type Groth16Proof = ark_groth16::Proof<ark_bn254::Bn254>;
pub type PublicInputs = Vec<ark_bn254::Fr>;
Expand All @@ -50,7 +49,7 @@ pub fn random_string(len: usize) -> String {
rand::thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Clone)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Bitvm2Parameters {
pub network: Network,
pub depositor_evm_address: [u8; 20],
Expand Down Expand Up @@ -115,7 +114,7 @@ impl Bitvm2Parameters {
}
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Clone)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Bitvm2Graph {
pub(crate) operator_pre_signed: bool,
pub(crate) committee_pre_signed: bool,
Expand All @@ -133,7 +132,7 @@ pub struct Bitvm2Graph {
pub disprove: DisproveTransaction,
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Clone)]
#[derive(Serialize, Deserialize, Clone)]
pub struct SimplifiedBitvm2Graph {
pub parameters: Bitvm2Parameters,
pub operator_pre_sigs: Option<Witness>,
Expand Down Expand Up @@ -343,7 +342,7 @@ impl Bitvm2Graph {
}
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Clone)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct CustomInputs {
pub inputs: Vec<Input>,
/// stake amount / pegin_amount
Expand Down
532 changes: 272 additions & 260 deletions crates/bitvm2/tests/e2e.rs

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion crates/client/src/chain/goat_adaptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ sol!(
}
);

/// testnet rpc https://rpc.testnet3.goat.network
pub struct GoatInitConfig {
pub rpc_url: Url,
pub gateway_address: EvmAddress,
Expand All @@ -110,6 +109,22 @@ pub struct GoatInitConfig {
pub private_key: Option<String>,
pub chain_id: u32,
}

impl GoatInitConfig {
pub fn from_env_for_test() -> Self {
GoatInitConfig {
rpc_url: "https://rpc.testnet3.goat.network".parse::<Url>().expect("decode url"),
gateway_address: "0xeD8AeeD334fA446FA03Aa00B28aFf02FA8aC02df"
.parse()
.expect("parse contract address"),
gateway_creation_block: 0,
to_block: None,
private_key: None,
chain_id: 48816_u32,
}
}
}

pub struct GoatAdaptor {
chain_id: ChainId,
_gateway_address: EvmAddress,
Expand Down
12 changes: 1 addition & 11 deletions crates/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,13 @@ mod tests {
use crate::chain::chain_adaptor::GoatNetwork;
use crate::chain::goat_adaptor::GoatInitConfig;
use crate::client::BitVM2Client;
use alloy::transports::http::reqwest::Url;
use bitcoin::hashes::Hash;
use bitcoin::{Network, Txid};
use std::str::FromStr;

#[tokio::test]
async fn test_spv_check() {
let global_init_config = GoatInitConfig {
rpc_url: "https://rpc.testnet3.goat.network".parse::<Url>().expect("decode url"),
gateway_address: "0xeD8AeeD334fA446FA03Aa00B28aFf02FA8aC02df"
.parse()
.expect("parse contract address"),
gateway_creation_block: 0,
to_block: None,
private_key: None,
chain_id: 48816_u32,
};
let global_init_config = GoatInitConfig::from_env_for_test();
// let local_db = LocalDB::new(&format!("sqlite:{db_path}"), true).await;
let tmp_db = tempfile::NamedTempFile::new().unwrap();
let client = BitVM2Client::new(
Expand Down
4 changes: 4 additions & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ log = "0.4.27"
[dev-dependencies]
tempfile = "3.19.1"
serial_test = "3.2.0"

[features]
default = []
tests = []
25 changes: 0 additions & 25 deletions node/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use goat::transactions::{assert::utils::COMMIT_TX_NUM, pre_signed::PreSignedTran
use libp2p::gossipsub::MessageId;
use libp2p::{PeerId, Swarm, gossipsub};
use musig2::{AggNonce, PartialSignature, PubNonce, SecNonce};
use reqwest::Request;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use store::GraphStatus;
use uuid::Uuid;
Expand Down Expand Up @@ -944,26 +942,3 @@ pub(crate) fn send_to_peer(
let gossipsub_topic = gossipsub::IdentTopic::new(actor);
Ok(swarm.behaviour_mut().gossipsub.publish(gossipsub_topic, serde_json::to_vec(&message)?)?)
}

/// call the rpc service
/// Method::GET/POST/PUT
#[allow(dead_code)]
pub(crate) async fn inner_rpc<S, R>(
addr: &str,
method: reqwest::Method,
uri: &str,
params: S,
) -> Result<R, Box<dyn std::error::Error>>
where
S: Serialize,
R: DeserializeOwned,
{
let client = reqwest::Client::new();
let url = reqwest::Url::parse(&format!("{addr}/{uri}"))?;

let req = Request::new(method, url);
let req_builder = reqwest::RequestBuilder::from_parts(client, req);
let resp = req_builder.json(&params).send().await?;
let txt = resp.text().await?;
Ok(serde_json::from_str(txt.as_str())?)
}
70 changes: 42 additions & 28 deletions node/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub const PEGIN_BASE_VBYTES: u64 = 200;
pub const CHALLENGE_BASE_VBYTES: u64 = 200;

// reduce costs to facilitate testing
pub const MIN_SATKE_AMOUNT: u64 = 700_000; // 0.007 BTC
pub const MIN_SATKE_AMOUNT: u64 = 700_000; // 0.007 BTC
pub const STAKE_RATE: u64 = 0; // 0%
pub const MIN_CHALLENGE_AMOUNT: u64 = 300_000; // 0.003 BTC
// pub const MIN_SATKE_AMOUNT: u64 = 20_000_000; // 0.2 BTC
Expand Down Expand Up @@ -76,33 +76,6 @@ pub fn get_committee_member_num() -> usize {
COMMITTEE_MEMBER_NUMBER
}

pub fn get_bitvm2_client_config() -> GoatInitConfig {
let rpc_url_str = std::env::var(ENV_GOAT_CHAIN_URL)
.unwrap_or_else(|_| panic!("Failed to read {ENV_GOAT_CHAIN_URL} variable"));
let gateway_address_str = std::env::var(ENV_GOAT_GATEWAY_CONTRACT_ADDRESS)
.unwrap_or_else(|_| panic!("Failed to read {ENV_GOAT_GATEWAY_CONTRACT_ADDRESS} variable"));
let gateway_creation = std::env::var(ENV_GOAT_GATEWAY_CONTRACT_CREATION)
.unwrap_or_else(|_| panic!("Failed to read {ENV_GOAT_GATEWAY_CONTRACT_CREATION} variable"));
let to_block = std::env::var(ENV_GOAT_GATEWAY_CONTRACT_TO_BLOCK);
let private_key = std::env::var(ENV_GOAT_PRIVATE_KEY).ok();
let chain_id = std::env::var(ENV_GOAT_CHAIN_ID)
.unwrap_or_else(|_| panic!("Failed to read {ENV_GOAT_CHAIN_ID} variable"));

let rpc_url = rpc_url_str.parse::<Url>();
let gateway_address = gateway_address_str.parse::<EvmAddress>();
GoatInitConfig {
rpc_url: rpc_url.unwrap(),
gateway_address: gateway_address.unwrap(),
gateway_creation_block: gateway_creation.parse::<u64>().unwrap(),
to_block: match to_block {
Ok(block) => Some(BlockNumberOrTag::from_str(block.as_str()).unwrap()),
Err(_) => Some(BlockNumberOrTag::Finalized),
},
private_key,
chain_id: chain_id.parse().expect("fail to parse int"),
}
}

pub enum IpfsTxName {
AssertCommit0,
AssertCommit1,
Expand Down Expand Up @@ -136,3 +109,44 @@ impl IpfsTxName {
}
}
}

pub fn goat_config_from_env() -> GoatInitConfig {
if cfg!(feature = "tests") {
return GoatInitConfig::from_env_for_test();
}

let rpc_url_str =
std::env::var(ENV_GOAT_CHAIN_URL).expect("Failed to read {ENV_GOAT_CHAIN_URL} variable");
let rpc_url = rpc_url_str.parse::<Url>().expect("Failed to parse {rpc_url_str} to URL");

let gateway_address_str = std::env::var(ENV_GOAT_GATEWAY_CONTRACT_ADDRESS)
.expect("Failed to read {ENV_GOAT_GATEWAY_CONTRACT_ADDRESS} variable");
let gateway_address = gateway_address_str
.parse::<EvmAddress>()
.expect("Failed to parse {gateway_address_str} to address");

let gateway_creation = std::env::var(ENV_GOAT_GATEWAY_CONTRACT_CREATION)
.expect("Failed to read {ENV_GOAT_GATEWAY_CONTRACT_CREATION} variable");
let gateway_creation_block =
gateway_creation.parse::<u64>().expect("{ENV_GOAT_GATEWAY_CONTRACT_CREATION} parse");

let to_block = match std::env::var(ENV_GOAT_GATEWAY_CONTRACT_TO_BLOCK).ok() {
Some(to_block_str) => BlockNumberOrTag::from_str(to_block_str.as_str()).ok(),
_ => None,
};

let private_key = std::env::var(ENV_GOAT_PRIVATE_KEY).ok();
let chain_id = std::env::var(ENV_GOAT_CHAIN_ID)
.expect("Failed to read {ENV_GOAT_CHAIN_ID} variable")
.parse::<u32>()
.expect("Failed to parse {chain_id_str} to u32");

GoatInitConfig {
rpc_url,
gateway_address,
gateway_creation_block,
to_block,
private_key,
chain_id,
}
}
3 changes: 2 additions & 1 deletion node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod metrics_service;
mod middleware;
mod relayer_action;
mod rpc_service;
mod tests;
mod utils;

use crate::action::GOATMessage;
Expand Down Expand Up @@ -217,7 +218,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
None,
env::get_network(),
env::get_goat_network(),
env::get_bitvm2_client_config(),
env::goat_config_from_env(),
&ipfs_url,
)
.await;
Expand Down
4 changes: 2 additions & 2 deletions node/src/rpc_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str::FromStr;
mod handler;
mod node;

use crate::env::{get_bitvm2_client_config, get_goat_network, get_network};
use crate::env::{get_goat_network, get_network, goat_config_from_env};
use crate::metrics_service::{MetricsState, metrics_handler, metrics_middleware};
use crate::rpc_service::handler::{bitvm2_handler::*, node_handler::*};
use axum::body::Body;
Expand Down Expand Up @@ -55,7 +55,7 @@ impl AppState {
None,
get_network(),
get_goat_network(),
get_bitvm2_client_config(),
goat_config_from_env(),
ipfs_url,
)
.await;
Expand Down
Loading