Skip to content

Commit f15f280

Browse files
committed
Update Dockerfile Rust version and fix Ethereum Adapter
1 parent 82e13d2 commit f15f280

File tree

9 files changed

+2030
-2402
lines changed

9 files changed

+2030
-2402
lines changed

Cargo.lock

Lines changed: 1981 additions & 2338 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
FROM rust:1.40 as builder
1+
FROM rust:1.45.2 as builder
22

3-
MAINTAINER dev@adex.network
3+
LABEL maintainer="dev@adex.network"
44

55
WORKDIR /usr/src/app
66

adapter/Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@ serde = { version = "^1.0", features = ['derive'] }
1414
serde_json = "1.0"
1515
serde-hex = "0.1.0"
1616
# Ethereum
17-
web3 = { git = "https://github.com/samparsky/rust-web3" }
17+
web3 = { git = "https://github.com/elpiel/rust-web3", branch = "export-ethabi" }
1818
eth_checksum = "0.1.1"
19-
ethabi = { git = "https://github.com/samparsky/ethabi", branch = "graph-patches" }
2019
tiny-keccak = "1.5"
21-
parity-crypto = { version = "0.5.0", features = ["publickey"] }
22-
ethstore = { version = "0.2.1", git = "https://github.com/paritytech/parity-ethereum"}
23-
ethkey = { version = "0.4.0", git = "https://github.com/paritytech/parity-ethereum"}
20+
ethstore = { git = "https://github.com/elpiel/openethereum", branch = "remove-dir-depenedency-for-ethstore" }
2421
# API client
2522
reqwest = { version = "0.10", features = ["json"] }
2623

