Skip to content

Commit f85611c

Browse files
committed
chore: test 4145 fix by supplying different fee estimators
1 parent 22297f7 commit f85611c

File tree

1 file changed

+84
-4
lines changed

1 file changed

+84
-4
lines changed

stackslib/src/net/api/tests/postfeerate.rs

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,29 @@
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
18+
use std::sync::Arc;
1819

1920
use clarity::vm::types::{PrincipalData, QualifiedContractIdentifier, StacksAddressExtensions};
2021
use clarity::vm::{ClarityName, ContractName, Value};
2122
use stacks_common::types::chainstate::StacksAddress;
2223
use stacks_common::types::net::PeerHost;
2324
use stacks_common::types::Address;
24-
use stacks_common::util::hash::to_hex;
25+
use stacks_common::util::hash::{to_hex, Sha256Sum};
2526

2627
use super::test_rpc;
2728
use crate::chainstate::stacks::TransactionPayload;
2829
use crate::core::BLOCK_LIMIT_MAINNET_21;
30+
use crate::cost_estimates::metrics::UnitMetric;
31+
use crate::cost_estimates::tests::fee_rate_fuzzer::ConstantFeeEstimator;
32+
use crate::cost_estimates::UnitEstimator;
33+
use crate::net::api::tests::TestRPC;
2934
use crate::net::api::*;
3035
use crate::net::connection::ConnectionOptions;
3136
use crate::net::httpcore::{
3237
HttpRequestContentsExtensions, RPCRequestHandler, StacksHttp, StacksHttpRequest,
3338
};
34-
use crate::net::{ProtocolFamily, TipRequest};
39+
use crate::net::test::RPCHandlerArgsType;
40+
use crate::net::{ProtocolFamily, RPCHandlerArgs, TipRequest};
3541

3642
#[test]
3743
fn test_try_parse_request() {
@@ -89,17 +95,50 @@ fn test_try_make_response() {
8995
TransactionPayload::new_contract_call(sender_addr, "hello-world", "add-unit", vec![])
9096
.unwrap();
9197

98+
// case 1: no fee estimates
9299
let mut requests = vec![];
93100
let request = StacksHttpRequest::new_post_fee_rate(
94-
addr.into(),
101+
addr.clone().into(),
102+
postfeerate::FeeRateEstimateRequestBody {
103+
estimated_len: Some(123),
104+
transaction_payload: to_hex(&tx_payload.serialize_to_vec()),
105+
},
106+
);
107+
requests.push(request);
108+
109+
let test_rpc = TestRPC::setup(function_name!());
110+
let mut responses = test_rpc.run(requests);
111+
112+
let response = responses.remove(0);
113+
debug!(
114+
"Response:\n{}\n",
115+
std::str::from_utf8(&response.try_serialize().unwrap()).unwrap()
116+
);
117+
118+
let (preamble, body) = response.destruct();
119+
let body_json: serde_json::Value = body.try_into().unwrap();
120+
121+
// get back a JSON string and a 400
122+
assert_eq!(preamble.status_code, 400);
123+
debug!("Response JSON no estimator: {}", &body_json);
124+
125+
// case 2: no estimate avaialable
126+
let mut requests = vec![];
127+
let request = StacksHttpRequest::new_post_fee_rate(
128+
addr.clone().into(),
95129
postfeerate::FeeRateEstimateRequestBody {
96130
estimated_len: Some(123),
97131
transaction_payload: to_hex(&tx_payload.serialize_to_vec()),
98132
},
99133
);
100134
requests.push(request);
101135

102-
let mut responses = test_rpc(function_name!(), requests);
136+
let test_rpc = TestRPC::setup_with_rpc_args(
137+
function_name!(),
138+
Some(RPCHandlerArgsType::Null),
139+
Some(RPCHandlerArgsType::Null),
140+
);
141+
let mut responses = test_rpc.run(requests);
103142

104143
let response = responses.remove(0);
105144
debug!(
@@ -108,5 +147,46 @@ fn test_try_make_response() {
108147
);
109148

110149
let (preamble, body) = response.destruct();
150+
let body_json: serde_json::Value = body.try_into().unwrap();
151+
152+
// get back a JSON object and a 400
111153
assert_eq!(preamble.status_code, 400);
154+
debug!("Response JSON no estimate fee: {}", &body_json);
155+
assert_eq!(
156+
body_json.get("reason").unwrap().as_str().unwrap(),
157+
"NoEstimateAvailable"
158+
);
159+
assert!(body_json.get("error").is_some());
160+
assert!(body_json.get("reason_data").is_some());
161+
162+
// case 3: get an estimate
163+
let mut requests = vec![];
164+
let request = StacksHttpRequest::new_post_fee_rate(
165+
addr.clone().into(),
166+
postfeerate::FeeRateEstimateRequestBody {
167+
estimated_len: Some(123),
168+
transaction_payload: to_hex(&tx_payload.serialize_to_vec()),
169+
},
170+
);
171+
requests.push(request);
172+
173+
let test_rpc = TestRPC::setup_with_rpc_args(
174+
function_name!(),
175+
Some(RPCHandlerArgsType::Unit),
176+
Some(RPCHandlerArgsType::Unit),
177+
);
178+
let mut responses = test_rpc.run(requests);
179+
180+
let response = responses.remove(0);
181+
debug!(
182+
"Response:\n{}\n",
183+
std::str::from_utf8(&response.try_serialize().unwrap()).unwrap()
184+
);
185+
186+
let (preamble, body) = response.destruct();
187+
let body_json: serde_json::Value = body.try_into().unwrap();
188+
189+
// get back a JSON object and a 200
190+
assert_eq!(preamble.status_code, 200);
191+
debug!("Response JSON success: {}", &body_json);
112192
}

0 commit comments

Comments
 (0)