Skip to content

Commit 943559a

Browse files
authored
Merge pull request #228 from samparsky/fix-ethereum-adapter-event-loop-handle
fix: add event_loop handle to etheruem_adapter
2 parents bd4044d + 9873ac1 commit 943559a

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

adapter/src/dummy.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use futures::future::BoxFuture;
1+
use futures::future::{BoxFuture, FutureExt};
22
use primitives::adapter::{Adapter, AdapterError, AdapterResult, DummyAdapterOptions, Session};
33
use primitives::channel_validator::ChannelValidator;
44
use primitives::config::Config;
@@ -65,16 +65,17 @@ impl Adapter for DummyAdapter {
6565
}
6666

6767
fn validate_channel<'a>(&'a self, channel: &'a Channel) -> BoxFuture<'a, AdapterResult<bool>> {
68-
Box::pin(async move {
68+
async move {
6969
match DummyAdapter::is_channel_valid(&self.config, self.whoami(), channel) {
7070
Ok(_) => Ok(true),
7171
Err(e) => Err(AdapterError::InvalidChannel(e.to_string())),
7272
}
73-
})
73+
}
74+
.boxed()
7475
}
7576

7677
fn session_from_token<'a>(&'a self, token: &'a str) -> BoxFuture<'a, AdapterResult<Session>> {
77-
Box::pin(async move {
78+
async move {
7879
let identity = self
7980
.authorization_tokens
8081
.iter()
@@ -90,7 +91,8 @@ impl Adapter for DummyAdapter {
9091
token
9192
))),
9293
}
93-
})
94+
}
95+
.boxed()
9496
}
9597