adapter/src/ethereum.rs

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ use crate::EthereumChannel;
22
use async_trait::async_trait;
33
use chrono::Utc;
44
use error::*;
5-
use ethabi::token::Token;
6-
use ethkey::Password;
7-
use ethstore::SafeAccount;
8-
use futures::{compat::Future01CompatExt, TryFutureExt};
9-
use lazy_static::lazy_static;
10-
use parity_crypto::publickey::{
11-
public_to_address, recover, verify_address, Address, Message, Signature,
5+
use ethstore::{
6+
ethkey::{public_to_address, recover, verify_address, Address, Message, Password, Signature},
7+
SafeAccount,
128
};
9+
use futures::TryFutureExt;
10+
use lazy_static::lazy_static;
1311
use primitives::{
1412
adapter::{Adapter, AdapterResult, Error as AdapterError, KeystoreOptions, Session},
1513
channel_validator::ChannelValidator,
@@ -21,13 +19,12 @@ use serde::{Deserialize, Serialize};
2119
use serde_json::Value;
2220
use std::convert::TryFrom;
2321
use std::fs;
24-
use std::sync::Arc;
2522
use tiny_keccak::Keccak;
2623
use web3::{
24+
contract::tokens::Tokenizable,
2725
contract::{Contract, Options},
28-
transports::EventLoopHandle,
2926
transports::Http,
30-
types::U256,
27+
types::{H256, U256},
3128
Web3,
3229
};
3330

@@ -46,7 +43,6 @@ pub struct EthereumAdapter {
4643
keystore_pwd: Password,
4744
config: Config,
4845
wallet: Option<SafeAccount>,
49-
event_loop: Arc<EventLoopHandle>,
5046
web3: Web3<Http>,
5147
relayer: RelayerClient,
5248
}
@@ -70,9 +66,8 @@ impl EthereumAdapter {
7066

7167
let address = ValidatorId::try_from(&address).map_err(KeystoreError::AddressInvalid)?;
7268

73-
let (eloop, transport) =
69+
let transport =
7470
web3::transports::Http::new(&config.ethereum_network).map_err(Error::Web3)?;
75-
let event_loop = Arc::new(eloop);
7671
let web3 = web3::Web3::new(transport);
7772
let relayer =
7873
RelayerClient::new(&config.ethereum_adapter_relayer).map_err(Error::RelayerClient)?;
@@ -83,7 +78,6 @@ impl EthereumAdapter {
8378
keystore_pwd: opts.keystore_pwd.into(),
8479
wallet: None,
8580
config: config.to_owned(),
86-
event_loop,
8781
web3,
8882
relayer,
8983
})
@@ -115,7 +109,7 @@ impl Adapter for EthereumAdapter {
115109
fn sign(&self, state_root: &str) -> AdapterResult<String, Self::AdapterError> {
116110
if let Some(wallet) = &self.wallet {
117111
let state_root = hex::decode(state_root).map_err(VerifyError::StateRootDecoding)?;
118-
let message = Message::from_slice(&hash_message(&state_root));
112+
let message = Message::from(hash_message(&state_root));
119113
let wallet_sign = wallet
120114
.sign(&self.keystore_pwd, &message)
121115
.map_err(EwtSigningError::SigningMessage)?;
@@ -139,10 +133,10 @@ impl Adapter for EthereumAdapter {
139133
return Err(VerifyError::SignatureNotPrefixed.into());
140134
}
141135
let decoded_signature = hex::decode(&sig[2..]).map_err(VerifyError::SignatureDecoding)?;
142-
let address = Address::from_slice(signer.inner());
136+
let address = Address::from(*signer.inner());
143137
let signature = Signature::from_electrum(&decoded_signature);
144138
let state_root = hex::decode(state_root).map_err(VerifyError::StateRootDecoding)?;
145-
let message = Message::from_slice(&hash_message(&state_root));
139+
let message = Message::from(hash_message(&state_root));
146140

147141
let verify_address = verify_address(&address, &signature, &message)
148142
.map_err(VerifyError::PublicKeyRecovery)?;
@@ -173,20 +167,21 @@ impl Adapter for EthereumAdapter {
173167
));
174168
}
175169

176-
let contract_address: Address = self.config.ethereum_core_address.into();
177-
178-
let contract = Contract::from_json(self.web3.eth(), contract_address, &ADEXCORE_ABI)
179-
.map_err(Error::ContractInitialization)?;
170+
let contract = Contract::from_json(
171+
self.web3.eth(),
172+
self.config.ethereum_core_address.into(),
173+
&ADEXCORE_ABI,
174+
)
175+
.map_err(Error::ContractInitialization)?;
180176

181177
let channel_status: U256 = contract
182178
.query(
183179
"states",
184-
Token::FixedBytes(channel.id.as_ref().to_vec()),
180+
H256(*channel.id).into_token(),
185181
None,
186182
Options::default(),
187183
None,
188184
)
189-
.compat()
190185
.await
191186
.map_err(Error::ContractQuerying)?;
192187

@@ -382,7 +377,7 @@ pub fn ewt_sign(
382377
&serde_json::to_string(payload).map_err(EwtSigningError::PayloadSerialization)?,
383378
base64::URL_SAFE_NO_PAD,
384379
);
385-
let message = Message::from_slice(&hash_message(
380+
let message = Message::from(hash_message(
386381
&format!("{}.{}", header_encoded, payload_encoded).as_bytes(),
387382
));
388383
let signature: Signature = signer
@@ -404,7 +399,7 @@ pub fn ewt_verify(
404399
payload_encoded: &str,
405400
token: &str,
406401
) -> Result<VerifyPayload, EwtVerifyError> {
407-
let message = Message::from_slice(&hash_message(
402+
let message = Message::from(hash_message(
408403
&format!("{}.{}", header_encoded, payload_encoded).as_bytes(),
409404
));
410405

@@ -424,7 +419,7 @@ pub fn ewt_verify(
424419
serde_json::from_str(&payload_string).map_err(EwtVerifyError::PayloadDeserialization)?;
425420

426421
let verified_payload = VerifyPayload {
427-
from: ValidatorId::from(address.as_fixed_bytes()),
422+
from: ValidatorId::from(&address.0),
428423
payload,
429424
};
430425

@@ -436,13 +431,13 @@ mod test {
436431
use super::*;
437432
use crate::EthereumChannel;
438433
use chrono::{Duration, Utc};
439-
use ethabi::token::Token;
440434
use hex::FromHex;
441435
use primitives::adapter::KeystoreOptions;
442436
use primitives::config::configuration;
443437
use primitives::ChannelId;
444438
use primitives::{ChannelSpec, EventSubmission, SpecValidators, ValidatorDesc};
445439
use std::convert::TryFrom;
440+
use web3::types::Address;
446441

447442
fn setup_eth_adapter(contract_address: Option<[u8; 20]>) -> EthereumAdapter {
448443
let mut config = configuration("development", None).expect("failed parse config");
@@ -577,7 +572,7 @@ mod test {
577572

578573
#[tokio::test]
579574
async fn should_validate_valid_channel_properly() {
580-
let (_eloop, http) =
575+
let http =
581576
web3::transports::Http::new("http://localhost:8545").expect("failed to init transport");
582577

583578
let web3 = web3::Web3::new(http);
@@ -604,10 +599,8 @@ mod test {
604599
opt.gas = Some(6_721_975.into());
605600
}))
606601
.execute(token_bytecode, (), leader_account)
607-
.expect("Correct parameters are passed to the constructor.")
608-
.compat()
609602
.await
610-
.expect("failed to wait");
603+
.expect("Correct parameters are passed to the constructor.");
611604

612605
let adex_contract = Contract::deploy(web3.eth(), &ADEXCORE_ABI)
613606
.expect("invalid adex contract")
@@ -617,23 +610,17 @@ mod test {
617610
opt.gas = Some(6_721_975.into());
618611
}))
619612
.execute(adex_bytecode, (), leader_account)
620-
.expect("Correct parameters are passed to the constructor.")
621-
.compat()
622613
.await
623-
.expect("failed to init adex contract");
614+
.expect("Correct parameters are passed to the constructor.");
624615

625616
// contract call set balance
626617
token_contract
627618
.call(
628619
"setBalanceTo",
629-
(
630-
Token::Address(leader_account),
631-
Token::Uint(U256::from(2000 as u64)),
632-
),
620+
(Address::from(leader_account), U256::from(2000_u64)),
633621
leader_account,
634622
Options::default(),
635623
)
636-
.compat()
637624
.await
638625
.expect("Failed to set balance");
639626

@@ -697,7 +684,6 @@ mod test {
697684
leader_account,
698685
Options::default(),
699686
)
700-
.compat()
701687
.await
702688
.expect("open channel");
703689

adapter/src/ethereum/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub enum Error {
1818
/// Signing of the message failed
1919
SignMessage(EwtSigningError),
2020
VerifyMessage(EwtVerifyError),
21-
ContractInitialization(ethabi::Error),
21+
ContractInitialization(web3::ethabi::Error),
2222
ContractQuerying(web3::contract::Error),
2323
/// Error occurred during verification of Signature and/or StateRoot and/or Address
2424
VerifyAddress(VerifyError),
@@ -52,7 +52,7 @@ impl fmt::Display for Error {
5252
/// Error returned on `eth_adapter.verify()` when the combination of
5353
/// (signer, state_root, signature) **doesn't align**.
5454
pub enum VerifyError {
55-
PublicKeyRecovery(parity_crypto::publickey::Error),
55+
PublicKeyRecovery(ethstore::ethkey::Error),
5656
StateRootDecoding(hex::FromHexError),
5757
SignatureDecoding(hex::FromHexError),
5858
SignatureNotPrefixed,
@@ -150,7 +150,7 @@ impl From<EwtSigningError> for AdapterError<Error> {
150150

151151
#[derive(Debug)]
152152
pub enum EwtVerifyError {
153-
AddressRecovery(parity_crypto::publickey::Error),
153+
AddressRecovery(ethstore::ethkey::Error),
154154
SignatureDecoding(base64::DecodeError),
155155
PayloadDecoding(base64::DecodeError),
156156
PayloadDeserialization(serde_json::Error),

adapter/src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
use std::error::Error;
66

77
use chrono::{DateTime, Utc};
8-
use ethabi::encode;
9-
use ethabi::token::Token;
108
use hex::FromHex;
11-
use primitives::channel::ChannelError;
12-
use primitives::BigNum;
13-
use primitives::{Channel, ValidatorId};
9+
use primitives::{channel::ChannelError, BigNum, Channel, ValidatorId};
1410
use sha2::{Digest, Sha256};
1511
use std::convert::TryFrom;
1612
use tiny_keccak::Keccak;
17-
use web3::types::{Address, U256};
13+
use web3::{
14+
ethabi::{encode, token::Token},
15+
types::{Address, U256},
16+
};
1817

1918
pub use self::dummy::DummyAdapter;
2019
pub use self::ethereum::EthereumAdapter;

primitives/src/channel.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ fn validate_channel_id(s: &str) -> Result<[u8; 32], FromHexError> {
4343
}
4444

4545
impl Deref for ChannelId {
46-
type Target = [u8];
46+
type Target = [u8; 32];
4747

48-
fn deref(&self) -> &[u8] {
48+
fn deref(&self) -> &[u8; 32] {
4949
&self.0
5050
}
5151
}
@@ -348,18 +348,21 @@ mod test {
348348
let prefixed_value = serde_json::Value::String(prefixed_string.clone());
349349

350350
// Deserialization from JSON
351-
let de_hex_json = serde_json::from_value::<ChannelId>(hex_value.clone())
351+
let de_hex_json =
352+
serde_json::from_value::<ChannelId>(hex_value.clone()).expect("Should deserialize");
353+
let de_prefixed_json = serde_json::from_value::<ChannelId>(prefixed_value.clone())
352354
.expect("Should deserialize");
353-
let de_prefixed_json =
354-
serde_json::from_value::<ChannelId>(prefixed_value.clone()).expect("Should deserialize");
355355

356356
assert_eq!(de_hex_json, expected_id);
357357
assert_eq!(de_prefixed_json, expected_id);
358358

359359
// Serialization to JSON
360360
let actual_serialized = serde_json::to_value(expected_id).expect("Should Serialize");
361361
// we don't expect any capitalization
362-
assert_eq!(actual_serialized, serde_json::Value::String(prefixed_string))
362+
assert_eq!(
363+
actual_serialized,
364+
serde_json::Value::String(prefixed_string)
365+
)
363366
}
364367
}
365368

validator_worker/src/heartbeat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async fn send_heartbeat<A: Adapter + 'static>(
2424

2525
let merkle_tree = MerkleTree::new(&[timestamp_buf])?;
2626

27-
let state_root_raw = get_signable_state_root(&iface.channel.id, &merkle_tree.root())?;
27+
let state_root_raw = get_signable_state_root(iface.channel.id.as_ref(), &merkle_tree.root())?;
2828
let state_root = hex::encode(state_root_raw);
2929

3030
let signature = iface.adapter.sign(&state_root)?;

validator_worker/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(crate) fn get_state_root_hash<A: Adapter + 'static>(
3535

3636
let tree = MerkleTree::new(&elems)?;
3737
// keccak256(channelId, balanceRoot
38-
get_signable_state_root(&iface.channel.id, &tree.root())
38+
get_signable_state_root(iface.channel.id.as_ref(), &tree.root())
3939
}
4040

4141
#[cfg(test)]

0 commit comments

Comments
 (0)