@@ -99,7 +99,8 @@ pub enum Path {
99
99
Commitment ( CommitmentPath ) ,
100
100
Ack ( AckPath ) ,
101
101
Receipt ( ReceiptPath ) ,
102
- UpgradeClient ( UpgradeClientPath ) ,
102
+ UpgradeClientState ( UpgradeClientStatePath ) ,
103
+ UpgradeConsensusState ( UpgradeConsensusStatePath ) ,
103
104
}
104
105
105
106
#[ cfg_attr(
@@ -693,13 +694,51 @@ impl ReceiptPath {
693
694
derive( borsh:: BorshSerialize , borsh:: BorshDeserialize )
694
695
) ]
695
696
#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
696
- /// Paths that are specific for client upgrades.
697
697
#[ derive( Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Hash , Display ) ]
698
- pub enum UpgradeClientPath {
699
- #[ display( fmt = "{UPGRADED_IBC_STATE}/{_0}/{UPGRADED_CLIENT_STATE}" ) ]
700
- UpgradedClientState ( u64 ) ,
701
- #[ display( fmt = "{UPGRADED_IBC_STATE}/{_0}/{UPGRADED_CLIENT_CONSENSUS_STATE}" ) ]
702
- UpgradedClientConsensusState ( u64 ) ,
698
+ #[ display( fmt = "{upgrade_path}/{height}/{UPGRADED_CLIENT_STATE}" ) ]
699
+ pub struct UpgradeClientStatePath {
700
+ pub upgrade_path : String ,
701
+ pub height : u64 ,
702
+ }
703
+
704
+ impl UpgradeClientStatePath {
705
+ /// Create with the default upgrade path
706
+ pub fn new_with_default_path ( height : u64 ) -> Self {
707
+ Self {
708
+ upgrade_path : UPGRADED_IBC_STATE . to_string ( ) ,
709
+ height,
710
+ }
711
+ }
712
+ }
713
+
714
+ #[ cfg_attr(
715
+ feature = "parity-scale-codec" ,
716
+ derive(
717
+ parity_scale_codec:: Encode ,
718
+ parity_scale_codec:: Decode ,
719
+ scale_info:: TypeInfo
720
+ )
721
+ ) ]
722
+ #[ cfg_attr(
723
+ feature = "borsh" ,
724
+ derive( borsh:: BorshSerialize , borsh:: BorshDeserialize )
725
+ ) ]
726
+ #[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
727
+ #[ derive( Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Hash , Display ) ]
728
+ #[ display( fmt = "{upgrade_path}/{height}/{UPGRADED_CLIENT_CONSENSUS_STATE}" ) ]
729
+ pub struct UpgradeConsensusStatePath {
730
+ pub upgrade_path : String ,
731
+ pub height : u64 ,
732
+ }
733
+
734
+ impl UpgradeConsensusStatePath {
735
+ /// Create with the default upgrade path
736
+ pub fn new_with_default_path ( height : u64 ) -> Self {
737
+ Self {
738
+ upgrade_path : UPGRADED_IBC_STATE . to_string ( ) ,
739
+ height,
740
+ }
741
+ }
703
742
}
704
743
705
744
#[ cfg_attr(
@@ -760,7 +799,8 @@ impl FromStr for Path {
760
799
. or_else ( || parse_commitments ( & components) )
761
800
. or_else ( || parse_acks ( & components) )
762
801
. or_else ( || parse_receipts ( & components) )
763
- . or_else ( || parse_upgrades ( & components) )
802
+ . or_else ( || parse_upgrade_client_state ( & components) )
803
+ . or_else ( || parse_upgrade_consensus_state ( & components) )
764
804
. ok_or ( PathError :: ParseFailure {
765
805
path : s. to_string ( ) ,
766
806
} )
@@ -1084,28 +1124,52 @@ fn parse_receipts(components: &[&str]) -> Option<Path> {
1084
1124
)
1085
1125
}
1086
1126
1087
- fn parse_upgrades ( components : & [ & str ] ) -> Option < Path > {
1127
+ fn parse_upgrade_client_state ( components : & [ & str ] ) -> Option < Path > {
1088
1128
if components. len ( ) != 3 {
1089
1129
return None ;
1090
1130
}
1091
1131
1092
- let first = * components. first ( ) ?;
1132
+ let last = * components. last ( ) ?;
1093
1133
1094
- if first != UPGRADED_IBC_STATE {
1134
+ if last != UPGRADED_CLIENT_STATE {
1095
1135
return None ;
1096
1136
}
1097
1137
1098
- let last = * components. last ( ) ?;
1138
+ let upgrade_path = components. first ( ) ?. to_string ( ) ;
1099
1139
1100
- let height = components[ 1 ] . parse :: < u64 > ( ) . ok ( ) ?;
1140
+ let height = u64 :: from_str ( components[ 1 ] ) . ok ( ) ?;
1101
1141
1102
- match last {
1103
- UPGRADED_CLIENT_STATE => Some ( UpgradeClientPath :: UpgradedClientState ( height ) . into ( ) ) ,
1104
- UPGRADED_CLIENT_CONSENSUS_STATE => {
1105
- Some ( UpgradeClientPath :: UpgradedClientConsensusState ( height) . into ( ) )
1142
+ Some (
1143
+ UpgradeClientStatePath {
1144
+ upgrade_path ,
1145
+ height,
1106
1146
}
1107
- _ => None ,
1147
+ . into ( ) ,
1148
+ )
1149
+ }
1150
+
1151
+ fn parse_upgrade_consensus_state ( components : & [ & str ] ) -> Option < Path > {
1152
+ if components. len ( ) != 3 {
1153
+ return None ;
1154
+ }
1155
+
1156
+ let last = * components. last ( ) ?;
1157
+
1158
+ if last != UPGRADED_CLIENT_CONSENSUS_STATE {
1159
+ return None ;
1108
1160
}
1161
+
1162
+ let upgrade_path = components. first ( ) ?. to_string ( ) ;
1163
+
1164
+ let height = u64:: from_str ( components[ 1 ] ) . ok ( ) ?;
1165
+
1166
+ Some (
1167
+ UpgradeConsensusStatePath {
1168
+ upgrade_path,
1169
+ height,
1170
+ }
1171
+ . into ( ) ,
1172
+ )
1109
1173
}
1110
1174
1111
1175
#[ cfg( test) ]
@@ -1207,11 +1271,17 @@ mod tests {
1207
1271
) ]
1208
1272
#[ case(
1209
1273
"upgradedIBCState/0/upgradedClient" ,
1210
- Path :: UpgradeClient ( UpgradeClientPath :: UpgradedClientState ( 0 ) )
1274
+ Path :: UpgradeClientState ( UpgradeClientStatePath {
1275
+ upgrade_path: UPGRADED_IBC_STATE . to_string( ) ,
1276
+ height: 0 ,
1277
+ } )
1211
1278
) ]
1212
1279
#[ case(
1213
1280
"upgradedIBCState/0/upgradedConsState" ,
1214
- Path :: UpgradeClient ( UpgradeClientPath :: UpgradedClientConsensusState ( 0 ) )
1281
+ Path :: UpgradeConsensusState ( UpgradeConsensusStatePath {
1282
+ upgrade_path: UPGRADED_IBC_STATE . to_string( ) ,
1283
+ height: 0 ,
1284
+ } )
1215
1285
) ]
1216
1286
fn test_successful_parsing ( #[ case] path_str : & str , #[ case] path : Path ) {
1217
1287
// can be parsed into Path
@@ -1419,25 +1489,30 @@ mod tests {
1419
1489
}
1420
1490
1421
1491
#[ test]
1422
- fn test_parse_upgrades_fn ( ) {
1492
+ fn test_parse_upgrade_client_state_fn ( ) {
1423
1493
let path = "upgradedIBCState/0/upgradedClient" ;
1424
1494
let components: Vec < & str > = path. split ( '/' ) . collect ( ) ;
1425
1495
1426
1496
assert_eq ! (
1427
- parse_upgrades( & components) ,
1428
- Some ( Path :: UpgradeClient ( UpgradeClientPath :: UpgradedClientState (
1429
- 0
1430
- ) ) ) ,
1497
+ parse_upgrade_client_state( & components) ,
1498
+ Some ( Path :: UpgradeClientState ( UpgradeClientStatePath {
1499
+ upgrade_path: UPGRADED_IBC_STATE . to_string( ) ,
1500
+ height: 0 ,
1501
+ } ) ) ,
1431
1502
) ;
1503
+ }
1432
1504
1505
+ #[ test]
1506
+ fn test_parse_upgrade_consensus_state_fn ( ) {
1433
1507
let path = "upgradedIBCState/0/upgradedConsState" ;
1434
1508
let components: Vec < & str > = path. split ( '/' ) . collect ( ) ;
1435
1509
1436
1510
assert_eq ! (
1437
- parse_upgrades( & components) ,
1438
- Some ( Path :: UpgradeClient (
1439
- UpgradeClientPath :: UpgradedClientConsensusState ( 0 )
1440
- ) ) ,
1511
+ parse_upgrade_consensus_state( & components) ,
1512
+ Some ( Path :: UpgradeConsensusState ( UpgradeConsensusStatePath {
1513
+ upgrade_path: UPGRADED_IBC_STATE . to_string( ) ,
1514
+ height: 0 ,
1515
+ } ) ) ,
1441
1516
)
1442
1517
}
1443
1518
}
0 commit comments