Skip to content

Commit fd972a7

Browse files
authored
Merge pull request #289 from samparsky/couple-fix-original-message
Couple of fixes PR #277; original message
2 parents bd4ba8c + 2120ec2 commit fd972a7

File tree

9 files changed

+39
-64
lines changed

9 files changed

+39
-64
lines changed

adapter/src/ethereum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ mod test {
536536
.expect("Valid ValidatorId"),
537537
payload: Payload {
538538
id: "awesomeValidator".to_string(),
539-
era: 100000,
539+
era: 100_000,
540540
address: "0x2bDeAFAE53940669DaA6F519373f686c1f3d3393".to_string(),
541541
identity: None,
542542
},

adapter/src/ethereum/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ impl fmt::Display for VerifyError {
6666
PublicKeyRecovery(err) => {
6767
write!(f, "Recovering the public key from the signature: {}", err)
6868
}
69-
StateRootDecoding(err) => write!(f, "Decoding the hex of the state root: {}", err),
70-
SignatureDecoding(err) => write!(f, "Decoding the hex of the signature: {}", err),
69+
StateRootDecoding(err) => write!(f, "Decoding state root: {}", err),
70+
SignatureDecoding(err) => write!(f, "Decoding signature: {}", err),
7171
SignatureNotPrefixed => write!(f, "Signature is not prefixed with `0x`"),
7272
}
7373
}

primitives/src/adapter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<AE: AdapterErrorKind> fmt::Display for Error<AE> {
3737
Error::Authentication(error) => write!(f, "Authentication: {}", error),
3838
Error::Authorization(error) => write!(f, "Authorization: {}", error),
3939
Error::InvalidChannel(error) => write!(f, "{}", error),
40-
Error::Adapter(error) => write!(f, "Adapter specific: {}", error),
40+
Error::Adapter(error) => write!(f, "Adapter: {}", error),
4141
Error::Domain(error) => write!(f, "Domain: {}", error),
4242
Error::LockedWallet => write!(f, "You must `.unlock()` the wallet first"),
4343
}

