@@ -4,7 +4,7 @@ use ethabi::token::Token;
4
4
use ethkey:: Password ;
5
5
use ethstore:: SafeAccount ;
6
6
use futures:: compat:: Future01CompatExt ;
7
- use futures:: future:: BoxFuture ;
7
+ use futures:: future:: { BoxFuture , FutureExt } ;
8
8
use lazy_static:: lazy_static;
9
9
use parity_crypto:: publickey:: {
10
10
public_to_address, recover, verify_address, Address , Message , Signature ,
@@ -21,10 +21,14 @@ use serde_json::Value;
21
21
use std:: convert:: TryFrom ;
22
22
use std:: error:: Error ;
23
23
use std:: fs;
24
+ use std:: sync:: Arc ;
24
25
use tiny_keccak:: Keccak ;
25
26
use web3:: {
26
27
contract:: { Contract , Options } ,
28
+ transports:: EventLoopHandle ,
29
+ transports:: Http ,
27
30
types:: U256 ,
31
+ Web3 ,
28
32
} ;
29
33
30
34
lazy_static ! {
@@ -43,6 +47,8 @@ pub struct EthereumAdapter {
43
47
keystore_pwd : Password ,
44
48
config : Config ,
45
49
wallet : Option < SafeAccount > ,
50
+ event_loop : Arc < EventLoopHandle > ,
51
+ web3 : Web3 < Http > ,
46
52
}
47
53
48
54
// Enables EthereumAdapter to be able to
@@ -68,12 +74,19 @@ impl EthereumAdapter {
68
74
69
75
let address = ValidatorId :: try_from ( & address) ?;
70
76
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
+
71
82
Ok ( Self {
72
83
address,
73
84
keystore_json,
74
85
keystore_pwd : opts. keystore_pwd . into ( ) ,
75
86
wallet : None ,
76
87
config : config. to_owned ( ) ,
88
+ event_loop,
89
+ web3,
77
90
} )
78
91
}
79
92
}
@@ -124,7 +137,7 @@ impl Adapter for EthereumAdapter {
124
137
}
125
138
126
139
fn validate_channel < ' a > ( & ' a self , channel : & ' a Channel ) -> BoxFuture < ' a , AdapterResult < bool > > {
127
- Box :: pin ( async move {
140
+ async move {
128
141
// check if channel is valid
129
142
if let Err ( e) = EthereumAdapter :: is_channel_valid ( & self . config , self . whoami ( ) , channel)
130
143
{
@@ -147,11 +160,7 @@ impl Adapter for EthereumAdapter {
147
160
148
161
let contract_address: Address = self . config . ethereum_core_address . into ( ) ;
149
162
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 )
155
164
. map_err ( |_| map_error ( "failed to init core contract" ) ) ?;
156
165
157
166
let channel_status: U256 = contract
@@ -173,13 +182,14 @@ impl Adapter for EthereumAdapter {
173
182
}
174
183
175
184
Ok ( true )
176
- } )
185
+ }
186
+ . boxed ( )
177
187
}
178
188
179
189
/// Creates a `Session` from a provided Token by calling the Contract.
180
190
/// Does **not** cache the (`Token`, `Session`) pair.
181
191
fn session_from_token < ' a > ( & ' a self , token : & ' a str ) -> BoxFuture < ' a , AdapterResult < Session > > {
182
- Box :: pin ( async move {
192
+ async move {
183
193
if token. len ( ) < 16 {
184
194
return Err ( AdapterError :: Failed ( "invalid token id" . to_string ( ) ) ) ;
185
195
}
@@ -210,12 +220,9 @@ impl Adapter for EthereumAdapter {
210
220
let sess = match & verified. payload . identity {
211
221
Some ( identity) => {
212
222
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" ) ) ?;
219
226
220
227
let privilege_level: U256 = contract
221
228
. query (
@@ -246,7 +253,8 @@ impl Adapter for EthereumAdapter {
246
253
} ;
247
254
248
255
Ok ( sess)
249
- } )
256
+ }
257
+ . boxed ( )
250
258
}
251
259
252
260
fn get_auth ( & self , validator : & ValidatorId ) -> AdapterResult < String > {
@@ -476,9 +484,8 @@ mod test {
476
484
477
485
#[ tokio:: test]
478
486
async fn should_validate_valid_channel_properly ( ) {
479
- let ( eloop , http) =
487
+ let ( _eloop , http) =
480
488
web3:: transports:: Http :: new ( "http://localhost:8545" ) . expect ( "failed to init transport" ) ;
481
- eloop. into_remote ( ) ;
482
489
483
490
let web3 = web3:: Web3 :: new ( http) ;
484
491
let leader_account: Address = "Df08F82De32B8d460adbE8D72043E3a7e25A3B39"
@@ -621,12 +628,6 @@ mod test {
621
628
let mut eth_adapter = setup_eth_adapter ( None ) ;
622
629
eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
623
630
624
- // deploy identity contract
625
- let ( eloop, http) =
626
- web3:: transports:: Http :: new ( "http://localhost:8545" ) . expect ( "failed to init transport" ) ;
627
- eloop. into_remote ( ) ;
628
-
629
- let web3 = web3:: Web3 :: new ( http) ;
630
631
// part of address used in initializing ganache-cli
631
632
let leader_account: Address = "Df08F82De32B8d460adbE8D72043E3a7e25A3B39"
632
633
. parse ( )
@@ -641,7 +642,7 @@ mod test {
641
642
let identity_bytecode = include_str ! ( "../test/resources/identitybytecode.json" ) ;
642
643
643
644
// deploy identity contract
644
- let identity_contract = Contract :: deploy ( web3. eth ( ) , & IDENTITY_ABI )
645
+ let identity_contract = Contract :: deploy ( eth_adapter . web3 . eth ( ) , & IDENTITY_ABI )
645
646
. expect ( "invalid token token contract" )
646
647
. confirmations ( 0 )
647
648
. options ( Options :: with ( |opt| {
0 commit comments