Skip to content

Commit 1df4fa8

Browse files
authored
Issue #81 & #74 message repository (#82)
* validator - domain - validator - MessageRepository * validator - persistence - validator - memory - dummy MemoryMessageRepository * validator - domain - validator - MessageRepository - make `add` a method * memory-repository - extract generic impl to be used in MemoryRepositories * memory-repository - remove ExistCmp * Cargo.toml - add `memory-repository` to workspace * memory-repository - add denies: `clippy` & `rust_2018_idioms` * sentry - Remove `derive(Debug)` from ChannelResource & ChannelRepository * memory - make `cmp` function `Arc` & `Send` + `Sync` for thread safety requirements * sentry - use `memory-repository` * memory-repository - impl `list_all` & cleanup `list` * [test] memory-repository - list_all * memory-repository: - cmp_value instead of direct record search - `find()` method * sentry - persistence - channel - memory - refactor to use `MemoryRepository` * validator - Cargo.toml - add `memory-repository` * memory-repository - add comment for the `new` method * domain - validator - message - `derive(Clone)` * sentry - persistence - channel - memory - fix `clippy` warnings * validator - domain - validator - MessageRepository add generic for State * validator - persistence - validator - MemoryMessageRepository `add()` impl * domain - validator - ValidatorId * domain - validator - message: - add constants for all types - Message - impl `is_type` & `is_types` * validator - domain - validator - `latest` method - persistence - validator - MemoryMessageRepository - `latest` method - persistence - validator - MemoryMessageRepository - `add` method fix - persistence - validator - MemoryMessage struct * domain - validator - add TODO for ValidatorDesc * validator - persistence - validator - fix comment
1 parent 757a968 commit 1df4fa8

File tree

17 files changed

+468
-98
lines changed

17 files changed

+468
-98
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ members = [
55
"domain",
66
"sentry",
77
"validator",
8+
"memory-repository",
89
]

domain/src/validator.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ use crate::BigNum;
66

77
pub mod message;
88

9+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
10+
#[serde(transparent)]
11+
pub struct ValidatorId(String);
12+
913
#[derive(Serialize, Deserialize, Debug, Clone)]
1014
#[serde(rename_all = "camelCase")]
1115
pub struct ValidatorDesc {
16+
// @TODO: Replace id `String` with `ValidatorId` https://github.com/AdExNetwork/adex-validator-stack-rust/issues/83
1217
pub id: String,
1318
pub url: String,
1419
pub fee: BigNum,

domain/src/validator/message.rs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
use std::fmt;
2+
13
use chrono::{DateTime, Utc};
24
use serde::de::DeserializeOwned;
3-
use serde::export::fmt::Debug;
45
use serde::{Deserialize, Serialize};
56

67
use crate::BalancesMap;
78

89
pub trait State {
9-
type Signature: DeserializeOwned + Serialize + Debug;
10-
type StateRoot: DeserializeOwned + Serialize + Debug;
10+
type Signature: DeserializeOwned + Serialize + fmt::Debug + Clone;
11+
type StateRoot: DeserializeOwned + Serialize + fmt::Debug + Clone;
1112
}
1213

13-
#[derive(Serialize, Deserialize, Debug)]
14+
#[derive(Serialize, Deserialize, Clone)]
1415
#[serde(tag = "type")]
1516
pub enum Message<S: State> {
1617
ApproveState(ApproveState<S>),
@@ -20,36 +21,72 @@ pub enum Message<S: State> {
2021
Accounting(Accounting),
2122
}
2223

23-
#[derive(Serialize, Deserialize, Debug)]
24+
impl<S: State> Message<S> {
25+
pub fn is_type(&self, message_type: &MessageType) -> bool {
26+
assert!(ALL_TYPES.contains(&message_type));
27+
28+
let self_message_type = match self {
29+
Message::ApproveState(_) => &TYPE_APPROVE,
30+
Message::NewState(_) => &TYPE_NEW,
31+
Message::RejectState(_) => &TYPE_REJECT,
32+
Message::Heartbeat(_) => &TYPE_HEARTBEAT,
33+
Message::Accounting(_) => &TYPE_ACCOUNTING,
34+
};
35+
36+
self_message_type == message_type
37+
}
38+
39+
pub fn is_types(&self, types: &[&MessageType]) -> bool {
40+
types.iter().any(|&m_type| self.is_type(m_type))
41+
}
42+
}
43+
44+
#[derive(Debug, PartialEq, Eq)]
45+
pub struct MessageType(&'static str);
46+
47+
pub const TYPE_APPROVE: MessageType = MessageType("approve");
48+
pub const TYPE_NEW: MessageType = MessageType("new");
49+
pub const TYPE_REJECT: MessageType = MessageType("reject");
50+
pub const TYPE_HEARTBEAT: MessageType = MessageType("heartbeat");
51+
pub const TYPE_ACCOUNTING: MessageType = MessageType("accounting");
52+
pub const ALL_TYPES: [&MessageType; 5] = [
53+
&TYPE_APPROVE,
54+
&TYPE_NEW,
55+
&TYPE_REJECT,
56+
&TYPE_HEARTBEAT,
57+
&TYPE_ACCOUNTING,
58+
];
59+
60+
#[derive(Serialize, Deserialize, Debug, Clone)]
2461
#[serde(rename_all = "camelCase")]
2562
pub struct ApproveState<S: State> {
2663
state_root: S::StateRoot,
2764
signature: S::Signature,
2865
is_healthy: bool,
2966
}
3067

31-
#[derive(Serialize, Deserialize, Debug)]
68+
#[derive(Serialize, Deserialize, Debug, Clone)]
3269
#[serde(rename_all = "camelCase")]
3370
pub struct NewState<S: State> {
3471
state_root: S::StateRoot,
3572
signature: S::Signature,
3673
balances: BalancesMap,
3774
}
3875

39-
#[derive(Serialize, Deserialize, Debug)]
76+
#[derive(Serialize, Deserialize, Debug, Clone)]
4077
#[serde(rename_all = "camelCase")]
4178
pub struct RejectState {
4279
reason: String,
4380
}
4481

45-
#[derive(Serialize, Deserialize, Debug)]
82+
#[derive(Serialize, Deserialize, Debug, Clone)]
4683
#[serde(rename_all = "camelCase")]
4784
pub struct Heartbeat<S: State> {
4885
signature: S::Signature,
4986
timestamp: DateTime<Utc>,
5087
}
5188

52-
#[derive(Serialize, Deserialize, Debug)]
89+
#[derive(Serialize, Deserialize, Debug, Clone)]
5390
#[serde(rename_all = "camelCase")]
5491
pub struct Accounting {
5592
#[serde(rename = "last_ev_aggr")]

memory-repository/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "memory-repository"
3+
version = "0.1.0"
4+
authors = ["Lachezar Lechev <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
domain = { version = "0.1", path = "../domain", features = ["repositories"] }
11+
# Futures
12+
futures-preview = { version = "=0.3.0-alpha.16", features = ["compat"] }

0 commit comments

Comments
 (0)