@@ -34,6 +34,13 @@ fn split_in_half(a: [u8; 32]) -> ([u8; 16], [u8; 16]) {
3434 ( high, low)
3535}
3636
37+ /// Update an accumulator by adding or spending a pre-hashed outpoint
38+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
39+ pub enum AccumulatorUpdate {
40+ Add ( [ u8 ; 32 ] ) ,
41+ Spent ( [ u8 ; 32 ] ) ,
42+ }
43+
3744impl Accumulator {
3845 /// The zero accumulator
3946 pub const ZERO : Accumulator = Accumulator { high : 0 , low : 0 } ;
@@ -59,6 +66,14 @@ impl Accumulator {
5966 * self = Self { high, low } ;
6067 }
6168
69+ /// Update the accumulator
70+ pub fn update ( & mut self , update : AccumulatorUpdate ) {
71+ match update {
72+ AccumulatorUpdate :: Add ( added) => self . add_hashed_outpoint ( added) ,
73+ AccumulatorUpdate :: Spent ( spent) => self . spend_hashed_outpoint ( spent) ,
74+ }
75+ }
76+
6277 /// Spend the inputs in a block by subtracing them from the accumulator.
6378 pub fn spend ( & mut self , outpoint : OutPoint ) {
6479 let hash = hash_outpoint ( outpoint) ;
@@ -226,4 +241,27 @@ mod tests {
226241 acc. spend_hashed_outpoint ( hash_two) ;
227242 acc. spend_hashed_outpoint ( hash_three) ;
228243 }
244+
245+ #[ test]
246+ fn test_update_method ( ) {
247+ let [ outpoint_one, outpoint_two, outpoint_three, outpoint_four, outpoint_five] =
248+ make_five_outpoint ( ) ;
249+ let hash_one = hash_outpoint ( outpoint_one) ;
250+ let hash_two = hash_outpoint ( outpoint_two) ;
251+ let hash_three = hash_outpoint ( outpoint_three) ;
252+ let hash_four = hash_outpoint ( outpoint_four) ;
253+ let hash_five = hash_outpoint ( outpoint_five) ;
254+ let mut acc = Accumulator :: default ( ) ;
255+ acc. update ( AccumulatorUpdate :: Add ( hash_one) ) ;
256+ acc. update ( AccumulatorUpdate :: Add ( hash_two) ) ;
257+ acc. update ( AccumulatorUpdate :: Add ( hash_three) ) ;
258+ acc. update ( AccumulatorUpdate :: Add ( hash_four) ) ;
259+ acc. update ( AccumulatorUpdate :: Add ( hash_five) ) ;
260+ acc. update ( AccumulatorUpdate :: Spent ( hash_five) ) ;
261+ acc. update ( AccumulatorUpdate :: Spent ( hash_four) ) ;
262+ acc. update ( AccumulatorUpdate :: Spent ( hash_three) ) ;
263+ acc. update ( AccumulatorUpdate :: Spent ( hash_two) ) ;
264+ acc. update ( AccumulatorUpdate :: Spent ( hash_one) ) ;
265+ assert ! ( acc. is_zero( ) ) ;
266+ }
229267}
0 commit comments