1
+ use anyhow:: { anyhow, Context } ;
1
2
use clap:: { Parser , Subcommand } ;
2
3
use config:: { builder:: DefaultState , ConfigBuilder } ;
3
4
use mithril_common:: {
4
5
crypto_helper:: { key_decode_hex, ProtocolGenesisSigner } ,
5
6
entities:: HexEncodedGenesisSecretKey ,
7
+ StdResult ,
6
8
} ;
7
9
use slog_scope:: debug;
8
- use std:: { error :: Error , path:: PathBuf } ;
10
+ use std:: path:: PathBuf ;
9
11
10
12
use crate :: { dependency_injection:: DependenciesBuilder , tools:: GenesisTools , Configuration } ;
11
13
@@ -18,10 +20,7 @@ pub struct GenesisCommand {
18
20
}
19
21
20
22
impl GenesisCommand {
21
- pub async fn execute (
22
- & self ,
23
- config_builder : ConfigBuilder < DefaultState > ,
24
- ) -> Result < ( ) , Box < dyn Error > > {
23
+ pub async fn execute ( & self , config_builder : ConfigBuilder < DefaultState > ) -> StdResult < ( ) > {
25
24
self . genesis_subcommand . execute ( config_builder) . await
26
25
}
27
26
}
@@ -43,10 +42,7 @@ pub enum GenesisSubCommand {
43
42
}
44
43
45
44
impl GenesisSubCommand {
46
- pub async fn execute (
47
- & self ,
48
- config_builder : ConfigBuilder < DefaultState > ,
49
- ) -> Result < ( ) , Box < dyn Error > > {
45
+ pub async fn execute ( & self , config_builder : ConfigBuilder < DefaultState > ) -> StdResult < ( ) > {
50
46
match self {
51
47
Self :: Bootstrap ( cmd) => cmd. execute ( config_builder) . await ,
52
48
Self :: Export ( cmd) => cmd. execute ( config_builder) . await ,
@@ -65,15 +61,12 @@ pub struct ExportGenesisSubCommand {
65
61
}
66
62
67
63
impl ExportGenesisSubCommand {
68
- pub async fn execute (
69
- & self ,
70
- config_builder : ConfigBuilder < DefaultState > ,
71
- ) -> Result < ( ) , Box < dyn Error > > {
64
+ pub async fn execute ( & self , config_builder : ConfigBuilder < DefaultState > ) -> StdResult < ( ) > {
72
65
let config: Configuration = config_builder
73
66
. build ( )
74
- . map_err ( |e| format ! ( "configuration build error: {e}" ) ) ?
67
+ . with_context ( || "configuration build error" ) ?
75
68
. try_deserialize ( )
76
- . map_err ( |e| format ! ( "configuration deserialize error: {e}" ) ) ?;
69
+ . with_context ( || "configuration deserialize error" ) ?;
77
70
debug ! ( "EXPORT GENESIS command" ; "config" => format!( "{config:?}" ) ) ;
78
71
println ! (
79
72
"Genesis export payload to sign to {}" ,
@@ -84,10 +77,10 @@ impl ExportGenesisSubCommand {
84
77
85
78
let genesis_tools = GenesisTools :: from_dependencies ( dependencies)
86
79
. await
87
- . map_err ( |err| format ! ( "genesis-tools: initialization error: {err}" ) ) ?;
80
+ . with_context ( || "genesis-tools: initialization error" ) ?;
88
81
genesis_tools
89
82
. export_payload_to_sign ( & self . target_path )
90
- . map_err ( |err| format ! ( "genesis-tools: export error: {err}" ) ) ?;
83
+ . with_context ( || "genesis-tools: export error" ) ?;
91
84
Ok ( ( ) )
92
85
}
93
86
}
@@ -100,15 +93,12 @@ pub struct ImportGenesisSubCommand {
100
93
}
101
94
102
95
impl ImportGenesisSubCommand {
103
- pub async fn execute (
104
- & self ,
105
- config_builder : ConfigBuilder < DefaultState > ,
106
- ) -> Result < ( ) , Box < dyn Error > > {
96
+ pub async fn execute ( & self , config_builder : ConfigBuilder < DefaultState > ) -> StdResult < ( ) > {
107
97
let config: Configuration = config_builder
108
98
. build ( )
109
- . map_err ( |e| format ! ( "configuration build error: {e}" ) ) ?
99
+ . with_context ( || "configuration build error" ) ?
110
100
. try_deserialize ( )
111
- . map_err ( |e| format ! ( "configuration deserialize error: {e}" ) ) ?;
101
+ . with_context ( || "configuration deserialize error" ) ?;
112
102
debug ! ( "IMPORT GENESIS command" ; "config" => format!( "{config:?}" ) ) ;
113
103
println ! (
114
104
"Genesis import signed payload from {}" ,
@@ -119,11 +109,11 @@ impl ImportGenesisSubCommand {
119
109
120
110
let genesis_tools = GenesisTools :: from_dependencies ( dependencies)
121
111
. await
122
- . map_err ( |err| format ! ( "genesis-tools: initialization error: {err}" ) ) ?;
112
+ . with_context ( || "genesis-tools: initialization error" ) ?;
123
113
genesis_tools
124
114
. import_payload_signature ( & self . signed_payload_path )
125
115
. await
126
- . map_err ( |err| format ! ( "genesis-tools: import error: {err}" ) ) ?;
116
+ . with_context ( || "genesis-tools: import error" ) ?;
127
117
Ok ( ( ) )
128
118
}
129
119
}
@@ -144,10 +134,7 @@ pub struct SignGenesisSubCommand {
144
134
}
145
135
146
136
impl SignGenesisSubCommand {
147
- pub async fn execute (
148
- & self ,
149
- _config_builder : ConfigBuilder < DefaultState > ,
150
- ) -> Result < ( ) , Box < dyn Error > > {
137
+ pub async fn execute ( & self , _config_builder : ConfigBuilder < DefaultState > ) -> StdResult < ( ) > {
151
138
debug ! ( "SIGN GENESIS command" ) ;
152
139
println ! (
153
140
"Genesis sign payload from {} to {}" ,
@@ -161,7 +148,7 @@ impl SignGenesisSubCommand {
161
148
& self . genesis_secret_key_path ,
162
149
)
163
150
. await
164
- . map_err ( |err| format ! ( "genesis-tools: sign error: {err}" ) ) ?;
151
+ . with_context ( || "genesis-tools: sign error" ) ?;
165
152
166
153
Ok ( ( ) )
167
154
}
@@ -174,29 +161,27 @@ pub struct BootstrapGenesisSubCommand {
174
161
}
175
162
176
163
impl BootstrapGenesisSubCommand {
177
- pub async fn execute (
178
- & self ,
179
- config_builder : ConfigBuilder < DefaultState > ,
180
- ) -> Result < ( ) , Box < dyn Error > > {
164
+ pub async fn execute ( & self , config_builder : ConfigBuilder < DefaultState > ) -> StdResult < ( ) > {
181
165
let config: Configuration = config_builder
182
166
. build ( )
183
- . map_err ( |e| format ! ( "configuration build error: {e}" ) ) ?
167
+ . with_context ( || "configuration build error" ) ?
184
168
. try_deserialize ( )
185
- . map_err ( |e| format ! ( "configuration deserialize error: {e}" ) ) ?;
169
+ . with_context ( || "configuration deserialize error" ) ?;
186
170
debug ! ( "BOOTSTRAP GENESIS command" ; "config" => format!( "{config:?}" ) ) ;
187
171
println ! ( "Genesis bootstrap for test only!" ) ;
188
172
let mut dependencies_builder = DependenciesBuilder :: new ( config. clone ( ) ) ;
189
173
let dependencies = dependencies_builder. create_genesis_container ( ) . await ?;
190
174
191
175
let genesis_tools = GenesisTools :: from_dependencies ( dependencies)
192
176
. await
193
- . map_err ( |err| format ! ( "genesis-tools: initialization error: {err}" ) ) ?;
194
- let genesis_secret_key = key_decode_hex ( & self . genesis_secret_key ) ?;
177
+ . with_context ( || "genesis-tools: initialization error" ) ?;
178
+ let genesis_secret_key = key_decode_hex ( & self . genesis_secret_key )
179
+ . map_err ( |e| anyhow ! ( e) . context ( "json hex decode of genesis secret key failure" ) ) ?;
195
180
let genesis_signer = ProtocolGenesisSigner :: from_secret_key ( genesis_secret_key) ;
196
181
genesis_tools
197
182
. bootstrap_test_genesis_certificate ( genesis_signer)
198
183
. await
199
- . map_err ( |err| format ! ( "genesis-tools: bootstrap error: {err}" ) ) ?;
184
+ . with_context ( || "genesis-tools: bootstrap error" ) ?;
200
185
Ok ( ( ) )
201
186
}
202
187
}
0 commit comments