@@ -74,22 +74,84 @@ impl ClientStatePath {
74
74
feature = "borsh" ,
75
75
derive( borsh:: BorshSerialize , borsh:: BorshDeserialize )
76
76
) ]
77
- #[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
78
77
#[ derive( Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Hash , Display ) ]
79
- #[ display( fmt = "clients/{client_id}/consensusStates/{epoch}-{height}" ) ]
78
+ #[ display(
79
+ fmt = "clients/{}/consensusStates/{}-{}" ,
80
+ client_id,
81
+ "height.revision_number()" ,
82
+ "height.revision_height()"
83
+ ) ]
80
84
pub struct ClientConsensusStatePath {
81
85
pub client_id : ClientId ,
82
- pub epoch : u64 ,
83
- pub height : u64 ,
86
+ pub height : Height ,
84
87
}
85
88
86
89
impl ClientConsensusStatePath {
87
90
pub fn new ( client_id : & ClientId , height : & Height ) -> ClientConsensusStatePath {
88
91
ClientConsensusStatePath {
89
92
client_id : client_id. clone ( ) ,
90
- epoch : height. revision_number ( ) ,
91
- height : height. revision_height ( ) ,
93
+ height : height. clone ( ) ,
94
+ }
95
+ }
96
+
97
+ pub fn revision_number ( & self ) -> u64 {
98
+ self . height . revision_number ( )
99
+ }
100
+
101
+ pub fn revision_height ( & self ) -> u64 {
102
+ self . height . revision_height ( )
103
+ }
104
+ }
105
+
106
+ #[ cfg( feature = "serde" ) ]
107
+ impl serde:: Serialize for ClientConsensusStatePath {
108
+ /// Serialises the path as a struct with three elements.
109
+ ///
110
+ /// Rather than serialising the path as a nested type with `height` being an
111
+ /// embedded structure, serialises it as a structure with three fields:
112
+ ///
113
+ /// ```ignore
114
+ /// struct ClientConsensusStatePath {
115
+ /// client_id: ClientId,
116
+ /// epoch: u64, // equal height.revision_number()
117
+ /// height: u64, // equal height.revision_height()
118
+ /// }
119
+ /// ```
120
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
121
+ where
122
+ S : serde:: Serializer ,
123
+ {
124
+ use serde:: ser:: SerializeStruct ;
125
+
126
+ let mut serializer = serializer. serialize_struct ( "ClientConsensusStatePath" , 3 ) ?;
127
+ serializer. serialize_field ( "client_id" , & self . client_id ) ?;
128
+ serializer. serialize_field ( "epoch" , & self . revision_number ( ) ) ?;
129
+ serializer. serialize_field ( "height" , & self . revision_height ( ) ) ?;
130
+ serializer. end ( )
131
+ }
132
+ }
133
+
134
+ #[ cfg( feature = "serde" ) ]
135
+ impl < ' de > serde:: Deserialize < ' de > for ClientConsensusStatePath {
136
+ /// Deserialises the path from a struct with three elements.
137
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
138
+ where
139
+ D : serde:: Deserializer < ' de > ,
140
+ {
141
+ use serde:: de:: Error ;
142
+
143
+ #[ derive( serde:: Deserialize ) ]
144
+ struct ClientConsensusStatePath {
145
+ client_id : ClientId ,
146
+ epoch : u64 ,
147
+ height : u64 ,
92
148
}
149
+
150
+ let path = ClientConsensusStatePath :: deserialize ( deserializer) ?;
151
+ let client_id = path. client_id ;
152
+ Height :: new ( path. epoch , path. height )
153
+ . map ( |height| Self { client_id, height } )
154
+ . map_err ( |_| D :: Error :: custom ( "height cannot be zero" ) )
93
155
}
94
156
}
95
157
@@ -480,8 +542,7 @@ fn parse_client_paths(components: &[&str]) -> Option<Path> {
480
542
Some (
481
543
ClientConsensusStatePath {
482
544
client_id,
483
- epoch,
484
- height,
545
+ height : Height :: new ( epoch, height) . ok ( ) ?,
485
546
}
486
547
. into ( ) ,
487
548
)
@@ -862,8 +923,7 @@ mod tests {
862
923
parse_client_paths( & components) ,
863
924
Some ( Path :: ClientConsensusState ( ClientConsensusStatePath {
864
925
client_id: ClientId :: default ( ) ,
865
- epoch: 15 ,
866
- height: 31 ,
926
+ height: Height :: new( 15 , 31 ) . unwrap( ) ,
867
927
} ) )
868
928
) ;
869
929
}
@@ -890,8 +950,7 @@ mod tests {
890
950
path. unwrap( ) ,
891
951
Path :: ClientConsensusState ( ClientConsensusStatePath {
892
952
client_id: ClientId :: default ( ) ,
893
- epoch: 15 ,
894
- height: 31 ,
953
+ height: Height :: new( 15 , 31 ) . unwrap( ) ,
895
954
} )
896
955
) ;
897
956
}
0 commit comments