9698
fn get_auth(&self, _validator: &ValidatorId) -> AdapterResult<String> {

adapter/src/ethereum.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ethabi::token::Token;
44
use ethkey::Password;
55
use ethstore::SafeAccount;
66
use futures::compat::Future01CompatExt;
7-
use futures::future::BoxFuture;
7+
use futures::future::{BoxFuture, FutureExt};
88
use lazy_static::lazy_static;
99
use parity_crypto::publickey::{
1010
public_to_address, recover, verify_address, Address, Message, Signature,
@@ -21,10 +21,14 @@ use serde_json::Value;
2121
use std::convert::TryFrom;
2222
use std::error::Error;
2323
use std::fs;
24+
use std::sync::Arc;
2425
use tiny_keccak::Keccak;
2526
use web3::{
2627
contract::{Contract, Options},
28+
transports::EventLoopHandle,
29+
transports::Http,
2730
types::U256,
31+
Web3,
2832
};
2933

3034
lazy_static! {
@@ -43,6 +47,8 @@ pub struct EthereumAdapter {
4347
keystore_pwd: Password,
4448
config: Config,
4549
wallet: Option<SafeAccount>,
50+
event_loop: Arc<EventLoopHandle>,
51+
web3: Web3<Http>,
4652
}
4753

4854
// Enables EthereumAdapter to be able to
@@ -68,12 +74,19 @@ impl EthereumAdapter {
6874

6975
let address = ValidatorId::try_from(&address)?;
7076

77+
let (eloop, transport) = web3::transports::Http::new(&config.ethereum_network)
78+
.map_err(|_| map_error("failed to init http transport"))?;
79+
let event_loop = Arc::new(eloop);
80+
let web3 = web3::Web3::new(transport);
81+
7182
Ok(Self {
7283
address,
7384
keystore_json,
7485
keystore_pwd: opts.keystore_pwd.into(),
7586
wallet: None,
7687
config: config.to_owned(),
88+
event_loop,
89+
web3,
7790
})
7891
}
7992
}
@@ -124,7 +137,7 @@ impl Adapter for EthereumAdapter {
124137
}
125138

126139
fn validate_channel<'a>(&'a self, channel: &'a Channel) -> BoxFuture<'a, AdapterResult<bool>> {
127-
Box::pin(async move {
140+
async move {
128141
// check if channel is valid
129142
if let Err(e) = EthereumAdapter::is_channel_valid(&self.config, self.whoami(), channel)
130143
{
@@ -147,11 +160,7 @@ impl Adapter for EthereumAdapter {
147160

148161
let contract_address: Address = self.config.ethereum_core_address.into();
149162

150-
let (_eloop, transport) = web3::transports::Http::new(&self.config.ethereum_network)
151-
.map_err(|_| map_error("failed to init http transport"))?;
152-
let web3 = web3::Web3::new(transport);
153-
154-
let contract = Contract::from_json(web3.eth(), contract_address, &ADEXCORE_ABI)
163+
let contract = Contract::from_json(self.web3.eth(), contract_address, &ADEXCORE_ABI)
155164
.map_err(|_| map_error("failed to init core contract"))?;
156165

157166
let channel_status: U256 = contract
@@ -173,13 +182,14 @@ impl Adapter for EthereumAdapter {
173182
}
174183

175184
Ok(true)
176-
})
185+
}
186+
.boxed()
177187
}
178188

179189
/// Creates a `Session` from a provided Token by calling the Contract.
180190
/// Does **not** cache the (`Token`, `Session`) pair.
181191
fn session_from_token<'a>(&'a self, token: &'a str) -> BoxFuture<'a, AdapterResult<Session>> {
182-
Box::pin(async move {
192+
async move {
183193
if token.len() < 16 {
184194
return Err(AdapterError::Failed("invalid token id".to_string()));
185195
}
@@ -210,12 +220,9 @@ impl Adapter for EthereumAdapter {
210220
let sess = match &verified.payload.identity {
211221
Some(identity) => {
212222
let contract_address: Address = identity.into();
213-
let (_eloop, transport) =
214-
web3::transports::Http::new(&self.config.ethereum_network)
215-
.map_err(|_| map_error("failed to init http transport"))?;
216-
let web3 = web3::Web3::new(transport);
217-
let contract = Contract::from_json(web3.eth(), contract_address, &IDENTITY_ABI)
218-
.map_err(|_| map_error("failed to init identity contract"))?;
223+
let contract =
224+
Contract::from_json(self.web3.eth(), contract_address, &IDENTITY_ABI)
225+
.map_err(|_| map_error("failed to init identity contract"))?;
219226

220227
let privilege_level: U256 = contract
221228
.query(
@@ -246,7 +253,8 @@ impl Adapter for EthereumAdapter {
246253
};
247254

248255
Ok(sess)
249-
})
256+
}
257+
.boxed()
250258
}
251259

252260
fn get_auth(&self, validator: &ValidatorId) -> AdapterResult<String> {
@@ -476,9 +484,8 @@ mod test {
476484

477485
#[tokio::test]
478486
async fn should_validate_valid_channel_properly() {
479-
let (eloop, http) =
487+
let (_eloop, http) =
480488
web3::transports::Http::new("http://localhost:8545").expect("failed to init transport");
481-
eloop.into_remote();
482489

483490
let web3 = web3::Web3::new(http);
484491
let leader_account: Address = "Df08F82De32B8d460adbE8D72043E3a7e25A3B39"
@@ -620,12 +627,6 @@ mod test {
620627
let mut eth_adapter = setup_eth_adapter(None);
621628
eth_adapter.unlock().expect("should unlock eth adapter");
622629

623-
// deploy identity contract
624-
let (eloop, http) =
625-
web3::transports::Http::new("http://localhost:8545").expect("failed to init transport");
626-
eloop.into_remote();
627-
628-
let web3 = web3::Web3::new(http);
629630
// part of address used in initializing ganache-cli
630631
let leader_account: Address = "Df08F82De32B8d460adbE8D72043E3a7e25A3B39"
631632
.parse()
@@ -640,7 +641,7 @@ mod test {
640641
let identity_bytecode = include_str!("../test/resources/identitybytecode.json");
641642

642643
// deploy identity contract
643-
let identity_contract = Contract::deploy(web3.eth(), &IDENTITY_ABI)
644+
let identity_contract = Contract::deploy(eth_adapter.web3.eth(), &IDENTITY_ABI)
644645
.expect("invalid token token contract")
645646
.confirmations(0)
646647
.options(Options::with(|opt| {

0 commit comments

Comments
 (0)