1
1
//! `/v5/channel` routes
2
2
//!
3
3
4
+ use axum:: { extract:: Path , Extension , Json } ;
5
+ use futures:: future:: try_join_all;
6
+ use serde:: { Deserialize , Serialize } ;
7
+ use slog:: { error, Logger } ;
8
+ use std:: { any:: Any , collections:: HashMap , sync:: Arc } ;
9
+
10
+ use adapter:: { client:: Locked , Adapter , Dummy , util:: { get_balance_leaf, get_signable_state_root} } ;
11
+ use primitives:: {
12
+ balances:: { Balances , CheckedState , UncheckedState } ,
13
+ merkle_tree:: MerkleTree ,
14
+ sentry:: {
15
+ channel_list:: { ChannelListQuery , ChannelListResponse } ,
16
+ AccountingResponse , AllSpendersQuery , AllSpendersResponse , ChannelPayRequest , GetLeafResponse , LastApproved ,
17
+ LastApprovedQuery , LastApprovedResponse , SpenderResponse , SuccessResponse ,
18
+ } ,
19
+ spender:: { Spendable , Spender } ,
20
+ validator:: NewState ,
21
+ Address , ChainOf , Channel , ChannelId , Deposit , UnifiedNum ,
22
+ } ;
23
+
4
24
use crate :: {
5
25
application:: Qs ,
6
26
db:: {
@@ -13,32 +33,9 @@ use crate::{
13
33
DbPool ,
14
34
} ,
15
35
response:: ResponseError ,
16
- routes:: campaign:: fetch_campaign_ids_for_channel,
36
+ routes:: { campaign:: fetch_campaign_ids_for_channel, routers :: LeafFor } ,
17
37
Application , Auth ,
18
38
} ;
19
- use adapter:: {
20
- client:: Locked ,
21
- util:: { get_balance_leaf, get_signable_state_root} ,
22
- Adapter , Dummy ,
23
- } ;
24
- use axum:: { extract:: Path , Extension , Json } ;
25
- use futures:: future:: try_join_all;
26
- use primitives:: {
27
- balances:: { Balances , CheckedState , UncheckedState } ,
28
- merkle_tree:: MerkleTree ,
29
- sentry:: {
30
- channel_list:: { ChannelListQuery , ChannelListResponse } ,
31
- AccountingResponse , AllSpendersQuery , AllSpendersResponse , ChannelPayRequest ,
32
- GetLeafResponse , LastApproved , LastApprovedQuery , LastApprovedResponse , SpenderResponse ,
33
- SuccessResponse ,
34
- } ,
35
- spender:: { Spendable , Spender } ,
36
- validator:: NewState ,
37
- Address , ChainOf , Channel , ChannelId , Deposit , UnifiedNum ,
38
- } ;
39
- use serde:: { Deserialize , Serialize } ;
40
- use slog:: { error, Logger } ;
41
- use std:: { any:: Any , collections:: HashMap , sync:: Arc } ;
42
39
43
40
/// Request body for Channel deposit when using the Dummy adapter.
44
41
///
@@ -502,9 +499,10 @@ pub async fn channel_payout<C: Locked + 'static>(
502
499
Ok ( Json ( SuccessResponse { success : true } ) )
503
500
}
504
501
505
- pub async fn get_spender_leaf < C : Locked + ' static > (
502
+ pub async fn get_leaf < C : Locked + ' static > (
506
503
Extension ( app) : Extension < Arc < Application < C > > > ,
507
504
Extension ( channel_context) : Extension < ChainOf < Channel > > ,
505
+ Extension ( leaf_for) : Extension < LeafFor > ,
508
506
Path ( params) : Path < ( ChannelId , Address ) > ,
509
507
) -> Result < Json < GetLeafResponse > , ResponseError > {
510
508
let channel = channel_context. context ;
@@ -524,66 +522,40 @@ pub async fn get_spender_leaf<C: Locked + 'static>(
524
522
. await ?
525
523
. ok_or_else ( || ResponseError :: BadRequest ( "No NewState message for spender" . to_string ( ) ) ) ?;
526
524
527
- let spender = params. 1 ;
528
- let amount = new_state
529
- . msg
530
- . balances
531
- . spenders
532
- . get ( & spender)
533
- . ok_or_else ( || ResponseError :: BadRequest ( "No balance entry for spender!" . to_string ( ) ) ) ?;
534
- let element = get_balance_leaf (
535
- true ,
536
- & spender,
537
- & amount. to_precision ( channel_context. token . precision . get ( ) ) ,
538
- )
539
- . map_err ( |err| ResponseError :: BadRequest ( err. to_string ( ) ) ) ?;
540
-
541
- let merkle_tree =
542
- MerkleTree :: new ( & [ element] ) . map_err ( |err| ResponseError :: BadRequest ( err. to_string ( ) ) ) ?;
543
-
544
- let signable_state_root = get_signable_state_root ( & * channel. id ( ) , & merkle_tree. root ( ) ) ;
525
+ let addr = params. 1 ;
545
526
546
- let res = hex:: encode ( signable_state_root) ;
547
-
548
- Ok ( Json ( GetLeafResponse { merkle_proof : res } ) )
549
- }
527
+ let element = match leaf_for {
528
+ LeafFor :: Spender => {
529
+ let amount = new_state
530
+ . msg
531
+ . balances
532
+ . spenders
533
+ . get ( & addr)
534
+ . ok_or_else ( || ResponseError :: BadRequest ( "No balance entry for spender!" . to_string ( ) ) ) ?;
550
535
551
- pub async fn get_earner_leaf < C : Locked + ' static > (
552
- Extension ( app) : Extension < Arc < Application < C > > > ,
553
- Extension ( channel_context) : Extension < ChainOf < Channel > > ,
554
- Path ( params) : Path < ( ChannelId , Address ) > ,
555
- ) -> Result < Json < GetLeafResponse > , ResponseError > {
556
- let channel = channel_context. context ;
536
+ get_balance_leaf (
537
+ true ,
538
+ & addr,
539
+ & amount. to_precision ( channel_context. token . precision . get ( ) ) ,
540
+ )
541
+ . map_err ( |err| ResponseError :: BadRequest ( err. to_string ( ) ) ) ?
542
+ } ,
543
+ LeafFor :: Earner => {
544
+ let amount = new_state
545
+ . msg
546
+ . balances
547
+ . earners
548
+ . get ( & addr)
549
+ . ok_or_else ( || ResponseError :: BadRequest ( "No balance entry for spender!" . to_string ( ) ) ) ?;
557
550
558
- let approve_state = match latest_approve_state ( & app . pool , & channel ) . await ? {
559
- Some ( approve_state ) => approve_state ,
560
- None => {
561
- return Err ( ResponseError :: BadRequest (
562
- "No ApproveState message for earner" . to_string ( ) ,
563
- ) )
551
+ get_balance_leaf (
552
+ false ,
553
+ & addr ,
554
+ & amount . to_precision ( channel_context . token . precision . get ( ) ) ,
555
+ )
556
+ . map_err ( |err| ResponseError :: BadRequest ( err . to_string ( ) ) ) ?
564
557
}
565
558
} ;
566
-
567
- let state_root = approve_state. msg . state_root . clone ( ) ;
568
-
569
- let new_state = latest_new_state ( & app. pool , & channel, & state_root)
570
- . await ?
571
- . ok_or_else ( || ResponseError :: BadRequest ( "No NewState message for earner" . to_string ( ) ) ) ?;
572
-
573
- let earner = params. 1 ;
574
- let amount = new_state
575
- . msg
576
- . balances
577
- . earners
578
- . get ( & earner)
579
- . ok_or_else ( || ResponseError :: BadRequest ( "No balance entry for earner!" . to_string ( ) ) ) ?;
580
- let element = get_balance_leaf (
581
- false ,
582
- & earner,
583
- & amount. to_precision ( channel_context. token . precision . get ( ) ) ,
584
- )
585
- . map_err ( |err| ResponseError :: BadRequest ( err. to_string ( ) ) ) ?;
586
-
587
559
let merkle_tree =
588
560
MerkleTree :: new ( & [ element] ) . map_err ( |err| ResponseError :: BadRequest ( err. to_string ( ) ) ) ?;
589
561
@@ -765,17 +737,11 @@ pub mod validator_message {
765
737
766
738
#[ cfg( test) ]
767
739
mod test {
768
- use super :: * ;
769
- use crate :: {
770
- db:: {
771
- insert_campaign, insert_channel, validator_message:: insert_validator_message,
772
- CampaignRemaining ,
773
- } ,
774
- test_util:: setup_dummy_app,
775
- } ;
740
+ use std:: str:: FromStr ;
776
741
use adapter:: {
777
742
ethereum:: test_util:: { GANACHE_INFO_1 , GANACHE_INFO_1337 } ,
778
743
primitives:: Deposit as AdapterDeposit ,
744
+ prelude:: Unlocked ,
779
745
} ;
780
746
use primitives:: {
781
747
balances:: UncheckedState ,
@@ -785,9 +751,16 @@ mod test {
785
751
PUBLISHER , PUBLISHER_2 ,
786
752
} ,
787
753
validator:: { ApproveState , MessageTypes , NewState } ,
788
- BigNum , ChainId , Deposit , ToETHChecksum , UnifiedMap , ValidatorId ,
754
+ BigNum , ChainId , Deposit , UnifiedMap , ValidatorId ,
755
+ } ;
756
+ use super :: * ;
757
+ use crate :: {
758
+ db:: {
759
+ insert_campaign, insert_channel, validator_message:: insert_validator_message,
760
+ CampaignRemaining ,
761
+ } ,
762
+ test_util:: setup_dummy_app,
789
763
} ;
790
- use std:: str:: FromStr ;
791
764
792
765
#[ tokio:: test]
793
766
async fn create_and_fetch_spendable ( ) {
@@ -1539,17 +1512,16 @@ mod test {
1539
1512
. expect ( "should insert channel" ) ;
1540
1513
1541
1514
// Setting up the validator messages
1542
- let state_root = balances
1543
- . encode ( channel. id ( ) , channel_context. token . precision . get ( ) )
1544
- . expect ( "should encode" ) ;
1515
+ let state_root = "b1a4fc6c1a1e1ab908a487e504006edcebea297f61b4b8ce6cad3b29e29454cc" . to_string ( ) ;
1516
+ let signature = app. adapter . clone ( ) . unlock ( ) . expect ( "should unlock" ) . sign ( & state_root. clone ( ) ) . expect ( "should sign" ) ;
1545
1517
let new_state: NewState < UncheckedState > = NewState {
1546
1518
state_root : state_root. clone ( ) ,
1547
- signature : IDS [ & * LEADER ] . to_checksum ( ) ,
1519
+ signature : signature . clone ( ) ,
1548
1520
balances : balances. into_unchecked ( ) ,
1549
1521
} ;
1550
1522
let approve_state = ApproveState {
1551
1523
state_root,
1552
- signature : IDS [ & * FOLLOWER ] . to_checksum ( ) ,
1524
+ signature,
1553
1525
is_healthy : true ,
1554
1526
} ;
1555
1527
@@ -1570,48 +1542,23 @@ mod test {
1570
1542
. await
1571
1543
. expect ( "Should insert NewState msg" ) ;
1572
1544
1573
- // generate proofs
1574
- let spender_proof = {
1575
- let element = get_balance_leaf (
1576
- true ,
1577
- & * ADVERTISER ,
1578
- & UnifiedNum :: from_u64 ( 2_000 ) . to_precision ( channel_context. token . precision . get ( ) ) ,
1579
- )
1580
- . expect ( "should get" ) ;
1581
-
1582
- let merkle_tree = MerkleTree :: new ( & [ element] ) . expect ( "Should build MerkleTree" ) ;
1583
-
1584
- let signable_state_root = get_signable_state_root ( & * channel. id ( ) , & merkle_tree. root ( ) ) ;
1585
-
1586
- hex:: encode ( signable_state_root)
1587
- } ;
1588
-
1589
- let earner_proof = {
1590
- let element = get_balance_leaf (
1591
- false ,
1592
- & * PUBLISHER ,
1593
- & UnifiedNum :: from_u64 ( 2_000 ) . to_precision ( channel_context. token . precision . get ( ) ) ,
1594
- )
1595
- . expect ( "should get balance leaf" ) ;
1596
-
1597
- let merkle_tree = MerkleTree :: new ( & [ element] ) . expect ( "Should build MerkleTree" ) ;
1598
-
1599
- let signable_state_root = get_signable_state_root ( & * channel. id ( ) , & merkle_tree. root ( ) ) ;
1600
-
1601
- hex:: encode ( signable_state_root)
1602
- } ;
1545
+ // hardcoded proofs
1546
+ let spender_proof = "8ea7760ca2dbbe00673372afbf8b05048717ce8a305f1f853afac8c244182e0c" . to_string ( ) ;
1547
+ let earner_proof = "dc94141cb41550df047ba3a965ce36d98eb6098eb952ca3cb6fd9682e5810b51" . to_string ( ) ;
1603
1548
1604
1549
// call functions
1605
- let spender_leaf = get_spender_leaf (
1550
+ let spender_leaf = get_leaf (
1606
1551
app. clone ( ) ,
1607
1552
channel_context. clone ( ) ,
1553
+ Extension ( LeafFor :: Spender ) ,
1608
1554
Path ( ( channel. id ( ) , * ADVERTISER ) ) ,
1609
1555
)
1610
1556
. await
1611
1557
. expect ( "should get spender leaf" ) ;
1612
- let earner_leaf = get_earner_leaf (
1558
+ let earner_leaf = get_leaf (
1613
1559
app. clone ( ) ,
1614
1560
channel_context. clone ( ) ,
1561
+ Extension ( LeafFor :: Earner ) ,
1615
1562
Path ( ( channel. id ( ) , * PUBLISHER ) ) ,
1616
1563
)
1617
1564
. await
0 commit comments