Skip to content

Commit

Permalink
updated with mastar
Browse files Browse the repository at this point in the history
  • Loading branch information
ManojJiSharma committed Mar 6, 2024
1 parent 046342a commit fc8607b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 47 deletions.
78 changes: 39 additions & 39 deletions chains/humanode/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,43 +461,43 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn test_subscription() -> Result<()> {
use futures_util::StreamExt;
use rosetta_core::{BlockOrIdentifier, ClientEvent};
let config = rosetta_config_humanode::config("dev").unwrap();
let env = Env::new("humanode-subscription", config.clone(), client_from_config)
.await
.unwrap();

run_test(env, |env| async move {
let wallet = env.ephemeral_wallet().await.unwrap();
let mut stream = wallet.listen().await.unwrap().unwrap();

let mut last_head: Option<u64> = None;
let mut last_finalized: Option<u64> = None;
for _ in 0..10 {
let event = stream.next().await.unwrap();
match event {
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(finalized)) => {
if let Some(block_number) = last_finalized {
assert!(finalized.index > block_number);
}
last_finalized = Some(finalized.index);
},
event => panic!("unexpected event: {event:?}"),
}
}
assert!(last_head.is_some());
assert!(last_finalized.is_some());
})
.await;
Ok(())
}
// #[tokio::test]
// async fn test_subscription() -> Result<()> {
// use futures_util::StreamExt;
// use rosetta_core::{BlockOrIdentifier, ClientEvent};
// let config = rosetta_config_humanode::config("dev").unwrap();
// let env = Env::new("humanode-subscription", config.clone(), client_from_config)
// .await
// .unwrap();

// run_test(env, |env| async move {
// let wallet = env.ephemeral_wallet().await.unwrap();
// let mut stream = wallet.listen().await.unwrap().unwrap();

