15
15
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
17
17
use std:: net:: { IpAddr , Ipv4Addr , SocketAddr } ;
18
+ use std:: sync:: Arc ;
18
19
19
20
use clarity:: vm:: types:: { PrincipalData , QualifiedContractIdentifier , StacksAddressExtensions } ;
20
21
use clarity:: vm:: { ClarityName , ContractName , Value } ;
21
22
use stacks_common:: types:: chainstate:: StacksAddress ;
22
23
use stacks_common:: types:: net:: PeerHost ;
23
24
use stacks_common:: types:: Address ;
24
- use stacks_common:: util:: hash:: to_hex;
25
+ use stacks_common:: util:: hash:: { to_hex, Sha256Sum } ;
25
26
26
27
use super :: test_rpc;
27
28
use crate :: chainstate:: stacks:: TransactionPayload ;
28
29
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 ;
29
34
use crate :: net:: api:: * ;
30
35
use crate :: net:: connection:: ConnectionOptions ;
31
36
use crate :: net:: httpcore:: {
32
37
HttpRequestContentsExtensions , RPCRequestHandler , StacksHttp , StacksHttpRequest ,
33
38
} ;
34
- use crate :: net:: { ProtocolFamily , TipRequest } ;
39
+ use crate :: net:: test:: RPCHandlerArgsType ;
40
+ use crate :: net:: { ProtocolFamily , RPCHandlerArgs , TipRequest } ;
35
41
36
42
#[ test]
37
43
fn test_try_parse_request ( ) {
@@ -89,17 +95,50 @@ fn test_try_make_response() {
89
95
TransactionPayload :: new_contract_call ( sender_addr, "hello-world" , "add-unit" , vec ! [ ] )
90
96
. unwrap ( ) ;
91
97
98
+ // case 1: no fee estimates
92
99
let mut requests = vec ! [ ] ;
93
100
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 ( ) ,
95
129
postfeerate:: FeeRateEstimateRequestBody {
96
130
estimated_len : Some ( 123 ) ,
97
131
transaction_payload : to_hex ( & tx_payload. serialize_to_vec ( ) ) ,
98
132
} ,
99
133
) ;
100
134
requests. push ( request) ;
101
135
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) ;
103
142
104
143
let response = responses. remove ( 0 ) ;
105
144
debug ! (
@@ -108,5 +147,46 @@ fn test_try_make_response() {
108
147
) ;
109
148
110
149
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
111
153
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) ;
112
192
}
0 commit comments