1use darkfi_serial::deserialize_async;
20use tinyjson::JsonValue;
21use tracing::{error, warn};
22
23use darkfi::{
24 rpc::jsonrpc::{
25 ErrorCode::{InternalError, InvalidParams},
26 JsonError, JsonResponse, JsonResult,
27 },
28 tx::Transaction,
29 util::encoding::base64,
30};
31
32use super::DarkfiNode;
33use crate::{server_error, RpcError};
34
35impl DarkfiNode {
36 pub async fn tx_simulate(&self, id: u16, params: JsonValue) -> JsonResult {
44 let Some(params) = params.get::<Vec<JsonValue>>() else {
45 return JsonError::new(InvalidParams, None, id).into()
46 };
47 if params.len() != 1 || !params[0].is_string() {
48 return JsonError::new(InvalidParams, None, id).into()
49 }
50
51 if !*self.validator.synced.read().await {
52 error!(target: "darkfid::rpc::tx_simulate", "Blockchain is not synced");
53 return server_error(RpcError::NotSynced, id, None)
54 }
55
56 let tx_enc = params[0].get::<String>().unwrap().trim();
58 let tx_bytes = match base64::decode(tx_enc) {
59 Some(v) => v,
60 None => {
61 error!(target: "darkfid::rpc::tx_simulate", "Failed decoding base64 transaction");
62 return server_error(RpcError::ParseError, id, None)
63 }
64 };
65
66 let tx: Transaction = match deserialize_async(&tx_bytes).await {
67 Ok(v) => v,
68 Err(e) => {
69 error!(target: "darkfid::rpc::tx_simulate", "Failed deserializing bytes into Transaction: {e}");
70 return server_error(RpcError::ParseError, id, None)
71 }
72 };
73
74 let result = self.validator.append_tx(&tx, false).await;
76 if result.is_err() {
77 error!(
78 target: "darkfid::rpc::tx_simulate", "Failed to validate state transition: {}",
79 result.err().unwrap()
80 );
81 return server_error(RpcError::TxSimulationFail, id, None)
82 };
83
84 JsonResponse::new(JsonValue::Boolean(true), id).into()
85 }
86
87 pub async fn tx_broadcast(&self, id: u16, params: JsonValue) -> JsonResult {
97 let Some(params) = params.get::<Vec<JsonValue>>() else {
98 return JsonError::new(InvalidParams, None, id).into()
99 };
100 if params.len() != 1 || !params[0].is_string() {
101 return JsonError::new(InvalidParams, None, id).into()
102 }
103
104 if !*self.validator.synced.read().await {
105 error!(target: "darkfid::rpc::tx_broadcast", "Blockchain is not synced");
106 return server_error(RpcError::NotSynced, id, None)
107 }
108
109 let tx_enc = params[0].get::<String>().unwrap().trim();
111 let tx_bytes = match base64::decode(tx_enc) {
112 Some(v) => v,
113 None => {
114 error!(target: "darkfid::rpc::tx_broadcast", "Failed decoding base64 transaction");
115 return server_error(RpcError::ParseError, id, None)
116 }
117 };
118
119 let tx: Transaction = match deserialize_async(&tx_bytes).await {
120 Ok(v) => v,
121 Err(e) => {
122 error!(target: "darkfid::rpc::tx_broadcast", "Failed deserializing bytes into Transaction: {e}");
123 return server_error(RpcError::ParseError, id, None)
124 }
125 };
126
127 if let Err(e) = self.validator.append_tx(&tx, true).await {
129 error!(target: "darkfid::rpc::tx_broadcast", "Failed to append transaction to mempool: {e}");
130 return server_error(RpcError::TxSimulationFail, id, None)
131 };
132
133 self.p2p_handler.p2p.broadcast(&tx).await;
134 if !self.p2p_handler.p2p.is_connected() {
135 warn!(target: "darkfid::rpc::tx_broadcast", "No connected channels to broadcast tx");
136 }
137
138 let tx_hash = tx.hash().to_string();
139 JsonResponse::new(JsonValue::String(tx_hash), id).into()
140 }
141
142 pub async fn tx_pending(&self, id: u16, params: JsonValue) -> JsonResult {
149 let Some(params) = params.get::<Vec<JsonValue>>() else {
150 return JsonError::new(InvalidParams, None, id).into()
151 };
152 if !params.is_empty() {
153 return JsonError::new(InvalidParams, None, id).into()
154 }
155
156 if !*self.validator.synced.read().await {
157 error!(target: "darkfid::rpc::tx_pending", "Blockchain is not synced");
158 return server_error(RpcError::NotSynced, id, None)
159 }
160
161 let pending_txs = match self.validator.blockchain.get_pending_txs() {
162 Ok(v) => v,
163 Err(e) => {
164 error!(target: "darkfid::rpc::tx_pending", "Failed fetching pending txs: {e}");
165 return JsonError::new(InternalError, None, id).into()
166 }
167 };
168
169 let pending_txs: Vec<JsonValue> =
170 pending_txs.iter().map(|x| JsonValue::String(x.hash().to_string())).collect();
171
172 JsonResponse::new(JsonValue::Array(pending_txs), id).into()
173 }
174
175 pub async fn tx_clean_pending(&self, id: u16, params: JsonValue) -> JsonResult {
182 let Some(params) = params.get::<Vec<JsonValue>>() else {
183 return JsonError::new(InvalidParams, None, id).into()
184 };
185 if !params.is_empty() {
186 return JsonError::new(InvalidParams, None, id).into()
187 }
188
189 if !*self.validator.synced.read().await {
190 error!(target: "darkfid::rpc::tx_clean_pending", "Blockchain is not synced");
191 return server_error(RpcError::NotSynced, id, None)
192 }
193
194 let pending_txs = match self.validator.blockchain.get_pending_txs() {
195 Ok(v) => v,
196 Err(e) => {
197 error!(target: "darkfid::rpc::tx_clean_pending", "Failed fetching pending txs: {e}");
198 return JsonError::new(InternalError, None, id).into()
199 }
200 };
201
202 if let Err(e) = self.validator.blockchain.remove_pending_txs(&pending_txs) {
203 error!(target: "darkfid::rpc::tx_clean_pending", "Failed fetching pending txs: {e}");
204 return JsonError::new(InternalError, None, id).into()
205 };
206
207 let pending_txs: Vec<JsonValue> =
208 pending_txs.iter().map(|x| JsonValue::String(x.hash().to_string())).collect();
209
210 JsonResponse::new(JsonValue::Array(pending_txs), id).into()
211 }
212
213 pub async fn tx_calculate_fee(&self, id: u16, params: JsonValue) -> JsonResult {
221 let Some(params) = params.get::<Vec<JsonValue>>() else {
222 return JsonError::new(InvalidParams, None, id).into()
223 };
224 if params.len() != 2 || !params[0].is_string() || !params[1].is_bool() {
225 return JsonError::new(InvalidParams, None, id).into()
226 }
227
228 if !*self.validator.synced.read().await {
229 error!(target: "darkfid::rpc::tx_calculate_fee", "Blockchain is not synced");
230 return server_error(RpcError::NotSynced, id, None)
231 }
232
233 let tx_enc = params[0].get::<String>().unwrap().trim();
235 let tx_bytes = match base64::decode(tx_enc) {
236 Some(v) => v,
237 None => {
238 error!(target: "darkfid::rpc::tx_calculate_fee", "Failed decoding base64 transaction");
239 return server_error(RpcError::ParseError, id, None)
240 }
241 };
242
243 let tx: Transaction = match deserialize_async(&tx_bytes).await {
244 Ok(v) => v,
245 Err(e) => {
246 error!(target: "darkfid::rpc::tx_calculate_fee", "Failed deserializing bytes into Transaction: {e}");
247 return server_error(RpcError::ParseError, id, None)
248 }
249 };
250
251 let include_fee = params[1].get::<bool>().unwrap();
253
254 let result = self.validator.calculate_fee(&tx, *include_fee).await;
256 if result.is_err() {
257 error!(
258 target: "darkfid::rpc::tx_calculate_fee", "Failed to validate state transition: {}",
259 result.err().unwrap()
260 );
261 return server_error(RpcError::TxGasCalculationFail, id, None)
262 };
263
264 JsonResponse::new(JsonValue::Number(result.unwrap() as f64), id).into()
265 }
266}