Skip to content

Commit

Permalink
Merge pull request fedimint#6524 from elsirion/2024-12-specify-primar…
Browse files Browse the repository at this point in the history
…y-module-kind

feat: allow specifying primary module kind
dpc authored Dec 10, 2024
2 parents 37965cf + 95c21ec commit 894a25b
Showing 8 changed files with 74 additions and 23 deletions.
2 changes: 1 addition & 1 deletion fedimint-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -654,7 +654,7 @@ impl FedimintCli {
let db = cli.load_rocks_db().await?;
let mut client_builder = Client::builder(db).await.map_err_cli()?;
client_builder.with_module_inits(self.module_inits.clone());
client_builder.with_primary_module(1);
client_builder.with_primary_module_kind(fedimint_mint_client::KIND);

#[cfg(feature = "tor")]
if cli.use_tor {
52 changes: 51 additions & 1 deletion fedimint-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -2300,6 +2300,7 @@ pub struct AdminCreds {
pub struct ClientBuilder {
module_inits: ClientModuleInitRegistry,
primary_module_instance: Option<ModuleInstanceId>,
primary_module_kind: Option<ModuleKind>,
admin_creds: Option<AdminCreds>,
db_no_decoders: Database,
meta_service: Arc<MetaService>,
@@ -2316,6 +2317,7 @@ impl ClientBuilder {
ClientBuilder {
module_inits: ModuleInitRegistry::new(),
primary_module_instance: None,
primary_module_kind: None,
connector: Connector::default(),
admin_creds: None,
db_no_decoders: db,
@@ -2329,6 +2331,7 @@ impl ClientBuilder {
ClientBuilder {
module_inits: client.module_inits.clone(),
primary_module_instance: Some(client.primary_module_instance),
primary_module_kind: None,
admin_creds: None,
db_no_decoders: client.db.with_decoders(ModuleRegistry::default()),
stopped: false,
@@ -2358,7 +2361,28 @@ impl ClientBuilder {
///
/// ## Panics
/// If there was a primary module specified previously
#[deprecated(
since = "0.6.0",
note = "Use `with_primary_module_kind` instead, as the instance id can't be known upfront. If you *really* need the old behavior you can use `with_primary_module_instance_id`."
)]
pub fn with_primary_module(&mut self, primary_module_instance: ModuleInstanceId) {
self.with_primary_module_instance_id(primary_module_instance);
}

/// **You are likely looking for
/// [`ClientBuilder::with_primary_module_kind`]. This function is rarely
/// useful and often dangerous, handle with care.**
///
/// Uses this module with the given instance id as the primary module. See
/// [`ClientModule::supports_being_primary`] for more information. Since the
/// module instance id of modules of a specific kind may differ between
/// different federations it is generally not recommended to specify it, but
/// rather to specify the module kind that should be used as primary. See
/// [`ClientBuilder::with_primary_module_kind`].
///
/// ## Panics
/// If there was a primary module specified previously
pub fn with_primary_module_instance_id(&mut self, primary_module_instance: ModuleInstanceId) {
let was_replaced = self
.primary_module_instance
.replace(primary_module_instance)
@@ -2369,6 +2393,22 @@ impl ClientBuilder {
);
}

/// Uses this module kind as the primary module if present in the config.
/// See [`ClientModule::supports_being_primary`] for more information.
///
/// ## Panics
/// If there was a primary module kind specified previously
pub fn with_primary_module_kind(&mut self, primary_module_kind: ModuleKind) {
let was_replaced = self
.primary_module_kind
.replace(primary_module_kind)
.is_some();
assert!(
!was_replaced,
"Only one primary module kind can be given to the builder."
);
}

pub fn with_meta_service(&mut self, meta_service: Arc<MetaService>) {
self.meta_service = meta_service;
}
@@ -2726,7 +2766,17 @@ impl ClientBuilder {

let primary_module_instance = self
.primary_module_instance
.ok_or(anyhow!("No primary module instance id was provided"))?;
.or_else(|| {
let primary_module_kind = self.primary_module_kind?;
config
.modules
.iter()
.find_map(|(module_instance_id, module_config)| {
(module_config.kind() == &primary_module_kind)
.then_some(*module_instance_id)
})
})
.ok_or(anyhow!("No primary module set or found"))?;

let notifier = Notifier::new(db.clone());

2 changes: 1 addition & 1 deletion fedimint-load-test-tool/src/common.rs
Original file line number Diff line number Diff line change
@@ -153,7 +153,7 @@ pub async fn build_client(
client_builder.with_module(MintClientInit);
client_builder.with_module(LightningClientInit::default());
client_builder.with_module(WalletClientInit::default());
client_builder.with_primary_module(1);
client_builder.with_primary_module_kind(fedimint_mint_client::KIND);
let client_secret =
Client::load_or_generate_client_secret(client_builder.db_no_decoders()).await?;
let root_secret = PlainRootSecretStrategy::to_root_secret(&client_secret);
17 changes: 9 additions & 8 deletions fedimint-testing/src/federation.rs
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ use fedimint_core::config::{
ClientConfig, FederationId, ServerModuleConfigGenParamsRegistry, ServerModuleInitRegistry,
META_FEDERATION_NAME_KEY,
};
use fedimint_core::core::ModuleInstanceId;
use fedimint_core::core::ModuleKind;
use fedimint_core::db::mem_impl::MemDatabase;
use fedimint_core::db::Database;
use fedimint_core::endpoint_constants::SESSION_COUNT_ENDPOINT;
@@ -37,7 +37,7 @@ pub struct FederationTest {
configs: BTreeMap<PeerId, ServerConfig>,
server_init: ServerModuleInitRegistry,
client_init: ClientModuleInitRegistry,
primary_client: ModuleInstanceId,
primary_module_kind: ModuleKind,
_task: TaskGroup,
}

@@ -97,7 +97,7 @@ impl FederationTest {
info!(target: LOG_TEST, "Setting new client with config");
let mut client_builder = Client::builder(db).await.expect("Failed to build client");
client_builder.with_module_inits(self.client_init.clone());
client_builder.with_primary_module(self.primary_client);
client_builder.with_primary_module_kind(self.primary_module_kind.clone());
if let Some(admin_creds) = admin_creds {
client_builder.set_admin_creds(admin_creds);
}
@@ -149,7 +149,7 @@ pub struct FederationTestBuilder {
num_peers: u16,
num_offline: u16,
base_port: u16,
primary_client: ModuleInstanceId,
primary_module_kind: ModuleKind,
version_hash: String,
params: ServerModuleConfigGenParamsRegistry,
server_init: ServerModuleInitRegistry,
@@ -161,14 +161,15 @@ impl FederationTestBuilder {
params: ServerModuleConfigGenParamsRegistry,
server_init: ServerModuleInitRegistry,
client_init: ClientModuleInitRegistry,
primary_module_kind: ModuleKind,
) -> FederationTestBuilder {
let num_peers = 4;
Self {
num_peers,
num_offline: 1,
base_port: block_in_place(|| fedimint_portalloc::port_alloc(num_peers * 2))
.expect("Failed to allocate a port range"),
primary_client: 0,
primary_module_kind,
version_hash: "fedimint-testing-dummy-version-hash".to_owned(),
params,
server_init,
@@ -191,8 +192,8 @@ impl FederationTestBuilder {
self
}

pub fn primary_client(mut self, primary_client: ModuleInstanceId) -> FederationTestBuilder {
self.primary_client = primary_client;
pub fn primary_module_kind(mut self, primary_module_kind: ModuleKind) -> FederationTestBuilder {
self.primary_module_kind = primary_module_kind;
self
}

@@ -277,7 +278,7 @@ impl FederationTestBuilder {
configs,
server_init: self.server_init,
client_init: self.client_init,
primary_client: self.primary_client,
primary_module_kind: self.primary_module_kind,
_task: task_group,
}
}
5 changes: 4 additions & 1 deletion fedimint-testing/src/fixtures.rs
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@ pub struct Fixtures {
bitcoin_rpc: BitcoinRpcConfig,
bitcoin: Arc<dyn BitcoinTest>,
dyn_bitcoin_rpc: DynBitcoindRpc,
primary_module_kind: ModuleKind,
id: ModuleInstanceId,
}

@@ -101,6 +102,7 @@ impl Fixtures {
bitcoin_rpc: config,
bitcoin,
dyn_bitcoin_rpc,
primary_module_kind: IClientModuleInit::module_kind(&client),
id: 0,
}
.with_module(client, server, params)
@@ -152,6 +154,7 @@ impl Fixtures {
self.params.clone(),
ServerModuleInitRegistry::from(self.servers.clone()),
ClientModuleInitRegistry::from(self.clients.clone()),
self.primary_module_kind.clone(),
)
}

@@ -178,7 +181,7 @@ impl Fixtures {

// Create federation client builder for the gateway
let client_builder: GatewayClientBuilder =
GatewayClientBuilder::new(path.clone(), registry, 0);
GatewayClientBuilder::new(path.clone(), registry, ModuleKind::from_static_str("dummy"));

let lightning_builder: Arc<dyn LightningBuilder + Send + Sync> =
Arc::new(FakeLightningBuilder);
2 changes: 1 addition & 1 deletion fedimint-wasm-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ async fn make_client_builder() -> Result<fedimint_client::ClientBuilder> {
builder.with_module(LightningClientInit::default());
builder.with_module(MintClientInit);
builder.with_module(WalletClientInit::default());
builder.with_primary_module(1);
builder.with_primary_module_kind(fedimint_mint_client::KIND);

Ok(builder)
}
10 changes: 5 additions & 5 deletions gateway/ln-gateway/src/client.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ use fedimint_client::module::init::ClientModuleInitRegistry;
use fedimint_client::secret::{PlainRootSecretStrategy, RootSecretStrategy};
use fedimint_client::{Client, ClientBuilder};
use fedimint_core::config::FederationId;
use fedimint_core::core::ModuleInstanceId;
use fedimint_core::core::ModuleKind;
use fedimint_core::db::{Database, IDatabaseTransactionOpsCoreTyped};
use fedimint_core::encoding::Encodable;
use fedimint_core::module::registry::ModuleDecoderRegistry;
@@ -25,19 +25,19 @@ use crate::{AdminResult, Gateway};
pub struct GatewayClientBuilder {
work_dir: PathBuf,
registry: ClientModuleInitRegistry,
primary_module: ModuleInstanceId,
primary_module_kind: ModuleKind,
}

impl GatewayClientBuilder {
pub fn new(
work_dir: PathBuf,
registry: ClientModuleInitRegistry,
primary_module: ModuleInstanceId,
primary_module_kind: ModuleKind,
) -> Self {
Self {
work_dir,
registry,
primary_module,
primary_module_kind,
}
}

@@ -85,7 +85,7 @@ impl GatewayClientBuilder {
.await
.map_err(AdminGatewayError::ClientCreationError)?;
client_builder.with_module_inits(registry);
client_builder.with_primary_module(self.primary_module);
client_builder.with_primary_module_kind(self.primary_module_kind.clone());
client_builder.with_connector(connector);
Ok(client_builder)
}
7 changes: 2 additions & 5 deletions gateway/ln-gateway/src/lib.rs
Original file line number Diff line number Diff line change
@@ -306,11 +306,8 @@ impl Gateway {
decoders,
);

let client_builder = GatewayClientBuilder::new(
opts.data_dir.clone(),
registry,
LEGACY_HARDCODED_INSTANCE_ID_MINT,
);
let client_builder =
GatewayClientBuilder::new(opts.data_dir.clone(), registry, fedimint_mint_client::KIND);

info!(
"Starting gatewayd (version: {})",

0 comments on commit 894a25b

Please sign in to comment.