@@ -8,12 +8,11 @@ use fil_actors_runtime::builtin::singletons::SYSTEM_ACTOR_ADDR;
88use fil_actors_runtime:: runtime:: { ActorCode , Runtime } ;
99use fil_actors_runtime:: ActorDowncast ;
1010use fil_actors_runtime:: ActorError ;
11- use fil_actors_runtime:: Map ;
1211use fvm_ipld_hamt:: BytesKey ;
1312use fvm_shared:: error:: ExitCode ;
1413
1514use crate :: DeleteObjectParams ;
16- use crate :: { Method , PutObjectParams , State , BIT_WIDTH , OBJECTSTORE_ACTOR_NAME } ;
15+ use crate :: { Method , PutObjectParams , State , OBJECTSTORE_ACTOR_NAME } ;
1716
1817fil_actors_runtime:: wasm_trampoline!( Actor ) ;
1918
@@ -26,7 +25,10 @@ impl Actor {
2625 rt. validate_immediate_caller_is ( std:: iter:: once ( & SYSTEM_ACTOR_ADDR ) ) ?;
2726
2827 let state = State :: new ( rt. store ( ) ) . map_err ( |e| {
29- e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to create empty Hamt" )
28+ e. downcast_default (
29+ ExitCode :: USR_ILLEGAL_STATE ,
30+ "failed to construct empty store" ,
31+ )
3032 } ) ?;
3133
3234 rt. create ( & state)
@@ -37,37 +39,10 @@ impl Actor {
3739 rt. validate_immediate_caller_accept_any ( ) ?;
3840
3941 rt. transaction ( |st : & mut State , rt| {
40- // Load the root Hamt
41- let mut hamt = Map :: < _ , Vec < u8 > > :: load_with_bit_width ( & st. root , rt. store ( ) , BIT_WIDTH )
42+ st. append ( rt. store ( ) , BytesKey ( params. key ) , params. content )
4243 . map_err ( |e| {
43- e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to load Hamt root" )
44- } ) ?;
45-
46- let key = BytesKey ( params. key ) ;
47-
48- let new_content = match hamt. get ( & key) . map_err ( |e| {
49- e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to get object" )
50- } ) ? {
51- Some ( existing) => {
52- // Append the object to the existing object
53- let mut new_content = existing. clone ( ) ;
54- new_content. extend ( params. content ) ;
55- new_content
56- }
57- None => params. content ,
58- } ;
59-
60- // Put the new content into the Hamt
61- hamt. set ( key, new_content) . map_err ( |e| {
62- e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to update key" )
63- } ) ?;
64-
65- // Save the new Hamt cid root
66- st. root = hamt. flush ( ) . map_err ( |e| {
67- e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to save root" )
68- } ) ?;
69-
70- Ok ( ( ) )
44+ e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to append to object" )
45+ } )
7146 } ) ?;
7247
7348 Ok ( ( ) )
@@ -78,25 +53,10 @@ impl Actor {
7853 rt. validate_immediate_caller_accept_any ( ) ?;
7954
8055 rt. transaction ( |st : & mut State , rt| {
81- // Load the root Hamt
82- let mut hamt =
83- Map :: load_with_bit_width ( & st. root , rt. store ( ) , BIT_WIDTH ) . map_err ( |e| {
84- e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to load Hamt root" )
85- } ) ?;
86-
87- // Put the object into the Hamt
88- // TODO: We could use set_if_absent here to avoid overwriting existing objects.
89- hamt. set ( BytesKey ( params. key ) , params. content )
56+ st. put ( rt. store ( ) , BytesKey ( params. key ) , params. content )
9057 . map_err ( |e| {
91- e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to update key" )
92- } ) ?;
93-
94- // Save the new Hamt cid root
95- st. root = hamt. flush ( ) . map_err ( |e| {
96- e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to save root" )
97- } ) ?;
98-
99- Ok ( ( ) )
58+ e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to put object" )
59+ } )
10060 } ) ?;
10161
10262 Ok ( ( ) )
@@ -107,23 +67,9 @@ impl Actor {
10767 rt. validate_immediate_caller_accept_any ( ) ?;
10868
10969 rt. transaction ( |st : & mut State , rt| {
110- // Load the root Hamt
111- let mut hamt = Map :: < _ , Vec < u8 > > :: load_with_bit_width ( & st. root , rt. store ( ) , BIT_WIDTH )
112- . map_err ( |e| {
113- e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to load Hamt root" )
114- } ) ?;
115-
116- // Delete the object from the Hamt
117- hamt. delete ( & BytesKey ( params. key ) ) . map_err ( |e| {
118- e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to delete object" )
119- } ) ?;
120-
121- // Save the new Hamt cid root
122- st. root = hamt. flush ( ) . map_err ( |e| {
123- e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to save root" )
124- } ) ?;
125-
126- Ok ( ( ) )
70+ st. delete ( rt. store ( ) , & BytesKey ( params. key ) ) . map_err ( |e| {
71+ e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to delete to object" )
72+ } )
12773 } ) ?;
12874
12975 Ok ( ( ) )
@@ -132,37 +78,17 @@ impl Actor {
13278 fn get_object ( rt : & impl Runtime , key : Vec < u8 > ) -> Result < Option < Vec < u8 > > , ActorError > {
13379 let st: State = rt. state ( ) ?;
13480
135- // Load the root Hamt
136- let hamt = Map :: < _ , Vec < u8 > > :: load_with_bit_width ( & st. root , rt. store ( ) , BIT_WIDTH )
137- . map_err ( |e| {
138- e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to load Hamt root" )
139- } ) ?;
140-
141- // Get the object from the Hamt
142- hamt. get ( & BytesKey ( key) )
81+ st. get ( rt. store ( ) , & BytesKey ( key) )
14382 . map_err ( |e| e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to get object" ) )
144- . map ( |v| v. map ( |inner| inner. to_owned ( ) ) )
14583 }
14684
14785 fn list_objects ( rt : & impl Runtime ) -> Result < Option < Vec < Vec < u8 > > > , ActorError > {
14886 let st: State = rt. state ( ) ?;
14987
150- // Load the root Hamt
151- let hamt = Map :: < _ , Vec < u8 > > :: load_with_bit_width ( & st. root , rt. store ( ) , BIT_WIDTH )
152- . map_err ( |e| {
153- e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to load Hamt root" )
154- } ) ?;
155-
156- let mut keys = Vec :: new ( ) ;
157-
158- // List the keys from each item in the Hamt
159- hamt. for_each ( |k, _| {
160- keys. push ( k. 0 . to_owned ( ) ) ;
161- Ok ( ( ) )
162- } )
163- . map_err ( |e| e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to list objects" ) ) ?;
164-
165- Ok ( Some ( keys) )
88+ let objects = st. list ( rt. store ( ) ) . map_err ( |e| {
89+ e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to list objects" )
90+ } ) ?;
91+ Ok ( Some ( objects) )
16692 }
16793}
16894
0 commit comments