forked from tari-project/tari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_rpc.rs
146 lines (133 loc) · 5.31 KB
/
json_rpc.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2021, The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//! Methods for converting responses to json.
use json::json;
use serde_json as json;
/// Default accept response for submit block that goes back to XMRig
/// Refer to XMRig. Do not change existing json values unless it is changed in XMRig.
pub fn default_block_accept_response(req_id: Option<i64>) -> json::Value {
json!({
"id": req_id.unwrap_or(-1),
"jsonrpc": "2.0",
"result": "{}",
"status": "OK",
"untrusted": false,
})
}
/// Create a JSON RPC success response
/// More info: <https://www.jsonrpc.org/specification#response_object>
pub fn success_response(req_id: Option<i64>, result: json::Value) -> json::Value {
json!({
"id": req_id.unwrap_or(-1),
"jsonrpc": "2.0",
"result": result,
})
}
/// Create a standard JSON RPC error response
/// More info: <https://www.jsonrpc.org/specification#error_object>
pub fn standard_error_response(
req_id: Option<i64>,
err: jsonrpc::error::StandardError,
data: Option<json::Value>,
) -> json::Value {
let data = data.and_then(|value| json::value::to_raw_value(&value).ok());
let err = jsonrpc::error::standard_error(err, data);
json!({
"id": req_id.unwrap_or(-1),
"jsonrpc": "2.0",
"error": err,
})
}
/// Create a JSON RPC error response
/// More info: <https://www.jsonrpc.org/specification#error_object>
pub fn error_response(
req_id: Option<i64>,
err_code: i32,
err_message: &str,
err_data: Option<json::Value>,
) -> json::Value {
let mut err = json!({
"code": err_code,
"message": err_message,
});
if let Some(d) = err_data {
err["data"] = d;
}
json!({
"id": req_id.unwrap_or(-1),
"jsonrpc": "2.0",
"error": err
})
}
#[cfg(test)]
pub mod test {
use super::*;
#[test]
pub fn test_default_block_accept_response() {
let resp = default_block_accept_response(Some(12));
assert_eq!(resp["id"], 12);
assert_eq!(resp["result"], "{}");
let resp = default_block_accept_response(None);
assert_eq!(resp["id"], -1);
assert_eq!(resp["result"], "{}");
}
#[test]
pub fn test_success_response() {
let result = json::json!({"test key": "test value"});
let resp = success_response(Some(12), result.clone());
assert_eq!(resp["id"], 12);
assert_eq!(resp["result"], result);
let resp = success_response(None, result.clone());
assert_eq!(resp["id"], -1);
assert_eq!(resp["result"], result);
}
#[test]
pub fn test_standard_error_response() {
let result = json::json!({"test key": "test value"});
let resp = standard_error_response(
Some(12),
jsonrpc::error::StandardError::ParseError,
Some(result.clone()),
);
assert!(!resp["error"].is_null());
assert_eq!(resp["id"], json::json!(12i64));
assert_eq!(resp["error"]["data"], result);
assert_eq!(resp["error"]["message"], "Parse error");
}
#[test]
pub fn test_error_response() {
let req_id = 12;
let err_code = 200;
let err_message = "error message";
let err_data = json::json!({"test key":"test value"});
let response = error_response(Some(req_id), err_code, err_message, Some(err_data.clone()));
assert_eq!(response["id"], req_id);
assert_eq!(response["error"]["data"], err_data);
assert_eq!(response["error"]["code"], err_code);
assert_eq!(response["error"]["message"], err_message);
let response = error_response(None, err_code, err_message, None);
assert_eq!(response["id"], -1);
assert!(response["error"]["data"].is_null());
assert_eq!(response["error"]["code"], err_code);
assert_eq!(response["error"]["message"], err_message);
}
}