darkfid/rpc/
tx.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2026 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 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        // Try to deserialize the transaction
57        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        // Simulate state transition
75        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    // RPCAPI:
88    // Append a given transaction to the mempool and broadcast it to
89    // the P2P network. The function will first simulate the state
90    // transition in order to see if the transaction is actually valid,
91    // and in turn it will return an error if this is the case.
92    // Otherwise, a transaction ID will be returned.
93    //
94    // --> {"jsonrpc": "2.0", "method": "tx.broadcast", "params": ["base64encodedTX"], "id": 1}
95    // <-- {"jsonrpc": "2.0", "result": "txID...", "id": 1}
96    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        // Try to deserialize the transaction
110        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        // We'll perform the state transition check here.
128        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    // RPCAPI:
143    // Queries the node pending transactions store to retrieve all transactions.
144    // Returns a vector of hex-encoded transaction hashes.
145    //
146    // --> {"jsonrpc": "2.0", "method": "tx.pending", "params": [], "id": 1}
147    // <-- {"jsonrpc": "2.0", "result": ["TxHash" , "..."], "id": 1}
148    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    // RPCAPI:
176    // Queries the node pending transactions store to remove all transactions.
177    // Returns a vector of hex-encoded transaction hashes.
178    //
179    // --> {"jsonrpc": "2.0", "method": "tx.clean_pending", "params": [], "id": 1}
180    // <-- {"jsonrpc": "2.0", "result": ["TxHash", "..."], "id": 1}
181    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    // RPCAPI:
214    // Compute provided transaction's total gas, against current best fork.
215    // Returns the gas value if the transaction is valid, otherwise, a corresponding
216    // error.
217    //
218    // --> {"jsonrpc": "2.0", "method": "tx.calculate_fee", "params": ["base64encodedTX", "include_fee"], "id": 1}
219    // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
220    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        // Try to deserialize the transaction
234        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        // Parse the include fee flag
252        let include_fee = params[1].get::<bool>().unwrap();
253
254        // Simulate state transition
255        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}