darkfid/
rpc_tx.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use 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    // RPCAPI:
37    // Simulate a network state transition with the given transaction.
38    // Returns `true` if the transaction is valid, otherwise, a corresponding
39    // error.
40    //
41    // --> {"jsonrpc": "2.0", "method": "tx.simulate", "params": ["base64encodedTX"], "id": 1}
42    // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
43    pub async fn tx_simulate(&self, id: u16, params: JsonValue) -> JsonResult {
44        let params = params.get::<Vec<JsonValue>>().unwrap();
45        if params.len() != 1 || !params[0].is_string() {
46            return JsonError::new(InvalidParams, None, id).into()
47        }
48
49        if !*self.validator.synced.read().await {
50            error!(target: "darkfid::rpc::tx_simulate", "Blockchain is not synced");
51            return server_error(RpcError::NotSynced, id, None)
52        }
53
54        // Try to deserialize the transaction
55        let tx_enc = params[0].get::<String>().unwrap().trim();
56        let tx_bytes = match base64::decode(tx_enc) {
57            Some(v) => v,
58            None => {
59                error!(target: "darkfid::rpc::tx_simulate", "Failed decoding base64 transaction");
60                return server_error(RpcError::ParseError, id, None)
61            }
62        };
63
64        let tx: Transaction = match deserialize_async(&tx_bytes).await {
65            Ok(v) => v,
66            Err(e) => {
67                error!(target: "darkfid::rpc::tx_simulate", "Failed deserializing bytes into Transaction: {e}");
68                return server_error(RpcError::ParseError, id, None)
69            }
70        };
71
72        // Simulate state transition
73        let result = self.validator.append_tx(&tx, false).await;
74        if result.is_err() {
75            error!(
76                target: "darkfid::rpc::tx_simulate", "Failed to validate state transition: {}",
77                result.err().unwrap()
78            );
79            return server_error(RpcError::TxSimulationFail, id, None)
80        };
81
82        JsonResponse::new(JsonValue::Boolean(true), id).into()
83    }
84
85    // RPCAPI:
86    // Append a given transaction to the mempool and broadcast it to
87    // the P2P network. The function will first simulate the state
88    // transition in order to see if the transaction is actually valid,
89    // and in turn it will return an error if this is the case.
90    // Otherwise, a transaction ID will be returned.
91    //
92    // --> {"jsonrpc": "2.0", "method": "tx.broadcast", "params": ["base64encodedTX"], "id": 1}
93    // <-- {"jsonrpc": "2.0", "result": "txID...", "id": 1}
94    pub async fn tx_broadcast(&self, id: u16, params: JsonValue) -> JsonResult {
95        let params = params.get::<Vec<JsonValue>>().unwrap();
96        if params.len() != 1 || !params[0].is_string() {
97            return JsonError::new(InvalidParams, None, id).into()
98        }
99
100        if !*self.validator.synced.read().await {
101            error!(target: "darkfid::rpc::tx_broadcast", "Blockchain is not synced");
102            return server_error(RpcError::NotSynced, id, None)
103        }
104
105        // Try to deserialize the transaction
106        let tx_enc = params[0].get::<String>().unwrap().trim();
107        let tx_bytes = match base64::decode(tx_enc) {
108            Some(v) => v,
109            None => {
110                error!(target: "darkfid::rpc::tx_broadcast", "Failed decoding base64 transaction");
111                return server_error(RpcError::ParseError, id, None)
112            }
113        };
114
115        let tx: Transaction = match deserialize_async(&tx_bytes).await {
116            Ok(v) => v,
117            Err(e) => {
118                error!(target: "darkfid::rpc::tx_broadcast", "Failed deserializing bytes into Transaction: {e}");
119                return server_error(RpcError::ParseError, id, None)
120            }
121        };
122
123        // We'll perform the state transition check here.
124        if let Err(e) = self.validator.append_tx(&tx, true).await {
125            error!(target: "darkfid::rpc::tx_broadcast", "Failed to append transaction to mempool: {e}");
126            return server_error(RpcError::TxSimulationFail, id, None)
127        };
128
129        self.p2p_handler.p2p.broadcast(&tx).await;
130        if !self.p2p_handler.p2p.is_connected() {
131            warn!(target: "darkfid::rpc::tx_broadcast", "No connected channels to broadcast tx");
132        }
133
134        let tx_hash = tx.hash().to_string();
135        JsonResponse::new(JsonValue::String(tx_hash), id).into()
136    }
137
138    // RPCAPI:
139    // Queries the node pending transactions store to retrieve all transactions.
140    // Returns a vector of hex-encoded transaction hashes.
141    //
142    // --> {"jsonrpc": "2.0", "method": "tx.pending", "params": [], "id": 1}
143    // <-- {"jsonrpc": "2.0", "result": "[TxHash,...]", "id": 1}
144    pub async fn tx_pending(&self, id: u16, params: JsonValue) -> JsonResult {
145        let params = params.get::<Vec<JsonValue>>().unwrap();
146        if !params.is_empty() {
147            return JsonError::new(InvalidParams, None, id).into()
148        }
149
150        if !*self.validator.synced.read().await {
151            error!(target: "darkfid::rpc::tx_pending", "Blockchain is not synced");
152            return server_error(RpcError::NotSynced, id, None)
153        }
154
155        let pending_txs = match self.validator.blockchain.get_pending_txs() {
156            Ok(v) => v,
157            Err(e) => {
158                error!(target: "darkfid::rpc::tx_pending", "Failed fetching pending txs: {e}");
159                return JsonError::new(InternalError, None, id).into()
160            }
161        };
162
163        let pending_txs: Vec<JsonValue> =
164            pending_txs.iter().map(|x| JsonValue::String(x.hash().to_string())).collect();
165
166        JsonResponse::new(JsonValue::Array(pending_txs), id).into()
167    }
168
169    // RPCAPI:
170    // Queries the node pending transactions store to remove all transactions.
171    // Returns a vector of hex-encoded transaction hashes.
172    //
173    // --> {"jsonrpc": "2.0", "method": "tx.clean_pending", "params": [], "id": 1}
174    // <-- {"jsonrpc": "2.0", "result": "[TxHash,...]", "id": 1}
175    pub async fn tx_clean_pending(&self, id: u16, params: JsonValue) -> JsonResult {
176        let params = params.get::<Vec<JsonValue>>().unwrap();
177        if !params.is_empty() {
178            return JsonError::new(InvalidParams, None, id).into()
179        }
180
181        if !*self.validator.synced.read().await {
182            error!(target: "darkfid::rpc::tx_clean_pending", "Blockchain is not synced");
183            return server_error(RpcError::NotSynced, id, None)
184        }
185
186        let pending_txs = match self.validator.blockchain.get_pending_txs() {
187            Ok(v) => v,
188            Err(e) => {
189                error!(target: "darkfid::rpc::tx_clean_pending", "Failed fetching pending txs: {e}");
190                return JsonError::new(InternalError, None, id).into()
191            }
192        };
193
194        if let Err(e) = self.validator.blockchain.remove_pending_txs(&pending_txs) {
195            error!(target: "darkfid::rpc::tx_clean_pending", "Failed fetching pending txs: {e}");
196            return JsonError::new(InternalError, None, id).into()
197        };
198
199        let pending_txs: Vec<JsonValue> =
200            pending_txs.iter().map(|x| JsonValue::String(x.hash().to_string())).collect();
201
202        JsonResponse::new(JsonValue::Array(pending_txs), id).into()
203    }
204
205    // RPCAPI:
206    // Compute provided transaction's total gas, against current best fork.
207    // Returns the gas value if the transaction is valid, otherwise, a corresponding
208    // error.
209    //
210    // --> {"jsonrpc": "2.0", "method": "tx.calculate_fee", "params": ["base64encodedTX", "include_fee"], "id": 1}
211    // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
212    pub async fn tx_calculate_fee(&self, id: u16, params: JsonValue) -> JsonResult {
213        let params = params.get::<Vec<JsonValue>>().unwrap();
214        if params.len() != 2 || !params[0].is_string() || !params[1].is_bool() {
215            return JsonError::new(InvalidParams, None, id).into()
216        }
217
218        if !*self.validator.synced.read().await {
219            error!(target: "darkfid::rpc::tx_calculate_fee", "Blockchain is not synced");
220            return server_error(RpcError::NotSynced, id, None)
221        }
222
223        // Try to deserialize the transaction
224        let tx_enc = params[0].get::<String>().unwrap().trim();
225        let tx_bytes = match base64::decode(tx_enc) {
226            Some(v) => v,
227            None => {
228                error!(target: "darkfid::rpc::tx_calculate_fee", "Failed decoding base64 transaction");
229                return server_error(RpcError::ParseError, id, None)
230            }
231        };
232
233        let tx: Transaction = match deserialize_async(&tx_bytes).await {
234            Ok(v) => v,
235            Err(e) => {
236                error!(target: "darkfid::rpc::tx_calculate_fee", "Failed deserializing bytes into Transaction: {e}");
237                return server_error(RpcError::ParseError, id, None)
238            }
239        };
240
241        // Parse the include fee flag
242        let include_fee = params[1].get::<bool>().unwrap();
243
244        // Simulate state transition
245        let result = self.validator.calculate_fee(&tx, *include_fee).await;
246        if result.is_err() {
247            error!(
248                target: "darkfid::rpc::tx_calculate_fee", "Failed to validate state transition: {}",
249                result.err().unwrap()
250            );
251            return server_error(RpcError::TxGasCalculationFail, id, None)
252        };
253
254        JsonResponse::new(JsonValue::Number(result.unwrap() as f64), id).into()
255    }
256}