diff --git a/chains/astar/server/src/lib.rs b/chains/astar/server/src/lib.rs index d2598411..33beafe0 100644 --- a/chains/astar/server/src/lib.rs +++ b/chains/astar/server/src/lib.rs @@ -461,7 +461,6 @@ mod tests { #[tokio::test] async fn test_subscription() -> Result<()> { use futures_util::StreamExt; - use rosetta_client::client::GenericBlockIdentifier; use rosetta_core::{BlockOrIdentifier, ClientEvent}; let config = rosetta_config_astar::config("dev").unwrap(); let env = Env::new("astar-subscription", config.clone(), client_from_config) @@ -477,17 +476,13 @@ mod tests { for _ in 0..10 { let event = stream.next().await.unwrap(); match event { - ClientEvent::NewHead(BlockOrIdentifier::Identifier( - GenericBlockIdentifier::Ethereum(head), - )) => { + ClientEvent::NewHead(BlockOrIdentifier::Identifier(head)) => { if let Some(block_number) = last_head { assert!(head.index > block_number); } last_head = Some(head.index); }, - ClientEvent::NewFinalized(BlockOrIdentifier::Identifier( - GenericBlockIdentifier::Ethereum(finalized), - )) => { + ClientEvent::NewFinalized(BlockOrIdentifier::Identifier(finalized)) => { if let Some(block_number) = last_finalized { assert!(finalized.index > block_number); } diff --git a/chains/ethereum/server/src/lib.rs b/chains/ethereum/server/src/lib.rs index efe6b839..047efadb 100644 --- a/chains/ethereum/server/src/lib.rs +++ b/chains/ethereum/server/src/lib.rs @@ -387,7 +387,6 @@ mod tests { #[tokio::test] async fn test_subscription() -> Result<()> { use futures_util::StreamExt; - use rosetta_client::client::GenericBlockIdentifier; use rosetta_core::{BlockOrIdentifier, ClientEvent}; let config = rosetta_config_ethereum::config("dev").unwrap(); let env = Env::new("ethereum-subscription", config.clone(), client_from_config) @@ -403,17 +402,13 @@ mod tests { for _ in 0..10 { let event = stream.next().await.unwrap(); match event { - ClientEvent::NewHead(BlockOrIdentifier::Identifier( - GenericBlockIdentifier::Ethereum(head), - )) => { + ClientEvent::NewHead(BlockOrIdentifier::Identifier(head)) => { if let Some(block_number) = last_head { assert!(head.index > block_number); } last_head = Some(head.index); }, - ClientEvent::NewFinalized(BlockOrIdentifier::Identifier( - GenericBlockIdentifier::Ethereum(finalized), - )) => { + ClientEvent::NewFinalized(BlockOrIdentifier::Identifier(finalized)) => { if let Some(block_number) = last_finalized { assert!(finalized.index > block_number); } diff --git a/rosetta-client/src/client.rs b/rosetta-client/src/client.rs index a7711e49..a0c6ff59 100644 --- a/rosetta-client/src/client.rs +++ b/rosetta-client/src/client.rs @@ -8,7 +8,10 @@ use anyhow::Result; use derive_more::From; use futures::Stream; use futures_util::StreamExt; -use rosetta_core::{BlockchainClient, ClientEvent}; +use rosetta_core::{ + types::{BlockIdentifier, PartialBlockIdentifier}, + BlockchainClient, ClientEvent, +}; use rosetta_server_astar::{AstarClient, AstarMetadata, AstarMetadataParams}; use rosetta_server_ethereum::{ config::{Query as EthQuery, QueryResult as EthQueryResult}, @@ -115,27 +118,6 @@ pub enum GenericCallResult { Polkadot(Value), } -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum GenericAtBlock { - Ethereum(::AtBlock), - Polkadot(::AtBlock), -} - -impl From for GenericAtBlock { - fn from(block: GenericBlockIdentifier) -> Self { - match block { - GenericBlockIdentifier::Ethereum(block) => Self::Ethereum(block.into()), - GenericBlockIdentifier::Polkadot(block) => Self::Polkadot(block.into()), - } - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum GenericBlockIdentifier { - Ethereum(::BlockIdentifier), - Polkadot(::BlockIdentifier), -} - #[derive(Clone, Debug, PartialEq, Eq)] pub enum GenericTransaction { Ethereum(::Transaction), @@ -160,8 +142,8 @@ impl BlockchainClient for GenericClient { type Call = GenericCall; type CallResult = GenericCallResult; - type AtBlock = GenericAtBlock; - type BlockIdentifier = GenericBlockIdentifier; + type AtBlock = PartialBlockIdentifier; + type BlockIdentifier = BlockIdentifier; type Query = (); type Transaction = GenericTransaction; @@ -182,56 +164,35 @@ impl BlockchainClient for GenericClient { fn genesis_block(&self) -> Self::BlockIdentifier { // dispatch!(self.genesis_block()) match self { - Self::Ethereum(client) => GenericBlockIdentifier::Ethereum(client.genesis_block()), - Self::Astar(client) => GenericBlockIdentifier::Ethereum(client.genesis_block()), - Self::Polkadot(client) => GenericBlockIdentifier::Polkadot(client.genesis_block()), + Self::Ethereum(client) => client.genesis_block(), + Self::Astar(client) => client.genesis_block(), + Self::Polkadot(client) => client.genesis_block(), } } async fn current_block(&self) -> Result { // dispatch!(self.current_block().await) match self { - Self::Ethereum(client) => { - client.current_block().await.map(GenericBlockIdentifier::Ethereum) - }, - Self::Astar(client) => { - client.current_block().await.map(GenericBlockIdentifier::Ethereum) - }, - Self::Polkadot(client) => { - client.current_block().await.map(GenericBlockIdentifier::Polkadot) - }, + Self::Ethereum(client) => client.current_block().await, + Self::Astar(client) => client.current_block().await, + Self::Polkadot(client) => client.current_block().await, } } async fn finalized_block(&self) -> Result { // dispatch!(self.finalized_block().await) match self { - Self::Ethereum(client) => { - client.finalized_block().await.map(GenericBlockIdentifier::Ethereum) - }, - Self::Astar(client) => { - client.finalized_block().await.map(GenericBlockIdentifier::Ethereum) - }, - Self::Polkadot(client) => { - client.finalized_block().await.map(GenericBlockIdentifier::Polkadot) - }, + Self::Ethereum(client) => client.finalized_block().await, + Self::Astar(client) => client.finalized_block().await, + Self::Polkadot(client) => client.finalized_block().await, } } async fn balance(&self, address: &Address, block: &Self::AtBlock) -> Result { match self { - Self::Ethereum(client) => match block { - GenericAtBlock::Ethereum(at_block) => client.balance(address, at_block).await, - GenericAtBlock::Polkadot(_) => anyhow::bail!("invalid block identifier"), - }, - Self::Astar(client) => match block { - GenericAtBlock::Ethereum(at_block) => client.balance(address, at_block).await, - GenericAtBlock::Polkadot(_) => anyhow::bail!("invalid block identifier"), - }, - Self::Polkadot(client) => match block { - GenericAtBlock::Polkadot(at_block) => client.balance(address, at_block).await, - GenericAtBlock::Ethereum(_) => anyhow::bail!("invalid block identifier"), - }, + Self::Ethereum(client) => client.balance(address, block).await, + Self::Astar(client) => client.balance(address, block).await, + Self::Polkadot(client) => client.balance(address, block).await, } } @@ -349,7 +310,7 @@ pub enum GenericClientStream<'a> { } impl<'a> Stream for GenericClientStream<'a> { - type Item = ClientEvent; + type Item = ClientEvent; fn poll_next( mut self: Pin<&mut Self>, @@ -357,27 +318,15 @@ impl<'a> Stream for GenericClientStream<'a> { ) -> Poll> { let this = &mut *self; match this { - Self::Ethereum(stream) => stream.poll_next_unpin(cx).map(|opt| { - opt.map(|event| { - event - .map_block_identifier(GenericBlockIdentifier::Ethereum) - .map_event(GenericClientEvent::Ethereum) - }) - }), - Self::Astar(stream) => stream.poll_next_unpin(cx).map(|opt| { - opt.map(|event| { - event - .map_block_identifier(GenericBlockIdentifier::Ethereum) - .map_event(GenericClientEvent::Astar) - }) - }), - Self::Polkadot(stream) => stream.poll_next_unpin(cx).map(|opt| { - opt.map(|event| { - event - .map_block_identifier(GenericBlockIdentifier::Polkadot) - .map_event(GenericClientEvent::Polkadot) - }) - }), + Self::Ethereum(stream) => stream + .poll_next_unpin(cx) + .map(|opt| opt.map(|event| event.map_event(GenericClientEvent::Ethereum))), + Self::Astar(stream) => stream + .poll_next_unpin(cx) + .map(|opt| opt.map(|event| event.map_event(GenericClientEvent::Astar))), + Self::Polkadot(stream) => stream + .poll_next_unpin(cx) + .map(|opt| opt.map(|event| event.map_event(GenericClientEvent::Polkadot))), } } } diff --git a/rosetta-client/src/wallet.rs b/rosetta-client/src/wallet.rs index 06943118..e494f1ed 100644 --- a/rosetta-client/src/wallet.rs +++ b/rosetta-client/src/wallet.rs @@ -1,5 +1,5 @@ use crate::{ - client::{GenericBlockIdentifier, GenericClient, GenericMetadata, GenericMetadataParams}, + client::{GenericClient, GenericMetadata, GenericMetadataParams}, crypto::{address::Address, bip32::DerivedSecretKey, bip44::ChildNumber}, mnemonic::MnemonicStore, signer::{RosettaAccount, RosettaPublicKey, Signer}, @@ -113,29 +113,14 @@ impl Wallet { let address = Address::new(self.client.config().address_format, self.account.address.clone()); let balance = match &self.client { - GenericClient::Astar(client) => match block { - GenericBlockIdentifier::Ethereum(block) => { - client.balance(&address, &PartialBlockIdentifier::from(block)).await? - }, - GenericBlockIdentifier::Polkadot(_) => { - anyhow::bail!("[this is bug] client returned an invalid block identifier") - }, + GenericClient::Astar(client) => { + client.balance(&address, &PartialBlockIdentifier::from(block)).await? }, - GenericClient::Ethereum(client) => match block { - GenericBlockIdentifier::Ethereum(block) => { - client.balance(&address, &PartialBlockIdentifier::from(block)).await? - }, - GenericBlockIdentifier::Polkadot(_) => { - anyhow::bail!("[this is bug] client returned an invalid block identifier") - }, + GenericClient::Ethereum(client) => { + client.balance(&address, &PartialBlockIdentifier::from(block)).await? }, - GenericClient::Polkadot(client) => match block { - GenericBlockIdentifier::Polkadot(block) => { - client.balance(&address, &PartialBlockIdentifier::from(block)).await? - }, - GenericBlockIdentifier::Ethereum(_) => { - anyhow::bail!("[this is bug] client returned an invalid block identifier") - }, + GenericClient::Polkadot(client) => { + client.balance(&address, &PartialBlockIdentifier::from(block)).await? }, }; Ok(balance) diff --git a/rosetta-core/src/types.rs b/rosetta-core/src/types.rs index 7453d7ca..13334bd7 100644 --- a/rosetta-core/src/types.rs +++ b/rosetta-core/src/types.rs @@ -65,6 +65,24 @@ pub struct PartialBlockIdentifier { pub hash: Option<[u8; 32]>, } +impl From for PartialBlockIdentifier { + fn from(block_number: u64) -> Self { + Self { index: Some(block_number), hash: None } + } +} + +impl From<[u8; 32]> for PartialBlockIdentifier { + fn from(block_hash: [u8; 32]) -> Self { + Self { index: None, hash: Some(block_hash) } + } +} + +impl From<&[u8; 32]> for PartialBlockIdentifier { + fn from(block_hash: &[u8; 32]) -> Self { + Self { index: None, hash: Some(*block_hash) } + } +} + impl From for PartialBlockIdentifier { fn from(block_identifier: BlockIdentifier) -> Self { Self { index: Some(block_identifier.index), hash: Some(block_identifier.hash) }