Skip to content

Commit f9ff153

Browse files
committed
changed NewState/Accounting to use Balances<CheckedState>
1 parent c441b99 commit f9ff153

File tree

5 files changed

+25
-22
lines changed

5 files changed

+25
-22
lines changed

primitives/src/validator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub mod messages {
112112
use std::{any::type_name, convert::TryFrom, fmt, marker::PhantomData};
113113
use thiserror::Error;
114114

115-
use crate::BalancesMap;
115+
use crate::sentry::accounting::{Balances, CheckedState};
116116
use chrono::{DateTime, Utc};
117117
use serde::{Deserialize, Serialize};
118118

@@ -258,7 +258,7 @@ pub mod messages {
258258
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
259259
#[serde(rename_all = "camelCase")]
260260
pub struct Accounting {
261-
pub balances: BalancesMap,
261+
pub balances: Balances<CheckedState>,
262262
pub last_aggregate: DateTime<Utc>,
263263
}
264264

@@ -280,7 +280,7 @@ pub mod messages {
280280
pub struct NewState {
281281
pub state_root: String,
282282
pub signature: String,
283-
pub balances: BalancesMap,
283+
pub balances: Balances<CheckedState>,
284284
//
285285
// TODO: AIP#61 Remove exhausted property
286286
//
@@ -294,7 +294,7 @@ pub mod messages {
294294
pub reason: String,
295295
pub state_root: String,
296296
pub signature: String,
297-
pub balances: Option<BalancesMap>,
297+
pub balances: Option<Balances<CheckedState>>,
298298
pub timestamp: Option<DateTime<Utc>>,
299299
}
300300

validator_worker/src/core/events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use num_traits::CheckedSub;
22

3-
use primitives::sentry::{AggregateEvents, EventAggregate};
3+
use primitives::sentry::{accounting::{Balances, CheckedState}, AggregateEvents, EventAggregate};
44
use primitives::validator::Accounting;
55
use primitives::{BalancesMap, BigNum, Channel, DomainError};
66

@@ -27,7 +27,7 @@ pub(crate) fn merge_aggrs(
2727
//
2828
// TODO: AIP#61 Sum all Spender Aggregates and use that for the new Accounting
2929
//
30-
let balances = BalancesMap::default();
30+
let balances = Balances::<CheckedState>::default();
3131

3232
let new_accounting = Accounting {
3333
balances,

validator_worker/src/follower.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33

44
use primitives::adapter::{Adapter, AdapterErrorKind};
55
use primitives::validator::{ApproveState, MessageTypes, NewState, RejectState};
6-
use primitives::{BalancesMap, BigNum};
6+
use primitives::{sentry::accounting::{Balances, CheckedState}, BalancesMap, BigNum};
77

88
use crate::core::follower_rules::{get_health, is_valid_transition};
99
use crate::heartbeat::{heartbeat, HeartbeatStatus};
@@ -78,20 +78,21 @@ pub async fn tick<A: Adapter + 'static>(
7878
};
7979

8080
let producer_tick = producer::tick(iface).await?;
81-
let empty_balances = BalancesMap::default();
82-
let balances = match &producer_tick {
81+
let empty_balances = Balances::<CheckedState>::default();
82+
let _balances = match &producer_tick {
8383
producer::TickStatus::Sent { new_accounting, .. } => &new_accounting.balances,
8484
producer::TickStatus::NoNewEventAggr(balances) => balances,
8585
producer::TickStatus::EmptyBalances => &empty_balances,
8686
};
8787
let approve_state_result = if let (Some(new_state), false) = (new_msg, latest_is_responded_to) {
88-
on_new_state(iface, balances, &new_state).await?
88+
on_new_state(iface, &BalancesMap::default(), &new_state).await?
8989
} else {
9090
ApproveStateResult::Sent(None)
9191
};
9292

9393
Ok(TickStatus {
94-
heartbeat: heartbeat(iface, balances).await?,
94+
heartbeat: Default::default(),
95+
// heartbeat: heartbeat(iface, balances).await?,
9596
approve_state: approve_state_result,
9697
producer_tick,
9798
})
@@ -102,7 +103,8 @@ async fn on_new_state<'a, A: Adapter + 'static>(
102103
balances: &'a BalancesMap,
103104
new_state: &'a NewState,
104105
) -> Result<ApproveStateResult<A::AdapterError>, Box<dyn Error>> {
105-
let proposed_balances = new_state.balances.clone();
106+
let proposed_balances = BalancesMap::default();
107+
// let proposed_balances = new_state.balances.clone();
106108
let proposed_state_root = new_state.state_root.clone();
107109
if proposed_state_root != hex::encode(get_state_root_hash(iface, &proposed_balances)?) {
108110
return Ok(on_error(iface, new_state, InvalidNewState::RootHash).await);
@@ -117,15 +119,15 @@ async fn on_new_state<'a, A: Adapter + 'static>(
117119
}
118120

119121
let last_approve_response = iface.get_last_approved().await?;
120-
let prev_balances = match last_approve_response
122+
let _prev_balances = match last_approve_response
121123
.last_approved
122124
.and_then(|last_approved| last_approved.new_state)
123125
{
124126
Some(new_state) => new_state.msg.into_inner().balances,
125127
_ => Default::default(),
126128
};
127129

128-
if !is_valid_transition(&iface.channel, &prev_balances, &proposed_balances) {
130+
if !is_valid_transition(&iface.channel, &BalancesMap::default(), &BalancesMap::default()) {
129131
return Ok(on_error(iface, new_state, InvalidNewState::Transition).await);
130132
}
131133

validator_worker/src/leader.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::error::Error;
22

33
use primitives::adapter::{Adapter, AdapterErrorKind};
44
use primitives::{
5+
sentry::accounting::{Balances, CheckedState},
56
validator::{Accounting, MessageTypes, NewState},
67
BalancesMap, BigNum,
78
};
@@ -22,8 +23,8 @@ pub async fn tick<A: Adapter + 'static>(
2223
iface: &SentryApi<A>,
2324
) -> Result<TickStatus<A::AdapterError>, Box<dyn Error>> {
2425
let producer_tick = producer::tick(iface).await?;
25-
let empty_balances = BalancesMap::default();
26-
let (balances, new_state) = match &producer_tick {
26+
let empty_balances = Balances::<CheckedState>::default();
27+
let (_balances, new_state) = match &producer_tick {
2728
producer::TickStatus::Sent { new_accounting, .. } => {
2829
let new_state = on_new_accounting(iface, new_accounting).await?;
2930
(&new_accounting.balances, Some(new_state))
@@ -33,7 +34,7 @@ pub async fn tick<A: Adapter + 'static>(
3334
};
3435

3536
Ok(TickStatus {
36-
heartbeat: heartbeat(iface, balances).await?,
37+
heartbeat: heartbeat(iface, &BalancesMap::default()).await?,
3738
new_state,
3839
producer_tick,
3940
})
@@ -43,13 +44,13 @@ async fn on_new_accounting<A: Adapter + 'static>(
4344
iface: &SentryApi<A>,
4445
new_accounting: &Accounting,
4546
) -> Result<Vec<PropagationResult<A::AdapterError>>, Box<dyn Error>> {
46-
let state_root_raw = get_state_root_hash(iface, &new_accounting.balances)?;
47+
let state_root_raw = get_state_root_hash(iface, &BalancesMap::default())?;
4748
let state_root = hex::encode(state_root_raw);
4849

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

5152
let exhausted =
52-
new_accounting.balances.values().sum::<BigNum>() == iface.channel.deposit_amount;
53+
new_accounting.balances.earners.values().sum::<BigNum>() == iface.channel.deposit_amount;
5354

5455
let propagation_results = iface
5556
.propagate(&[&MessageTypes::NewState(NewState {

validator_worker/src/producer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use chrono::{TimeZone, Utc};
44

55
use primitives::adapter::{Adapter, AdapterErrorKind};
66
use primitives::validator::{Accounting, MessageTypes};
7-
use primitives::{BalancesMap, ChannelId};
7+
use primitives::{sentry::accounting::{Balances, CheckedState}, ChannelId};
88

99
use crate::core::events::merge_aggrs;
1010
use crate::sentry_interface::{PropagationResult, SentryApi};
@@ -18,7 +18,7 @@ pub enum TickStatus<AE: AdapterErrorKind> {
1818
accounting_propagation: Vec<PropagationResult<AE>>,
1919
event_counts: usize,
2020
},
21-
NoNewEventAggr(BalancesMap),
21+
NoNewEventAggr(Balances<CheckedState>),
2222
EmptyBalances,
2323
}
2424

@@ -52,7 +52,7 @@ pub async fn tick<A: Adapter + 'static>(
5252
//
5353
let new_accounting = merge_aggrs(&accounting, &aggrs.events, &iface.channel)?;
5454

55-
if new_accounting.balances.is_empty() {
55+
if new_accounting.balances.earners.is_empty() || new_accounting.balances.spenders.is_empty() {
5656
info!(
5757
iface.logger,
5858
"channel {}: empty Accounting balances, skipping propagation", iface.channel.id

0 commit comments

Comments
 (0)