Skip to content

Commit

Permalink
Merge pull request fedimint#6336 from joschisan/fix_network_encoding
Browse files Browse the repository at this point in the history
cleanup: sane network encoding for lnv2
  • Loading branch information
joschisan authored Nov 12, 2024
2 parents da9ba35 + 52a55c7 commit 911e672
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
24 changes: 24 additions & 0 deletions fedimint-core/src/encoding/btc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bitcoin::address::NetworkUnchecked;
use bitcoin::hashes::Hash as BitcoinHash;
use hex::{FromHex, ToHex};
use miniscript::{Descriptor, MiniscriptKey};
use serde::{Deserialize, Serialize};

use super::{BufBitcoinReader, CountWrite, SimpleBitcoinRead};
use crate::bitcoin_migration::{bitcoin30_to_bitcoin32_network, bitcoin32_to_bitcoin30_address};
Expand Down Expand Up @@ -170,6 +171,29 @@ impl Decodable for bitcoin::Network {
}
}

#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct NetworkSaneEncodingWrapper(pub bitcoin::Network);

impl Encodable for NetworkSaneEncodingWrapper {
fn consensus_encode<W: Write>(&self, writer: &mut W) -> Result<usize, Error> {
self.0.magic().to_bytes().consensus_encode(writer)
}
}

impl Decodable for NetworkSaneEncodingWrapper {
fn consensus_decode<D: std::io::Read>(
d: &mut D,
modules: &ModuleDecoderRegistry,
) -> Result<Self, DecodeError> {
Ok(Self(
bitcoin::Network::from_magic(bitcoin::p2p::Magic::from_bytes(
Decodable::consensus_decode(d, modules)?,
))
.ok_or_else(|| DecodeError::new_custom(format_err!("Unknown network magic")))?,
))
}
}

impl Encodable for bitcoin::Amount {
fn consensus_encode<W: std::io::Write>(&self, writer: &mut W) -> Result<usize, std::io::Error> {
self.to_sat().consensus_encode(writer)
Expand Down
2 changes: 1 addition & 1 deletion fedimint-core/src/encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
pub mod as_hex;
mod bls12_381;
mod btc;
pub mod btc;
mod secp256k1;
mod threshold_crypto;

Expand Down
6 changes: 3 additions & 3 deletions gateway/ln-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1872,14 +1872,14 @@ impl Gateway {
))))?;
let ln_cfg: &fedimint_lnv2_common::config::LightningClientConfig = cfg.cast()?;

if ln_cfg.network != network {
if ln_cfg.network.0 != network {
error!(
"Federation {federation_id} runs on {} but this gateway supports {network}",
ln_cfg.network,
ln_cfg.network.0,
);
return Err(AdminGatewayError::ClientCreationError(anyhow!(format!(
"Unsupported network {}",
ln_cfg.network
ln_cfg.network.0
))));
}

Expand Down
4 changes: 2 additions & 2 deletions modules/fedimint-lnv2-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,10 @@ impl LightningClientModule {
return Err(SendPaymentError::InvoiceExpired);
}

if self.cfg.network != invoice.currency().into() {
if self.cfg.network.0 != invoice.currency().into() {
return Err(SendPaymentError::WrongCurrency {
invoice_currency: invoice.currency(),
federation_currency: self.cfg.network.into(),
federation_currency: self.cfg.network.0.into(),
});
}

Expand Down
7 changes: 4 additions & 3 deletions modules/fedimint-lnv2-common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::BTreeMap;

pub use bitcoin::Network;
use fedimint_core::core::ModuleKind;
use fedimint_core::encoding::btc::NetworkSaneEncodingWrapper;
use fedimint_core::encoding::{Decodable, Encodable};
use fedimint_core::envs::BitcoinRpcConfig;
use fedimint_core::{plugin_types_trait_impl_config, Amount, PeerId};
Expand Down Expand Up @@ -58,7 +59,7 @@ pub struct LightningConfigConsensus {
pub tpe_agg_pk: AggregatePublicKey,
pub tpe_pks: BTreeMap<PeerId, PublicKeyShare>,
pub fee_consensus: FeeConsensus,
pub network: Network,
pub network: NetworkSaneEncodingWrapper,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -71,7 +72,7 @@ pub struct LightningClientConfig {
pub tpe_agg_pk: AggregatePublicKey,
pub tpe_pks: BTreeMap<PeerId, PublicKeyShare>,
pub fee_consensus: FeeConsensus,
pub network: Network,
pub network: NetworkSaneEncodingWrapper,
}

impl std::fmt::Display for LightningClientConfig {
Expand Down Expand Up @@ -188,7 +189,7 @@ fn migrate_config_consensus(
})
.collect(),
fee_consensus: FeeConsensus::new(1000).expect("Relative fee is within range"),
network: config.network,
network: NetworkSaneEncodingWrapper(config.network),
}
}

Expand Down
5 changes: 3 additions & 2 deletions modules/fedimint-lnv2-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use fedimint_core::config::{
};
use fedimint_core::core::ModuleInstanceId;
use fedimint_core::db::{Database, DatabaseTransaction, IDatabaseTransactionOpsCoreTyped};
use fedimint_core::encoding::btc::NetworkSaneEncodingWrapper;
use fedimint_core::module::audit::Audit;
use fedimint_core::module::{
api_endpoint, ApiEndpoint, ApiVersion, CoreConsensusVersion, InputMeta, ModuleConsensusVersion,
Expand Down Expand Up @@ -205,7 +206,7 @@ impl ServerModuleInit for LightningInit {
tpe_agg_pk,
tpe_pks: tpe_pks.clone(),
fee_consensus: params.consensus.fee_consensus.clone(),
network: params.consensus.network,
network: NetworkSaneEncodingWrapper(params.consensus.network),
},
private: LightningConfigPrivate {
sk: sks[peer.to_usize()],
Expand Down Expand Up @@ -247,7 +248,7 @@ impl ServerModuleInit for LightningInit {
})
.collect(),
fee_consensus: params.consensus.fee_consensus.clone(),
network: params.consensus.network,
network: NetworkSaneEncodingWrapper(params.consensus.network),
},
private: LightningConfigPrivate {
sk: SecretKeyShare(sk),
Expand Down

0 comments on commit 911e672

Please sign in to comment.