1
1
// Copyright 2019-2022 ChainSafe Systems
2
2
// SPDX-License-Identifier: Apache-2.0, MIT
3
3
4
- use anyhow:: anyhow;
5
4
use cid:: Cid ;
6
5
use fil_actors_runtime:: {
7
- actor_error, make_empty_map, make_map_with_root_and_bitwidth, FIRST_NON_SINGLETON_ADDR ,
6
+ actor_error, make_empty_map, make_map_with_root_and_bitwidth, ActorError , AsActorError ,
7
+ FIRST_NON_SINGLETON_ADDR ,
8
8
} ;
9
9
use fvm_ipld_blockstore:: Blockstore ;
10
10
use fvm_ipld_encoding:: tuple:: * ;
11
11
use fvm_ipld_encoding:: Cbor ;
12
12
use fvm_shared:: address:: { Address , Protocol } ;
13
+ use fvm_shared:: error:: ExitCode ;
13
14
use fvm_shared:: { ActorID , HAMT_BIT_WIDTH } ;
14
15
15
16
/// State is reponsible for creating
@@ -21,10 +22,10 @@ pub struct State {
21
22
}
22
23
23
24
impl State {
24
- pub fn new < BS : Blockstore > ( store : & BS , network_name : String ) -> anyhow :: Result < Self > {
25
+ pub fn new < BS : Blockstore > ( store : & BS , network_name : String ) -> Result < Self , ActorError > {
25
26
let empty_map = make_empty_map :: < _ , ( ) > ( store, HAMT_BIT_WIDTH )
26
27
. flush ( )
27
- . map_err ( |e| anyhow ! ( "failed to create empty map: {}" , e ) ) ?;
28
+ . context_code ( ExitCode :: USR_ILLEGAL_STATE , "failed to create empty map" ) ?;
28
29
Ok ( Self { address_map : empty_map, next_id : FIRST_NON_SINGLETON_ADDR , network_name } )
29
30
}
30
31
@@ -36,22 +37,26 @@ impl State {
36
37
& mut self ,
37
38
store : & BS ,
38
39
addr : & Address ,
39
- ) -> anyhow :: Result < ActorID > {
40
+ ) -> Result < ActorID , ActorError > {
40
41
let id = self . next_id ;
41
42
self . next_id += 1 ;
42
43
43
- let mut map = make_map_with_root_and_bitwidth ( & self . address_map , store, HAMT_BIT_WIDTH ) ?;
44
- let is_new = map. set_if_absent ( addr. to_bytes ( ) . into ( ) , id) ?;
44
+ let mut map = make_map_with_root_and_bitwidth ( & self . address_map , store, HAMT_BIT_WIDTH )
45
+ . context_code ( ExitCode :: USR_ILLEGAL_STATE , "failed to load address map" ) ?;
46
+ let is_new = map
47
+ . set_if_absent ( addr. to_bytes ( ) . into ( ) , id)
48
+ . context_code ( ExitCode :: USR_ILLEGAL_STATE , "failed to set map key" ) ?;
45
49
if !is_new {
46
50
// this is impossible today as the robust address is a hash of unique inputs
47
51
// but in close future predictable address generation will make this possible
48
- return Err ( anyhow ! ( actor_error!(
52
+ return Err ( actor_error ! (
49
53
forbidden,
50
54
"robust address {} is already allocated in the address map" ,
51
55
addr
52
- ) ) ) ;
56
+ ) ) ;
53
57
}
54
- self . address_map = map. flush ( ) ?;
58
+ self . address_map =
59
+ map. flush ( ) . context_code ( ExitCode :: USR_ILLEGAL_STATE , "failed to store address map" ) ?;
55
60
56
61
Ok ( id)
57
62
}
@@ -70,14 +75,18 @@ impl State {
70
75
& self ,
71
76
store : & BS ,
72
77
addr : & Address ,
73
- ) -> anyhow :: Result < Option < Address > > {
78
+ ) -> Result < Option < Address > , ActorError > {
74
79
if addr. protocol ( ) == Protocol :: ID {
75
80
return Ok ( Some ( * addr) ) ;
76
81
}
77
82
78
- let map = make_map_with_root_and_bitwidth ( & self . address_map , store, HAMT_BIT_WIDTH ) ?;
83
+ let map = make_map_with_root_and_bitwidth ( & self . address_map , store, HAMT_BIT_WIDTH )
84
+ . context_code ( ExitCode :: USR_ILLEGAL_STATE , "failed to load address map" ) ?;
79
85
80
- Ok ( map. get ( & addr. to_bytes ( ) ) ?. copied ( ) . map ( Address :: new_id) )
86
+ let found = map
87
+ . get ( & addr. to_bytes ( ) )
88
+ . context_code ( ExitCode :: USR_ILLEGAL_STATE , "failed to get address entry" ) ?;
89
+ Ok ( found. copied ( ) . map ( Address :: new_id) )
81
90
}
82
91
}
83
92
0 commit comments