Skip to content

Commit 585c93e

Browse files
committed
fix: merge /dev, fix merge conflicts
2 parents 2274529 + c8f3fcc commit 585c93e

File tree

22 files changed

+804
-535
lines changed

22 files changed

+804
-535
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ members = [
44
"adapter",
55
"primitives",
66
"validator_worker",
7-
# "sentry",
7+
"sentry",
88
"adview-manager",
9-
]
9+
]
10+
11+
[patch.crates-io]
12+
slog-term = { git = "https://github.com/slog-rs/term", branch = "master"}

adapter/src/dummy.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use primitives::adapter::{Adapter, AdapterError, AdapterOptions, AdapterResult, Session};
1+
use primitives::adapter::{Adapter, AdapterError, AdapterResult, DummyAdapterOptions, Session};
22
use primitives::channel_validator::ChannelValidator;
33
use primitives::config::Config;
44
use primitives::{Channel, ValidatorId};
@@ -18,31 +18,18 @@ pub struct DummyAdapter {
1818
// check if a channel is valid
1919
impl ChannelValidator for DummyAdapter {}
2020

21-
impl Adapter for DummyAdapter {
22-
type Output = DummyAdapter;
23-
24-
fn init(opts: AdapterOptions, config: &Config) -> AdapterResult<DummyAdapter> {
25-
let (identity, session_tokens, authorization_tokens) = match opts {
26-
AdapterOptions::DummAdapter {
27-
dummy_identity,
28-
dummy_auth,
29-
dummy_auth_tokens,
30-
} => (dummy_identity, dummy_auth, dummy_auth_tokens),
31-
_ => {
32-
return Err(AdapterError::Configuration(
33-
"dummy_identity, dummy_auth, dummy_auth_tokens required".to_string(),
34-
))
35-
}
36-
};
37-
38-
Ok(Self {
39-
identity,
21+
impl DummyAdapter {
22+
pub fn init(opts: DummyAdapterOptions, config: &Config) -> Self {
23+
Self {
24+
identity: opts.dummy_identity,
4025
config: config.to_owned(),
41-
session_tokens,
42-
authorization_tokens,
43-
})
26+
session_tokens: opts.dummy_auth,
27+
authorization_tokens: opts.dummy_auth_tokens,
28+
}
4429
}
30+
}
4531

32+
impl Adapter for DummyAdapter {
4633
fn unlock(&mut self) -> AdapterResult<()> {
4734
Ok(())
4835
}

adapter/src/ethereum.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ethkey::{public_to_address, recover, verify_address, Address, Message, Passw
44
use ethstore::SafeAccount;
55
use lazy_static::lazy_static;
66
use primitives::{
7-
adapter::{Adapter, AdapterError, AdapterOptions, AdapterResult, Session},
7+
adapter::{Adapter, AdapterError, AdapterResult, KeystoreOptions, Session},
88
channel_validator::ChannelValidator,
99
config::Config,
1010
Channel, ValidatorId,
@@ -49,22 +49,9 @@ pub struct EthereumAdapter {
4949
// check if a channel is valid
5050
impl ChannelValidator for EthereumAdapter {}
5151

52-
impl Adapter for EthereumAdapter {
53-
type Output = EthereumAdapter;
54-
55-
fn init(opts: AdapterOptions, config: &Config) -> AdapterResult<EthereumAdapter> {
56-
let (keystore_file, keystore_pwd) = match opts {
57-
AdapterOptions::EthereumAdapter(keystore_opts) => {
58-
(keystore_opts.keystore_file, keystore_opts.keystore_pwd)
59-
}
60-
_ => {
61-
return Err(AdapterError::Configuration(
62-
"Missing keystore json file or password".to_string(),
63-
))
64-
}
65-
};
66-
67-
let keystore_contents = fs::read_to_string(&keystore_file)
52+
impl EthereumAdapter {
53+
pub fn init(opts: KeystoreOptions, config: &Config) -> AdapterResult<EthereumAdapter> {
54+
let keystore_contents = fs::read_to_string(&opts.keystore_file)
6855
.map_err(|_| map_error("Invalid keystore location provided"))?;
6956

7057
let keystore_json: Value = serde_json::from_str(&keystore_contents)
@@ -84,14 +71,16 @@ impl Adapter for EthereumAdapter {
8471
Ok(Self {
8572
address: identity,
8673
keystore_json,
87-
keystore_pwd: keystore_pwd.into(),
74+
keystore_pwd: opts.keystore_pwd.into(),
8875
session_tokens: HashMap::new(),
8976
authorization_tokens: HashMap::new(),
9077
wallet: None,
9178
config: config.to_owned(),
9279
})
9380
}
81+
}
9482

83+
impl Adapter for EthereumAdapter {
9584
fn unlock(&mut self) -> AdapterResult<()> {
9685
let account = SafeAccount::from_file(
9786
serde_json::from_value(self.keystore_json.clone())
@@ -404,9 +393,8 @@ mod test {
404393
keystore_file: "./test/resources/keystore.json".to_string(),
405394
keystore_pwd: "adexvalidator".to_string(),
406395
};
407-
let adapter_options = AdapterOptions::EthereumAdapter(keystore_options);
408396

409-
EthereumAdapter::init(adapter_options, &config).expect("should init ethereum adapter")
397+
EthereumAdapter::init(keystore_options, &config).expect("should init ethereum adapter")
410398
}
411399

412400
#[test]
@@ -465,8 +453,7 @@ mod test {
465453
let wallet = eth_adapter.wallet.clone();
466454
let response = ewt_sign(&wallet.unwrap(), &eth_adapter.keystore_pwd, &payload)
467455
.expect("failed to generate ewt signature");
468-
let expected =
469-
"eyJ0eXBlIjoiSldUIiwiYWxnIjoiRVRIIn0.eyJpZCI6ImF3ZXNvbWVWYWxpZGF0b3IiLCJlcmEiOjEwMDAwMCwiYWRkcmVzcyI6IjB4MmJEZUFGQUU1Mzk0MDY2OURhQTZGNTE5MzczZjY4NmMxZjNkMzM5MyJ9.gGw_sfnxirENdcX5KJQWaEt4FVRvfEjSLD4f3OiPrJIltRadeYP2zWy9T2GYcK5xxD96vnqAw4GebAW7rMlz4xw";
456+
let expected = "eyJ0eXBlIjoiSldUIiwiYWxnIjoiRVRIIn0.eyJpZCI6ImF3ZXNvbWVWYWxpZGF0b3IiLCJlcmEiOjEwMDAwMCwiYWRkcmVzcyI6IjB4MmJEZUFGQUU1Mzk0MDY2OURhQTZGNTE5MzczZjY4NmMxZjNkMzM5MyJ9.gGw_sfnxirENdcX5KJQWaEt4FVRvfEjSLD4f3OiPrJIltRadeYP2zWy9T2GYcK5xxD96vnqAw4GebAW7rMlz4xw";
470457
assert_eq!(response, expected, "generated wrong ewt signature");
471458

472459
let expected_verification_response = r#"VerifyPayload { from: ValidatorId([43, 222, 175, 174, 83, 148, 6, 105, 218, 166, 245, 25, 55, 63, 104, 108, 31, 61, 51, 147]), payload: Payload { id: "awesomeValidator", era: 100000, address: "0x2bDeAFAE53940669DaA6F519373f686c1f3d3393", identity: None } }"#;

primitives/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ serde = { version = "^1.0", features = ['derive'] }
1212
serde_json = "1.0"
1313
serde-hex = "0.1.0"
1414
toml = "0.5"
15+
# Logging
16+
slog = { version = "^2.5.2" , features = ["max_level_trace"] }
17+
slog-term = "^2.4.1"
18+
slog-async = "^2.3.0"
1519
# Domain
1620
chrono = { version = "0.4", features = ["serde"] }
1721
time = "0.1.42"

primitives/src/adapter.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::channel_validator::ChannelValidator;
2-
use crate::{Channel, Config, DomainError, ValidatorId};
2+
use crate::{Channel, DomainError, ValidatorId};
33
use std::collections::HashMap;
44
use std::convert::From;
55
use std::error::Error;
@@ -41,14 +41,10 @@ impl From<DomainError> for AdapterError {
4141
}
4242
}
4343

44-
#[derive(Debug, Clone)]
45-
pub enum AdapterOptions {
46-
DummAdapter {
47-
dummy_identity: ValidatorId,
48-
dummy_auth: HashMap<String, ValidatorId>,
49-
dummy_auth_tokens: HashMap<String, String>,
50-
},
51-
EthereumAdapter(KeystoreOptions),
44+
pub struct DummyAdapterOptions {
45+
pub dummy_identity: ValidatorId,
46+
pub dummy_auth: HashMap<String, ValidatorId>,
47+
pub dummy_auth_tokens: HashMap<String, String>,
5248
}
5349

5450
#[derive(Debug, Clone)]
@@ -63,12 +59,7 @@ pub struct Session {
6359
pub uid: ValidatorId,
6460
}
6561

66-
pub trait Adapter: ChannelValidator + Clone + Debug + Send {
67-
type Output;
68-
69-
/// Initialize adapter
70-
fn init(opts: AdapterOptions, config: &Config) -> AdapterResult<Self::Output>;
71-
62+
pub trait Adapter: ChannelValidator + Send + Clone + Debug {
7263
/// Unlock adapter
7364
fn unlock(&mut self) -> AdapterResult<()>;
7465

primitives/src/channel.rs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -131,49 +131,6 @@ impl<'a> IntoIterator for &'a SpecValidators {
131131
}
132132
}
133133

134-
pub struct ChannelListParams {
135-
/// page to show, should be >= 1
136-
pub page: u64,
137-
/// channels limit per page, should be >= 1
138-
pub limit: u32,
139-
/// filters `valid_until` to be `>= valid_until_ge`
140-
pub valid_until_ge: DateTime<Utc>,
141-
/// filters the channels containing a specific validator if provided
142-
pub validator: Option<String>,
143-
/// Ensures that this struct can only be created by calling `new()`
144-
_secret: (),
145-
}
146-
147-
impl ChannelListParams {
148-
pub fn new(
149-
valid_until_ge: DateTime<Utc>,
150-
limit: u32,
151-
page: u64,
152-
validator: Option<&str>,
153-
) -> Result<Self, ChannelError> {
154-
if page < 1 {
155-
return Err(ChannelError::InvalidArgument(
156-
"Page should be >= 1".to_string(),
157-
));
158-
}
159-
160-
if limit < 1 {
161-
return Err(ChannelError::InvalidArgument(
162-
"Limit should be >= 1".to_string(),
163-
));
164-
}
165-
166-
let validator = validator.and_then(|s| if s.is_empty() { None } else { Some(s.into()) });
167-
Ok(Self {
168-
valid_until_ge,
169-
page,
170-
limit,
171-
validator,
172-
_secret: (),
173-
})
174-
}
175-
}
176-
177134
#[derive(Debug, PartialEq, Eq)]
178135
pub enum ChannelError {
179136
InvalidArgument(String),

primitives/src/util.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,118 @@ pub mod serde {
6666
}
6767
}
6868
}
69+
70+
pub mod logging {
71+
use slog::{Drain, OwnedKVList, Record, KV};
72+
use slog_term::{
73+
timestamp_local, CompactFormatSerializer, CountingWriter, Decorator, RecordDecorator,
74+
Serializer, ThreadSafeTimestampFn,
75+
};
76+
use std::cell::RefCell;
77+
use std::{io, io::Write};
78+
79+
pub use slog_async::Async;
80+
pub use slog_term::TermDecorator;
81+
82+
pub struct PrefixedCompactFormat<D>
83+
where
84+
D: Decorator,
85+
{
86+
decorator: D,
87+
history: RefCell<Vec<(Vec<u8>, Vec<u8>)>>,
88+
fn_timestamp: Box<dyn ThreadSafeTimestampFn<Output = io::Result<()>>>,
89+
prefix: String,
90+
}
91+
92+
impl<D> Drain for PrefixedCompactFormat<D>
93+
where
94+
D: Decorator,
95+
{
96+
type Ok = ();
97+
type Err = io::Error;
98+
99+
fn log(&self, record: &Record<'_>, values: &OwnedKVList) -> Result<Self::Ok, Self::Err> {
100+
self.format_compact(record, values)
101+
}
102+
}
103+
104+
impl<D> PrefixedCompactFormat<D>
105+
where
106+
D: Decorator,
107+
{
108+
pub fn new(prefix: &str, d: D) -> PrefixedCompactFormat<D> {
109+
Self {
110+
fn_timestamp: Box::new(timestamp_local),
111+
decorator: d,
112+
history: RefCell::new(vec![]),
113+
prefix: prefix.to_owned(),
114+
}
115+
}
116+
117+
fn format_compact(&self, record: &Record<'_>, values: &OwnedKVList) -> io::Result<()> {
118+
self.decorator.with_record(record, values, |decorator| {
119+
let indent = {
120+
let mut history_ref = self.history.borrow_mut();
121+
let mut serializer = CompactFormatSerializer::new(decorator, &mut *history_ref);
122+
123+
values.serialize(record, &mut serializer)?;
124+
125+
serializer.finish()?
126+
};
127+
128+
decorator.start_whitespace()?;
129+
130+
for _ in 0..indent {
131+
write!(decorator, " ")?;
132+
}
133+
134+
let comma_needed =
135+
print_msg_header(&self.prefix, &*self.fn_timestamp, decorator, record)?;
136+
{
137+
let mut serializer = Serializer::new(decorator, comma_needed, false);
138+
139+
record.kv().serialize(record, &mut serializer)?;
140+
141+
serializer.finish()?;
142+
}
143+
144+
decorator.start_whitespace()?;
145+
writeln!(decorator)?;
146+
147+
decorator.flush()?;
148+
149+
Ok(())
150+
})
151+
}
152+
}
153+
154+
pub fn print_msg_header(
155+
prefix: &str,
156+
fn_timestamp: &dyn ThreadSafeTimestampFn<Output = io::Result<()>>,
157+
mut rd: &mut dyn RecordDecorator,
158+
record: &Record<'_>,
159+
) -> io::Result<bool> {
160+
rd.start_timestamp()?;
161+
fn_timestamp(&mut rd)?;
162+
163+
rd.start_whitespace()?;
164+
write!(rd, " ")?;
165+
166+
rd.start_level()?;
167+
write!(rd, "{}", record.level().as_short_str())?;
168+
169+
rd.start_whitespace()?;
170+
write!(rd, " ")?;
171+
172+
rd.start_msg()?;
173+
write!(rd, "{}:", prefix)?;
174+
175+
rd.start_whitespace()?;
176+
write!(rd, " ")?;
177+
178+
rd.start_msg()?;
179+
let mut count_rd = CountingWriter::new(&mut rd);
180+
write!(count_rd, "{}", record.msg())?;
181+
Ok(count_rd.count() != 0)
182+
}
183+
}

sentry/Cargo.toml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
[package]
22
name = "sentry"
33
version = "0.1.0"
4-
authors = ["Samparsky"]
4+
authors = ["Omidiora Samuel <[email protected]>"]
55
edition = "2018"
66

7-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8-
97
[dependencies]
8+
# Futures
9+
futures-preview = { version = "=0.3.0-alpha.19", features = ["compat"]}
10+
# Primitives
1011
primitives = {path = "../primitives"}
12+
adapter = { version = "0.1", path = "../adapter" }
13+
chrono = { version = "0.4", features = ["serde"] }
1114
# CLI
1215
clap = "2.33.0"
16+
# Server
17+
tokio = "0.2.0-alpha.6"
18+
hyper = {version = "=0.13.0-alpha.4", features = ["unstable-stream"]}
19+
regex = "1"
1320
# Logger
1421
slog = { version = "^2.2.3" , features = ["max_level_trace"] }
15-
slog-term = "^2.4.0"
16-
slog-async = "^2.3.0"
17-
# Database
18-
diesel = { version = "1.3", features = ["postgres"] }
22+
# Serde
23+
serde = {version = "^1.0", features = ['derive']}
24+
serde_json = "^1.0"
25+
serde_urlencoded = "0.6.1"
File renamed without changes.

0 commit comments

Comments
 (0)