Skip to content

Commit

Permalink
Fix http request response (#106)
Browse files Browse the repository at this point in the history
* change params account_id and args_base64 to String type

* Replace serde_json::to_string to serde_json::to_string_pretty
  • Loading branch information
DaviRain-Su authored Jan 17, 2023
1 parent d5582e7 commit a256f19
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
35 changes: 21 additions & 14 deletions appchain/src/mainchain.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use scale_info::prelude::format;

#[derive(Serialize, Deserialize, RuntimeDebug, Default)]
pub struct Response {
Expand Down Expand Up @@ -90,9 +91,9 @@ impl HttpBody {
pub struct Params {
request_type: String,
finality: String,
account_id: Vec<u8>,
account_id: String,
method_name: String,
args_base64: Vec<u8>,
args_base64: String,
}

impl Params {
Expand All @@ -106,7 +107,7 @@ impl Params {
self
}

pub fn with_account_id(mut self, value: impl Into<Vec<u8>>) -> Self {
pub fn with_account_id(mut self, value: impl Into<String>) -> Self {
self.account_id = value.into();
self
}
Expand All @@ -116,7 +117,7 @@ impl Params {
self
}

pub fn with_args_base64(mut self, value: impl Into<Vec<u8>>) -> Self {
pub fn with_args_base64(mut self, value: impl Into<String>) -> Self {
self.args_base64 = value.into();
self
}
Expand Down Expand Up @@ -145,7 +146,10 @@ impl<T: Config> Pallet<T> {
let params = Params::default()
.with_request_type("call_function")
.with_finality("final")
.with_account_id(anchor_contract)
.with_account_id(String::from_utf8(anchor_contract.to_vec()).map_err(|e| {
log!(warn, "decode anchor_contract erorr: {:?}", e);
http::Error::Unknown
})?)
.with_method_name("get_validator_list_of")
.with_args_base64(args);

Expand All @@ -155,7 +159,7 @@ impl<T: Config> Pallet<T> {
.with_method("query")
.with_params(params);

let body = serde_json::to_string(&body)
let body = serde_json::to_string_pretty(&body)
.map_err(|_| {
log!(warn, "serde http body error");
http::Error::Unknown
Expand Down Expand Up @@ -214,12 +218,12 @@ impl<T: Config> Pallet<T> {
Ok(obs)
}

pub(crate) fn encode_get_validator_args(era: u32) -> Vec<u8> {
pub(crate) fn encode_get_validator_args(era: u32) -> String {
let a = String::from("{\"era_number\":\"");
let era = era.to_string();
let b = String::from("\"}");
let json = a + &era + &b;
let res = base64::encode(json).into_bytes();
let json = format!("{}{}{}", a, era, b);
let res = base64::encode(json);
res
}

Expand All @@ -245,7 +249,10 @@ impl<T: Config> Pallet<T> {
let params = Params::default()
.with_request_type("call_function")
.with_finality("final")
.with_account_id(anchor_contract)
.with_account_id(String::from_utf8(anchor_contract.to_vec()).map_err(|e| {
log!(warn, "decode anchor_contract erorr: {:?}", e);
http::Error::Unknown
})?)
.with_method_name("get_appchain_notification_histories")
.with_args_base64(args);

Expand All @@ -255,7 +262,7 @@ impl<T: Config> Pallet<T> {
.with_method("query")
.with_params(params);

let body = serde_json::to_string(&body)
let body = serde_json::to_string_pretty(&body)
.map_err(|_| {
log!(warn, "serde http body error");
http::Error::Unknown
Expand Down Expand Up @@ -329,14 +336,14 @@ impl<T: Config> Pallet<T> {
Ok(obs)
}

pub(crate) fn encode_get_notification_args(start: u32, limit: u32) -> Vec<u8> {
pub(crate) fn encode_get_notification_args(start: u32, limit: u32) -> String {
let a = String::from("{\"start_index\":\"");
let start_index = start.to_string();
let b = String::from("\",\"quantity\":\"");
let quantity = limit.to_string();
let c = String::from("\"}");
let json = a + &start_index + &b + &quantity + &c;
let res = base64::encode(json).into_bytes();
let json = format!("{}{}{}{}{}", a, start_index, b, quantity, c);
let res = base64::encode(json);
res
}
}
34 changes: 17 additions & 17 deletions appchain/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,22 @@ fn test_submit_observations() {
#[test]
fn test_encode_args_works() {
let test_get_validators_data = vec![
(0u32, Some(b"eyJlcmFfbnVtYmVyIjoiMCJ9".to_vec())),
(4294967295u32, Some(b"eyJlcmFfbnVtYmVyIjoiNDI5NDk2NzI5NSJ9".to_vec())),
(0u32, Some("eyJlcmFfbnVtYmVyIjoiMCJ9".to_string())),
(4294967295u32, Some("eyJlcmFfbnVtYmVyIjoiNDI5NDk2NzI5NSJ9".to_string())),
];

for (set_id, expected) in test_get_validators_data {
assert_eq!(expected, Some(OctopusAppchain::encode_get_validator_args(set_id)));
}

let test_get_notify_data = vec![
(0u32, 0u32, Some(b"eyJzdGFydF9pbmRleCI6IjAiLCJxdWFudGl0eSI6IjAifQ==".to_vec())),
(0u32, 0u32, Some("eyJzdGFydF9pbmRleCI6IjAiLCJxdWFudGl0eSI6IjAifQ==".to_string())),
(
4294967295u32,
4294967295u32,
Some(
b"eyJzdGFydF9pbmRleCI6IjQyOTQ5NjcyOTUiLCJxdWFudGl0eSI6IjQyOTQ5NjcyOTUifQ=="
.to_vec(),
"eyJzdGFydF9pbmRleCI6IjQyOTQ5NjcyOTUiLCJxdWFudGl0eSI6IjQyOTQ5NjcyOTUifQ=="
.to_string(),
),
),
];
Expand Down Expand Up @@ -200,17 +200,17 @@ fn validator_set_1_response(state: &mut testing::OffchainState) {
let params = Params::default()
.with_request_type("call_function")
.with_finality("final")
.with_account_id("oct-test.testnet".as_bytes())
.with_account_id("oct-test.testnet")
.with_method_name("get_validator_list_of")
.with_args_base64("eyJlcmFfbnVtYmVyIjoiMSJ9".as_bytes());
.with_args_base64("eyJlcmFfbnVtYmVyIjoiMSJ9");

let body = HttpBody::default()
.with_jsonrpc("2.0")
.with_id("dontcare")
.with_method("query")
.with_params(params);

let body = serde_json::to_string(&body).unwrap().as_bytes().to_vec();
let body = serde_json::to_string_pretty(&body).unwrap().as_bytes().to_vec();

let response_result = ResponseResult::default()
.with_result(vec![
Expand Down Expand Up @@ -250,7 +250,7 @@ fn validator_set_1_response(state: &mut testing::OffchainState) {
.with_id("dontcare")
.with_response_result(response_result);

let response = serde_json::to_string(&response).unwrap().as_bytes().to_vec();
let response = serde_json::to_string_pretty(&response).unwrap().as_bytes().to_vec();

state.expect_request(testing::PendingRequest {
method: "POST".into(),
Expand All @@ -267,17 +267,17 @@ fn empty_validator_set_1_response(state: &mut testing::OffchainState) {
let params = Params::default()
.with_request_type("call_function")
.with_finality("final")
.with_account_id("oct-test.testnet".as_bytes())
.with_account_id("oct-test.testnet")
.with_method_name("get_validator_list_of")
.with_args_base64("eyJlcmFfbnVtYmVyIjoiMSJ9".as_bytes());
.with_args_base64("eyJlcmFfbnVtYmVyIjoiMSJ9");

let body = HttpBody::default()
.with_jsonrpc("2.0")
.with_id("dontcare")
.with_method("query")
.with_params(params);

let body = serde_json::to_string(&body).unwrap().as_bytes().to_vec();
let body = serde_json::to_string_pretty(&body).unwrap().as_bytes().to_vec();

let response_result = ResponseResult::default()
.with_result(vec![91, 93])
Expand All @@ -290,7 +290,7 @@ fn empty_validator_set_1_response(state: &mut testing::OffchainState) {
.with_id("dontcare")
.with_response_result(response_result);

let response = serde_json::to_string(&response).unwrap().as_bytes().to_vec();
let response = serde_json::to_string_pretty(&response).unwrap().as_bytes().to_vec();

state.expect_request(testing::PendingRequest {
method: "POST".into(),
Expand Down Expand Up @@ -321,17 +321,17 @@ fn burn_notify_response(state: &mut testing::OffchainState) {
let params = Params::default()
.with_request_type("call_function")
.with_finality("final")
.with_account_id("oct-test.testnet".as_bytes())
.with_account_id("oct-test.testnet")
.with_method_name("get_appchain_notification_histories")
.with_args_base64("eyJzdGFydF9pbmRleCI6IjAiLCJxdWFudGl0eSI6IjEwIn0=".as_bytes());
.with_args_base64("eyJzdGFydF9pbmRleCI6IjAiLCJxdWFudGl0eSI6IjEwIn0=");

let body = HttpBody::default()
.with_jsonrpc("2.0")
.with_id("dontcare")
.with_method("query")
.with_params(params);

let body = serde_json::to_string(&body).unwrap().as_bytes().to_vec();
let body = serde_json::to_string_pretty(&body).unwrap().as_bytes().to_vec();

let response_result = ResponseResult::default()
.with_result(vec![
Expand Down Expand Up @@ -360,7 +360,7 @@ fn burn_notify_response(state: &mut testing::OffchainState) {
.with_id("dontcare")
.with_response_result(response_result);

let response = serde_json::to_string(&response).unwrap().as_bytes().to_vec();
let response = serde_json::to_string_pretty(&response).unwrap().as_bytes().to_vec();

state.expect_request(testing::PendingRequest {
method: "POST".into(),
Expand Down

0 comments on commit a256f19

Please sign in to comment.