17
17
//! Runtime component for handling disputes of parachain candidates.
18
18
19
19
use crate :: {
20
+ shared,
20
21
configuration:: { self , HostConfiguration } ,
21
22
initializer:: SessionChangeNotification ,
22
23
session_info,
@@ -31,7 +32,7 @@ use primitives::v1::{
31
32
ValidDisputeStatementKind , ValidatorId , ValidatorIndex , ValidatorSignature ,
32
33
} ;
33
34
use sp_runtime:: {
34
- traits:: { AppVerify , One , Saturating , Zero } ,
35
+ traits:: { AppVerify , Saturating } ,
35
36
DispatchError , RuntimeDebug , SaturatedConversion ,
36
37
} ;
37
38
use sp_std:: { collections:: btree_set:: BTreeSet , prelude:: * } ;
@@ -116,10 +117,10 @@ pub trait DisputesHandler<BlockNumber> {
116
117
) -> Result < Vec < ( SessionIndex , CandidateHash ) > , DispatchError > ;
117
118
118
119
/// Note that the given candidate has been included.
119
- fn note_included (
120
+ fn process_included (
120
121
session : SessionIndex ,
121
122
candidate_hash : CandidateHash ,
122
- included_in : BlockNumber ,
123
+ revert_to : BlockNumber ,
123
124
) ;
124
125
125
126
/// Whether the given candidate could be invalid, i.e. there is an ongoing
@@ -151,10 +152,10 @@ impl<BlockNumber> DisputesHandler<BlockNumber> for () {
151
152
Ok ( Vec :: new ( ) )
152
153
}
153
154
154
- fn note_included (
155
+ fn process_included (
155
156
_session : SessionIndex ,
156
157
_candidate_hash : CandidateHash ,
157
- _included_in : BlockNumber ,
158
+ _revert_to : BlockNumber ,
158
159
) {
159
160
}
160
161
@@ -186,12 +187,12 @@ impl<T: Config> DisputesHandler<T::BlockNumber> for pallet::Pallet<T> {
186
187
pallet:: Pallet :: < T > :: provide_multi_dispute_data ( statement_sets)
187
188
}
188
189
189
- fn note_included (
190
+ fn process_included (
190
191
session : SessionIndex ,
191
192
candidate_hash : CandidateHash ,
192
- included_in : T :: BlockNumber ,
193
+ revert_to : T :: BlockNumber ,
193
194
) {
194
- pallet:: Pallet :: < T > :: note_included ( session, candidate_hash, included_in )
195
+ pallet:: Pallet :: < T > :: process_included ( session, candidate_hash, revert_to ) ;
195
196
}
196
197
197
198
fn could_be_invalid ( session : SessionIndex , candidate_hash : CandidateHash ) -> bool {
@@ -243,18 +244,6 @@ pub mod pallet {
243
244
DisputeState < T :: BlockNumber > ,
244
245
> ;
245
246
246
- /// All included blocks on the chain, as well as the block number in this chain that
247
- /// should be reverted back to if the candidate is disputed and determined to be invalid.
248
- #[ pallet:: storage]
249
- pub ( super ) type Included < T : Config > = StorageDoubleMap <
250
- _ ,
251
- Twox64Concat ,
252
- SessionIndex ,
253
- Blake2_128Concat ,
254
- CandidateHash ,
255
- T :: BlockNumber ,
256
- > ;
257
-
258
247
/// Maps session indices to a vector indicating the number of potentially-spam disputes
259
248
/// each validator is participating in. Potentially-spam disputes are remote disputes which have
260
249
/// fewer than `byzantine_threshold + 1` validators.
@@ -565,7 +554,11 @@ impl<T: Config> Pallet<T> {
565
554
dispute. concluded_at = Some ( now) ;
566
555
<Disputes < T > >:: insert ( session_index, candidate_hash, & dispute) ;
567
556
568
- if <Included < T > >:: contains_key ( & session_index, & candidate_hash) {
557
+ let is_local = <shared:: Pallet < T > >:: is_included_candidate (
558
+ & session_index,
559
+ & candidate_hash,
560
+ ) ;
561
+ if is_local {
569
562
// Local disputes don't count towards spam.
570
563
571
564
weight += T :: DbWeight :: get ( ) . reads_writes ( 1 , 1 ) ;
@@ -632,7 +625,7 @@ impl<T: Config> Pallet<T> {
632
625
633
626
// This is larger, and will be extracted to the `shared` module for more proper pruning.
634
627
// TODO: https://github.com/paritytech/polkadot/issues/3469
635
- < Included < T > > :: remove_prefix ( to_prune, None ) ;
628
+ shared :: Pallet :: < T > :: prune_included_candidates ( to_prune) ;
636
629
SpamSlots :: < T > :: remove ( to_prune) ;
637
630
}
638
631
@@ -787,7 +780,7 @@ impl<T: Config> Pallet<T> {
787
780
} ;
788
781
789
782
// Apply spam slot changes. Bail early if too many occupied.
790
- let is_local = <Included < T > >:: contains_key ( & set. session , & set. candidate_hash ) ;
783
+ let is_local = <shared :: Pallet < T > >:: is_included_candidate ( & set. session , & set. candidate_hash ) ;
791
784
if !is_local {
792
785
let mut spam_slots: Vec < u32 > =
793
786
SpamSlots :: < T > :: get ( & set. session ) . unwrap_or_else ( || vec ! [ 0 ; n_validators] ) ;
@@ -919,7 +912,10 @@ impl<T: Config> Pallet<T> {
919
912
} ;
920
913
921
914
// Apply spam slot changes. Bail early if too many occupied.
922
- let is_local = <Included < T > >:: contains_key ( & set. session , & set. candidate_hash ) ;
915
+ let is_local = <shared:: Pallet < T > >:: is_included_candidate (
916
+ & set. session ,
917
+ & set. candidate_hash ,
918
+ ) ;
923
919
if !is_local {
924
920
let mut spam_slots: Vec < u32 > =
925
921
SpamSlots :: < T > :: get ( & set. session ) . unwrap_or_else ( || vec ! [ 0 ; n_validators] ) ;
@@ -993,7 +989,10 @@ impl<T: Config> Pallet<T> {
993
989
994
990
// Freeze if just concluded against some local candidate
995
991
if summary. new_flags . contains ( DisputeStateFlags :: AGAINST_SUPERMAJORITY ) {
996
- if let Some ( revert_to) = <Included < T > >:: get ( & set. session , & set. candidate_hash ) {
992
+ if let Some ( ( revert_to, _) ) = <shared:: Pallet < T > >:: get_included_candidate (
993
+ & set. session ,
994
+ & set. candidate_hash ,
995
+ ) {
997
996
Self :: revert_and_freeze ( revert_to) ;
998
997
}
999
998
}
@@ -1006,19 +1005,11 @@ impl<T: Config> Pallet<T> {
1006
1005
<Disputes < T > >:: iter ( ) . collect ( )
1007
1006
}
1008
1007
1009
- pub ( crate ) fn note_included (
1008
+ pub ( crate ) fn process_included (
1010
1009
session : SessionIndex ,
1011
1010
candidate_hash : CandidateHash ,
1012
- included_in : T :: BlockNumber ,
1011
+ revert_to : T :: BlockNumber ,
1013
1012
) {
1014
- if included_in. is_zero ( ) {
1015
- return
1016
- }
1017
-
1018
- let revert_to = included_in - One :: one ( ) ;
1019
-
1020
- <Included < T > >:: insert ( & session, & candidate_hash, revert_to) ;
1021
-
1022
1013
// If we just included a block locally which has a live dispute, decrement spam slots
1023
1014
// for any involved validators, if the dispute is not already confirmed by f + 1.
1024
1015
if let Some ( state) = <Disputes < T > >:: get ( & session, candidate_hash) {
@@ -1120,6 +1111,7 @@ fn check_signature(
1120
1111
#[ cfg( test) ]
1121
1112
mod tests {
1122
1113
use super :: * ;
1114
+ use primitives:: v1:: CoreIndex ;
1123
1115
use crate :: mock:: {
1124
1116
new_test_ext, AccountId , AllPallets , Initializer , MockGenesisConfig , System , Test ,
1125
1117
PUNISH_VALIDATORS_AGAINST , PUNISH_VALIDATORS_FOR , PUNISH_VALIDATORS_INCONCLUSIVE ,
@@ -1168,6 +1160,21 @@ mod tests {
1168
1160
}
1169
1161
}
1170
1162
1163
+ fn include_candidate ( session : SessionIndex , candidate_hash : & CandidateHash , block_number : BlockNumber ) {
1164
+ if let Some ( revert_to) = shared:: Pallet :: < Test > :: note_included_candidate (
1165
+ session,
1166
+ candidate_hash. clone ( ) ,
1167
+ block_number,
1168
+ CoreIndex ( 0 ) ,
1169
+ ) {
1170
+ Pallet :: < Test > :: process_included (
1171
+ session,
1172
+ candidate_hash. clone ( ) ,
1173
+ revert_to,
1174
+ ) ;
1175
+ }
1176
+ }
1177
+
1171
1178
#[ test]
1172
1179
fn test_dispute_state_flag_from_state ( ) {
1173
1180
assert_eq ! (
@@ -1466,13 +1473,13 @@ mod tests {
1466
1473
let v0 = <ValidatorId as CryptoType >:: Pair :: generate ( ) . 0 ;
1467
1474
1468
1475
let candidate_hash = CandidateHash ( sp_core:: H256 :: repeat_byte ( 1 ) ) ;
1469
- Pallet :: < Test > :: note_included ( 0 , candidate_hash. clone ( ) , 0 ) ;
1470
- Pallet :: < Test > :: note_included ( 1 , candidate_hash. clone ( ) , 1 ) ;
1471
- Pallet :: < Test > :: note_included ( 2 , candidate_hash. clone ( ) , 2 ) ;
1472
- Pallet :: < Test > :: note_included ( 3 , candidate_hash. clone ( ) , 3 ) ;
1473
- Pallet :: < Test > :: note_included ( 4 , candidate_hash. clone ( ) , 4 ) ;
1474
- Pallet :: < Test > :: note_included ( 5 , candidate_hash. clone ( ) , 5 ) ;
1475
- Pallet :: < Test > :: note_included ( 6 , candidate_hash. clone ( ) , 5 ) ;
1476
+ include_candidate ( 0 , & candidate_hash, 0 ) ;
1477
+ include_candidate ( 1 , & candidate_hash, 1 ) ;
1478
+ include_candidate ( 2 , & candidate_hash, 2 ) ;
1479
+ include_candidate ( 3 , & candidate_hash, 3 ) ;
1480
+ include_candidate ( 4 , & candidate_hash, 4 ) ;
1481
+ include_candidate ( 5 , & candidate_hash, 5 ) ;
1482
+ include_candidate ( 6 , & candidate_hash, 5 ) ;
1476
1483
1477
1484
run_to_block ( 7 , |b| {
1478
1485
// a new session at each block
@@ -1482,13 +1489,13 @@ mod tests {
1482
1489
// current session is 7,
1483
1490
// we keep for dispute_period + 1 session and we remove in on_finalize
1484
1491
// thus we keep info for session 3, 4, 5, 6, 7.
1485
- assert_eq ! ( Included :: <Test >:: iter_prefix ( 0 ) . count( ) , 0 ) ;
1486
- assert_eq ! ( Included :: <Test >:: iter_prefix ( 1 ) . count( ) , 0 ) ;
1487
- assert_eq ! ( Included :: <Test >:: iter_prefix ( 2 ) . count( ) , 0 ) ;
1488
- assert_eq ! ( Included :: <Test >:: iter_prefix ( 3 ) . count( ) , 1 ) ;
1489
- assert_eq ! ( Included :: <Test >:: iter_prefix ( 4 ) . count( ) , 1 ) ;
1490
- assert_eq ! ( Included :: <Test >:: iter_prefix ( 5 ) . count( ) , 1 ) ;
1491
- assert_eq ! ( Included :: <Test >:: iter_prefix ( 6 ) . count( ) , 1 ) ;
1492
+ assert_eq ! ( shared :: Pallet :: <Test >:: included_candidates_iter_prefix ( 0 ) . count( ) , 0 ) ;
1493
+ assert_eq ! ( shared :: Pallet :: <Test >:: included_candidates_iter_prefix ( 1 ) . count( ) , 0 ) ;
1494
+ assert_eq ! ( shared :: Pallet :: <Test >:: included_candidates_iter_prefix ( 2 ) . count( ) , 0 ) ;
1495
+ assert_eq ! ( shared :: Pallet :: <Test >:: included_candidates_iter_prefix ( 3 ) . count( ) , 1 ) ;
1496
+ assert_eq ! ( shared :: Pallet :: <Test >:: included_candidates_iter_prefix ( 4 ) . count( ) , 1 ) ;
1497
+ assert_eq ! ( shared :: Pallet :: <Test >:: included_candidates_iter_prefix ( 5 ) . count( ) , 1 ) ;
1498
+ assert_eq ! ( shared :: Pallet :: <Test >:: included_candidates_iter_prefix ( 6 ) . count( ) , 1 ) ;
1492
1499
} ) ;
1493
1500
}
1494
1501
@@ -1590,7 +1597,7 @@ mod tests {
1590
1597
}
1591
1598
1592
1599
#[ test]
1593
- fn test_freeze_on_note_included ( ) {
1600
+ fn test_freeze_on_process_included ( ) {
1594
1601
new_test_ext ( Default :: default ( ) ) . execute_with ( || {
1595
1602
let v0 = <ValidatorId as CryptoType >:: Pair :: generate ( ) . 0 ;
1596
1603
@@ -1620,7 +1627,7 @@ mod tests {
1620
1627
} ] ;
1621
1628
assert ! ( Pallet :: <Test >:: provide_multi_dispute_data( stmts) . is_ok( ) ) ;
1622
1629
1623
- Pallet :: < Test > :: note_included ( 3 , candidate_hash. clone ( ) , 3 ) ;
1630
+ include_candidate ( 3 , & candidate_hash, 3 ) ;
1624
1631
assert_eq ! ( Frozen :: <Test >:: get( ) , Some ( 2 ) ) ;
1625
1632
} ) ;
1626
1633
}
@@ -1637,7 +1644,7 @@ mod tests {
1637
1644
1638
1645
let candidate_hash = CandidateHash ( sp_core:: H256 :: repeat_byte ( 1 ) ) ;
1639
1646
1640
- Pallet :: < Test > :: note_included ( 3 , candidate_hash. clone ( ) , 3 ) ;
1647
+ include_candidate ( 3 , & candidate_hash, 3 ) ;
1641
1648
1642
1649
// v0 votes for 3
1643
1650
let stmts = vec ! [ DisputeStatementSet {
@@ -1666,7 +1673,7 @@ mod tests {
1666
1673
// * provide_multi_dispute: with success scenario
1667
1674
// * disputes: correctness of datas
1668
1675
// * could_be_invalid: correctness of datas
1669
- // * note_included : decrement spam correctly
1676
+ // * process_included : decrement spam correctly
1670
1677
// * spam slots: correctly incremented and decremented
1671
1678
// * ensure rewards and punishment are correctly called.
1672
1679
#[ test]
@@ -1942,7 +1949,7 @@ mod tests {
1942
1949
1943
1950
// Ensure inclusion removes spam slots
1944
1951
assert_eq ! ( SpamSlots :: <Test >:: get( 4 ) , Some ( vec![ 0 , 0 , 0 , 1 ] ) ) ;
1945
- Pallet :: < Test > :: note_included ( 4 , candidate_hash. clone ( ) , 4 ) ;
1952
+ include_candidate ( 4 , & candidate_hash, 4 ) ;
1946
1953
assert_eq ! ( SpamSlots :: <Test >:: get( 4 ) , Some ( vec![ 0 , 0 , 0 , 0 ] ) ) ;
1947
1954
1948
1955
// Ensure the reward_validator function was correctly called
0 commit comments