@@ -15,12 +15,7 @@ pub fn is_valid_transition(channel: &Channel, prev: &BalancesMap, next: &Balance
15
15
sum_next >= sum_prev && sum_next <= deposit && prev_checks
16
16
}
17
17
18
- pub fn is_healthy (
19
- channel : & Channel ,
20
- our : & BalancesMap ,
21
- approved : & BalancesMap ,
22
- health_threshold : & BigNum ,
23
- ) -> bool {
18
+ pub fn get_health ( channel : & Channel , our : & BalancesMap , approved : & BalancesMap ) -> u64 {
24
19
let sum_our: BigNum = our. values ( ) . sum ( ) ;
25
20
26
21
let zero = BigNum :: from ( 0 ) ;
@@ -30,26 +25,21 @@ pub fn is_healthy(
30
25
. sum ( ) ;
31
26
32
27
if sum_approved_mins >= sum_our {
33
- return true ;
28
+ return 1_000 ;
34
29
}
35
30
36
- let deposit = & channel. deposit_amount ;
37
- let health_threshold_neg = & BigNum :: from ( 1_000 ) - health_threshold;
38
- let acceptable_difference = deposit * & health_threshold_neg / & BigNum :: from ( 1_000 ) ;
39
-
40
- sum_our - sum_approved_mins < acceptable_difference
31
+ let diff = sum_our - sum_approved_mins;
32
+ let health_penalty = diff * & BigNum :: from ( 1_000 ) / & channel. deposit_amount ;
33
+ 1_000 - health_penalty. to_u64 ( ) . unwrap_or ( 1_000 )
41
34
}
42
35
43
36
#[ cfg( test) ]
44
37
mod test {
45
- use lazy_static:: lazy_static;
46
38
use primitives:: util:: tests:: prep_db:: DUMMY_CHANNEL ;
47
39
48
40
use super :: * ;
49
41
50
- lazy_static ! {
51
- static ref HEALTH_THRESHOLD : BigNum = BigNum :: from( 950 ) ;
52
- }
42
+ const HEALTH_THRESHOLD : u64 = 950 ;
53
43
54
44
fn get_dummy_channel < T : Into < BigNum > > ( deposit : T ) -> Channel {
55
45
Channel {
@@ -161,107 +151,114 @@ mod test {
161
151
}
162
152
163
153
#[ test]
164
- fn is_healthy_the_approved_balance_tree_gte_our_accounting_is_healthy ( ) {
154
+ fn get_health_the_approved_balance_tree_gte_our_accounting_is_healthy ( ) {
165
155
let channel = get_dummy_channel ( 50 ) ;
166
156
let our = vec ! [ ( "a" . into( ) , 50 . into( ) ) ] . into_iter ( ) . collect ( ) ;
167
- assert ! ( is_healthy( & channel, & our, & our, & HEALTH_THRESHOLD ) ) ;
168
-
169
- assert ! ( is_healthy(
170
- & channel,
171
- & our,
172
- & vec![ ( "a" . into( ) , 60 . into( ) ) ] . into_iter( ) . collect( ) ,
173
- & HEALTH_THRESHOLD
174
- ) ) ;
157
+ assert ! ( get_health( & channel, & our, & our) >= HEALTH_THRESHOLD ) ;
158
+
159
+ assert ! (
160
+ get_health(
161
+ & channel,
162
+ & our,
163
+ & vec![ ( "a" . into( ) , 60 . into( ) ) ] . into_iter( ) . collect( )
164
+ ) >= HEALTH_THRESHOLD
165
+ ) ;
175
166
}
176
167
177
168
#[ test]
178
- fn is_healthy_the_approved_balance_tree_is_positive_our_accounting_is_0_and_it_is_healthy ( ) {
169
+ fn get_health_the_approved_balance_tree_is_positive_our_accounting_is_0_and_it_is_healthy ( ) {
179
170
let approved = vec ! [ ( "a" . into( ) , 50 . into( ) ) ] . into_iter ( ) . collect ( ) ;
180
171
181
- assert ! ( is_healthy(
182
- & get_dummy_channel( 50 ) ,
183
- & BalancesMap :: default ( ) ,
184
- & approved,
185
- & HEALTH_THRESHOLD
186
- ) ) ;
172
+ assert ! (
173
+ get_health( & get_dummy_channel( 50 ) , & BalancesMap :: default ( ) , & approved)
174
+ >= HEALTH_THRESHOLD
175
+ ) ;
187
176
}
188
177
189
178
#[ test]
190
- fn is_healthy_the_approved_balance_tree_has_less_but_within_margin_it_is_healthy ( ) {
179
+ fn get_health_the_approved_balance_tree_has_less_but_within_margin_it_is_healthy ( ) {
191
180
let channel = get_dummy_channel ( 80 ) ;
192
181
193
- assert ! ( is_healthy(
194
- & channel,
195
- & vec![ ( "a" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( ) ,
196
- & vec![ ( "a" . into( ) , 79 . into( ) ) ] . into_iter( ) . collect( ) ,
197
- & HEALTH_THRESHOLD
198
- ) ) ;
199
-
200
- assert ! ( is_healthy(
201
- & channel,
202
- & vec![ ( "a" . into( ) , 2 . into( ) ) ] . into_iter( ) . collect( ) ,
203
- & vec![ ( "a" . into( ) , 1 . into( ) ) ] . into_iter( ) . collect( ) ,
204
- & HEALTH_THRESHOLD
205
- ) ) ;
182
+ assert ! (
183
+ get_health(
184
+ & channel,
185
+ & vec![ ( "a" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( ) ,
186
+ & vec![ ( "a" . into( ) , 79 . into( ) ) ] . into_iter( ) . collect( )
187
+ ) >= HEALTH_THRESHOLD
188
+ ) ;
189
+
190
+ assert ! (
191
+ get_health(
192
+ & channel,
193
+ & vec![ ( "a" . into( ) , 2 . into( ) ) ] . into_iter( ) . collect( ) ,
194
+ & vec![ ( "a" . into( ) , 1 . into( ) ) ] . into_iter( ) . collect( )
195
+ ) >= HEALTH_THRESHOLD
196
+ ) ;
206
197
}
207
198
208
199
#[ test]
209
- fn is_healthy_the_approved_balance_tree_has_less_it_is_unhealthy ( ) {
200
+ fn get_health_the_approved_balance_tree_has_less_it_is_unhealthy ( ) {
210
201
let channel = get_dummy_channel ( 80 ) ;
211
202
212
- assert ! ( !is_healthy(
213
- & channel,
214
- & vec![ ( "a" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( ) ,
215
- & vec![ ( "a" . into( ) , 70 . into( ) ) ] . into_iter( ) . collect( ) ,
216
- & HEALTH_THRESHOLD
217
- ) ) ;
203
+ assert ! (
204
+ get_health(
205
+ & channel,
206
+ & vec![ ( "a" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( ) ,
207
+ & vec![ ( "a" . into( ) , 70 . into( ) ) ] . into_iter( ) . collect( )
208
+ ) < HEALTH_THRESHOLD
209
+ ) ;
218
210
}
219
211
220
212
#[ test]
221
- fn is_healthy_they_have_the_same_sum_but_different_entities_are_earning ( ) {
213
+ fn get_health_they_have_the_same_sum_but_different_entities_are_earning ( ) {
222
214
let channel = get_dummy_channel ( 80 ) ;
223
215
224
- assert ! ( !is_healthy(
225
- & channel,
226
- & vec![ ( "a" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( ) ,
227
- & vec![ ( "b" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( ) ,
228
- & HEALTH_THRESHOLD
229
- ) ) ;
230
-
231
- assert ! ( !is_healthy(
232
- & channel,
233
- & vec![ ( "a" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( ) ,
234
- & vec![ ( "b" . into( ) , 40 . into( ) ) , ( "a" . into( ) , 40 . into( ) ) ]
235
- . into_iter( )
236
- . collect( ) ,
237
- & HEALTH_THRESHOLD
238
- ) ) ;
239
-
240
- assert ! ( !is_healthy(
241
- & channel,
242
- & vec![ ( "a" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( ) ,
243
- & vec![ ( "b" . into( ) , 20 . into( ) ) , ( "a" . into( ) , 60 . into( ) ) ]
244
- . into_iter( )
245
- . collect( ) ,
246
- & HEALTH_THRESHOLD
247
- ) ) ;
248
-
249
- assert ! ( is_healthy(
250
- & channel,
251
- & vec![ ( "a" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( ) ,
252
- & vec![ ( "b" . into( ) , 2 . into( ) ) , ( "a" . into( ) , 78 . into( ) ) ]
253
- . into_iter( )
254
- . collect( ) ,
255
- & HEALTH_THRESHOLD
256
- ) ) ;
257
-
258
- assert ! ( is_healthy(
259
- & channel,
260
- & vec![ ( "a" . into( ) , 100 . into( ) ) , ( "b" . into( ) , 1 . into( ) ) ]
261
- . into_iter( )
262
- . collect( ) ,
263
- & vec![ ( "a" . into( ) , 100 . into( ) ) ] . into_iter( ) . collect( ) ,
264
- & HEALTH_THRESHOLD
265
- ) ) ;
216
+ assert ! (
217
+ get_health(
218
+ & channel,
219
+ & vec![ ( "a" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( ) ,
220
+ & vec![ ( "b" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( )
221
+ ) < HEALTH_THRESHOLD
222
+ ) ;
223
+
224
+ assert ! (
225
+ get_health(
226
+ & channel,
227
+ & vec![ ( "a" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( ) ,
228
+ & vec![ ( "b" . into( ) , 40 . into( ) ) , ( "a" . into( ) , 40 . into( ) ) ]
229
+ . into_iter( )
230
+ . collect( )
231
+ ) < HEALTH_THRESHOLD
232
+ ) ;
233
+
234
+ assert ! (
235
+ get_health(
236
+ & channel,
237
+ & vec![ ( "a" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( ) ,
238
+ & vec![ ( "b" . into( ) , 20 . into( ) ) , ( "a" . into( ) , 60 . into( ) ) ]
239
+ . into_iter( )
240
+ . collect( )
241
+ ) < HEALTH_THRESHOLD
242
+ ) ;
243
+
244
+ assert ! (
245
+ get_health(
246
+ & channel,
247
+ & vec![ ( "a" . into( ) , 80 . into( ) ) ] . into_iter( ) . collect( ) ,
248
+ & vec![ ( "b" . into( ) , 2 . into( ) ) , ( "a" . into( ) , 78 . into( ) ) ]
249
+ . into_iter( )
250
+ . collect( )
251
+ ) >= HEALTH_THRESHOLD
252
+ ) ;
253
+
254
+ assert ! (
255
+ get_health(
256
+ & channel,
257
+ & vec![ ( "a" . into( ) , 100 . into( ) ) , ( "b" . into( ) , 1 . into( ) ) ]
258
+ . into_iter( )
259
+ . collect( ) ,
260
+ & vec![ ( "a" . into( ) , 100 . into( ) ) ] . into_iter( ) . collect( )
261
+ ) >= HEALTH_THRESHOLD
262
+ ) ;
266
263
}
267
264
}
0 commit comments