22// SPDX-License-Identifier: Apache-2.0, MIT
33
44use std:: convert:: TryInto ;
5+ use std:: num:: TryFromIntError ;
56
67use cid:: Cid ;
7- use fil_actors_runtime:: { ActorContext , ActorError , Array } ;
8+ use fil_actors_runtime:: { ActorError , Array } ;
89use fvm_ipld_amt:: Error as AmtError ;
910use fvm_ipld_bitfield:: BitField ;
1011use fvm_ipld_blockstore:: Blockstore ;
@@ -18,8 +19,28 @@ pub struct BitFieldQueue<'db, BS> {
1819 quant : QuantSpec ,
1920}
2021
22+ #[ derive( thiserror:: Error , Debug ) ]
23+ pub enum Error < E > {
24+ #[ error( "amt {0}" ) ]
25+ Amt ( #[ from] AmtError < E > ) ,
26+ #[ error( "conversion failure {0}" ) ]
27+ Int ( #[ from] TryFromIntError ) ,
28+ #[ error( "bitfield {0}" ) ]
29+ Bitfield ( #[ from] fvm_ipld_bitfield:: OutOfRangeError ) ,
30+ }
31+
32+ impl < E : std:: error:: Error > From < Error < E > > for ActorError {
33+ fn from ( e : Error < E > ) -> Self {
34+ match e {
35+ Error :: Amt ( e) => e. into ( ) ,
36+ Error :: Int ( e) => e. into ( ) ,
37+ Error :: Bitfield ( e) => e. into ( ) ,
38+ }
39+ }
40+ }
41+
2142impl < ' db , BS : Blockstore > BitFieldQueue < ' db , BS > {
22- pub fn new ( store : & ' db BS , root : & Cid , quant : QuantSpec ) -> Result < Self , AmtError < BS :: Error > > {
43+ pub fn new ( store : & ' db BS , root : & Cid , quant : QuantSpec ) -> Result < Self , Error < BS :: Error > > {
2344 Ok ( Self { amt : Array :: load ( root, store) ?, quant } )
2445 }
2546
@@ -28,24 +49,17 @@ impl<'db, BS: Blockstore> BitFieldQueue<'db, BS> {
2849 & mut self ,
2950 raw_epoch : ChainEpoch ,
3051 values : & BitField ,
31- ) -> Result < ( ) , ActorError > {
52+ ) -> Result < ( ) , Error < BS :: Error > > {
3253 if values. is_empty ( ) {
3354 // nothing to do.
3455 return Ok ( ( ) ) ;
3556 }
3657
3758 let epoch: u64 = self . quant . quantize_up ( raw_epoch) . try_into ( ) ?;
3859
39- let bitfield = self
40- . amt
41- . get ( epoch)
42- . with_context ( || format ! ( "failed to lookup queue epoch {}" , epoch) ) ?
43- . cloned ( )
44- . unwrap_or_default ( ) ;
60+ let bitfield = self . amt . get ( epoch) ?. cloned ( ) . unwrap_or_default ( ) ;
4561
46- self . amt
47- . set ( epoch, & bitfield | values)
48- . with_context ( || format ! ( "failed to set queue epoch {}" , epoch) ) ?;
62+ self . amt . set ( epoch, & bitfield | values) ?;
4963
5064 Ok ( ( ) )
5165 }
@@ -54,40 +68,36 @@ impl<'db, BS: Blockstore> BitFieldQueue<'db, BS> {
5468 & mut self ,
5569 epoch : ChainEpoch ,
5670 values : impl IntoIterator < Item = u64 > ,
57- ) -> Result < ( ) , ActorError > {
71+ ) -> Result < ( ) , Error < BS :: Error > > {
5872 self . add_to_queue ( epoch, & BitField :: try_from_bits ( values) ?)
5973 }
6074
6175 /// Cut cuts the elements from the bits in the given bitfield out of the queue,
6276 /// shifting other bits down and removing any newly empty entries.
6377 ///
6478 /// See the docs on `BitField::cut` to better understand what it does.
65- pub fn cut ( & mut self , to_cut : & BitField ) -> Result < ( ) , ActorError > {
79+ pub fn cut ( & mut self , to_cut : & BitField ) -> Result < ( ) , Error < BS :: Error > > {
6680 let mut epochs_to_remove = Vec :: < u64 > :: new ( ) ;
6781
68- self . amt
69- . for_each_mut ( |epoch, bitfield| {
70- let bf = bitfield. cut ( to_cut) ;
82+ self . amt . for_each_mut ( |epoch, bitfield| {
83+ let bf = bitfield. cut ( to_cut) ;
7184
72- if bf. is_empty ( ) {
73- epochs_to_remove. push ( epoch) ;
74- } else {
75- * * bitfield = bf;
76- }
77- } )
78- . context ( "failed to cut from bitfield queue" ) ?;
85+ if bf. is_empty ( ) {
86+ epochs_to_remove. push ( epoch) ;
87+ } else {
88+ * * bitfield = bf;
89+ }
90+ } ) ?;
7991
80- self . amt
81- . batch_delete ( epochs_to_remove, true )
82- . context ( "failed to remove empty epochs from bitfield queue" ) ?;
92+ self . amt . batch_delete ( epochs_to_remove, true ) ?;
8393
8494 Ok ( ( ) )
8595 }
8696
8797 pub fn add_many_to_queue_values (
8898 & mut self ,
8999 values : impl IntoIterator < Item = ( ChainEpoch , u64 ) > ,
90- ) -> Result < ( ) , ActorError > {
100+ ) -> Result < ( ) , Error < BS :: Error > > {
91101 // Pre-quantize to reduce the number of updates.
92102 let mut quantized_values: Vec < _ > = values
93103 . into_iter ( )
@@ -112,7 +122,7 @@ impl<'db, BS: Blockstore> BitFieldQueue<'db, BS> {
112122
113123 /// Removes and returns all values with keys less than or equal to until.
114124 /// Modified return value indicates whether this structure has been changed by the call.
115- pub fn pop_until ( & mut self , until : ChainEpoch ) -> Result < ( BitField , bool ) , ActorError > {
125+ pub fn pop_until ( & mut self , until : ChainEpoch ) -> Result < ( BitField , bool ) , Error < BS :: Error > > {
116126 let mut popped_values = BitField :: new ( ) ;
117127 let mut popped_keys = Vec :: < u64 > :: new ( ) ;
118128
0 commit comments