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

Merge deposit-test review changes and refactor into ekrem/new-architecture #243

Merged
merged 2 commits into from
Aug 27, 2024
Merged
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
7 changes: 3 additions & 4 deletions core/src/database/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use std::str::FromStr;

use bitcoin::{address::NetworkUnchecked, Address, OutPoint, TxOut, Txid};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sqlx::{
postgres::{PgArgumentBuffer, PgRow, PgValueRef},
Decode, Encode, Postgres, Row,
postgres::{PgArgumentBuffer, PgValueRef},
Decode, Encode, Postgres,
};

use crate::{ByteArray66, EVMAddress, UTXO};
use crate::EVMAddress;

#[derive(Serialize, Deserialize)]
pub struct OutPointDB(pub OutPoint);
Expand Down
117 changes: 15 additions & 102 deletions core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ pub enum BridgeError {
InvalidPeriod(InvalidPeriodError),
/// Returned when the secp256k1 crate returns an error
#[error("Secpk256Error: {0}")]
Secpk256Error(secp256k1::Error),
Secpk256Error(#[from] secp256k1::Error),
/// Returned when the bitcoin crate returns an error in the sighash taproot module
#[error("BitcoinSighashTaprootError: {0}")]
BitcoinSighashTaprootError(bitcoin::sighash::TaprootError),
BitcoinSighashTaprootError(#[from] bitcoin::sighash::TaprootError),
/// Returned when a non finalized deposit request is found
#[error("DepositNotFinalized")]
DepositNotFinalized,
Expand All @@ -53,7 +53,7 @@ pub enum BridgeError {
TxidNotFound,
/// Returned in RPC error
#[error("BitcoinCoreRPCError: {0}")]
BitcoinRpcError(bitcoincore_rpc::Error),
BitcoinRpcError(#[from] bitcoincore_rpc::Error),
/// Returned if there is no confirmation data
#[error("NoConfirmationData")]
NoConfirmationData,
Expand All @@ -65,7 +65,7 @@ pub enum BridgeError {
TryFromSliceError,
/// Returned when bitcoin::Transaction error happens, also returns the error
#[error("BitcoinTransactionError: {0}")]
BitcoinConsensusEncodeError(bitcoin::consensus::encode::Error),
BitcoinConsensusEncodeError(#[from] bitcoin::consensus::encode::Error),
/// TxInputNotFound is returned when the input is not found in the transaction
#[error("TxInputNotFound")]
TxInputNotFound,
Expand Down Expand Up @@ -101,34 +101,31 @@ pub enum BridgeError {
BlockNotFound,
/// Merkle Block Error
#[error("MerkleBlockError: {0}")]
MerkleBlockError(MerkleBlockError),
MerkleBlockError(#[from] MerkleBlockError),
/// Merkle Proof Error
#[error("MerkleProofError")]
MerkleProofError,
/// JSON RPC call failed
#[error("JsonRpcError: {0}")]
JsonRpcError(jsonrpsee::core::client::Error),
/// Given key pair is invalid and new pairs can't be generated randomly
#[error("InvalidKeyPair")]
InvalidKeyPair(std::io::Error),
JsonRpcError(#[from] jsonrpsee::core::client::Error),
/// ConfigError is returned when the configuration is invalid
#[error("ConfigError: {0}")]
ConfigError(String),
/// Bitcoin Address Parse Error, probably given address network is invalid
#[error("BitcoinAddressParseError: {0}")]
BitcoinAddressParseError(bitcoin::address::ParseError),
BitcoinAddressParseError(#[from] bitcoin::address::ParseError),
/// Port error for tests
#[error("PortError: {0}")]
PortError(String),
/// Database error
#[error("DatabaseError: {0}")]
DatabaseError(sqlx::Error),
DatabaseError(#[from] sqlx::Error),
/// Operator tries to claim with different bridge funds with the same withdrawal idx
#[error("AlreadySpentWithdrawal")]
AlreadySpentWithdrawal,
/// There was an error while creating a server.
#[error("ServerError")]
ServerError(std::io::Error),
ServerError(#[from] std::io::Error),
/// When the operators funding utxo is not found
#[error("OperatorFundingUtxoNotFound: Funding utxo not found, pls send some amount here: {0}, then call the set_operator_funding_utxo RPC")]
OperatorFundingUtxoNotFound(bitcoin::Address),
Expand All @@ -140,30 +137,30 @@ pub enum BridgeError {
InvalidKickoffUtxo,

#[error("KeyAggContextError: {0}")]
KeyAggContextError(musig2::errors::KeyAggError),
KeyAggContextError(#[from] musig2::errors::KeyAggError),

#[error("KeyAggContextTweakError: {0}")]
KeyAggContextTweakError(musig2::errors::TweakError),
KeyAggContextTweakError(#[from] musig2::errors::TweakError),

#[error("InvalidScalarBytes: {0}")]
InvalidScalarBytes(InvalidScalarBytes),
InvalidScalarBytes(#[from] InvalidScalarBytes),

#[error("NoncesNotFound")]
NoncesNotFound,

#[error("MuSig2VerifyError: {0}")]
MuSig2VerifyError(musig2::errors::VerifyError),
MuSig2VerifyError(#[from] musig2::errors::VerifyError),

#[error("KickoffOutpointsNotFound")]
KickoffOutpointsNotFound,
#[error("DepositInfoNotFound")]
DepositInfoNotFound,

#[error("FromHexError: {0}")]
FromHexError(hex::FromHexError),
FromHexError(#[from] hex::FromHexError),

#[error("FromSliceError: {0}")]
FromSliceError(bitcoin::hashes::FromSliceError),
FromSliceError(#[from] bitcoin::hashes::FromSliceError),
}

impl Into<ErrorObject<'static>> for BridgeError {
Expand All @@ -172,18 +169,6 @@ impl Into<ErrorObject<'static>> for BridgeError {
}
}

impl From<secp256k1::Error> for BridgeError {
fn from(err: secp256k1::Error) -> Self {
BridgeError::Secpk256Error(err)
}
}

impl From<bitcoin::sighash::TaprootError> for BridgeError {
fn from(err: bitcoin::sighash::TaprootError) -> Self {
BridgeError::BitcoinSighashTaprootError(err)
}
}

impl From<Vec<u8>> for BridgeError {
fn from(_error: Vec<u8>) -> Self {
BridgeError::VecConversionError
Expand All @@ -196,12 +181,6 @@ impl From<TryFromSliceError> for BridgeError {
}
}

impl From<bitcoin::consensus::encode::Error> for BridgeError {
fn from(err: bitcoin::consensus::encode::Error) -> Self {
BridgeError::BitcoinConsensusEncodeError(err)
}
}

impl From<TaprootBuilderError> for BridgeError {
fn from(_error: TaprootBuilderError) -> Self {
BridgeError::TaprootBuilderError
Expand All @@ -213,69 +192,3 @@ impl From<TaprootBuilder> for BridgeError {
BridgeError::TaprootBuilderError
}
}

impl From<bitcoincore_rpc::Error> for BridgeError {
fn from(err: bitcoincore_rpc::Error) -> Self {
BridgeError::BitcoinRpcError(err)
}
}

impl From<MerkleBlockError> for BridgeError {
fn from(err: MerkleBlockError) -> Self {
BridgeError::MerkleBlockError(err)
}
}

impl From<jsonrpsee::core::client::Error> for BridgeError {
fn from(err: jsonrpsee::core::client::Error) -> Self {
BridgeError::JsonRpcError(err)
}
}

impl From<bitcoin::address::ParseError> for BridgeError {
fn from(err: bitcoin::address::ParseError) -> Self {
BridgeError::BitcoinAddressParseError(err)
}
}

impl From<sqlx::Error> for BridgeError {
fn from(err: sqlx::Error) -> Self {
BridgeError::DatabaseError(err)
}
}

impl From<musig2::errors::KeyAggError> for BridgeError {
fn from(err: musig2::errors::KeyAggError) -> Self {
BridgeError::KeyAggContextError(err)
}
}

impl From<musig2::errors::TweakError> for BridgeError {
fn from(err: musig2::errors::TweakError) -> Self {
BridgeError::KeyAggContextTweakError(err)
}
}

impl From<InvalidScalarBytes> for BridgeError {
fn from(err: InvalidScalarBytes) -> Self {
BridgeError::InvalidScalarBytes(err)
}
}

impl From<musig2::errors::VerifyError> for BridgeError {
fn from(err: musig2::errors::VerifyError) -> Self {
BridgeError::MuSig2VerifyError(err)
}
}

impl From<hex::FromHexError> for BridgeError {
fn from(err: hex::FromHexError) -> Self {
BridgeError::FromHexError(err)
}
}

impl From<bitcoin::hashes::FromSliceError> for BridgeError {
fn from(err: bitcoin::hashes::FromSliceError) -> Self {
BridgeError::FromSliceError(err)
}
}
9 changes: 0 additions & 9 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ pub struct UTXO {
pub txout: bitcoin::TxOut,
}

impl UTXO {
fn to_vec(&self) -> Vec<u8> {
let outpoint_hex = bitcoin::consensus::encode::serialize_hex(&self.outpoint);
let txout_hex = bitcoin::consensus::encode::serialize_hex(&self.txout);
let all = format!("{}{}", outpoint_hex, txout_hex);
hex::decode(all).unwrap()
}
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, sqlx::Type)]
#[sqlx(type_name = "bytea")]
pub struct ByteArray66(#[serde(with = "hex::serde")] pub [u8; 66]);
1 change: 0 additions & 1 deletion core/src/musig2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ mod tests {
use crate::{
actor::Actor,
errors::BridgeError,
script_builder,
transaction_builder::{TransactionBuilder, TxHandler},
utils,
};
Expand Down
11 changes: 3 additions & 8 deletions core/src/operator.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use std::borrow::Borrow;
use std::mem::swap;

use crate::actor::Actor;
use crate::config::BridgeConfig;
use crate::database::operator::OperatorDB;
use crate::errors::BridgeError;
use crate::extended_rpc::ExtendedRpc;
use crate::musig2::{self, MuSigAggNonce, MuSigPartialSignature, MuSigPubNonce};
use crate::musig2;
use crate::traits::rpc::OperatorRpcServer;
use crate::transaction_builder::TransactionBuilder;
use crate::utils::parse_hex_to_btc_tx;
use crate::{script_builder, utils, EVMAddress, UTXO};
use ::musig2::secp::Point;
use bitcoin::address::NetworkUnchecked;
Expand All @@ -18,7 +14,6 @@ use bitcoin::hashes::Hash;
use bitcoin::sighash::SighashCache;
use bitcoin::{Address, OutPoint, TapSighash, Transaction, TxOut, Txid};
use bitcoin_mock_rpc::RpcApiWrapper;
use bitcoincore_rpc::json::{self, SigHashType};
use bitcoincore_rpc::RawTx;
use clementine_circuits::constants::BRIDGE_AMOUNT_SATS;
use clementine_circuits::sha256_hash;
Expand Down Expand Up @@ -300,8 +295,8 @@ where

async fn withdrawal_proved_on_citrea(
&self,
withdrawal_idx: usize,
kickoff_merkle_root: [u8; 32],
_withdrawal_idx: usize,
_kickoff_merkle_root: [u8; 32],
) -> Result<(), BridgeError> {
Ok(())
}
Expand Down
4 changes: 1 addition & 3 deletions core/src/script_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
//! scripts.

use crate::EVMAddress;
use bitcoin::address::NetworkUnchecked;
use bitcoin::blockdata::opcodes::all::OP_PUSHNUM_1;
use bitcoin::hashes::Hash;
use bitcoin::Amount;
use bitcoin::{
opcodes::{all::*, OP_FALSE},
script::Builder,
ScriptBuf, TxOut,
};
use bitcoin::{Address, Amount, OutPoint};
use secp256k1::XOnlyPublicKey;

pub fn anyone_can_spend_txout() -> TxOut {
Expand Down
17 changes: 7 additions & 10 deletions core/src/servers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! # Servers
//!
//! Utilities for operator and verifier servers.
use crate::create_extended_rpc;
use crate::mock::common;
use crate::{config, create_extended_rpc};
use crate::{
config::BridgeConfig,
create_test_config, create_test_config_with_thread_name,
Expand Down Expand Up @@ -38,7 +38,6 @@ where
.await
.unwrap();
database.close().await;
// let config = create_test_config!(config.db_name, config);
let server = match Server::builder()
.build(format!("{}:{}", config.host, config.port))
.await
Expand All @@ -48,10 +47,9 @@ where
};
let verifier = Verifier::new(rpc, config).await?;

let addr: std::net::SocketAddr = match server.local_addr() {
Ok(a) => a,
Err(e) => return Err(BridgeError::ServerError(e)),
};
let addr: std::net::SocketAddr = server
.local_addr()
.map_err(|e| BridgeError::ServerError(e))?;
let handle = server.start(verifier.into_rpc());

let client = HttpClientBuilder::default()
Expand Down Expand Up @@ -89,10 +87,9 @@ where
Err(e) => return Err(BridgeError::ServerError(e)),
};

let addr = match server.local_addr() {
Ok(s) => s,
Err(e) => return Err(BridgeError::ServerError(e)),
};
let addr: std::net::SocketAddr = server
.local_addr()
.map_err(|e| BridgeError::ServerError(e))?;
let handle = server.start(operator.into_rpc());

let client = HttpClientBuilder::default()
Expand Down
Loading