Skip to content

Commit

Permalink
Merge branch 'develop' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum authored Oct 29, 2024
2 parents a72a773 + da29cf7 commit ede72c5
Show file tree
Hide file tree
Showing 18 changed files with 819 additions and 429 deletions.
582 changes: 295 additions & 287 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[package]
name = "nomic"
version = "9.1.4"
version = "9.2.0"
authors = ["Nomic DAO Foundation <[email protected]>"]
edition = "2021"
default-run = "nomic"

[dependencies]
orga = { git = "https://github.com/nomic-io/orga.git", rev = "35988d76b58008e37794064c41f3d0ba102ca0c8", features = [
bitcoin = { version = "0.29.2", features = ["serde", "rand"] }
orga = { git = "https://github.com/nomic-io/orga.git", rev = "3b3d25ade40d81cb64f19335535e3a47bb47778f", features = [
"merk-verify",
"feat-ibc",
] }
Expand Down Expand Up @@ -53,7 +54,6 @@ ripemd = "0.1.3"
frost-secp256k1-tr = { git = "https://github.com/ZcashFoundation/frost", rev = "51fa7d09f3742563a35d065afcff6ad486430dac", features = [
"nightly",
], optional = true }
bitcoin = { version = "0.29.2", features = ["serde", "rand"] }
serde-hex = "0.1.0"
alloy-core = { version = "0.8.5", optional = true }
alloy-sol-types = { version = "0.8.5", optional = true }
Expand Down
6 changes: 3 additions & 3 deletions networks/testnet.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
state_sync_rpc = [
"http://147.182.171.216:26657",
"http://147.182.171.216:26657",
"http://147.182.171.216:26657",
"http://147.182.171.216:26657",
]
tendermint_flags = ["--p2p.seeds", """
[email protected]:26656,\
"""]
btc_relayer = ["https://relayer.nomic-testnet.mappum.io:8443"]

legacy_version = "8.1.x"
legacy_version = "9.1.x"

genesis = """
{
Expand Down
6 changes: 3 additions & 3 deletions rest/Cargo.lock

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

90 changes: 75 additions & 15 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use orga::describe::{Describe, Descriptor};
use orga::encoding::{Decode, Encode, LengthString, LengthVec};
use orga::ibc::ibc_rs::apps::transfer::types::Memo;
use orga::ibc::ClientIdKey as ClientId;
use sha2::{Digest, Sha256};

use std::io::Read;
use std::str::FromStr;
Expand Down Expand Up @@ -103,7 +104,7 @@ const CALL_FEE_USATS: u64 = 100_000_000;
#[cfg(feature = "ethereum")]
const ETH_CREATE_CONNECTION_FEE_USATS: u64 = 10_000_000_000;

const OSMOSIS_CHANNEL_ID: &str = "channel-1";
pub const OSMOSIS_CHANNEL_ID: &str = "channel-1";

#[cfg(feature = "frost")]
const FROST_GROUP_INTERVAL: i64 = 10 * 60;
Expand Down Expand Up @@ -201,7 +202,7 @@ impl InnerApp {
/// breaking changes are made to either the state encoding or logic of the
/// protocol, and requires a network upgrade to be coordinated via the
/// upgrade module.
pub const CONSENSUS_VERSION: u8 = 13;
pub const CONSENSUS_VERSION: u8 = 14;

#[cfg(feature = "full")]
fn configure_faucets(&mut self) -> Result<()> {
Expand Down Expand Up @@ -304,12 +305,11 @@ impl InnerApp {
) -> Result<()> {
#[cfg(feature = "ethereum")]
{
crate::bitcoin::exempt_from_fee()?;

// TODO: fee

let signer = self.signer()?;
let coins = self.bitcoin.accounts.withdraw(signer, amount)?;
let mut coins = self.bitcoin.accounts.withdraw(signer, amount)?;

let fee = coins.take(20_000_000)?;
self.bitcoin.give_rewards(fee)?;

let dest = Dest::EthAccount {
network,
Expand Down Expand Up @@ -510,6 +510,7 @@ impl InnerApp {

match sender {
Identity::NativeAccount { address } => {
log::debug!("Returning funds to NativeAccount sender");
self.bitcoin.accounts.deposit(address, coins)?;
}
#[cfg(feature = "ethereum")]
Expand All @@ -518,10 +519,17 @@ impl InnerApp {
connection,
address,
} => {
self.ethereum
let res = self
.ethereum
.network_mut(network)?
.connection_mut(connection.into())?
.transfer(address.into(), coins)?;
.transfer(address.into(), coins);
if let Err(e) = res {
log::debug!("Error returning funds to EthAccount sender: {:?}", e);
// TODO: place funds in rewards pool?
} else {
log::debug!("Returning funds to EthAccount sender");
}
}
_ => {}
}
Expand Down Expand Up @@ -1609,7 +1617,7 @@ pub struct IbcDest {
pub receiver: LengthString<u8>,
pub sender: LengthString<u8>,
pub timeout_timestamp: u64,
pub memo: LengthString<u8>,
pub memo: LengthString<u16>,
}

impl IbcDest {
Expand Down Expand Up @@ -1696,16 +1704,57 @@ impl IbcDest {
Ok(())
}

pub fn legacy_encode(&self) -> Result<Vec<u8>> {
pub fn legacy_encode(&self) -> Result<Vec<Vec<u8>>> {
let mut encodings = vec![];

let mut bytes = vec![];
self.source_port.encode_into(&mut bytes)?;
self.source_channel.encode_into(&mut bytes)?;
EdAdapter(self.receiver_signer()?).encode_into(&mut bytes)?;
EdAdapter(self.sender_signer()?).encode_into(&mut bytes)?;
self.timeout_timestamp.encode_into(&mut bytes)?;
self.memo.encode_into(&mut bytes)?;
encodings.push(Sha256::digest(bytes).to_vec());

if self.memo.len() < 256 {
let mut bytes = vec![];
self.source_port.encode_into(&mut bytes)?;
self.source_channel.encode_into(&mut bytes)?;
self.receiver.encode_into(&mut bytes)?;
self.sender.encode_into(&mut bytes)?;
self.timeout_timestamp.encode_into(&mut bytes)?;
LengthString::<u8>::new(self.memo.len() as u8, self.memo.to_string())
.encode_into(&mut bytes)?;

let hash = Sha256::digest(bytes);
let mut bytes = Vec::with_capacity(hash.len() + 1);
bytes.push(0); // version byte
bytes.extend_from_slice(&hash);
encodings.push(bytes);
}

Ok(bytes)
Ok(encodings)
}
}

impl Migrate for IbcDest {
#[allow(clippy::needless_borrows_for_generic_args)]
fn migrate(_src: Store, _dest: Store, mut bytes: &mut &[u8]) -> Result<Self> {
let source_port = LengthString::<u8>::decode(&mut bytes)?;
let source_channel = LengthString::<u8>::decode(&mut bytes)?;
let receiver = LengthString::<u8>::decode(&mut bytes)?;
let sender = LengthString::<u8>::decode(&mut bytes)?;
let timeout_timestamp = u64::decode(&mut bytes)?;
let memo = LengthString::<u8>::decode(&mut bytes)?;

Ok(IbcDest {
source_port,
source_channel,
receiver,
sender,
timeout_timestamp,
memo: memo.to_string().try_into().unwrap(),
})
}
}

Expand Down Expand Up @@ -1920,11 +1969,11 @@ impl Dest {
}

// TODO: remove once there are no legacy commitments in-flight
pub fn legacy_commitment_bytes(&self) -> Result<Vec<u8>> {
pub fn legacy_commitment_bytes(&self) -> Result<Vec<Vec<u8>>> {
use sha2::{Digest, Sha256};
let bytes = match self {
Dest::NativeAccount { address } => address.bytes().into(),
Dest::Ibc { data } => Sha256::digest(data.legacy_encode()?).to_vec(),
Dest::NativeAccount { address } => vec![address.bytes().into()],
Dest::Ibc { data } => data.legacy_encode()?,
_ => return Err(Error::App("Invalid dest for legacy commitment".to_string())),
};

Expand Down Expand Up @@ -2002,7 +2051,18 @@ impl Query for Dest {
}

impl Migrate for Dest {
#[allow(clippy::needless_borrows_for_generic_args)]
fn migrate(src: Store, dest: Store, bytes: &mut &[u8]) -> Result<Self> {
// TODO: !!!!!!!! remove from here once there are no legacy IBC dests
// Migrate IBC dests
let mut maybe_ibc_bytes = &mut &**bytes;
let variant = u8::decode(&mut maybe_ibc_bytes)?;
if variant == 1 {
let ibc_dest = IbcDest::migrate(src, dest, maybe_ibc_bytes)?;
return Ok(Self::Ibc { data: ibc_dest });
}
// TODO: !!!!!!!! remove to here once there are no legacy IBC dests

Self::load(src, bytes)
}
}
Expand Down
42 changes: 30 additions & 12 deletions src/bin/nomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ pub enum Command {
BabylonRelayer(BabylonRelayerCmd),
#[cfg(feature = "babylon")]
StakeNbtc(StakeNbtcCmd),
#[cfg(feature = "frost")]
FrostSigner(FrostSignerCmd),
#[cfg(feature = "ethereum")]
RelayEthereum(RelayEthereumCmd),
#[cfg(feature = "ethereum")]
Expand Down Expand Up @@ -264,8 +262,6 @@ impl Command {
BabylonRelayer(cmd) => cmd.run().await,
#[cfg(feature = "babylon")]
StakeNbtc(cmd) => cmd.run().await,
#[cfg(feature = "frost")]
FrostSigner(cmd) => cmd.run().await,
#[cfg(feature = "ethereum")]
RelayEthereum(cmd) => cmd.run().await,
#[cfg(feature = "ethereum")]
Expand Down Expand Up @@ -1346,7 +1342,7 @@ impl ClaimAirdropCmd {
/// Relays data between the Bitcoin and Nomic networks.
#[derive(Parser, Debug)]
pub struct RelayerCmd {
/// The port of the Bitcoin RPC server.
/// The port of the local Bitcoin RPC server.
// TODO: get the default based on the network
#[clap(short = 'p', long, default_value_t = 8332)]
rpc_port: u16,
Expand All @@ -1359,14 +1355,22 @@ pub struct RelayerCmd {
#[clap(short = 'P', long)]
rpc_pass: Option<String>,

/// The URL for the Bitcoin RPC server, e.g. http://localhost:8332.
#[clap(short = 'r', long, conflicts_with = "rpc-port")]
rpc_url: Option<String>,

#[clap(flatten)]
config: nomic::network::Config,
}

impl RelayerCmd {
/// Builds Bitcoin RPC client.
async fn btc_client(&self) -> Result<BtcClient> {
let rpc_url = format!("http://localhost:{}", self.rpc_port);
let rpc_url = if let Some(rpc) = self.rpc_url.clone() {
rpc
} else {
format!("http://localhost:{}", self.rpc_port)
};
let auth = match (self.rpc_user.clone(), self.rpc_pass.clone()) {
(Some(user), Some(pass)) => Auth::UserPass(user, pass),
_ => Auth::None,
Expand Down Expand Up @@ -1503,7 +1507,17 @@ impl SignerCmd {

let relaunch = relaunch_on_migrate(&self.config);

futures::try_join!(signer, relaunch).unwrap();
#[cfg(feature = "frost")]
let frost_signer = {
let frost_cmd = FrostSignerCmd {
config: self.config.clone(),
};
frost_cmd.run()
};
#[cfg(not(feature = "frost"))]
let frost_signer = async { Ok(()) };

futures::try_join!(signer, relaunch, frost_signer).unwrap();

Ok(())
}
Expand Down Expand Up @@ -1806,8 +1820,10 @@ impl EthTransferNbtcCmd {
#[derive(Parser, Debug)]
pub struct GrpcCmd {
/// The port to listen on.
#[clap(default_value_t = 9001)]
#[clap(long, default_value_t = 9001)]
port: u16,
#[clap(long, default_value = "127.0.0.1")]
host: String,

#[clap(flatten)]
config: nomic::network::Config,
Expand All @@ -1817,12 +1833,14 @@ impl GrpcCmd {
/// Runs the `grpc` command.
async fn run(&self) -> Result<()> {
use orga::ibc::GrpcOpts;
std::panic::set_hook(Box::new(|_| {}));
std::panic::set_hook(Box::new(|e| {
log::error!("{}", e.to_string());
}));
log::info!("Starting gRPC server on {}:{}", self.host, self.port);
orga::ibc::start_grpc(
// TODO: support configuring RPC address
|| nomic::app_client("http://localhost:26657").sub(|app| Ok(app.ibc.ctx)),
|| self.config.client().sub(|app| Ok(app.ibc.ctx)),
&GrpcOpts {
host: "127.0.0.1".to_string(),
host: self.host.to_string(),
port: self.port,
chain_id: self.config.chain_id.clone().unwrap(),
},
Expand Down
13 changes: 13 additions & 0 deletions src/bitcoin/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2423,6 +2423,19 @@ impl CheckpointQueue {

Ok(())
}

/// Returns the pending transfers for the building checkpoint.
///
/// This query is a temporary workaround for a client iteration issue.
#[query]
pub fn pending(&self) -> Result<Vec<(Dest, Identity, u64)>> {
let mut pending = vec![];
for entry in self.building()?.pending.iter()? {
let (dest, coin) = entry?;
pending.push((dest.0.clone(), dest.1, coin.amount.into()));
}
Ok(pending)
}
}

/// Takes a previous fee rate and returns a new fee rate, adjusted up or down by
Expand Down
Loading

0 comments on commit ede72c5

Please sign in to comment.