util.rs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. use std::io::Cursor;
  19. use darkfi_sdk::wasm;
  20. use darkfi_serial::Decodable;
  21. use log::{debug, error};
  22. use wasmer::{FunctionEnvMut, WasmPtr};
  23. use super::acl::acl_allow;
  24. use crate::runtime::vm_runtime::{ContractSection, Env};
  25. /// Host function for logging strings.
  26. pub(crate) fn drk_log(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, len: u32) {
  27. let (env, mut store) = ctx.data_and_store_mut();
  28. // Subtract used gas. Here we count the length of the string.
  29. env.subtract_gas(&mut store, len as u64);
  30. let memory_view = env.memory_view(&store);
  31. match ptr.read_utf8_string(&memory_view, len) {
  32. Ok(msg) => {
  33. let mut logs = env.logs.borrow_mut();
  34. logs.push(msg);
  35. std::mem::drop(logs);
  36. }
  37. Err(_) => {
  38. error!(
  39. target: "runtime::util::drk_log",
  40. "[WASM] [{}] drk_log(): Failed to read UTF-8 string from VM memory",
  41. env.contract_id,
  42. );
  43. }
  44. }
  45. }
  46. /// Writes data to the `contract_return_data` field of [`Env`].
  47. /// The data will be read from `ptr` at a memory offset specified by `len`.
  48. ///
  49. /// Returns `SUCCESS` on success, otherwise returns an error code corresponding
  50. /// to a [`ContractError`].
  51. ///
  52. /// Permissions: metadata, exec
  53. pub(crate) fn set_return_data(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, len: u32) -> i64 {
  54. let (env, mut store) = ctx.data_and_store_mut();
  55. let cid = &env.contract_id;
  56. // Enforce function ACL
  57. if let Err(e) = acl_allow(env, &[ContractSection::Metadata, ContractSection::Exec]) {
  58. error!(
  59. target: "runtime::util::set_return_data",
  60. "[WASM] [{}] set_return_data(): Called in unauthorized section: {}", cid, e,
  61. );
  62. return darkfi_sdk::error::CALLER_ACCESS_DENIED
  63. }
  64. // Subtract used gas. Here we count the length read from the memory slice.
  65. env.subtract_gas(&mut store, len as u64);
  66. let memory_view = env.memory_view(&store);
  67. let Ok(slice) = ptr.slice(&memory_view, len) else { return darkfi_sdk::error::INTERNAL_ERROR };
  68. let Ok(return_data) = slice.read_to_vec() else { return darkfi_sdk::error::INTERNAL_ERROR };
  69. // This function should only ever be called once on the runtime.
  70. if env.contract_return_data.take().is_some() {
  71. return darkfi_sdk::error::SET_RETVAL_ERROR
  72. }
  73. env.contract_return_data.set(Some(return_data));
  74. wasm::entrypoint::SUCCESS
  75. }
  76. /// Retrieve an object from the object store specified by the index `idx`.
  77. /// The object's data is written to `ptr`.
  78. ///
  79. /// Returns `SUCCESS` on success and an error code otherwise.
  80. ///
  81. /// Permissions: deploy, metadata, exec
  82. pub(crate) fn get_object_bytes(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, idx: u32) -> i64 {
  83. // Get the slice, where we will read the size of the buffer
  84. let (env, mut store) = ctx.data_and_store_mut();
  85. let cid = env.contract_id;
  86. // Enforce function ACL
  87. if let Err(e) =
  88. acl_allow(env, &[ContractSection::Deploy, ContractSection::Metadata, ContractSection::Exec])
  89. {
  90. error!(
  91. target: "runtime::util::get_object_bytes()",
  92. "[WASM] [{}] get_object_bytes(): Called in unauthorized section: {}", cid, e,
  93. );
  94. return darkfi_sdk::error::CALLER_ACCESS_DENIED
  95. }
  96. // Get the object from env
  97. let objects = env.objects.borrow();
  98. if idx as usize >= objects.len() {
  99. error!(
  100. target: "runtime::util::get_object_bytes",
  101. "[WASM] [{}] get_object_bytes(): Tried to access object out of bounds", cid,
  102. );
  103. return darkfi_sdk::error::DATA_TOO_LARGE
  104. }
  105. let obj = objects[idx as usize].clone();
  106. drop(objects);
  107. if obj.len() > u32::MAX as usize {
  108. return darkfi_sdk::error::DATA_TOO_LARGE
  109. }
  110. // Subtract used gas. Here we count the bytes written to the memory slice
  111. env.subtract_gas(&mut store, obj.len() as u64);
  112. // Read N bytes from the object and write onto the ptr.
  113. let memory_view = env.memory_view(&store);
  114. let Ok(slice) = ptr.slice(&memory_view, obj.len() as u32) else {
  115. error!(
  116. target: "runtime::util::get_object_bytes",
  117. "[WASM] [{}] get_object_bytes(): Failed to make slice from ptr", cid,
  118. );
  119. return darkfi_sdk::error::INTERNAL_ERROR
  120. };
  121. // Put the result in the VM
  122. if let Err(e) = slice.write_slice(&obj) {
  123. error!(
  124. target: "runtime::util::get_object_bytes",
  125. "[WASM] [{}] get_object_bytes(): Failed to write to memory slice: {}", cid, e,
  126. );
  127. return darkfi_sdk::error::INTERNAL_ERROR
  128. };
  129. wasm::entrypoint::SUCCESS
  130. }
  131. /// Returns the size (number of bytes) of an object in the object store
  132. /// specified by index `idx`.
  133. ///
  134. /// Permissions: deploy, metadata, exec
  135. pub(crate) fn get_object_size(mut ctx: FunctionEnvMut<Env>, idx: u32) -> i64 {
  136. // Get the slice, where we will read the size of the buffer
  137. let (env, mut store) = ctx.data_and_store_mut();
  138. let cid = env.contract_id;
  139. // Enforce function ACL
  140. if let Err(e) =
  141. acl_allow(env, &[ContractSection::Deploy, ContractSection::Metadata, ContractSection::Exec])
  142. {
  143. error!(
  144. target: "runtime::util::get_object_size()",
  145. "[WASM] [{}] get_object_size(): Called in unauthorized section: {}", cid, e,
  146. );
  147. return darkfi_sdk::error::CALLER_ACCESS_DENIED
  148. }
  149. // Get the object from env
  150. let objects = env.objects.borrow();
  151. if idx as usize >= objects.len() {
  152. error!(
  153. target: "runtime::util::get_object_size",
  154. "[WASM] [{}] get_object_size(): Tried to access object out of bounds", cid,
  155. );
  156. return darkfi_sdk::error::DATA_TOO_LARGE
  157. }
  158. let obj = &objects[idx as usize];
  159. let obj_len = obj.len();
  160. drop(objects);
  161. if obj_len > u32::MAX as usize {
  162. return darkfi_sdk::error::DATA_TOO_LARGE
  163. }
  164. // Subtract used gas. Here we count the size of the object.
  165. // TODO: This could probably be fixed-cost
  166. env.subtract_gas(&mut store, obj_len as u64);
  167. obj_len as i64
  168. }
  169. /// Will return current runtime configured verifying block height number
  170. ///
  171. /// Permissions: deploy, metadata, exec
  172. pub(crate) fn get_verifying_block_height(mut ctx: FunctionEnvMut<Env>) -> i64 {
  173. let (env, mut store) = ctx.data_and_store_mut();
  174. let cid = env.contract_id;
  175. if let Err(e) =
  176. acl_allow(env, &[ContractSection::Deploy, ContractSection::Metadata, ContractSection::Exec])
  177. {
  178. error!(
  179. target: "runtime::util::get_verifying_block_height",
  180. "[WASM] [{}] get_verifying_block_height(): Called in unauthorized section: {}", cid, e,
  181. );
  182. return darkfi_sdk::error::CALLER_ACCESS_DENIED
  183. }
  184. // Subtract used gas. Here we count the size of the object.
  185. // u32 is 4 bytes.
  186. env.subtract_gas(&mut store, 4);
  187. env.verifying_block_height as i64
  188. }
  189. /// Will return currently configured block time target, in seconds
  190. ///
  191. /// Permissions: deploy, metadata, exec
  192. pub(crate) fn get_block_target(mut ctx: FunctionEnvMut<Env>) -> i64 {
  193. let (env, mut store) = ctx.data_and_store_mut();
  194. let cid = env.contract_id;
  195. if let Err(e) =
  196. acl_allow(env, &[ContractSection::Deploy, ContractSection::Metadata, ContractSection::Exec])
  197. {
  198. error!(
  199. target: "runtime::util::get_block_target",
  200. "[WASM] [{}] get_block_target(): Called in unauthorized section: {}", cid, e,
  201. );
  202. return darkfi_sdk::error::CALLER_ACCESS_DENIED
  203. }
  204. // Subtract used gas. Here we count the size of the object.
  205. // u32 is 4 bytes.
  206. env.subtract_gas(&mut store, 4);
  207. env.block_target as i64
  208. }
  209. /// Will return current runtime configured transaction hash
  210. ///
  211. /// Permissions: deploy, metadata, exec
  212. pub(crate) fn get_tx_hash(mut ctx: FunctionEnvMut<Env>) -> i64 {
  213. let (env, mut store) = ctx.data_and_store_mut();
  214. let cid = env.contract_id;
  215. if let Err(e) =
  216. acl_allow(env, &[ContractSection::Deploy, ContractSection::Metadata, ContractSection::Exec])
  217. {
  218. error!(
  219. target: "runtime::util::get_tx_hash",
  220. "[WASM] [{}] get_tx_hash(): Called in unauthorized section: {}", cid, e,
  221. );
  222. return darkfi_sdk::error::CALLER_ACCESS_DENIED
  223. }
  224. // Subtract used gas. Here we count the size of the object.
  225. env.subtract_gas(&mut store, 32);
  226. // Return the length of the objects Vector.
  227. // This is the location of the data that was retrieved and pushed
  228. let mut objects = env.objects.borrow_mut();
  229. objects.push(env.tx_hash.inner().to_vec());
  230. (objects.len() - 1) as i64
  231. }
  232. /// Will return current runtime configured verifying block height number
  233. ///
  234. /// Permissions: deploy, metadata, exec
  235. pub(crate) fn get_call_index(mut ctx: FunctionEnvMut<Env>) -> i64 {
  236. let (env, mut store) = ctx.data_and_store_mut();
  237. let cid = env.contract_id;
  238. if let Err(e) =
  239. acl_allow(env, &[ContractSection::Deploy, ContractSection::Metadata, ContractSection::Exec])
  240. {
  241. error!(
  242. target: "runtime::util::get_call_index",
  243. "[WASM] [{}] get_call_index(): Called in unauthorized section: {}", cid, e,
  244. );
  245. return darkfi_sdk::error::CALLER_ACCESS_DENIED
  246. }
  247. // Subtract used gas. Here we count the size of the object.
  248. // u8 is 1 byte.
  249. env.subtract_gas(&mut store, 1);
  250. env.call_idx as i64
  251. }
  252. /// Will return current blockchain timestamp,
  253. /// defined as the last block's timestamp.
  254. ///
  255. /// Permissions: deploy, metadata, exec
  256. pub(crate) fn get_blockchain_time(mut ctx: FunctionEnvMut<Env>) -> i64 {
  257. let (env, mut store) = ctx.data_and_store_mut();
  258. let cid = &env.contract_id;
  259. if let Err(e) =
  260. acl_allow(env, &[ContractSection::Deploy, ContractSection::Metadata, ContractSection::Exec])
  261. {
  262. error!(
  263. target: "runtime::util::get_blockchain_time",
  264. "[WASM] [{}] get_blockchain_time(): Called in unauthorized section: {}", cid, e,
  265. );
  266. return darkfi_sdk::error::CALLER_ACCESS_DENIED
  267. }
  268. // Grab current last block
  269. let timestamp = match env.blockchain.lock().unwrap().last_block_timestamp() {
  270. Ok(b) => b,
  271. Err(e) => {
  272. error!(
  273. target: "runtime::util::get_blockchain_time",
  274. "[WASM] [{}] get_blockchain_time(): Internal error getting from blocks tree: {}", cid, e,
  275. );
  276. return darkfi_sdk::error::DB_GET_FAILED
  277. }
  278. };
  279. // Subtract used gas. Here we count the size of the object.
  280. // u64 is 8 bytes.
  281. env.subtract_gas(&mut store, 8);
  282. // Create the return object
  283. let mut ret = Vec::with_capacity(8);
  284. ret.extend_from_slice(&timestamp.inner().to_be_bytes());
  285. // Copy Vec<u8> to the VM
  286. let mut objects = env.objects.borrow_mut();
  287. objects.push(ret.to_vec());
  288. if objects.len() > u32::MAX as usize {
  289. return darkfi_sdk::error::DATA_TOO_LARGE
  290. }
  291. (objects.len() - 1) as i64
  292. }
  293. /// Grabs last block from the `Blockchain` overlay and then copies its
  294. /// height to the VM's object store.
  295. ///
  296. /// On success, returns the index of the new object in the object store.
  297. /// Otherwise, returns an error code.
  298. ///
  299. /// Permissions: deploy, metadata, exec
  300. pub(crate) fn get_last_block_height(mut ctx: FunctionEnvMut<Env>) -> i64 {
  301. let (env, mut store) = ctx.data_and_store_mut();
  302. let cid = &env.contract_id;
  303. // Enforce function ACL
  304. if let Err(e) =
  305. acl_allow(env, &[ContractSection::Deploy, ContractSection::Metadata, ContractSection::Exec])
  306. {
  307. error!(
  308. target: "runtime::util::get_last_block_height",
  309. "[WASM] [{}] get_last_block_height(): Called in unauthorized section: {}", cid, e,
  310. );
  311. return darkfi_sdk::error::CALLER_ACCESS_DENIED
  312. }
  313. // Grab current last block height
  314. let height = match env.blockchain.lock().unwrap().last_block_height() {
  315. Ok(b) => b,
  316. Err(e) => {
  317. error!(
  318. target: "runtime::util::get_last_block_height",
  319. "[WASM] [{}] get_last_block_height(): Internal error getting from blocks tree: {}", cid, e,
  320. );
  321. return darkfi_sdk::error::DB_GET_FAILED
  322. }
  323. };
  324. // Subtract used gas. Here we count the size of the object.
  325. // u64 is 8 bytes.
  326. env.subtract_gas(&mut store, 8);
  327. // Create the return object
  328. let mut ret = Vec::with_capacity(8);
  329. ret.extend_from_slice(&darkfi_serial::serialize(&height));
  330. // Copy Vec<u8> to the VM
  331. let mut objects = env.objects.borrow_mut();
  332. objects.push(ret.to_vec());
  333. if objects.len() > u32::MAX as usize {
  334. return darkfi_sdk::error::DATA_TOO_LARGE
  335. }
  336. (objects.len() - 1) as i64
  337. }
  338. /// Reads a transaction by hash from the transactions store.
  339. ///
  340. /// This function can be called from the Exec or Metadata [`ContractSection`].
  341. ///
  342. /// On success, returns the length of the transaction bytes vector in the environment.
  343. /// Otherwise, returns an error code.
  344. ///
  345. /// Permissions: deploy, metadata, exec
  346. pub(crate) fn get_tx(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>) -> i64 {
  347. let (env, mut store) = ctx.data_and_store_mut();
  348. let cid = env.contract_id;
  349. if let Err(e) =
  350. acl_allow(env, &[ContractSection::Deploy, ContractSection::Metadata, ContractSection::Exec])
  351. {
  352. error!(
  353. target: "runtime::util::get_tx",
  354. "[WASM] [{}] get_tx(): Called in unauthorized section: {}", cid, e,
  355. );
  356. return darkfi_sdk::error::CALLER_ACCESS_DENIED
  357. }
  358. // Subtract used gas. Here we count the length of the looked-up hash.
  359. env.subtract_gas(&mut store, blake3::OUT_LEN as u64);
  360. // Ensure that it is possible to read memory
  361. let memory_view = env.memory_view(&store);
  362. let Ok(mem_slice) = ptr.slice(&memory_view, blake3::OUT_LEN as u32) else {
  363. error!(
  364. target: "runtime::util::get_tx",
  365. "[WASM] [{}] get_tx(): Failed to make slice from ptr", cid,
  366. );
  367. return darkfi_sdk::error::DB_GET_FAILED
  368. };
  369. let mut buf = vec![0_u8; blake3::OUT_LEN];
  370. if let Err(e) = mem_slice.read_slice(&mut buf) {
  371. error!(
  372. target: "runtime::util::get_tx",
  373. "[WASM] [{}] get_tx(): Failed to read from memory slice: {}", cid, e,
  374. );
  375. return darkfi_sdk::error::DB_GET_FAILED
  376. };
  377. let mut buf_reader = Cursor::new(buf);
  378. // Decode hash bytes for transaction that we wish to retrieve
  379. let hash: [u8; blake3::OUT_LEN] = match Decodable::decode(&mut buf_reader) {
  380. Ok(v) => v,
  381. Err(e) => {
  382. error!(
  383. target: "runtime::util::get_tx",
  384. "[WASM] [{}] get_tx(): Failed to decode hash from vec: {}", cid, e,
  385. );
  386. return darkfi_sdk::error::DB_GET_FAILED
  387. }
  388. };
  389. // Make sure there are no trailing bytes in the buffer. This means we've used all data that was
  390. // supplied.
  391. if buf_reader.position() != blake3::OUT_LEN as u64 {
  392. error!(
  393. target: "runtime::util::get_tx",
  394. "[WASM] [{}] get_tx(): Trailing bytes in argument stream", cid,
  395. );
  396. return darkfi_sdk::error::DB_GET_FAILED
  397. }
  398. // Retrieve transaction using the `hash`
  399. let ret = match env.blockchain.lock().unwrap().transactions.get_raw(&hash) {
  400. Ok(v) => v,
  401. Err(e) => {
  402. error!(
  403. target: "runtime::util::get_tx",
  404. "[WASM] [{}] get_tx(): Internal error getting from tree: {}", cid, e,
  405. );
  406. return darkfi_sdk::error::DB_GET_FAILED
  407. }
  408. };
  409. // Return special error if the data is empty
  410. let Some(return_data) = ret else {
  411. debug!(
  412. target: "runtime::util::get_tx",
  413. "[WASM] [{}] get_tx(): Return data is empty", cid,
  414. );
  415. return darkfi_sdk::error::DB_GET_EMPTY
  416. };
  417. if return_data.len() > u32::MAX as usize {
  418. return darkfi_sdk::error::DATA_TOO_LARGE
  419. }
  420. // Subtract used gas. Here we count the length of the data read from db.
  421. env.subtract_gas(&mut store, return_data.len() as u64);
  422. // Copy the data (Vec<u8>) to the VM by pushing it to the objects Vector.
  423. let mut objects = env.objects.borrow_mut();
  424. if objects.len() == u32::MAX as usize {
  425. return darkfi_sdk::error::DATA_TOO_LARGE
  426. }
  427. // Return the length of the objects Vector.
  428. // This is the location of the data that was retrieved and pushed
  429. objects.push(return_data.to_vec());
  430. (objects.len() - 1) as i64
  431. }
  432. /// Reads a transaction location by hash from the transactions store.
  433. ///
  434. /// This function can be called from the Exec or Metadata [`ContractSection`].
  435. ///
  436. /// On success, returns the length of the transaction location bytes vector in
  437. /// the environment. Otherwise, returns an error code.
  438. ///
  439. /// Permissions: deploy, metadata, exec
  440. pub(crate) fn get_tx_location(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>) -> i64 {
  441. let (env, mut store) = ctx.data_and_store_mut();
  442. let cid = env.contract_id;
  443. if let Err(e) =
  444. acl_allow(env, &[ContractSection::Deploy, ContractSection::Metadata, ContractSection::Exec])
  445. {
  446. error!(
  447. target: "runtime::util::get_tx_location",
  448. "[WASM] [{}] get_tx_location(): Called in unauthorized section: {}", cid, e,
  449. );
  450. return darkfi_sdk::error::CALLER_ACCESS_DENIED
  451. }
  452. // Subtract used gas. Here we count the length of the looked-up hash.
  453. env.subtract_gas(&mut store, blake3::OUT_LEN as u64);
  454. // Ensure that it is possible to read memory
  455. let memory_view = env.memory_view(&store);
  456. let Ok(mem_slice) = ptr.slice(&memory_view, blake3::OUT_LEN as u32) else {
  457. error!(
  458. target: "runtime::util::get_tx_location",
  459. "[WASM] [{}] get_tx_location(): Failed to make slice from ptr", cid,
  460. );
  461. return darkfi_sdk::error::DB_GET_FAILED
  462. };
  463. let mut buf = vec![0_u8; blake3::OUT_LEN];
  464. if let Err(e) = mem_slice.read_slice(&mut buf) {
  465. error!(
  466. target: "runtime::util::get_tx_location",
  467. "[WASM] [{}] get_tx_location(): Failed to read from memory slice: {}", cid, e,
  468. );
  469. return darkfi_sdk::error::DB_GET_FAILED
  470. };
  471. let mut buf_reader = Cursor::new(buf);
  472. // Decode hash bytes for transaction that we wish to retrieve
  473. let hash: [u8; blake3::OUT_LEN] = match Decodable::decode(&mut buf_reader) {
  474. Ok(v) => v,
  475. Err(e) => {
  476. error!(
  477. target: "runtime::util::get_tx_location",
  478. "[WASM] [{}] get_tx_location(): Failed to decode hash from vec: {}", cid, e,
  479. );
  480. return darkfi_sdk::error::DB_GET_FAILED
  481. }
  482. };
  483. // Make sure there are no trailing bytes in the buffer. This means we've used all data that was
  484. // supplied.
  485. if buf_reader.position() != blake3::OUT_LEN as u64 {
  486. error!(
  487. target: "runtime::util::get_tx_location",
  488. "[WASM] [{}] get_tx_location(): Trailing bytes in argument stream", cid,
  489. );
  490. return darkfi_sdk::error::DB_GET_FAILED
  491. }
  492. // Retrieve transaction using the `hash`
  493. let ret = match env.blockchain.lock().unwrap().transactions.get_location_raw(&hash) {
  494. Ok(v) => v,
  495. Err(e) => {
  496. error!(
  497. target: "runtime::util::get_tx_location",
  498. "[WASM] [{}] get_tx_location(): Internal error getting from tree: {}", cid, e,
  499. );
  500. return darkfi_sdk::error::DB_GET_FAILED
  501. }
  502. };
  503. // Return special error if the data is empty
  504. let Some(return_data) = ret else {
  505. debug!(
  506. target: "runtime::util::get_tx_location",
  507. "[WASM] [{}] get_tx_location(): Return data is empty", cid,
  508. );
  509. return darkfi_sdk::error::DB_GET_EMPTY
  510. };
  511. if return_data.len() > u32::MAX as usize {
  512. return darkfi_sdk::error::DATA_TOO_LARGE
  513. }
  514. // Subtract used gas. Here we count the length of the data read from db.
  515. env.subtract_gas(&mut store, return_data.len() as u64);
  516. // Copy the data (Vec<u8>) to the VM by pushing it to the objects Vector.
  517. let mut objects = env.objects.borrow_mut();
  518. if objects.len() == u32::MAX as usize {
  519. return darkfi_sdk::error::DATA_TOO_LARGE
  520. }
  521. // Return the length of the objects Vector.
  522. // This is the location of the data that was retrieved and pushed
  523. objects.push(return_data.to_vec());
  524. (objects.len() - 1) as i64
  525. }