Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#223 - Remove the generic block identifier #222

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions chains/astar/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
9 changes: 2 additions & 7 deletions chains/ethereum/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
107 changes: 28 additions & 79 deletions rosetta-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -115,27 +118,6 @@ pub enum GenericCallResult {
Polkadot(Value),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GenericAtBlock {
Ethereum(<EthereumClient as BlockchainClient>::AtBlock),
Polkadot(<PolkadotClient as BlockchainClient>::AtBlock),
}

impl From<GenericBlockIdentifier> 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(<EthereumClient as BlockchainClient>::BlockIdentifier),
Polkadot(<PolkadotClient as BlockchainClient>::BlockIdentifier),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GenericTransaction {
Ethereum(<EthereumClient as BlockchainClient>::Transaction),
Expand All @@ -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;
Expand All @@ -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<Self::BlockIdentifier> {
// 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<Self::BlockIdentifier> {
// 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<u128> {
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,
}
}

Expand Down Expand Up @@ -349,35 +310,23 @@ pub enum GenericClientStream<'a> {
}

impl<'a> Stream for GenericClientStream<'a> {
type Item = ClientEvent<GenericBlockIdentifier, GenericClientEvent>;
type Item = ClientEvent<BlockIdentifier, GenericClientEvent>;

fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Option<Self::Item>> {
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))),
}
}
}
29 changes: 7 additions & 22 deletions rosetta-client/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions rosetta-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ pub struct PartialBlockIdentifier {
pub hash: Option<[u8; 32]>,
}

impl From<u64> 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<BlockIdentifier> for PartialBlockIdentifier {
fn from(block_identifier: BlockIdentifier) -> Self {
Self { index: Some(block_identifier.index), hash: Some(block_identifier.hash) }
Expand Down
Loading