11use derivative:: Derivative ;
22use smallvec:: { smallvec, SmallVec } ;
33use ssz:: { Decode , Encode } ;
4- use state_processing:: { SigVerifiedOp , VerifyOperation } ;
4+ use state_processing:: { SigVerifiedOp , VerifyOperation , VerifyOperationAt } ;
55use std:: collections:: HashSet ;
66use std:: marker:: PhantomData ;
77use types:: {
8- AttesterSlashing , BeaconState , ChainSpec , EthSpec , ForkName , ProposerSlashing ,
8+ AttesterSlashing , BeaconState , ChainSpec , Epoch , EthSpec , ForkName , ProposerSlashing ,
99 SignedBlsToExecutionChange , SignedVoluntaryExit , Slot ,
1010} ;
1111
@@ -87,12 +87,16 @@ impl<E: EthSpec> ObservableOperation<E> for SignedBlsToExecutionChange {
8787}
8888
8989impl < T : ObservableOperation < E > , E : EthSpec > ObservedOperations < T , E > {
90- pub fn verify_and_observe (
90+ pub fn verify_and_observe_parametric < F > (
9191 & mut self ,
9292 op : T ,
93+ validate : F ,
9394 head_state : & BeaconState < E > ,
9495 spec : & ChainSpec ,
95- ) -> Result < ObservationOutcome < T , E > , T :: Error > {
96+ ) -> Result < ObservationOutcome < T , E > , T :: Error >
97+ where
98+ F : Fn ( T ) -> Result < SigVerifiedOp < T , E > , T :: Error > ,
99+ {
96100 self . reset_at_fork_boundary ( head_state. slot ( ) , spec) ;
97101
98102 let observed_validator_indices = & mut self . observed_validator_indices ;
@@ -112,7 +116,7 @@ impl<T: ObservableOperation<E>, E: EthSpec> ObservedOperations<T, E> {
112116 }
113117
114118 // Validate the op using operation-specific logic (`verify_attester_slashing`, etc).
115- let verified_op = op . validate ( head_state , spec ) ?;
119+ let verified_op = validate ( op ) ?;
116120
117121 // Add the relevant indices to the set of known indices to prevent processing of duplicates
118122 // in the future.
@@ -121,6 +125,16 @@ impl<T: ObservableOperation<E>, E: EthSpec> ObservedOperations<T, E> {
121125 Ok ( ObservationOutcome :: New ( verified_op) )
122126 }
123127
128+ pub fn verify_and_observe (
129+ & mut self ,
130+ op : T ,
131+ head_state : & BeaconState < E > ,
132+ spec : & ChainSpec ,
133+ ) -> Result < ObservationOutcome < T , E > , T :: Error > {
134+ let validate = |op : T | op. validate ( head_state, spec) ;
135+ self . verify_and_observe_parametric ( op, validate, head_state, spec)
136+ }
137+
124138 /// Reset the cache when crossing a fork boundary.
125139 ///
126140 /// This prevents an attacker from crafting a self-slashing which is only valid before the fork
@@ -140,3 +154,16 @@ impl<T: ObservableOperation<E>, E: EthSpec> ObservedOperations<T, E> {
140154 }
141155 }
142156}
157+
158+ impl < T : ObservableOperation < E > + VerifyOperationAt < E > , E : EthSpec > ObservedOperations < T , E > {
159+ pub fn verify_and_observe_at (
160+ & mut self ,
161+ op : T ,
162+ verify_at_epoch : Epoch ,
163+ head_state : & BeaconState < E > ,
164+ spec : & ChainSpec ,
165+ ) -> Result < ObservationOutcome < T , E > , T :: Error > {
166+ let validate = |op : T | op. validate_at ( head_state, verify_at_epoch, spec) ;
167+ self . verify_and_observe_parametric ( op, validate, head_state, spec)
168+ }
169+ }
0 commit comments