@@ -4,10 +4,10 @@ use chrono::{DateTime, Utc};
4
4
use num_traits:: CheckedSub ;
5
5
use serde:: { Deserialize , Serialize } ;
6
6
7
- use domain :: balances_map :: get_balances_after_fees_tree;
8
- use domain :: validator :: message :: Accounting ;
9
- use domain :: { BalancesMap , BigNum , Channel , ChannelId , DomainError } ;
10
-
7
+ use crate :: core :: fees :: get_balances_after_fees_tree;
8
+ use primitives :: sentry :: { AggregateEvents , EventAggregate } ;
9
+ use primitives :: validator :: Accounting ;
10
+ use primitives :: { BalancesMap , BigNum , Channel , DomainError } ;
11
11
12
12
#[ allow( dead_code) ]
13
13
fn merge_aggrs (
@@ -25,7 +25,7 @@ fn merge_aggrs(
25
25
. to_owned ( ) ;
26
26
27
27
// Build an intermediary balances representation
28
- let mut balances_before_fees = accounting. pre_fees . clone ( ) ;
28
+ let mut balances_before_fees = accounting. balances_before_fees . clone ( ) ;
29
29
30
30
// Merge in all the aggrs
31
31
for aggr in aggregates {
@@ -38,7 +38,7 @@ fn merge_aggrs(
38
38
39
39
let new_accounting = Accounting {
40
40
last_event_aggregate,
41
- pre_fees : balances_before_fees,
41
+ balances_before_fees,
42
42
balances : balances. clone ( ) ,
43
43
} ;
44
44
@@ -83,20 +83,29 @@ fn merge_payouts_into_balances<'a, T: Iterator<Item = &'a AggregateEvents>>(
83
83
#[ cfg( test) ]
84
84
mod test {
85
85
use super :: * ;
86
- use domain:: channel:: fixtures:: { get_channel, get_channel_spec, ValidatorsOption } ;
87
- use domain:: fixtures:: get_channel_id;
88
- use domain:: validator:: fixtures:: get_validator;
86
+ use primitives:: util:: tests:: prep_db:: {
87
+ DUMMY_CHANNEL , DUMMY_VALIDATOR_FOLLOWER , DUMMY_VALIDATOR_LEADER ,
88
+ } ;
89
+ use primitives:: { Channel , ChannelSpec , ValidatorDesc } ;
89
90
90
91
#[ test]
91
92
fn should_merge_event_aggrs_and_apply_fees ( ) {
92
93
// fees: 100
93
- // deposit: 10000
94
- let leader = get_validator ( "one" , Some ( 50 . into ( ) ) ) ;
95
- let follower = get_validator ( "two" , Some ( 50 . into ( ) ) ) ;
94
+ // deposit: 10 000
95
+ let leader = ValidatorDesc {
96
+ fee : 50 . into ( ) ,
97
+ ..DUMMY_VALIDATOR_LEADER . clone ( )
98
+ } ;
99
+ let follower = ValidatorDesc {
100
+ fee : 50 . into ( ) ,
101
+ ..DUMMY_VALIDATOR_FOLLOWER . clone ( )
102
+ } ;
96
103
97
- let spec = get_channel_spec ( ValidatorsOption :: Pair { leader, follower } ) ;
98
- let mut channel = get_channel ( "channel" , & None , Some ( spec) ) ;
99
- channel. deposit_amount = 10_000 . into ( ) ;
104
+ let mut channel = Channel {
105
+ deposit_amount : 10_000 . into ( ) ,
106
+ ..DUMMY_CHANNEL . clone ( )
107
+ } ;
108
+ channel. spec . validators = [ leader, follower] . into ( ) ;
100
109
101
110
let balances_before_fees: BalancesMap =
102
111
vec ! [ ( "a" . to_string( ) , 100 . into( ) ) , ( "b" . to_string( ) , 200 . into( ) ) ]
@@ -105,7 +114,7 @@ mod test {
105
114
106
115
let acc = Accounting {
107
116
last_event_aggregate : Utc :: now ( ) ,
108
- pre_fees : balances_before_fees,
117
+ balances_before_fees,
109
118
balances : BalancesMap :: default ( ) ,
110
119
} ;
111
120
@@ -114,7 +123,7 @@ mod test {
114
123
115
124
assert_eq ! ( balances, new_accounting. balances, "balances is the same" ) ;
116
125
assert_eq ! (
117
- new_accounting. pre_fees [ "a" ] ,
126
+ new_accounting. balances_before_fees [ "a" ] ,
118
127
150 . into( ) ,
119
128
"balance of recipient incremented accordingly"
120
129
) ;
@@ -127,12 +136,24 @@ mod test {
127
136
128
137
#[ test]
129
138
fn should_never_allow_exceeding_the_deposit ( ) {
130
- let leader = get_validator ( "one" , Some ( 50 . into ( ) ) ) ;
131
- let follower = get_validator ( "two" , Some ( 50 . into ( ) ) ) ;
139
+ let leader = ValidatorDesc {
140
+ fee : 50 . into ( ) ,
141
+ ..DUMMY_VALIDATOR_LEADER . clone ( )
142
+ } ;
143
+ let follower = ValidatorDesc {
144
+ fee : 50 . into ( ) ,
145
+ ..DUMMY_VALIDATOR_FOLLOWER . clone ( )
146
+ } ;
132
147
133
- let spec = get_channel_spec ( ValidatorsOption :: Pair { leader, follower } ) ;
134
- let mut channel = get_channel ( "channel" , & None , Some ( spec) ) ;
135
- channel. deposit_amount = 10_000 . into ( ) ;
148
+ let spec = ChannelSpec {
149
+ validators : [ leader, follower] . into ( ) ,
150
+ ..DUMMY_CHANNEL . spec . clone ( )
151
+ } ;
152
+ let channel = Channel {
153
+ deposit_amount : 10_000 . into ( ) ,
154
+ spec,
155
+ ..DUMMY_CHANNEL . clone ( )
156
+ } ;
136
157
137
158
let balances_before_fees: BalancesMap =
138
159
vec ! [ ( "a" . to_string( ) , 100 . into( ) ) , ( "b" . to_string( ) , 200 . into( ) ) ]
@@ -141,7 +162,7 @@ mod test {
141
162
142
163
let acc = Accounting {
143
164
last_event_aggregate : Utc :: now ( ) ,
144
- pre_fees : balances_before_fees,
165
+ balances_before_fees,
145
166
balances : BalancesMap :: default ( ) ,
146
167
} ;
147
168
@@ -150,12 +171,12 @@ mod test {
150
171
151
172
assert_eq ! ( balances, new_accounting. balances, "balances is the same" ) ;
152
173
assert_eq ! (
153
- new_accounting. pre_fees [ "a" ] ,
174
+ new_accounting. balances_before_fees [ "a" ] ,
154
175
9_800 . into( ) ,
155
176
"balance of recipient incremented accordingly"
156
177
) ;
157
178
assert_eq ! (
158
- new_accounting. pre_fees [ "b" ] ,
179
+ new_accounting. balances_before_fees [ "b" ] ,
159
180
200 . into( ) ,
160
181
"balances of non-recipient remains the same"
161
182
) ;
@@ -165,7 +186,7 @@ mod test {
165
186
"balanceAfterFees is ok"
166
187
) ;
167
188
assert_eq ! (
168
- & new_accounting. pre_fees . values( ) . sum:: <BigNum >( ) ,
189
+ & new_accounting. balances_before_fees . values( ) . sum:: <BigNum >( ) ,
169
190
& channel. deposit_amount,
170
191
"sum(balancesBeforeFees) == depositAmount"
171
192
) ;
@@ -187,11 +208,11 @@ mod test {
187
208
} ;
188
209
189
210
EventAggregate {
190
- channel_id : get_channel_id ( "one" ) ,
211
+ channel_id : DUMMY_CHANNEL . id . to_owned ( ) ,
191
212
created : Utc :: now ( ) ,
192
213
events : vec ! [ ( "IMPRESSION" . to_string( ) , aggregate_events) ]
193
214
. into_iter ( )
194
215
. collect ( ) ,
195
216
}
196
217
}
197
- }
218
+ }
0 commit comments