From 260e62d51bf994afda56e6978e72a55497be518c Mon Sep 17 00:00:00 2001 From: silverpill Date: Tue, 30 Aug 2022 16:13:58 +0000 Subject: [PATCH] Allow blockchain configuration to be defined using a list Multi-chain configurations are still not allowed. --- config.yaml.example | 33 ++++++++++++------------- contrib/mitra_config.yaml | 22 ++++++++--------- src/cli.rs | 2 +- src/config/main.rs | 21 +++++++++++++--- src/main.rs | 7 ++++-- src/mastodon_api/accounts/views.rs | 2 +- src/mastodon_api/instance/types.rs | 2 +- src/mastodon_api/statuses/views.rs | 2 +- src/mastodon_api/subscriptions/views.rs | 4 +-- 9 files changed, 56 insertions(+), 39 deletions(-) diff --git a/config.yaml.example b/config.yaml.example index f3ac9fa..f05b3f4 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -10,24 +10,23 @@ instance_short_description: My instance instance_description: My instance registrations_open: true -blockchain: +blockchains: # Parameters for hardhat local node - chain_id: eip155:31337 - chain_info: null - contract_address: '0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9' - contract_dir: contracts - api_url: 'http://127.0.0.1:8546' - explorer_url: null - signing_key: 'ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80' - chain_sync_step: 100 - chain_reorg_max_depth: 0 - -#blockchain: -# chain_id: monero:regtest -# daemon_url: 'http://127.0.0.1:58081' -# wallet_url: 'http://127.0.0.1:58083' -# wallet_name: test -# wallet_password: test + - chain_id: eip155:31337 + chain_info: null + contract_address: '0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9' + contract_dir: contracts + api_url: 'http://127.0.0.1:8546' + explorer_url: null + signing_key: 'ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80' + chain_sync_step: 100 + chain_reorg_max_depth: 0 +# # Parameters for local Monero node +# - chain_id: monero:regtest +# daemon_url: 'http://127.0.0.1:58081' +# wallet_url: 'http://127.0.0.1:58083' +# wallet_name: test +# wallet_password: test ipfs_api_url: 'http://127.0.0.1:5001' ipfs_gateway_url: 'http://127.0.0.1:8001' diff --git a/contrib/mitra_config.yaml b/contrib/mitra_config.yaml index c9d7e91..9f3c314 100644 --- a/contrib/mitra_config.yaml +++ b/contrib/mitra_config.yaml @@ -26,18 +26,18 @@ registrations_open: false # List of blocked domains #blocked_instances: [] -# Blockchain integration +# Blockchain integrations # Signing key for ethereum integration can be generated with `mitractl generate-ethereum-address` -#blockchain: -# chain_id: eip155:31337 -# chain_info: null -# contract_address: '0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9' -# contract_dir: /usr/share/mitra/contracts -# api_url: 'http://127.0.0.1:8545' -# explorer_url: null -# signing_key: null -# chain_sync_step: 1000 -# chain_reorg_max_depth: 10 +#blockchains: +# - chain_id: eip155:31337 +# chain_info: null +# contract_address: '0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9' +# contract_dir: /usr/share/mitra/contracts +# api_url: 'http://127.0.0.1:8545' +# explorer_url: null +# signing_key: null +# chain_sync_step: 1000 +# chain_reorg_max_depth: 10 # IPFS integration #ipfs_api_url: 'http://127.0.0.1:5001' diff --git a/src/cli.rs b/src/cli.rs index ec85fb4..bc52d18 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -301,7 +301,7 @@ impl CreateMoneroWallet { &self, config: &Config, ) -> Result<(), Error> { - let monero_config = config.blockchain.as_ref() + let monero_config = config.blockchain() .and_then(|conf| conf.monero_config()) .ok_or(anyhow!("monero configuration not found"))?; create_monero_wallet(monero_config).await?; diff --git a/src/config/main.rs b/src/config/main.rs index 7bc597b..5ec7a8f 100644 --- a/src/config/main.rs +++ b/src/config/main.rs @@ -100,8 +100,11 @@ pub struct Config { #[serde(default)] pub blocked_instances: Vec, - // Blockchain integration - pub blockchain: Option, + // Blockchain integrations + #[serde(rename = "blockchain")] + pub _blockchain: Option, // deprecated + #[serde(default)] + blockchains: Vec, // IPFS pub ipfs_api_url: Option, @@ -137,6 +140,18 @@ impl Config { pub fn media_dir(&self) -> PathBuf { self.storage_dir.join("media") } + + pub fn blockchain(&self) -> Option<&BlockchainConfig> { + if let Some(ref blockchain_config) = self._blockchain { + Some(blockchain_config) + } else { + match &self.blockchains[..] { + [blockchain_config] => Some(blockchain_config), + [] => None, + _ => panic!("multichain deployments are not supported"), + } + } + } } #[derive(Clone)] @@ -237,7 +252,7 @@ pub fn parse_config() -> Config { }; check_directory_owner(&config.storage_dir); config.try_instance_url().expect("invalid instance URI"); - if let Some(blockchain_config) = config.blockchain.as_ref() { + if let Some(blockchain_config) = config.blockchain() { if let Some(ethereum_config) = blockchain_config.ethereum_config() { ethereum_config.try_ethereum_chain_id().unwrap(); if !ethereum_config.contract_dir.exists() { diff --git a/src/main.rs b/src/main.rs index bc36be4..a525873 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,7 +51,10 @@ async fn main() -> std::io::Result<()> { config.environment, ); - let maybe_blockchain = if let Some(blockchain_config) = &config.blockchain { + if config._blockchain.is_some() { + log::warn!("'blockchain' property is deprecated, use 'blockchains' instead"); + }; + let maybe_blockchain = if let Some(blockchain_config) = config.blockchain() { if let Some(ethereum_config) = blockchain_config.ethereum_config() { // Create blockchain interface get_contracts(ethereum_config, &config.storage_dir).await @@ -147,7 +150,7 @@ async fn main() -> std::io::Result<()> { .service(atom::get_atom_feed) .service(nodeinfo::get_nodeinfo) .service(nodeinfo::get_nodeinfo_2_0); - if let Some(blockchain_config) = &config.blockchain { + if let Some(blockchain_config) = config.blockchain() { if let Some(ethereum_config) = blockchain_config.ethereum_config() { // Serve artifacts if available app = app.service(actix_files::Files::new( diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index 39d9058..a4979ce 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -134,7 +134,7 @@ pub async fn create_account( return Err(ValidationError("not allowed to sign up").into()); }; } else { - assert!(config.blockchain.is_none()); + assert!(config.blockchain().is_none()); }; // Generate RSA private key for actor diff --git a/src/mastodon_api/instance/types.rs b/src/mastodon_api/instance/types.rs index c938758..2121ee8 100644 --- a/src/mastodon_api/instance/types.rs +++ b/src/mastodon_api/instance/types.rs @@ -41,7 +41,7 @@ fn get_full_api_version(version: &str) -> String { impl InstanceInfo { pub fn create(config: &Config, maybe_blockchain: Option<&ContractSet>) -> Self { - let ethereum_config = config.blockchain.as_ref() + let ethereum_config = config.blockchain() .and_then(|conf| conf.ethereum_config()); let blockchain_features = maybe_blockchain.map(|contract_set| { BlockchainFeatures { diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index 1940088..7f7e6d5 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -426,7 +426,7 @@ async fn get_signature( ) -> Result { let db_client = &**get_database_client(&db_pool).await?; let current_user = get_current_user(db_client, auth.token()).await?; - let ethereum_config = config.blockchain.as_ref() + let ethereum_config = config.blockchain() .ok_or(HttpError::NotSupported)? .ethereum_config() .ok_or(HttpError::NotSupported)?; diff --git a/src/mastodon_api/subscriptions/views.rs b/src/mastodon_api/subscriptions/views.rs index 49314a6..052e51c 100644 --- a/src/mastodon_api/subscriptions/views.rs +++ b/src/mastodon_api/subscriptions/views.rs @@ -32,7 +32,7 @@ pub async fn authorize_subscription( ) -> Result { let db_client = &**get_database_client(&db_pool).await?; let current_user = get_current_user(db_client, auth.token()).await?; - let ethereum_config = config.blockchain.as_ref() + let ethereum_config = config.blockchain() .ok_or(HttpError::NotSupported)? .ethereum_config() .ok_or(HttpError::NotSupported)?; @@ -63,7 +63,7 @@ pub async fn subscriptions_enabled( let mut maybe_payment_option = None; match subscription_settings.into_inner() { SubscriptionSettings::Ethereum => { - let ethereum_config = config.blockchain.as_ref() + let ethereum_config = config.blockchain() .and_then(|conf| conf.ethereum_config()) .ok_or(HttpError::NotSupported)?; let contract_set = maybe_blockchain.as_ref().as_ref()