@@ -67,7 +67,7 @@ use bp_runtime::{BasicOperatingMode, ChainId, OwnedBridgeModule, PreComputedSize
6767use codec:: { Decode , Encode , MaxEncodedLen } ;
6868use frame_support:: { dispatch:: PostDispatchInfo , ensure, fail, traits:: Get } ;
6969use sp_runtime:: traits:: UniqueSaturatedFrom ;
70- use sp_std:: { cell :: RefCell , marker:: PhantomData , prelude:: * } ;
70+ use sp_std:: { marker:: PhantomData , prelude:: * } ;
7171
7272mod inbound_lane;
7373mod outbound_lane;
@@ -319,7 +319,7 @@ pub mod pallet {
319319
320320 // subtract extra storage proof bytes from the actual PoV size - there may be
321321 // less unrewarded relayers than the maximal configured value
322- let lane_extra_proof_size_bytes = lane. storage ( ) . extra_proof_size_bytes ( ) ;
322+ let lane_extra_proof_size_bytes = lane. storage_mut ( ) . extra_proof_size_bytes ( ) ;
323323 actual_weight = actual_weight. set_proof_size (
324324 actual_weight. proof_size ( ) . saturating_sub ( lane_extra_proof_size_bytes) ,
325325 ) ;
@@ -332,7 +332,7 @@ pub mod pallet {
332332 "Received lane {:?} state update: latest_confirmed_nonce={}. Unrewarded relayers: {:?}" ,
333333 lane_id,
334334 updated_latest_confirmed_nonce,
335- UnrewardedRelayersState :: from( & lane. storage ( ) . data ( ) ) ,
335+ UnrewardedRelayersState :: from( & lane. storage_mut ( ) . get_or_init_data ( ) ) ,
336336 ) ;
337337 }
338338 }
@@ -531,12 +531,12 @@ pub mod pallet {
531531 NotOperatingNormally ,
532532 /// The outbound lane is inactive.
533533 InactiveOutboundLane ,
534- /// The message is too large to be sent over the bridge.
535- MessageIsTooLarge ,
536534 /// Message has been treated as invalid by chain verifier.
537535 MessageRejectedByChainVerifier ( VerificationError ) ,
538536 /// Message has been treated as invalid by lane verifier.
539537 MessageRejectedByLaneVerifier ( VerificationError ) ,
538+ /// Message has been treated as invalid by the pallet logic.
539+ MessageRejectedByPallet ( VerificationError ) ,
540540 /// Submitter has failed to pay fee for delivering and dispatching messages.
541541 FailedToWithdrawMessageFee ,
542542 /// The transaction brings too many messages.
@@ -727,11 +727,9 @@ fn send_message<T: Config<I>, I: 'static>(
727727 // finally, save message in outbound storage and emit event
728728 let encoded_payload = payload. encode ( ) ;
729729 let encoded_payload_len = encoded_payload. len ( ) ;
730- ensure ! (
731- encoded_payload_len <= T :: MaximalOutboundPayloadSize :: get( ) as usize ,
732- Error :: <T , I >:: MessageIsTooLarge
733- ) ;
734- let nonce = lane. send_message ( encoded_payload) ;
730+ let nonce = lane
731+ . send_message ( encoded_payload)
732+ . map_err ( Error :: < T , I > :: MessageRejectedByPallet ) ?;
735733
736734 log:: trace!(
737735 target: LOG_TARGET ,
@@ -761,18 +759,7 @@ fn ensure_normal_operating_mode<T: Config<I>, I: 'static>() -> Result<(), Error<
761759fn inbound_lane < T : Config < I > , I : ' static > (
762760 lane_id : LaneId ,
763761) -> InboundLane < RuntimeInboundLaneStorage < T , I > > {
764- InboundLane :: new ( inbound_lane_storage :: < T , I > ( lane_id) )
765- }
766-
767- /// Creates new runtime inbound lane storage.
768- fn inbound_lane_storage < T : Config < I > , I : ' static > (
769- lane_id : LaneId ,
770- ) -> RuntimeInboundLaneStorage < T , I > {
771- RuntimeInboundLaneStorage {
772- lane_id,
773- cached_data : RefCell :: new ( None ) ,
774- _phantom : Default :: default ( ) ,
775- }
762+ InboundLane :: new ( RuntimeInboundLaneStorage :: from_lane_id ( lane_id) )
776763}
777764
778765/// Creates new outbound lane object, backed by runtime storage.
@@ -785,10 +772,17 @@ fn outbound_lane<T: Config<I>, I: 'static>(
785772/// Runtime inbound lane storage.
786773struct RuntimeInboundLaneStorage < T : Config < I > , I : ' static = ( ) > {
787774 lane_id : LaneId ,
788- cached_data : RefCell < Option < InboundLaneData < T :: InboundRelayer > > > ,
775+ cached_data : Option < InboundLaneData < T :: InboundRelayer > > ,
789776 _phantom : PhantomData < I > ,
790777}
791778
779+ impl < T : Config < I > , I : ' static > RuntimeInboundLaneStorage < T , I > {
780+ /// Creates new runtime inbound lane storage.
781+ fn from_lane_id ( lane_id : LaneId ) -> RuntimeInboundLaneStorage < T , I > {
782+ RuntimeInboundLaneStorage { lane_id, cached_data : None , _phantom : Default :: default ( ) }
783+ }
784+ }
785+
792786impl < T : Config < I > , I : ' static > RuntimeInboundLaneStorage < T , I > {
793787 /// Returns number of bytes that may be subtracted from the PoV component of
794788 /// `receive_messages_proof` call, because the actual inbound lane state is smaller than the
@@ -798,9 +792,9 @@ impl<T: Config<I>, I: 'static> RuntimeInboundLaneStorage<T, I> {
798792 /// `MaxUnrewardedRelayerEntriesAtInboundLane` constant from the pallet configuration. The PoV
799793 /// of the call includes the maximal size of inbound lane state. If the actual size is smaller,
800794 /// we may subtract extra bytes from this component.
801- pub fn extra_proof_size_bytes ( & self ) -> u64 {
795+ pub fn extra_proof_size_bytes ( & mut self ) -> u64 {
802796 let max_encoded_len = StoredInboundLaneData :: < T , I > :: max_encoded_len ( ) ;
803- let relayers_count = self . data ( ) . relayers . len ( ) ;
797+ let relayers_count = self . get_or_init_data ( ) . relayers . len ( ) ;
804798 let actual_encoded_len =
805799 InboundLaneData :: < T :: InboundRelayer > :: encoded_size_hint ( relayers_count)
806800 . unwrap_or ( usize:: MAX ) ;
@@ -823,26 +817,20 @@ impl<T: Config<I>, I: 'static> InboundLaneStorage for RuntimeInboundLaneStorage<
823817 T :: MaxUnconfirmedMessagesAtInboundLane :: get ( )
824818 }
825819
826- fn data ( & self ) -> InboundLaneData < T :: InboundRelayer > {
827- match self . cached_data . clone ( ) . into_inner ( ) {
828- Some ( data) => data,
820+ fn get_or_init_data ( & mut self ) -> InboundLaneData < T :: InboundRelayer > {
821+ match self . cached_data {
822+ Some ( ref data) => data. clone ( ) ,
829823 None => {
830824 let data: InboundLaneData < T :: InboundRelayer > =
831825 InboundLanes :: < T , I > :: get ( self . lane_id ) . into ( ) ;
832- * self . cached_data . try_borrow_mut ( ) . expect (
833- "we're in the single-threaded environment;\
834- we have no recursive borrows; qed",
835- ) = Some ( data. clone ( ) ) ;
826+ self . cached_data = Some ( data. clone ( ) ) ;
836827 data
837828 } ,
838829 }
839830 }
840831
841832 fn set_data ( & mut self , data : InboundLaneData < T :: InboundRelayer > ) {
842- * self . cached_data . try_borrow_mut ( ) . expect (
843- "we're in the single-threaded environment;\
844- we have no recursive borrows; qed",
845- ) = Some ( data. clone ( ) ) ;
833+ self . cached_data = Some ( data. clone ( ) ) ;
846834 InboundLanes :: < T , I > :: insert ( self . lane_id , StoredInboundLaneData :: < T , I > ( data) )
847835 }
848836}
@@ -872,15 +860,17 @@ impl<T: Config<I>, I: 'static> OutboundLaneStorage for RuntimeOutboundLaneStorag
872860 . map ( Into :: into)
873861 }
874862
875- fn save_message ( & mut self , nonce : MessageNonce , message_payload : MessagePayload ) {
863+ fn save_message (
864+ & mut self ,
865+ nonce : MessageNonce ,
866+ message_payload : MessagePayload ,
867+ ) -> Result < ( ) , VerificationError > {
876868 OutboundMessages :: < T , I > :: insert (
877869 MessageKey { lane_id : self . lane_id , nonce } ,
878- StoredMessagePayload :: < T , I > :: try_from ( message_payload) . expect (
879- "save_message is called after all checks in send_message; \
880- send_message checks message size; \
881- qed",
882- ) ,
870+ StoredMessagePayload :: < T , I > :: try_from ( message_payload)
871+ . map_err ( |_| VerificationError :: MessageTooLarge ) ?,
883872 ) ;
873+ Ok ( ( ) )
884874 }
885875
886876 fn remove_message ( & mut self , nonce : & MessageNonce ) {
@@ -1128,7 +1118,9 @@ mod tests {
11281118 TEST_LANE_ID ,
11291119 message_payload. clone( ) ,
11301120 ) ,
1131- Error :: <TestRuntime , ( ) >:: MessageIsTooLarge ,
1121+ Error :: <TestRuntime , ( ) >:: MessageRejectedByPallet (
1122+ VerificationError :: MessageTooLarge
1123+ ) ,
11321124 ) ;
11331125
11341126 // let's check that we're able to send `MAX_OUTBOUND_PAYLOAD_SIZE` messages
@@ -2097,10 +2089,10 @@ mod tests {
20972089 fn storage ( relayer_entries : usize ) -> RuntimeInboundLaneStorage < TestRuntime , ( ) > {
20982090 RuntimeInboundLaneStorage {
20992091 lane_id : Default :: default ( ) ,
2100- cached_data : RefCell :: new ( Some ( InboundLaneData {
2092+ cached_data : Some ( InboundLaneData {
21012093 relayers : vec ! [ relayer_entry( ) ; relayer_entries] . into_iter ( ) . collect ( ) ,
21022094 last_confirmed_nonce : 0 ,
2103- } ) ) ,
2095+ } ) ,
21042096 _phantom : Default :: default ( ) ,
21052097 }
21062098 }
0 commit comments