@@ -7,8 +7,15 @@ extern crate hex;
7
7
extern crate protobuf;
8
8
9
9
use crate :: mock:: {
10
- grpc:: * ,
11
- proto:: { node:: * , node_grpc:: * } ,
10
+ grpc:: { ClientStubExt , Error as GrpcError , Metadata } ,
11
+ proto:: {
12
+ node:: {
13
+ Block , BlockIds , Fragment , FragmentIds , HandshakeRequest , HandshakeResponse , Header ,
14
+ PullBlocksToTipRequest , PullHeadersRequest , PushHeadersResponse , TipRequest ,
15
+ UploadBlocksResponse ,
16
+ } ,
17
+ node_grpc:: { Node , NodeClient , NodeServer } ,
18
+ } ,
12
19
read_into,
13
20
} ;
14
21
use chain_core:: property:: FromStr ;
@@ -42,6 +49,19 @@ macro_rules! response_to_err {
42
49
} } ;
43
50
}
44
51
52
+ error_chain ! {
53
+ errors {
54
+ InvalidRequest ( message: String ) {
55
+ display( "request failed with message {}" , message) ,
56
+ }
57
+
58
+ InvalidAddressFormat ( address: String ) {
59
+ display( "could not parse address '{}'. HINT: accepted format example: /ip4/127.0.0.1/tcp/9000" , address) ,
60
+ }
61
+
62
+ }
63
+ }
64
+
45
65
pub struct JormungandrClient {
46
66
client : NodeClient ,
47
67
host : String ,
@@ -55,6 +75,23 @@ impl Clone for JormungandrClient {
55
75
}
56
76
57
77
impl JormungandrClient {
78
+ pub fn from_address ( address : & str ) -> Result < Self > {
79
+ let elements: Vec < & str > = address. split ( "/" ) . collect ( ) ;
80
+
81
+ let host = elements. get ( 2 ) ;
82
+ let port = elements. get ( 4 ) ;
83
+
84
+ if host. is_none ( ) || port. is_none ( ) {
85
+ return Err ( ErrorKind :: InvalidAddressFormat ( address. to_owned ( ) ) . into ( ) ) ;
86
+ }
87
+
88
+ let port: u16 = port
89
+ . unwrap ( )
90
+ . parse ( )
91
+ . map_err ( |_err| ErrorKind :: InvalidAddressFormat ( address. to_owned ( ) ) ) ?;
92
+ Ok ( Self :: new ( host. unwrap ( ) , port) )
93
+ }
94
+
58
95
pub fn new ( host : & str , port : u16 ) -> Self {
59
96
let client_conf = Default :: default ( ) ;
60
97
let client = NodeClient :: new_plain ( host, port, client_conf) . unwrap ( ) ;
@@ -120,6 +157,7 @@ impl JormungandrClient {
120
157
grpc:: StreamingRequest :: single ( block) ,
121
158
) ;
122
159
resp. wait ( )
160
+ . map_err ( |err| ErrorKind :: InvalidRequest ( err. to_string ( ) ) . into ( ) )
123
161
}
124
162
125
163
pub fn pull_blocks_to_tip ( & self , from : Hash ) -> grpc:: StreamingResponse < Block > {
@@ -158,6 +196,7 @@ impl JormungandrClient {
158
196
grpc:: StreamingRequest :: single ( header) ,
159
197
) ;
160
198
resp. wait ( )
199
+ . map_err ( |err| ErrorKind :: InvalidRequest ( err. to_string ( ) ) . into ( ) )
161
200
}
162
201
163
202
pub fn get_fragments ( & self , ids : Vec < Hash > ) -> grpc:: StreamingResponse < Fragment > {
0 commit comments