Skip to content

Commit 56854f7

Browse files
committed
tests for post_dispatch and bare_post_dispatch weights
1 parent 281a226 commit 56854f7

1 file changed

Lines changed: 95 additions & 52 deletions

File tree

substrate/primitives/runtime/src/traits/transaction_extension/transaction_extension_pipeline.rs

Lines changed: 95 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,48 @@ declare_pipeline!(
275275
#[cfg(test)]
276276
mod tests {
277277
use super::*;
278-
use crate::transaction_validity::InvalidTransaction;
278+
use crate::{
279+
traits::{ExtensionPostDispatchWeightHandler, Printable, RefundWeight},
280+
transaction_validity::InvalidTransaction,
281+
};
279282
use std::cell::RefCell;
280283

284+
struct MockCall;
285+
286+
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode)]
287+
struct MockPostInfo(sp_weights::Weight);
288+
289+
impl Printable for MockPostInfo {
290+
fn print(&self) {
291+
self.0.print();
292+
}
293+
}
294+
295+
impl ExtensionPostDispatchWeightHandler<()> for MockPostInfo {
296+
fn set_extension_weight(&mut self, _info: &()) {
297+
unimplemented!();
298+
}
299+
}
300+
301+
impl RefundWeight for MockPostInfo {
302+
fn refund(&mut self, weight: sp_weights::Weight) {
303+
self.0 = self.0.saturating_sub(weight);
304+
}
305+
}
306+
307+
impl Dispatchable for MockCall {
308+
type RuntimeOrigin = ();
309+
type Config = ();
310+
type Info = ();
311+
type PostInfo = MockPostInfo;
312+
fn dispatch(
313+
self,
314+
_origin: Self::RuntimeOrigin,
315+
) -> crate::DispatchResultWithInfo<Self::PostInfo> {
316+
panic!("This implementation should not be used for actual dispatch.");
317+
}
318+
}
319+
281320
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, TypeInfo)]
282321
struct TransactionExtensionN<
283322
const WEIGHT: u64,
@@ -312,6 +351,7 @@ mod tests {
312351
Self(explicit)
313352
}
314353
}
354+
315355
impl<
316356
const WEIGHT: u64,
317357
const POST_DISPATCH_WEIGHT: u64,
@@ -320,7 +360,7 @@ mod tests {
320360
const IMPLICIT: u32,
321361
const BARE_VALIDATE: bool,
322362
const BARE_POST_DISPATCH: bool,
323-
> TransactionExtension<()>
363+
> TransactionExtension<MockCall>
324364
for TransactionExtensionN<
325365
WEIGHT,
326366
POST_DISPATCH_WEIGHT,
@@ -338,14 +378,14 @@ mod tests {
338378
}
339379
type Val = u32;
340380
type Pre = u32;
341-
fn weight(&self, _call: &()) -> Weight {
381+
fn weight(&self, _call: &MockCall) -> Weight {
342382
WEIGHT.into()
343383
}
344384
fn validate(
345385
&self,
346-
origin: DispatchOriginOf<()>,
347-
_call: &(),
348-
_info: &DispatchInfoOf<()>,
386+
origin: (),
387+
_call: &MockCall,
388+
_info: &(),
349389
_len: usize,
350390
self_implicit: Self::Implicit,
351391
_inherited_implication: &impl Encode,
@@ -357,37 +397,33 @@ mod tests {
357397
fn prepare(
358398
self,
359399
val: Self::Val,
360-
_origin: &DispatchOriginOf<()>,
361-
_call: &(),
362-
_info: &DispatchInfoOf<()>,
400+
_origin: &(),
401+
_call: &MockCall,
402+
_info: &(),
363403
_len: usize,
364404
) -> Result<Self::Pre, TransactionValidityError> {
365405
assert_eq!(val, VAL);
366406
Ok(PRE)
367407
}
368408
fn post_dispatch_details(
369409
_pre: Self::Pre,
370-
_info: &DispatchInfoOf<()>,
371-
_post_info: &PostDispatchInfoOf<()>,
410+
_info: &(),
411+
_post_info: &MockPostInfo,
372412
_len: usize,
373413
_result: &DispatchResult,
374414
) -> Result<Weight, TransactionValidityError> {
375415
Ok(POST_DISPATCH_WEIGHT.into())
376416
}
377-
fn bare_validate(
378-
_call: &(),
379-
_info: &DispatchInfoOf<()>,
380-
_len: usize,
381-
) -> TransactionValidity {
417+
fn bare_validate(_call: &MockCall, _info: &(), _len: usize) -> TransactionValidity {
382418
if BARE_VALIDATE {
383419
Ok(ValidTransaction::default())
384420
} else {
385421
Err(InvalidTransaction::Custom(0).into())
386422
}
387423
}
388424
fn bare_validate_and_prepare(
389-
_call: &(),
390-
_info: &DispatchInfoOf<()>,
425+
_call: &MockCall,
426+
_info: &(),
391427
_len: usize,
392428
) -> Result<(), TransactionValidityError> {
393429
if BARE_VALIDATE {
@@ -397,12 +433,13 @@ mod tests {
397433
}
398434
}
399435
fn bare_post_dispatch(
400-
_info: &DispatchInfoOf<()>,
401-
_post_info: &mut PostDispatchInfoOf<()>,
436+
_info: &(),
437+
post_info: &mut MockPostInfo,
402438
_len: usize,
403439
_result: &DispatchResult,
404440
) -> Result<(), TransactionValidityError> {
405441
if BARE_POST_DISPATCH {
442+
post_info.refund(POST_DISPATCH_WEIGHT.into());
406443
Ok(())
407444
} else {
408445
Err(InvalidTransaction::Custom(0).into())
@@ -412,27 +449,32 @@ mod tests {
412449

413450
#[test]
414451
fn test_bare() {
415-
type T1 = TransactionExtensionN<0, 0, 0, 0, 0, true, true>;
452+
type T1 = TransactionExtensionN<0, 1, 0, 0, 0, true, true>;
453+
type T1Bis = TransactionExtensionN<0, 2, 0, 0, 0, true, true>;
416454
type T2 = TransactionExtensionN<0, 0, 0, 0, 0, false, false>;
417455

418-
type P1 = TransactionExtensionPipeline<T1, T1>;
419-
P1::bare_validate_and_prepare(&(), &(), 0).expect("success");
420-
P1::bare_validate(&(), &(), 0).expect("success");
421-
P1::bare_post_dispatch(&(), &mut (), 0, &Ok(())).expect("success");
456+
type P1 = TransactionExtensionPipeline<T1, T1Bis, T1>;
457+
P1::bare_validate_and_prepare(&MockCall, &(), 0).expect("success");
458+
P1::bare_validate(&MockCall, &(), 0).expect("success");
459+
let mut post_info = MockPostInfo(100.into());
460+
P1::bare_post_dispatch(&(), &mut post_info, 0, &Ok(())).expect("success");
461+
assert_eq!(post_info.0, (100 - 1 - 2 - 1).into());
422462

423-
type P2 = TransactionExtensionPipeline<T1, T2>;
463+
type P2 = TransactionExtensionPipeline<T1, T1Bis, T2, T1>;
424464
assert_eq!(
425-
P2::bare_validate_and_prepare(&(), &(), 0).unwrap_err(),
465+
P2::bare_validate_and_prepare(&MockCall, &(), 0).unwrap_err(),
426466
InvalidTransaction::Custom(0).into()
427467
);
428468
assert_eq!(
429-
P2::bare_validate(&(), &(), 0).unwrap_err(),
469+
P2::bare_validate(&MockCall, &(), 0).unwrap_err(),
430470
InvalidTransaction::Custom(0).into()
431471
);
472+
let mut post_info = MockPostInfo(100.into());
432473
assert_eq!(
433-
P2::bare_post_dispatch(&(), &mut (), 0, &Ok(())).unwrap_err(),
474+
P2::bare_post_dispatch(&(), &mut post_info, 0, &Ok(())).unwrap_err(),
434475
InvalidTransaction::Custom(0).into()
435476
);
477+
assert_eq!(post_info.0, (100 - 1 - 2).into());
436478
}
437479

438480
const A_WEIGHT: u64 = 3;
@@ -473,22 +515,22 @@ mod tests {
473515

474516
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, TypeInfo)]
475517
struct TransactionExtensionCheck;
476-
impl TransactionExtension<()> for TransactionExtensionCheck {
518+
impl TransactionExtension<MockCall> for TransactionExtensionCheck {
477519
const IDENTIFIER: &'static str = "TransactionExtensionCheck";
478520
type Implicit = ();
479521
fn implicit(&self) -> Result<Self::Implicit, TransactionValidityError> {
480522
Ok(())
481523
}
482524
type Val = u32;
483525
type Pre = u32;
484-
fn weight(&self, _call: &()) -> Weight {
526+
fn weight(&self, _call: &MockCall) -> Weight {
485527
Weight::zero()
486528
}
487529
fn validate(
488530
&self,
489-
origin: DispatchOriginOf<()>,
490-
_call: &(),
491-
_info: &DispatchInfoOf<()>,
531+
origin: (),
532+
_call: &MockCall,
533+
_info: &(),
492534
_len: usize,
493535
_self_implicit: Self::Implicit,
494536
inherited_implication: &impl Encode,
@@ -500,17 +542,17 @@ mod tests {
500542
fn prepare(
501543
self,
502544
_val: Self::Val,
503-
_origin: &DispatchOriginOf<()>,
504-
_call: &(),
505-
_info: &DispatchInfoOf<()>,
545+
_origin: &(),
546+
_call: &MockCall,
547+
_info: &(),
506548
_len: usize,
507549
) -> Result<Self::Pre, TransactionValidityError> {
508550
Ok(0)
509551
}
510552
fn post_dispatch_details(
511553
_pre: Self::Pre,
512-
_info: &DispatchInfoOf<()>,
513-
_post_info: &PostDispatchInfoOf<()>,
554+
_info: &(),
555+
_post_info: &MockPostInfo,
514556
_len: usize,
515557
_result: &DispatchResult,
516558
) -> Result<Weight, TransactionValidityError> {
@@ -528,7 +570,7 @@ mod tests {
528570

529571
t1.validate(
530572
(),
531-
&(),
573+
&MockCall,
532574
&(),
533575
0,
534576
TransactionExtensionPipelineImplicit::from((A_IMPLICIT, B_IMPLICIT, ())),
@@ -552,7 +594,7 @@ mod tests {
552594

553595
t1.validate(
554596
(),
555-
&(),
597+
&MockCall,
556598
&(),
557599
0,
558600
TransactionExtensionPipelineImplicit::from((A_IMPLICIT, (), B_IMPLICIT)),
@@ -577,7 +619,7 @@ mod tests {
577619

578620
t2.validate(
579621
(),
580-
&(),
622+
&MockCall,
581623
&(),
582624
0,
583625
TransactionExtensionPipelineImplicit::from((A_IMPLICIT, (), B_IMPLICIT, A_IMPLICIT)),
@@ -632,7 +674,7 @@ mod tests {
632674

633675
t3.validate(
634676
(),
635-
&(),
677+
&MockCall,
636678
&(),
637679
0,
638680
TransactionExtensionPipelineImplicit::from((
@@ -673,36 +715,37 @@ mod tests {
673715
TransactionExtensionB::new(B_EXPLICIT),
674716
));
675717

676-
let weight = p.weight(&());
677-
718+
let weight = p.weight(&MockCall);
678719
assert_eq!(weight, (A_WEIGHT + B_WEIGHT).into());
679720

680721
let implicit = p.implicit().unwrap();
681-
682722
assert_eq!(implicit, (A_IMPLICIT, B_IMPLICIT).into());
683723

684724
let val = p
685725
.validate(
686726
(),
687-
&(),
727+
&MockCall,
688728
&(),
689729
0,
690730
TransactionExtensionPipelineImplicit::from((A_IMPLICIT, B_IMPLICIT)),
691731
&(),
692732
TransactionSource::Local,
693733
)
694734
.unwrap();
695-
696735
assert_eq!(val.1 .0, A_VAL);
697736
assert_eq!(val.1 .1, B_VAL);
698737

699-
let pre = p.prepare(val.1, &(), &(), &(), 0).unwrap();
700-
738+
let pre = p.prepare(val.1, &(), &MockCall, &(), 0).unwrap();
701739
assert_eq!(pre.0, A_PRE);
702740
assert_eq!(pre.1, B_PRE);
703741

704-
let details = Pipeline::post_dispatch_details(pre, &(), &(), 0, &Ok(())).unwrap();
705-
742+
let details =
743+
Pipeline::post_dispatch_details(pre, &(), &MockPostInfo(100.into()), 0, &Ok(()))
744+
.unwrap();
706745
assert_eq!(details, (A_POST_DISPATCH_WEIGHT + B_POST_DISPATCH_WEIGHT).into());
746+
747+
let mut post_info = MockPostInfo(100.into());
748+
Pipeline::post_dispatch(pre, &(), &mut post_info, 0, &Ok(())).unwrap();
749+
assert_eq!(post_info.0, (100 - A_POST_DISPATCH_WEIGHT - B_POST_DISPATCH_WEIGHT).into());
707750
}
708751
}

0 commit comments

Comments
 (0)