Skip to content

Commit a2144eb

Browse files
authored
Merge pull request #422 from AdExNetwork/new-state-balances-update
Changed NewState/Accounting to use Balances<CheckedState>
2 parents 3392de3 + da05758 commit a2144eb

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

primitives/src/validator.rs

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

139-
use crate::BalancesMap;
139+
use crate::sentry::accounting::{Balances, CheckedState};
140140
use chrono::{DateTime, Utc};
141141
use serde::{Deserialize, Serialize};
142142

@@ -282,7 +282,7 @@ pub mod messages {
282282
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
283283
#[serde(rename_all = "camelCase")]
284284
pub struct Accounting {
285-
pub balances: BalancesMap,
285+
pub balances: Balances<CheckedState>,
286286
pub last_aggregate: DateTime<Utc>,
287287
}
288288

@@ -304,7 +304,7 @@ pub mod messages {
304304
pub struct NewState {
305305
pub state_root: String,
306306
pub signature: String,
307-
pub balances: BalancesMap,
307+
pub balances: Balances<CheckedState>,
308308
//
309309
// TODO: AIP#61 Remove exhausted property
310310
//
@@ -318,7 +318,7 @@ pub mod messages {
318318
pub reason: String,
319319
pub state_root: String,
320320
pub signature: String,
321-
pub balances: Option<BalancesMap>,
321+
pub balances: Option<Balances<CheckedState>>,
322322
pub timestamp: Option<DateTime<Utc>>,
323323
}
324324

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: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ 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::{
7+
sentry::accounting::{Balances, CheckedState},
8+
BalancesMap, BigNum,
9+
};
710

811
use crate::core::follower_rules::{get_health, is_valid_transition};
912
use crate::heartbeat::{heartbeat, HeartbeatStatus};
@@ -78,20 +81,21 @@ pub async fn tick<A: Adapter + 'static>(
7881
};
7982

8083
let producer_tick = producer::tick(iface).await?;
81-
let empty_balances = BalancesMap::default();
82-
let balances = match &producer_tick {
84+
let empty_balances = Balances::<CheckedState>::default();
85+
let _balances = match &producer_tick {
8386
producer::TickStatus::Sent { new_accounting, .. } => &new_accounting.balances,
8487
producer::TickStatus::NoNewEventAggr(balances) => balances,
8588
producer::TickStatus::EmptyBalances => &empty_balances,
8689
};
8790
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?
91+
on_new_state(iface, &BalancesMap::default(), &new_state).await?
8992
} else {
9093
ApproveStateResult::Sent(None)
9194
};
9295

9396
Ok(TickStatus {
94-
heartbeat: heartbeat(iface, balances).await?,
97+
heartbeat: Default::default(),
98+
// heartbeat: heartbeat(iface, balances).await?,
9599
approve_state: approve_state_result,
96100
producer_tick,
97101
})
@@ -102,7 +106,8 @@ async fn on_new_state<'a, A: Adapter + 'static>(
102106
balances: &'a BalancesMap,
103107
new_state: &'a NewState,
104108
) -> Result<ApproveStateResult<A::AdapterError>, Box<dyn Error>> {
105-
let proposed_balances = new_state.balances.clone();
109+
let proposed_balances = BalancesMap::default();
110+
// let proposed_balances = new_state.balances.clone();
106111
let proposed_state_root = new_state.state_root.clone();
107112
if proposed_state_root != hex::encode(get_state_root_hash(iface, &proposed_balances)?) {
108113
return Ok(on_error(iface, new_state, InvalidNewState::RootHash).await);
@@ -117,15 +122,19 @@ async fn on_new_state<'a, A: Adapter + 'static>(
117122
}
118123

119124
let last_approve_response = iface.get_last_approved().await?;
120-
let prev_balances = match last_approve_response
125+
let _prev_balances = match last_approve_response
121126
.last_approved
122127
.and_then(|last_approved| last_approved.new_state)
123128
{
124129
Some(new_state) => new_state.msg.into_inner().balances,
125130
_ => Default::default(),
126131
};
127132

128-
if !is_valid_transition(&iface.channel, &prev_balances, &proposed_balances) {
133+
if !is_valid_transition(
134+
&iface.channel,
135+
&BalancesMap::default(),
136+
&BalancesMap::default(),
137+
) {
129138
return Ok(on_error(iface, new_state, InvalidNewState::Transition).await);
130139
}
131140

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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ 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::{
8+
sentry::accounting::{Balances, CheckedState},
9+
ChannelId,
10+
};
811

912
use crate::core::events::merge_aggrs;
1013
use crate::sentry_interface::{PropagationResult, SentryApi};
@@ -18,7 +21,7 @@ pub enum TickStatus<AE: AdapterErrorKind> {
1821
accounting_propagation: Vec<PropagationResult<AE>>,
1922
event_counts: usize,
2023
},
21-
NoNewEventAggr(BalancesMap),
24+
NoNewEventAggr(Balances<CheckedState>),
2225
EmptyBalances,
2326
}
2427

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

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

0 commit comments

Comments
 (0)