rpc.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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. use std::sync::Arc;
  19. use url::Url;
  20. use darkfi::{
  21. blockchain::BlockInfo,
  22. rpc::{
  23. client::RpcClient,
  24. jsonrpc::{JsonRequest, JsonResult},
  25. util::JsonValue,
  26. },
  27. system::{StoppableTask, Subscriber},
  28. tx::Transaction,
  29. util::encoding::base64,
  30. Error, Result,
  31. };
  32. use darkfi_sdk::{
  33. crypto::{ContractId, DAO_CONTRACT_ID, DEPLOYOOOR_CONTRACT_ID, MONEY_CONTRACT_ID},
  34. tx::TransactionHash,
  35. };
  36. use darkfi_serial::{deserialize_async, serialize_async};
  37. use crate::{
  38. error::{WalletDbError, WalletDbResult},
  39. money::{MONEY_INFO_COL_LAST_SCANNED_BLOCK, MONEY_INFO_TABLE},
  40. Drk,
  41. };
  42. impl Drk {
  43. /// Subscribes to darkfid's JSON-RPC notification endpoint that serves
  44. /// new finalized blocks. Upon receiving them, all the transactions are
  45. /// scanned and we check if any of them call the money contract, and if
  46. /// the payments are intended for us. If so, we decrypt them and append
  47. /// the metadata to our wallet.
  48. pub async fn subscribe_blocks(
  49. &self,
  50. endpoint: Url,
  51. ex: Arc<smol::Executor<'static>>,
  52. ) -> Result<()> {
  53. let req = JsonRequest::new("blockchain.last_known_block", JsonValue::Array(vec![]));
  54. let rep = self.rpc_client.as_ref().unwrap().request(req).await?;
  55. let last_known = *rep.get::<f64>().unwrap() as u32;
  56. let last_scanned = match self.last_scanned_block().await {
  57. Ok(l) => l,
  58. Err(e) => {
  59. return Err(Error::RusqliteError(format!(
  60. "[subscribe_blocks] Retrieving last scanned block failed: {e:?}"
  61. )))
  62. }
  63. };
  64. if last_known != last_scanned {
  65. eprintln!("Warning: Last scanned block is not the last known block.");
  66. eprintln!("You should first fully scan the blockchain, and then subscribe");
  67. return Err(Error::RusqliteError(
  68. "[subscribe_blocks] Blockchain not fully scanned".to_string(),
  69. ))
  70. }
  71. println!("Subscribing to receive notifications of incoming blocks");
  72. let subscriber = Subscriber::new();
  73. let subscription = subscriber.clone().subscribe().await;
  74. let _ex = ex.clone();
  75. StoppableTask::new().start(
  76. // Weird hack to prevent lifetimes hell
  77. async move {
  78. let ex = _ex.clone();
  79. let rpc_client = RpcClient::new(endpoint, ex).await?;
  80. let req = JsonRequest::new("blockchain.subscribe_blocks", JsonValue::Array(vec![]));
  81. rpc_client.subscribe(req, subscriber).await
  82. },
  83. |res| async move {
  84. match res {
  85. Ok(()) => {
  86. eprintln!("wtf");
  87. }
  88. Err(e) => eprintln!("[subscribe_blocks] JSON-RPC server error: {e:?}"),
  89. }
  90. },
  91. Error::RpcServerStopped,
  92. ex,
  93. );
  94. println!("Detached subscription to background");
  95. println!("All is good. Waiting for block notifications...");
  96. let e = loop {
  97. match subscription.receive().await {
  98. JsonResult::Notification(n) => {
  99. println!("Got Block notification from darkfid subscription");
  100. if n.method != "blockchain.subscribe_blocks" {
  101. break Error::UnexpectedJsonRpc(format!(
  102. "Got foreign notification from darkfid: {}",
  103. n.method
  104. ))
  105. }
  106. // Verify parameters
  107. if !n.params.is_array() {
  108. break Error::UnexpectedJsonRpc(
  109. "Received notification params are not an array".to_string(),
  110. )
  111. }
  112. let params = n.params.get::<Vec<JsonValue>>().unwrap();
  113. if params.is_empty() {
  114. break Error::UnexpectedJsonRpc(
  115. "Notification parameters are empty".to_string(),
  116. )
  117. }
  118. for param in params {
  119. let param = param.get::<String>().unwrap();
  120. let bytes = base64::decode(param).unwrap();
  121. let block_data: BlockInfo = deserialize_async(&bytes).await?;
  122. println!("=======================================");
  123. println!("Block header:\n{:#?}", block_data.header);
  124. println!("=======================================");
  125. println!("Deserialized successfully. Scanning block...");
  126. if let Err(e) = self.scan_block(&block_data).await {
  127. return Err(Error::RusqliteError(format!(
  128. "[subscribe_blocks] Scanning block failed: {e:?}"
  129. )))
  130. }
  131. if let Err(e) = self
  132. .update_tx_history_records_status(&block_data.txs, "Finalized")
  133. .await
  134. {
  135. return Err(Error::RusqliteError(format!(
  136. "[subscribe_blocks] Update transaction history record status failed: {e:?}"
  137. )))
  138. }
  139. }
  140. }
  141. JsonResult::Error(e) => {
  142. // Some error happened in the transmission
  143. break Error::UnexpectedJsonRpc(format!("Got error from JSON-RPC: {e:?}"))
  144. }
  145. x => {
  146. // And this is weird
  147. break Error::UnexpectedJsonRpc(format!(
  148. "Got unexpected data from JSON-RPC: {x:?}"
  149. ))
  150. }
  151. }
  152. };
  153. Err(e)
  154. }
  155. /// `scan_block` will go over over transactions in a block and handle their calls
  156. /// based on the called contract. Additionally, will update `last_scanned_block` to
  157. /// the probided block height.
  158. async fn scan_block(&self, block: &BlockInfo) -> Result<()> {
  159. println!("[scan_block] Iterating over {} transactions", block.txs.len());
  160. for tx in block.txs.iter() {
  161. println!("[scan_block] Processing transaction: {}", tx.hash());
  162. for (i, call) in tx.calls.iter().enumerate() {
  163. if call.data.contract_id == *MONEY_CONTRACT_ID {
  164. println!("[scan_block] Found Money contract in call {i}");
  165. self.apply_tx_money_data(&call.data.data).await?;
  166. continue
  167. }
  168. if call.data.contract_id == *DAO_CONTRACT_ID {
  169. println!("[scan_block] Found DAO contract in call {i}");
  170. self.apply_tx_dao_data(
  171. &call.data.data,
  172. TransactionHash::new(*blake3::hash(&serialize_async(tx).await).as_bytes()),
  173. i as u8,
  174. true,
  175. )
  176. .await?;
  177. continue
  178. }
  179. if call.data.contract_id == *DEPLOYOOOR_CONTRACT_ID {
  180. println!("[scan_block] Found DeployoOor contract in call {i}");
  181. // TODO: implement
  182. continue
  183. }
  184. // TODO: For now we skip non-native contract calls
  185. println!("[scan_block] Found non-native contract in call {i}, skipping.");
  186. }
  187. }
  188. // Write this block height into `last_scanned_block`
  189. let query =
  190. format!("UPDATE {} SET {} = ?1;", *MONEY_INFO_TABLE, MONEY_INFO_COL_LAST_SCANNED_BLOCK);
  191. if let Err(e) = self.wallet.exec_sql(&query, rusqlite::params![block.header.height]).await {
  192. return Err(Error::RusqliteError(format!(
  193. "[scan_block] Update last scanned block failed: {e:?}"
  194. )))
  195. }
  196. Ok(())
  197. }
  198. /// Scans the blockchain starting from the last scanned block, for relevant
  199. /// money transfer transactions. If reset flag is provided, Merkle tree state
  200. /// and coins are reset, and start scanning from beginning. Alternatively,
  201. /// it looks for a checkpoint in the wallet to reset and start scanning from.
  202. pub async fn scan_blocks(&self, reset: bool) -> WalletDbResult<()> {
  203. // Grab last scanned block height
  204. let mut height = self.last_scanned_block().await?;
  205. // If last scanned block is genesis (0) or reset flag
  206. // has been provided we reset, otherwise continue with
  207. // the next block height
  208. if height == 0 || reset {
  209. self.reset_money_tree().await?;
  210. self.reset_money_coins().await?;
  211. self.reset_dao_trees().await?;
  212. self.reset_daos().await?;
  213. self.reset_dao_proposals().await?;
  214. self.reset_dao_votes().await?;
  215. self.update_all_tx_history_records_status("Rejected").await?;
  216. height = 0;
  217. } else {
  218. height += 1;
  219. };
  220. loop {
  221. let req = JsonRequest::new("blockchain.last_known_block", JsonValue::Array(vec![]));
  222. let rep = match self.rpc_client.as_ref().unwrap().request(req).await {
  223. Ok(r) => r,
  224. Err(e) => {
  225. eprintln!("[scan_blocks] RPC client request failed: {e:?}");
  226. return Err(WalletDbError::GenericError)
  227. }
  228. };
  229. let last = *rep.get::<f64>().unwrap() as u32;
  230. println!("Requested to scan from block number: {height}");
  231. println!("Last known block number reported by darkfid: {last}");
  232. // Already scanned last known block
  233. if height >= last {
  234. return Ok(())
  235. }
  236. while height <= last {
  237. eprint!("Requesting block {}... ", height);
  238. let block = match self.get_block_by_height(height).await {
  239. Ok(r) => r,
  240. Err(e) => {
  241. eprintln!("[scan_blocks] RPC client request failed: {e:?}");
  242. return Err(WalletDbError::GenericError)
  243. }
  244. };
  245. if let Err(e) = self.scan_block(&block).await {
  246. eprintln!("[scan_blocks] Scan block failed: {e:?}");
  247. return Err(WalletDbError::GenericError)
  248. };
  249. self.update_tx_history_records_status(&block.txs, "Finalized").await?;
  250. height += 1;
  251. }
  252. }
  253. }
  254. // Queries darkfid for a block with given height.
  255. async fn get_block_by_height(&self, height: u32) -> Result<BlockInfo> {
  256. let req = JsonRequest::new(
  257. "blockchain.get_block",
  258. JsonValue::Array(vec![JsonValue::String(height.to_string())]),
  259. );
  260. let params = self.rpc_client.as_ref().unwrap().request(req).await?;
  261. let param = params.get::<String>().unwrap();
  262. let bytes = base64::decode(param).unwrap();
  263. let block = deserialize_async(&bytes).await?;
  264. Ok(block)
  265. }
  266. /// Broadcast a given transaction to darkfid and forward onto the network.
  267. /// Returns the transaction ID upon success.
  268. pub async fn broadcast_tx(&self, tx: &Transaction) -> Result<String> {
  269. println!("Broadcasting transaction...");
  270. let params =
  271. JsonValue::Array(vec![JsonValue::String(base64::encode(&serialize_async(tx).await))]);
  272. let req = JsonRequest::new("tx.broadcast", params);
  273. let rep = self.rpc_client.as_ref().unwrap().request(req).await?;
  274. let txid = rep.get::<String>().unwrap().clone();
  275. // Store transactions history record
  276. if let Err(e) = self.insert_tx_history_record(tx).await {
  277. return Err(Error::RusqliteError(format!(
  278. "[broadcast_tx] Inserting transaction history record failed: {e:?}"
  279. )))
  280. }
  281. Ok(txid)
  282. }
  283. /// Queries darkfid for a tx with given hash.
  284. pub async fn get_tx(&self, tx_hash: &TransactionHash) -> Result<Option<Transaction>> {
  285. let tx_hash_str = tx_hash.to_string();
  286. let req = JsonRequest::new(
  287. "blockchain.get_tx",
  288. JsonValue::Array(vec![JsonValue::String(tx_hash_str)]),
  289. );
  290. match self.rpc_client.as_ref().unwrap().request(req).await {
  291. Ok(param) => {
  292. let tx_bytes = base64::decode(param.get::<String>().unwrap()).unwrap();
  293. let tx = deserialize_async(&tx_bytes).await?;
  294. Ok(Some(tx))
  295. }
  296. Err(_) => Ok(None),
  297. }
  298. }
  299. /// Simulate the transaction with the state machine.
  300. pub async fn simulate_tx(&self, tx: &Transaction) -> Result<bool> {
  301. let tx_str = base64::encode(&serialize_async(tx).await);
  302. let req =
  303. JsonRequest::new("tx.simulate", JsonValue::Array(vec![JsonValue::String(tx_str)]));
  304. let rep = self.rpc_client.as_ref().unwrap().request(req).await?;
  305. let is_valid = *rep.get::<bool>().unwrap();
  306. Ok(is_valid)
  307. }
  308. /// Try to fetch zkas bincodes for the given `ContractId`.
  309. pub async fn lookup_zkas(&self, contract_id: &ContractId) -> Result<Vec<(String, Vec<u8>)>> {
  310. let params = JsonValue::Array(vec![JsonValue::String(format!("{contract_id}"))]);
  311. let req = JsonRequest::new("blockchain.lookup_zkas", params);
  312. let rep = self.rpc_client.as_ref().unwrap().request(req).await?;
  313. let params = rep.get::<Vec<JsonValue>>().unwrap();
  314. let mut ret = Vec::with_capacity(params.len());
  315. for param in params {
  316. let zkas_ns = param[0].get::<String>().unwrap().clone();
  317. let zkas_bincode_bytes = base64::decode(param[1].get::<String>().unwrap()).unwrap();
  318. ret.push((zkas_ns, zkas_bincode_bytes));
  319. }
  320. Ok(ret)
  321. }
  322. /// Queries darkfid for given transaction's gas.
  323. pub async fn get_tx_gas(&self, tx: &Transaction, include_fee: bool) -> Result<u64> {
  324. let params = JsonValue::Array(vec![
  325. JsonValue::String(base64::encode(&serialize_async(tx).await)),
  326. JsonValue::Boolean(include_fee),
  327. ]);
  328. let req = JsonRequest::new("tx.calculate_gas", params);
  329. let rep = self.rpc_client.as_ref().unwrap().request(req).await?;
  330. let gas = *rep.get::<f64>().unwrap() as u64;
  331. Ok(gas)
  332. }
  333. }