1
1
//! Schema for channel update storage.
2
2
3
- use crate :: message_listener :: LOG_TARGET ;
3
+ use crate :: RELAYER_PREFIX ;
4
4
use parity_scale_codec:: { Decode , Encode } ;
5
5
use sc_client_api:: backend:: AuxStore ;
6
6
use sp_blockchain:: { Error as ClientError , Info , Result as ClientResult } ;
7
+ use sp_core:: bytes:: to_hex;
7
8
use sp_core:: H256 ;
8
9
use sp_messenger:: messages:: { ChainId , ChannelId , ChannelState , Nonce } ;
9
10
use sp_messenger:: { ChannelNonce , XdmId } ;
10
11
use sp_runtime:: traits:: { Block as BlockT , NumberFor } ;
11
12
use subspace_runtime_primitives:: BlockNumber ;
12
13
13
14
const CHANNEL_DETAIL : & [ u8 ] = b"channel_detail" ;
15
+ const LOG_TARGET : & str = "gossip_aux_schema" ;
14
16
15
17
fn channel_detail_key (
16
18
src_chain_id : ChainId ,
@@ -58,35 +60,48 @@ pub struct ChannelDetail {
58
60
/// Load the channel state of self_chain_id on chain_id.
59
61
pub fn get_channel_state < Backend > (
60
62
backend : & Backend ,
61
- self_chain_id : ChainId ,
62
- chain_id : ChainId ,
63
+ dst_chain_id : ChainId ,
64
+ src_chain_id : ChainId ,
63
65
channel_id : ChannelId ,
64
66
) -> ClientResult < Option < ChannelDetail > >
65
67
where
66
68
Backend : AuxStore ,
67
69
{
68
70
load_decode (
69
71
backend,
70
- channel_detail_key ( chain_id , self_chain_id , channel_id) . as_slice ( ) ,
72
+ channel_detail_key ( src_chain_id , dst_chain_id , channel_id) . as_slice ( ) ,
71
73
)
72
74
}
73
75
74
76
/// Set the channel state of self_chain_id on chain_id.
75
77
pub fn set_channel_state < Backend > (
76
78
backend : & Backend ,
77
- self_chain_id : ChainId ,
78
- chain_id : ChainId ,
79
+ dst_chain_id : ChainId ,
80
+ src_chain_id : ChainId ,
79
81
channel_detail : ChannelDetail ,
80
82
) -> ClientResult < ( ) >
81
83
where
82
84
Backend : AuxStore ,
83
85
{
84
86
backend. insert_aux (
85
87
& [ (
86
- channel_detail_key ( chain_id , self_chain_id , channel_detail. channel_id ) . as_slice ( ) ,
88
+ channel_detail_key ( src_chain_id , dst_chain_id , channel_detail. channel_id ) . as_slice ( ) ,
87
89
channel_detail. encode ( ) . as_slice ( ) ,
88
90
) ] ,
89
91
vec ! [ ] ,
92
+ ) ?;
93
+
94
+ let channel_nonce = ChannelNonce {
95
+ relay_msg_nonce : Some ( channel_detail. next_inbox_nonce ) ,
96
+ relay_response_msg_nonce : channel_detail. latest_response_received_message_nonce ,
97
+ } ;
98
+ let prefix = ( RELAYER_PREFIX , src_chain_id) . encode ( ) ;
99
+ cleanup_chain_channel_storages (
100
+ backend,
101
+ & prefix,
102
+ src_chain_id,
103
+ channel_detail. channel_id ,
104
+ channel_nonce,
90
105
)
91
106
}
92
107
@@ -101,25 +116,36 @@ mod xdm_keys {
101
116
const XDM_RELAY_RESPONSE : & [ u8 ] = b"relay_msg_response" ;
102
117
const XDM_LAST_CLEANUP_NONCE : & [ u8 ] = b"xdm_last_cleanup_nonce" ;
103
118
104
- pub ( super ) fn get_key_for_xdm_id ( xdm_id : XdmId ) -> Vec < u8 > {
119
+ pub ( super ) fn get_key_for_xdm_id ( prefix : & [ u8 ] , xdm_id : XdmId ) -> Vec < u8 > {
105
120
match xdm_id {
106
- XdmId :: RelayMessage ( id) => get_key_for_xdm_relay ( id) ,
107
- XdmId :: RelayResponseMessage ( id) => get_key_for_xdm_relay_response ( id) ,
121
+ XdmId :: RelayMessage ( id) => get_key_for_xdm_relay ( prefix , id) ,
122
+ XdmId :: RelayResponseMessage ( id) => get_key_for_xdm_relay_response ( prefix , id) ,
108
123
}
109
124
}
110
125
111
126
pub ( super ) fn get_key_for_last_cleanup_relay_nonce (
127
+ prefix : & [ u8 ] ,
112
128
chain_id : ChainId ,
113
129
channel_id : ChannelId ,
114
130
) -> Vec < u8 > {
115
- ( XDM , XDM_RELAY , XDM_LAST_CLEANUP_NONCE , chain_id, channel_id) . encode ( )
131
+ (
132
+ prefix,
133
+ XDM ,
134
+ XDM_RELAY ,
135
+ XDM_LAST_CLEANUP_NONCE ,
136
+ chain_id,
137
+ channel_id,
138
+ )
139
+ . encode ( )
116
140
}
117
141
118
142
pub ( super ) fn get_key_for_last_cleanup_relay_response_nonce (
143
+ prefix : & [ u8 ] ,
119
144
chain_id : ChainId ,
120
145
channel_id : ChannelId ,
121
146
) -> Vec < u8 > {
122
147
(
148
+ prefix,
123
149
XDM ,
124
150
XDM_RELAY_RESPONSE ,
125
151
XDM_LAST_CLEANUP_NONCE ,
@@ -129,19 +155,19 @@ mod xdm_keys {
129
155
. encode ( )
130
156
}
131
157
132
- pub ( super ) fn get_key_for_xdm_relay ( id : MessageKey ) -> Vec < u8 > {
133
- ( XDM , XDM_RELAY , id) . encode ( )
158
+ pub ( super ) fn get_key_for_xdm_relay ( prefix : & [ u8 ] , id : MessageKey ) -> Vec < u8 > {
159
+ ( prefix , XDM , XDM_RELAY , id) . encode ( )
134
160
}
135
161
136
- pub ( super ) fn get_key_for_xdm_relay_response ( id : MessageKey ) -> Vec < u8 > {
137
- ( XDM , XDM_RELAY_RESPONSE , id) . encode ( )
162
+ pub ( super ) fn get_key_for_xdm_relay_response ( prefix : & [ u8 ] , id : MessageKey ) -> Vec < u8 > {
163
+ ( prefix , XDM , XDM_RELAY_RESPONSE , id) . encode ( )
138
164
}
139
165
}
140
166
141
167
#[ derive( Debug , Encode , Decode , Clone ) ]
142
- pub ( super ) struct BlockId < Block : BlockT > {
143
- pub ( super ) number : NumberFor < Block > ,
144
- pub ( super ) hash : Block :: Hash ,
168
+ pub struct BlockId < Block : BlockT > {
169
+ pub number : NumberFor < Block > ,
170
+ pub hash : Block :: Hash ,
145
171
}
146
172
147
173
impl < Block : BlockT > From < Info < Block > > for BlockId < Block > {
@@ -156,32 +182,38 @@ impl<Block: BlockT> From<Info<Block>> for BlockId<Block> {
156
182
/// Store the given XDM ID as processed at given block.
157
183
pub fn set_xdm_message_processed_at < Backend , Block > (
158
184
backend : & Backend ,
185
+ prefix : & [ u8 ] ,
159
186
xdm_id : XdmId ,
160
187
block_id : BlockId < Block > ,
161
188
) -> ClientResult < ( ) >
162
189
where
163
190
Backend : AuxStore ,
164
191
Block : BlockT ,
165
192
{
166
- let key = xdm_keys:: get_key_for_xdm_id ( xdm_id) ;
193
+ let key = xdm_keys:: get_key_for_xdm_id ( prefix , xdm_id) ;
167
194
backend. insert_aux ( & [ ( key. as_slice ( ) , block_id. encode ( ) . as_slice ( ) ) ] , vec ! [ ] )
168
195
}
169
196
170
197
/// Returns the maybe last processed block number for given xdm.
171
198
pub fn get_xdm_processed_block_number < Backend , Block > (
172
199
backend : & Backend ,
200
+ prefix : & [ u8 ] ,
173
201
xdm_id : XdmId ,
174
202
) -> ClientResult < Option < BlockId < Block > > >
175
203
where
176
204
Backend : AuxStore ,
177
205
Block : BlockT ,
178
206
{
179
- load_decode ( backend, xdm_keys:: get_key_for_xdm_id ( xdm_id) . as_slice ( ) )
207
+ load_decode (
208
+ backend,
209
+ xdm_keys:: get_key_for_xdm_id ( prefix, xdm_id) . as_slice ( ) ,
210
+ )
180
211
}
181
212
182
213
/// Cleans up all the xdm storages until the latest nonces.
183
214
pub fn cleanup_chain_channel_storages < Backend > (
184
215
backend : & Backend ,
216
+ prefix : & [ u8 ] ,
185
217
chain_id : ChainId ,
186
218
channel_id : ChannelId ,
187
219
channel_nonce : ChannelNonce ,
@@ -193,7 +225,7 @@ where
193
225
let mut to_delete = vec ! [ ] ;
194
226
if let Some ( latest_relay_nonce) = channel_nonce. relay_msg_nonce {
195
227
let last_cleanup_relay_nonce_key =
196
- xdm_keys:: get_key_for_last_cleanup_relay_nonce ( chain_id, channel_id) ;
228
+ xdm_keys:: get_key_for_last_cleanup_relay_nonce ( prefix , chain_id, channel_id) ;
197
229
let last_cleaned_up_nonce =
198
230
load_decode :: < _ , Nonce > ( backend, last_cleanup_relay_nonce_key. as_slice ( ) ) ?;
199
231
@@ -204,17 +236,19 @@ where
204
236
205
237
tracing:: debug!(
206
238
target: LOG_TARGET ,
207
- "Cleaning Relay xdm keys for {:?} channel: {:?} from: {:?} to: {:?}" ,
239
+ "[{:?}]Cleaning Relay xdm keys for {:?} channel: {:?} from: {:?} to: {:?}" ,
240
+ to_hex( prefix, false ) ,
208
241
chain_id,
209
242
channel_id,
210
243
from_nonce,
211
244
latest_relay_nonce
212
245
) ;
213
246
214
247
while from_nonce <= latest_relay_nonce {
215
- to_delete. push ( xdm_keys:: get_key_for_xdm_relay ( (
216
- chain_id, channel_id, from_nonce,
217
- ) ) ) ;
248
+ to_delete. push ( xdm_keys:: get_key_for_xdm_relay (
249
+ prefix,
250
+ ( chain_id, channel_id, from_nonce) ,
251
+ ) ) ;
218
252
from_nonce = from_nonce. saturating_add ( Nonce :: one ( ) ) ;
219
253
}
220
254
@@ -223,7 +257,7 @@ where
223
257
224
258
if let Some ( latest_relay_response_nonce) = channel_nonce. relay_response_msg_nonce {
225
259
let last_cleanup_relay_response_nonce_key =
226
- xdm_keys:: get_key_for_last_cleanup_relay_response_nonce ( chain_id, channel_id) ;
260
+ xdm_keys:: get_key_for_last_cleanup_relay_response_nonce ( prefix , chain_id, channel_id) ;
227
261
let last_cleaned_up_nonce =
228
262
load_decode :: < _ , Nonce > ( backend, last_cleanup_relay_response_nonce_key. as_slice ( ) ) ?;
229
263
@@ -234,17 +268,19 @@ where
234
268
235
269
tracing:: debug!(
236
270
target: LOG_TARGET ,
237
- "Cleaning Relay response xdm keys for {:?} channel: {:?} from: {:?} to: {:?}" ,
271
+ "[{:?}]Cleaning Relay response xdm keys for {:?} channel: {:?} from: {:?} to: {:?}" ,
272
+ to_hex( prefix, false ) ,
238
273
chain_id,
239
274
channel_id,
240
275
from_nonce,
241
276
latest_relay_response_nonce
242
277
) ;
243
278
244
279
while from_nonce <= latest_relay_response_nonce {
245
- to_delete. push ( xdm_keys:: get_key_for_xdm_relay_response ( (
246
- chain_id, channel_id, from_nonce,
247
- ) ) ) ;
280
+ to_delete. push ( xdm_keys:: get_key_for_xdm_relay_response (
281
+ prefix,
282
+ ( chain_id, channel_id, from_nonce) ,
283
+ ) ) ;
248
284
from_nonce = from_nonce. saturating_add ( Nonce :: one ( ) ) ;
249
285
}
250
286
0 commit comments