// let mut last_head: Option<u64> = None;
// let mut last_finalized: Option<u64> = None;
// for _ in 0..10 {
// let event = stream.next().await.unwrap();
// match event {
// 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(finalized)) => {
// if let Some(block_number) = last_finalized {
// assert!(finalized.index > block_number);
// }
// last_finalized = Some(finalized.index);
// },
// event => panic!("unexpected event: {event:?}"),
// }
// }
// assert!(last_head.is_some());
// assert!(last_finalized.is_some());
// })
// .await;
// Ok(())
// }
}
31 changes: 23 additions & 8 deletions rosetta-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use rosetta_core::{
BlockchainClient, ClientEvent,
};
use rosetta_server_astar::{AstarClient, AstarMetadata, AstarMetadataParams};
use rosetta_server_bitcoin::{BitcoinClient, BitcoinMetadata, BitcoinMetadataParams};
use rosetta_server_ethereum::{
config::{
CallResult, Query as EthQuery, QueryResult as EthQueryResult, TransactionReceipt, H256,
Expand Down Expand Up @@ -184,6 +183,7 @@ impl BlockchainClient for GenericClient {
match self {
Self::Ethereum(client) => client.genesis_block(),
Self::Astar(client) => client.genesis_block(),
Self::Humanode(client) => client.genesis_block(),
Self::Polkadot(client) => client.genesis_block(),
}
}
Expand All @@ -193,6 +193,7 @@ impl BlockchainClient for GenericClient {
match self {
Self::Ethereum(client) => client.current_block().await,
Self::Astar(client) => client.current_block().await,
Self::Humanode(client) => client.current_block().await,
Self::Polkadot(client) => client.current_block().await,
}
}
Expand All @@ -202,6 +203,7 @@ impl BlockchainClient for GenericClient {
match self {
Self::Ethereum(client) => client.finalized_block().await,
Self::Astar(client) => client.finalized_block().await,
Self::Humanode(client) => client.finalized_block().await,
Self::Polkadot(client) => client.finalized_block().await,
}
}
Expand All @@ -210,6 +212,7 @@ impl BlockchainClient for GenericClient {
match self {
Self::Ethereum(client) => client.balance(address, block).await,
Self::Astar(client) => client.balance(address, block).await,
Self::Humanode(client) => client.balance(address, block).await,
Self::Polkadot(client) => client.balance(address, block).await,
}
}
Expand Down Expand Up @@ -244,6 +247,7 @@ impl BlockchainClient for GenericClient {
match self {
Self::Ethereum(client) => client.submit(transaction).await,
Self::Astar(client) => client.submit(transaction).await,
Self::Humanode(client) => client.submit(transaction).await,
Self::Polkadot(client) => {
// TODO: implement a custom receipt for Polkadot
let result = client.submit(transaction).await?;
Expand Down Expand Up @@ -280,12 +284,6 @@ impl BlockchainClient for GenericClient {
},
GenericCall::Polkadot(_) => anyhow::bail!("invalid call"),
},
Self::Humanode(client) => match req {
GenericCall::Ethereum(args) => {
GenericCallResult::Ethereum(client.call(args).await?)
},
_ => anyhow::bail!("invalid call"),
},
Self::Polkadot(client) => match req {
GenericCall::Polkadot(args) => {
GenericCallResult::Polkadot(client.call(args).await?)
Expand All @@ -311,6 +309,12 @@ impl BlockchainClient for GenericClient {
};
Ok(Some(GenericClientStream::Astar(stream)))
},
Self::Humanode(client) => {
let Some(stream) = client.listen().await? else {
return Ok(None);
};
Ok(Some(GenericClientStream::Astar(stream)))
},
Self::Polkadot(client) => {
let Some(stream) = client.listen().await? else {
return Ok(None);
Expand All @@ -330,6 +334,11 @@ impl BlockchainClient for GenericClient {
GenericClientSubscription::Astar(sub) => client.subscribe(sub).await,
_ => anyhow::bail!("invalid subscription"),
},

Self::Humanode(client) => match sub {
GenericClientSubscription::Humanode(sub) => client.subscribe(sub).await,
_ => anyhow::bail!("invalid subscription"),
},
Self::Polkadot(client) => match sub {
GenericClientSubscription::Polkadot(sub) => client.subscribe(sub).await,
_ => anyhow::bail!("invalid subscription"),
Expand All @@ -342,19 +351,22 @@ impl BlockchainClient for GenericClient {
pub enum GenericClientSubscription {
Ethereum(<EthereumClient as BlockchainClient>::Subscription),
Astar(<AstarClient as BlockchainClient>::Subscription),
Humanode(<HumanodeClient as BlockchainClient>::Subscription),
Polkadot(<PolkadotClient as BlockchainClient>::Subscription),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GenericClientEvent {
Ethereum(<EthereumClient as BlockchainClient>::Event),
Astar(<AstarClient as BlockchainClient>::Event),
Humanode(<HumanodeClient as BlockchainClient>::Event),
Polkadot(<PolkadotClient as BlockchainClient>::Event),
}

pub enum GenericClientStream<'a> {
Ethereum(<EthereumClient as BlockchainClient>::EventStream<'a>),
Astar(<AstarClient as BlockchainClient>::EventStream<'a>),
Humanode(<HumanodeClient as BlockchainClient>::EventStream<'a>),
Polkadot(<PolkadotClient as BlockchainClient>::EventStream<'a>),
}

Expand All @@ -373,7 +385,10 @@ impl<'a> Stream for GenericClientStream<'a> {
Self::Astar(stream) => stream
.poll_next_unpin(cx)
.map(|opt| opt.map(|event| event.map_event(GenericClientEvent::Astar))),
Self::Polkadot(stream) => stream
Self::Humanode(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))),
}
Expand Down
10 changes: 10 additions & 0 deletions rosetta-client/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl Wallet {
// self.client.finalized_block().await
match &self.client {
GenericClient::Astar(client) => client.finalized_block().await,
GenericClient::Humanode(client) => client.finalized_block().await,
GenericClient::Ethereum(client) => client.finalized_block().await,
GenericClient::Polkadot(client) => client.finalized_block().await,
}
Expand All @@ -120,6 +121,9 @@ impl Wallet {
GenericClient::Astar(client) => {
client.balance(&address, &PartialBlockIdentifier::from(block)).await?
},
GenericClient::Humanode(client) => {
client.balance(&address, &PartialBlockIdentifier::from(block)).await?
},
GenericClient::Ethereum(client) => {
client.balance(&address, &PartialBlockIdentifier::from(block)).await?
},
Expand Down Expand Up @@ -238,6 +242,7 @@ impl Wallet {
match self.metadata(&metadata_params).await? {
GenericMetadata::Ethereum(metadata) => metadata,
GenericMetadata::Astar(metadata) => metadata.0,
GenericMetadata::Humanode(metadata) => metadata.0,
GenericMetadata::Polkadot(_) => anyhow::bail!("unsupported op"),
};
Ok(u128::from(metadata.gas_limit))
Expand Down Expand Up @@ -283,6 +288,7 @@ impl Wallet {
let result = match &self.client {
GenericClient::Ethereum(client) => client.call(&query).await?,
GenericClient::Astar(client) => client.call(&query).await?,
GenericClient::Humanode(client) => client.call(&query).await?,
GenericClient::Polkadot(_) => anyhow::bail!("polkadot doesn't support eth_view_call"),
};
let result = <Q as rosetta_server_ethereum::QueryItem>::parse_result(result)?;
Expand Down Expand Up @@ -406,6 +412,10 @@ fn update_metadata_params(
params.0.nonce = nonce;
params.0.gas_limit = gas_limit;
},
GenericMetadataParams::Humanode(params) => {
params.0.nonce = nonce;
params.0.gas_limit = gas_limit;
},
GenericMetadataParams::Polkadot(params) => {
if let Some(nonce) = nonce {
if let Ok(nonce) = u32::try_from(nonce) {
Expand Down

0 comments on commit fc8607b

Please sign in to comment.