From 1617f7a2518cc1ede660f6ab11ca3e2341fea5bd Mon Sep 17 00:00:00 2001 From: hrxi Date: Wed, 27 Nov 2024 01:08:41 +0100 Subject: [PATCH] Use the WASM implementation of the remote data store everywhere It has no WASM dependencies and is not broken unlike the thing it replaces. --- consensus/src/consensus/remote_data_store.rs | 105 ++----------------- 1 file changed, 8 insertions(+), 97 deletions(-) diff --git a/consensus/src/consensus/remote_data_store.rs b/consensus/src/consensus/remote_data_store.rs index 5efecc7099..13383f1783 100644 --- a/consensus/src/consensus/remote_data_store.rs +++ b/consensus/src/consensus/remote_data_store.rs @@ -4,7 +4,7 @@ use std::{ }; use nimiq_account::{ - Account, DataStoreReadOps, Staker, StakingContract, StakingContractStore, Tombstone, Validator, + Staker, StakingContractStore, Tombstone, Validator, }; use nimiq_blockchain_interface::AbstractBlockchain; use nimiq_blockchain_proxy::BlockchainProxy; @@ -36,8 +36,7 @@ pub(crate) struct RemoteDataStore { pub(crate) min_peers: usize, } -/// Internal Remote operations the Remote Data Store can perform over addresses on -/// wasm +/// Internal Remote operations the Remote Data Store can perform over addresses enum RemoteDataStoreOps { /// Gets a set of validators by their addresses Validator(Vec
), @@ -132,53 +131,6 @@ impl RemoteDataStore { )) } - async fn get_staking_contract(&self) -> Result { - let key = KeyNibbles::from(&Policy::STAKING_CONTRACT_ADDRESS); - let accounts = Self::get_trie( - Arc::clone(&self.network), - self.blockchain.clone(), - &[key.clone()], - self.min_peers, - ) - .await?; - - if accounts.len() != 1 { - log::error!( - len = accounts.len(), - "Expected only one account for the staking contract" - ); - return Err(RequestError::OutboundRequest( - OutboundRequestError::SendError, - )); - } - - if let Some(account) = accounts.get(&key) { - if let Some(account) = account { - match account { - Account::Staking(account) => Ok(account.clone()), - _ => { - log::error!( - "Requested a staking contract account and received another account type" - ); - Err(RequestError::OutboundRequest( - OutboundRequestError::SendError, - )) - } - } - } else { - log::error!("No proof was provided for the staking contract"); - Err(RequestError::OutboundRequest( - OutboundRequestError::SendError, - )) - } - } else { - log::error!("Returned accounts do not include staking contract"); - Err(RequestError::OutboundRequest( - OutboundRequestError::SendError, - )) - } - } - /// Gets a set of validators given their addresses. The returned type is a /// BTreeMap of addresses to an optional `Validator`. If a validator was not /// found, then `None` is returned in its corresponding entry. @@ -186,20 +138,8 @@ impl RemoteDataStore { &self, addresses: Vec
, ) -> Result>, RequestError> { - if cfg!(target_family = "wasm") { - self.wasm_exec(RemoteDataStoreOps::Validator(addresses)) - .await - } else { - let staking_contract = self.get_staking_contract().await?; - let mut validators = BTreeMap::new(); - for address in addresses { - validators.insert( - address.clone(), - staking_contract.get_validator(self, &address), - ); - } - Ok(validators) - } + self.exec(RemoteDataStoreOps::Validator(addresses)) + .await } /// Gets a set of stakers given their addresses. The returned type is a @@ -209,16 +149,7 @@ impl RemoteDataStore { &self, addresses: Vec
, ) -> Result>, RequestError> { - if cfg!(target_family = "wasm") { - self.wasm_exec(RemoteDataStoreOps::Staker(addresses)).await - } else { - let staking_contract = self.get_staking_contract().await?; - let mut stakers = BTreeMap::new(); - for address in addresses { - stakers.insert(address.clone(), staking_contract.get_staker(self, &address)); - } - Ok(stakers) - } + self.exec(RemoteDataStoreOps::Staker(addresses)).await } /// Gets a set of tombstones given their addresses. The returned type is a @@ -229,23 +160,11 @@ impl RemoteDataStore { &self, addresses: Vec
, ) -> Result>, RequestError> { - if cfg!(target_family = "wasm") { - self.wasm_exec(RemoteDataStoreOps::Tombstone(addresses)) - .await - } else { - let staking_contract = self.get_staking_contract().await?; - let mut tombstones = BTreeMap::new(); - for address in addresses { - tombstones.insert( - address.clone(), - staking_contract.get_tombstone(self, &address), - ); - } - Ok(tombstones) - } + self.exec(RemoteDataStoreOps::Tombstone(addresses)) + .await } - async fn wasm_exec( + async fn exec( &self, op: RemoteDataStoreOps, ) -> Result>, RequestError> { @@ -300,11 +219,3 @@ impl RemoteDataStore { .collect()) } } - -impl DataStoreReadOps for RemoteDataStore { - fn get(&self, key: &KeyNibbles) -> Option { - unimplemented!( - "this function is not implementable: a sync function cannot call an async one", - ); - } -}