validator_worker/src/core/events.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(crate) fn merge_aggrs(
1010
accounting: &Accounting,
1111
aggregates: &[EventAggregate],
1212
channel: &Channel,
13-
) -> Result<(BalancesMap, Accounting), DomainError> {
13+
) -> Result<Accounting, DomainError> {
1414
let deposit = channel.deposit_amount.clone();
1515

1616
let last_event_aggregate = [accounting.last_event_aggregate]
@@ -35,10 +35,10 @@ pub(crate) fn merge_aggrs(
3535
let new_accounting = Accounting {
3636
last_event_aggregate,
3737
balances_before_fees,
38-
balances: balances.clone(),
38+
balances,
3939
};
4040

41-
Ok((balances, new_accounting))
41+
Ok(new_accounting)
4242
}
4343

4444
fn merge_payouts_into_balances<'a, T: Iterator<Item = &'a AggregateEvents>>(
@@ -115,11 +115,9 @@ mod test {
115115
balances: BalancesMap::default(),
116116
};
117117

118-
let (balances, new_accounting) =
119-
merge_aggrs(&acc, &[gen_ev_aggr(5, &IDS["publisher"])], &channel)
120-
.expect("Something went wrong");
118+
let new_accounting = merge_aggrs(&acc, &[gen_ev_aggr(5, &IDS["publisher"])], &channel)
119+
.expect("Something went wrong");
121120

122-
assert_eq!(balances, new_accounting.balances, "balances is the same");
123121
assert_eq!(
124122
new_accounting.balances_before_fees[&IDS["publisher"]],
125123
150.into(),
@@ -166,11 +164,9 @@ mod test {
166164
balances: BalancesMap::default(),
167165
};
168166

169-
let (balances, new_accounting) =
170-
merge_aggrs(&acc, &[gen_ev_aggr(1_001, &IDS["publisher"])], &channel)
171-
.expect("Something went wrong");
167+
let new_accounting = merge_aggrs(&acc, &[gen_ev_aggr(1_001, &IDS["publisher"])], &channel)
168+
.expect("Something went wrong");
172169

173-
assert_eq!(balances, new_accounting.balances, "balances is the same");
174170
assert_eq!(
175171
new_accounting.balances_before_fees[&IDS["publisher"]],
176172
9_800.into(),

validator_worker/src/error.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub enum TickError {
1111
impl fmt::Display for TickError {
1212
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1313
match self {
14-
TickError::TimedOut(err) => write!(f, "Tick timed out ({})", err),
14+
TickError::TimedOut(err) => write!(f, "Tick TimedOut: ({})", err),
1515
TickError::Tick(err) => write!(f, "Tick: {}", err),
1616
}
1717
}
@@ -31,13 +31,9 @@ impl<AE: AdapterErrorKind> fmt::Display for Error<AE> {
3131
use Error::*;
3232

3333
match self {
34-
SentryApi(err) => write!(f, "Sentry Api: {}", err),
35-
LeaderTick(channel_id, err) => {
36-
write!(f, "Error for Leader tick of {:#?}: {}", channel_id, err)
37-
}
38-
FollowerTick(channel_id, err) => {
39-
write!(f, "Error for Follower tick of {:#?}: {}", channel_id, err)
40-
}
34+
SentryApi(err) => write!(f, "SentryApi: {}", err),
35+
LeaderTick(channel_id, err) => write!(f, "LeaderTick {:#?}: {}", channel_id, err),
36+
FollowerTick(channel_id, err) => write!(f, "FollowerTick {:#?}: {}", channel_id, err),
4137
}
4238
}
4339
}

validator_worker/src/follower.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ impl fmt::Display for InvalidNewState {
3636

3737
#[derive(Debug)]
3838
pub enum ApproveStateResult<AE: AdapterErrorKind> {
39-
Sent(Vec<PropagationResult<AE>>),
40-
/// Conditions for handling the new state haven't been met
41-
NotSent,
39+
/// If None, Conditions for handling the new state haven't been met
40+
Sent(Option<Vec<PropagationResult<AE>>>),
4241
RejectedState {
4342
reason: InvalidNewState,
44-
reject_state_propagation: Vec<PropagationResult<AE>>,
43+
state_root: String,
44+
propagation: Vec<PropagationResult<AE>>,
4545
},
4646
}
4747

@@ -79,13 +79,13 @@ pub async fn tick<A: Adapter + 'static>(
7979

8080
let producer_tick = producer::tick(&iface).await?;
8181
let balances = match &producer_tick {
82-
producer::TickStatus::Sent { balances, .. } => balances,
82+
producer::TickStatus::Sent { new_accounting, .. } => &new_accounting.balances,
8383
producer::TickStatus::NoNewEventAggr(balances) => balances,
8484
};
8585
let approve_state_result = if let (Some(new_state), false) = (new_msg, latest_is_responded_to) {
8686
on_new_state(&iface, &balances, &new_state).await?
8787
} else {
88-
ApproveStateResult::NotSent
88+
ApproveStateResult::Sent(None)
8989
};
9090

9191
Ok(TickStatus {
@@ -147,15 +147,15 @@ async fn on_new_state<'a, A: Adapter + 'static>(
147147
})])
148148
.await;
149149

150-
Ok(ApproveStateResult::Sent(propagation_result))
150+
Ok(ApproveStateResult::Sent(Some(propagation_result)))
151151
}
152152

153153
async fn on_error<'a, A: Adapter + 'static>(
154154
iface: &'a SentryApi<A>,
155155
new_state: &'a NewState,
156156
status: InvalidNewState,
157157
) -> ApproveStateResult<A::AdapterError> {
158-
let reject_state_propagation = iface
158+
let propagation = iface
159159
.propagate(&[&MessageTypes::RejectState(RejectState {
160160
reason: status.to_string(),
161161
state_root: new_state.state_root.clone(),
@@ -168,6 +168,7 @@ async fn on_error<'a, A: Adapter + 'static>(
168168

169169
ApproveStateResult::RejectedState {
170170
reason: status,
171-
reject_state_propagation,
171+
state_root: new_state.state_root.clone(),
172+
propagation,
172173
}
173174
}

validator_worker/src/heartbeat.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ use chrono::{Duration, Utc};
55

66
use adapter::get_signable_state_root;
77
use byteorder::{BigEndian, ByteOrder};
8-
use primitives::adapter::{Adapter, AdapterErrorKind};
8+
use primitives::adapter::Adapter;
99
use primitives::merkle_tree::MerkleTree;
1010
use primitives::validator::{Heartbeat, MessageTypes};
1111
use primitives::{BalancesMap, BigNum, Channel};
1212

1313
use crate::sentry_interface::{PropagationResult, SentryApi};
1414

15-
#[derive(Debug)]
16-
pub enum HeartbeatStatus<AE: AdapterErrorKind> {
17-
Sent(Vec<PropagationResult<AE>>),
18-
NotSent,
19-
}
15+
pub type HeartbeatStatus<A> = Option<Vec<PropagationResult<A>>>;
2016

2117
async fn send_heartbeat<A: Adapter + 'static>(
2218
iface: &SentryApi<A>,
@@ -59,9 +55,9 @@ pub async fn heartbeat<A: Adapter + 'static>(
5955
});
6056

6157
if should_send {
62-
Ok(HeartbeatStatus::Sent(send_heartbeat(&iface).await?))
58+
Ok(Some(send_heartbeat(&iface).await?))
6359
} else {
64-
Ok(HeartbeatStatus::NotSent)
60+
Ok(None)
6561
}
6662
}
6763

validator_worker/src/leader.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,16 @@ use std::error::Error;
22

33
use primitives::adapter::{Adapter, AdapterErrorKind};
44
use primitives::validator::{Accounting, MessageTypes, NewState};
5-
use primitives::BalancesMap;
65

76
use crate::heartbeat::{heartbeat, HeartbeatStatus};
87
use crate::sentry_interface::{PropagationResult, SentryApi};
98
use crate::{get_state_root_hash, producer};
109

11-
#[derive(Debug)]
12-
pub enum NewStateResult<AE: AdapterErrorKind> {
13-
Sent(Vec<PropagationResult<AE>>),
14-
/// Conditions for sending the new state haven't been met
15-
NotSent,
16-
}
17-
1810
#[derive(Debug)]
1911
pub struct TickStatus<AE: AdapterErrorKind> {
2012
pub heartbeat: HeartbeatStatus<AE>,
2113
/// If None, then the conditions for handling a new state haven't been met
22-
pub new_state: NewStateResult<AE>,
14+
pub new_state: Option<Vec<PropagationResult<AE>>>,
2315
pub producer_tick: producer::TickStatus<AE>,
2416
}
2517

@@ -28,15 +20,11 @@ pub async fn tick<A: Adapter + 'static>(
2820
) -> Result<TickStatus<A::AdapterError>, Box<dyn Error>> {
2921
let producer_tick = producer::tick(&iface).await?;
3022
let (balances, new_state) = match &producer_tick {
31-
producer::TickStatus::Sent {
32-
balances,
33-
new_accounting,
34-
..
35-
} => {
36-
let new_state = on_new_accounting(&iface, (balances, new_accounting)).await?;
37-
(balances, new_state)
23+
producer::TickStatus::Sent { new_accounting, .. } => {
24+
let new_state = on_new_accounting(&iface, new_accounting).await?;
25+
(&new_accounting.balances, Some(new_state))
3826
}
39-
producer::TickStatus::NoNewEventAggr(balances) => (balances, NewStateResult::NotSent),
27+
producer::TickStatus::NoNewEventAggr(balances) => (balances, None),
4028
};
4129

4230
Ok(TickStatus {
@@ -48,9 +36,9 @@ pub async fn tick<A: Adapter + 'static>(
4836

4937
async fn on_new_accounting<A: Adapter + 'static>(
5038
iface: &SentryApi<A>,
51-
(balances, new_accounting): (&BalancesMap, &Accounting),
52-
) -> Result<NewStateResult<A::AdapterError>, Box<dyn Error>> {
53-
let state_root_raw = get_state_root_hash(&iface, &balances)?;
39+
new_accounting: &Accounting,
40+
) -> Result<Vec<PropagationResult<A::AdapterError>>, Box<dyn Error>> {
41+
let state_root_raw = get_state_root_hash(&iface, &new_accounting.balances)?;
5442
let state_root = hex::encode(state_root_raw);
5543

5644
let signature = iface.adapter.sign(&state_root)?;
@@ -63,5 +51,5 @@ async fn on_new_accounting<A: Adapter + 'static>(
6351
})])
6452
.await;
6553

66-
Ok(NewStateResult::Sent(propagation_results))
54+
Ok(propagation_results)
6755
}

validator_worker/src/producer.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::sentry_interface::{PropagationResult, SentryApi};
1313
pub enum TickStatus<AE: AdapterErrorKind> {
1414
Sent {
1515
channel: ChannelId,
16-
balances: BalancesMap,
1716
new_accounting: Accounting,
1817
accounting_propagation: Vec<PropagationResult<AE>>,
1918
event_counts: usize,
@@ -40,14 +39,13 @@ pub async fn tick<A: Adapter + 'static>(
4039
.await?;
4140

4241
if !aggrs.events.is_empty() {
43-
let (balances, new_accounting) = merge_aggrs(&accounting, &aggrs.events, &iface.channel)?;
42+
let new_accounting = merge_aggrs(&accounting, &aggrs.events, &iface.channel)?;
4443

4544
let message_types = MessageTypes::Accounting(new_accounting.clone());
4645

4746
Ok(TickStatus::Sent {
4847
channel: iface.channel.id,
4948
accounting_propagation: iface.propagate(&[&message_types]).await,
50-
balances,
5149
new_accounting,
5250
event_counts: aggrs.events.len(),
5351
})

0 commit comments

Comments
 (0)