explorerd/service/
contracts.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 std::{
20    io::{Cursor, Read},
21    str::FromStr,
22};
23
24use log::info;
25use tar::Archive;
26use tinyjson::JsonValue;
27
28use darkfi::{
29    blockchain::BlockchainOverlay, validator::utils::deploy_native_contracts, Error, Result,
30};
31use darkfi_sdk::crypto::{ContractId, DAO_CONTRACT_ID, DEPLOYOOOR_CONTRACT_ID, MONEY_CONTRACT_ID};
32use darkfi_serial::deserialize;
33
34use crate::{
35    store::{
36        contract_metadata::{ContractMetaData, ContractSourceFile},
37        NATIVE_CONTRACT_SOURCE_ARCHIVES,
38    },
39    ExplorerService,
40};
41
42/// Represents a contract record embellished with details that are not stored on-chain.
43#[derive(Debug, Clone)]
44pub struct ContractRecord {
45    /// The Contract ID as a string
46    pub id: String,
47
48    /// The optional name of the contract
49    pub name: Option<String>,
50
51    /// The optional description of the contract
52    pub description: Option<String>,
53}
54
55impl ContractRecord {
56    /// Auxiliary function to convert a `ContractRecord` into a `JsonValue` array.
57    pub fn to_json_array(&self) -> JsonValue {
58        JsonValue::Array(vec![
59            JsonValue::String(self.id.clone()),
60            JsonValue::String(self.name.clone().unwrap_or_default()),
61            JsonValue::String(self.description.clone().unwrap_or_default()),
62        ])
63    }
64}
65
66impl ExplorerService {
67    /// Fetches the total contract count of all deployed contracts in the explorer database.
68    pub fn get_contract_count(&self) -> usize {
69        self.db.blockchain.contracts.wasm.len()
70    }
71
72    /// Retrieves all contracts from the store excluding native contracts (DAO, Deployooor, and Money),
73    /// transforming them into `Vec` of [`ContractRecord`]s, and returns the result.
74    pub fn get_contracts(&self) -> Result<Vec<ContractRecord>> {
75        let native_contracts = [*DAO_CONTRACT_ID, *DEPLOYOOOR_CONTRACT_ID, *MONEY_CONTRACT_ID];
76        self.get_filtered_contracts(|contract_id| !native_contracts.contains(contract_id))
77    }
78
79    /// Retrieves all native contracts (DAO, Deployooor, and Money) from the store, transforming them
80    /// into `Vec` of [`ContractRecord`]s and returns the result.
81    pub fn get_native_contracts(&self) -> Result<Vec<ContractRecord>> {
82        let native_contracts = [*DAO_CONTRACT_ID, *DEPLOYOOOR_CONTRACT_ID, *MONEY_CONTRACT_ID];
83        self.get_filtered_contracts(|contract_id| native_contracts.contains(contract_id))
84    }
85
86    /// Fetches a list of source code file paths for a given [ContractId], returning an empty vector
87    /// if no contracts are found.
88    pub fn get_contract_source_paths(&self, contract_id: &ContractId) -> Result<Vec<String>> {
89        self.db.contract_meta_store.get_source_paths(contract_id).map_err(|e| {
90            Error::DatabaseError(format!(
91                "[get_contract_source_paths] Retrieval of contract source code paths failed: {e:?}"
92            ))
93        })
94    }
95
96    /// Fetches [`ContractMetaData`] for a given [`ContractId`], returning `None` if no metadata is found.
97    pub fn get_contract_metadata(
98        &self,
99        contract_id: &ContractId,
100    ) -> Result<Option<ContractMetaData>> {
101        self.db.contract_meta_store.get(contract_id).map_err(|e| {
102            Error::DatabaseError(format!(
103                "[get_contract_metadata] Retrieval of contract metadata paths failed: {e:?}"
104            ))
105        })
106    }
107
108    /// Fetches the source code content for a specified [`ContractId`] and `path`, returning `None` if
109    /// no source content is found.
110    pub fn get_contract_source_content(
111        &self,
112        contract_id: &ContractId,
113        path: &str,
114    ) -> Result<Option<String>> {
115        self.db.contract_meta_store.get_source_content(contract_id, path).map_err(|e| {
116            Error::DatabaseError(format!(
117                "[get_contract_source_content] Retrieval of contract source file failed: {e:?}"
118            ))
119        })
120    }
121
122    /// Adds source code for a specified [`ContractId`] from a provided tar file (in bytes).
123    ///
124    /// This function extracts the tar archive from `tar_bytes`, then loads each source file
125    /// into the store. Each file is keyed by its path prefixed with the Contract ID.
126    /// Returns a successful result or an error.
127    pub fn add_contract_source(&self, contract_id: &ContractId, tar_bytes: &[u8]) -> Result<()> {
128        // Untar the source code
129        let source = untar_source(tar_bytes)?;
130
131        // Insert contract source code
132        self.db.contract_meta_store.insert_source(contract_id, &source).map_err(|e| {
133            Error::DatabaseError(format!(
134                "[add_contract_source] Adding of contract source code failed: {e:?}"
135            ))
136        })
137    }
138
139    /// Adds provided [`ContractId`] with corresponding [`ContractMetaData`] pairs into the contract
140    /// metadata store, returning a successful result upon success.
141    pub fn add_contract_metadata(
142        &self,
143        contract_ids: &[ContractId],
144        metadata: &[ContractMetaData],
145    ) -> Result<()> {
146        self.db.contract_meta_store.insert_metadata(contract_ids, metadata).map_err(|e| {
147            Error::DatabaseError(format!(
148                "[add_contract_metadata] Upload of contract source code failed: {e:?}"
149            ))
150        })
151    }
152
153    /// Deploys native contracts required for gas calculation and retrieval.
154    pub async fn deploy_native_contracts(&self) -> Result<()> {
155        let overlay = BlockchainOverlay::new(&self.db.blockchain)?;
156        deploy_native_contracts(&overlay, 10).await?;
157        overlay.lock().unwrap().overlay.lock().unwrap().apply()?;
158        Ok(())
159    }
160
161    /// Loads native contract source code into the explorer database by extracting it from tar archives
162    /// created during the explorer build process. The extracted source code is associated with
163    /// the corresponding [`ContractId`] for each loaded contract and stored.
164    pub fn load_native_contract_sources(&self) -> Result<()> {
165        // Iterate each native contract source archive
166        for (contract_id_str, archive_bytes) in NATIVE_CONTRACT_SOURCE_ARCHIVES.iter() {
167            // Untar the native contract source code
168            let source_code = untar_source(archive_bytes)?;
169
170            // Parse contract id into a contract id instance
171            let contract_id = &ContractId::from_str(contract_id_str)?;
172
173            // Add source code into the `ContractMetaStore`
174            self.db.contract_meta_store.insert_source(contract_id, &source_code)?;
175            info!(target: "explorerd: load_native_contract_sources", "Loaded native contract source {contract_id_str}");
176        }
177        Ok(())
178    }
179
180    /// Loads [`ContractMetaData`] for deployed native contracts into the explorer database by adding descriptive
181    /// information (e.g., name and description) used to display contract details.
182    pub fn load_native_contract_metadata(&self) -> Result<()> {
183        let contract_ids = [*MONEY_CONTRACT_ID, *DAO_CONTRACT_ID, *DEPLOYOOOR_CONTRACT_ID];
184
185        // Create pre-defined native contract metadata
186        let metadatas = [
187            ContractMetaData::new(
188                "Money".to_string(),
189                "Facilitates money transfers, atomic swaps, minting, freezing, and staking of consensus tokens".to_string(),
190            ),
191            ContractMetaData::new(
192                "DAO".to_string(),
193                "Provides functionality for Anonymous DAOs".to_string(),
194            ),
195            ContractMetaData::new(
196                "Deployoor".to_string(),
197                "Handles non-native smart contract deployments".to_string(),
198            ),
199        ];
200
201        // Load contract metadata into the `ContractMetaStore`
202        self.db.contract_meta_store.insert_metadata(&contract_ids, &metadatas)?;
203        info!(target: "explorerd: load_native_contract_metadata", "Loaded metadata for native contracts");
204
205        Ok(())
206    }
207
208    /// Converts a [`ContractId`] into a [`ContractRecord`].
209    ///
210    /// This function retrieves the [`ContractMetaData`] associated with the provided Contract ID
211    /// and uses any found metadata to construct a contract record. Upon success, the function
212    /// returns a [`ContractRecord`] containing relevant details about the contract.
213    fn to_contract_record(&self, contract_id: &ContractId) -> Result<ContractRecord> {
214        let metadata = self.db.contract_meta_store.get(contract_id)?;
215        let name: Option<String>;
216        let description: Option<String>;
217
218        // Set name and description based on the presence of metadata
219        if let Some(metadata) = metadata {
220            name = Some(metadata.name);
221            description = Some(metadata.description);
222        } else {
223            name = None;
224            description = None;
225        }
226
227        // Return transformed contract record
228        Ok(ContractRecord { id: contract_id.to_string(), name, description })
229    }
230
231    /// Auxiliary function that retrieves [`ContractRecord`]s filtered by a provided `filter_fn` closure.
232    ///
233    /// This function accepts a filter function `Fn(&ContractId) -> bool` that determines
234    /// which contracts are included based on their [`ContractId`]. It iterates over
235    /// Contract IDs stored in the blockchain's contract tree, applying the filter function to decide inclusion.
236    /// Converts the filtered Contract IDs into [`ContractRecord`] instances, returning them as a `Vec`,
237    /// or an empty `Vec` if no contracts are found.
238    fn get_filtered_contracts<F>(&self, filter_fn: F) -> Result<Vec<ContractRecord>>
239    where
240        F: Fn(&ContractId) -> bool,
241    {
242        let contract_keys = self.db.blockchain.contracts.wasm.iter().keys();
243
244        // Iterate through stored Contract IDs, filtering out the contracts based filter
245        contract_keys
246            .filter_map(|serialized_contract_id| {
247                // Deserialize the serialized Contract ID
248                let contract_id: ContractId = match serialized_contract_id
249                    .map_err(Error::from)
250                    .and_then(|id_bytes| deserialize(&id_bytes).map_err(Error::from))
251                {
252                    Ok(id) => id,
253                    Err(e) => {
254                        return Some(Err(Error::DatabaseError(format!(
255                            "[get_filtered_contracts] Contract ID retrieval or deserialization failed: {e:?}"
256                        ))));
257                    }
258                };
259
260                // Apply the filter
261                if filter_fn(&contract_id) {
262                    // Convert the matching Contract ID into a `ContractRecord`, return result
263                    return match self.to_contract_record(&contract_id).map_err(|e| {
264                        Error::DatabaseError(format!("[get_filtered_contracts] Failed to convert contract: {e:?}"))
265                    }) {
266                        Ok(record) => Some(Ok(record)),
267                        Err(e) => Some(Err(e)),
268                    };
269                }
270
271                // Skip contracts that do not match the filter
272                None
273            })
274            .collect::<Result<Vec<ContractRecord>>>()
275    }
276}
277
278/// Auxiliary function that extracts source code files from a TAR archive provided as a byte slice [`&[u8]`],
279/// returning a `Vec` of [`ContractSourceFile`]s representing the extracted file paths and their contents.
280pub fn untar_source(tar_bytes: &[u8]) -> Result<Vec<ContractSourceFile>> {
281    // Use a Cursor and archive to read the tar file
282    let cursor = Cursor::new(tar_bytes);
283    let mut archive = Archive::new(cursor);
284
285    // Vectors to hold the source paths and source contents
286    let mut source: Vec<ContractSourceFile> = Vec::new();
287
288    // Iterate through the entries in the tar archive
289    for tar_entry in archive.entries()? {
290        let mut tar_entry = tar_entry?;
291        let path = tar_entry.path()?.to_path_buf();
292
293        // Check if the entry is a file
294        if tar_entry.header().entry_type().is_file() {
295            let mut content = Vec::new();
296            tar_entry.read_to_end(&mut content)?;
297
298            // Convert the contents into a string
299            let source_content = String::from_utf8(content)
300                .map_err(|_| Error::ParseFailed("Failed converting source code to a string"))?;
301
302            // Collect source paths and contents
303            let path_str = path.to_string_lossy().into_owned();
304            source.push(ContractSourceFile::new(path_str, source_content));
305        }
306    }
307
308    Ok(source)
309}
310
311/// This test module ensures the correctness of the [`ExplorerService`] functionality with
312/// respect to smart contracts.
313///
314/// The tests in this module cover adding, loading, storing, retrieving, and validating contract
315/// metadata and source code. The primary goal is to validate the accuracy and reliability of
316/// the `ExplorerService` when handling contract-related operations.
317#[cfg(test)]
318mod tests {
319    use std::{fs::File, io::Read, path::Path, sync::Arc};
320
321    use tar::Archive;
322    use tempdir::TempDir;
323
324    use darkfi::Error::Custom;
325    use darkfi_sdk::crypto::MONEY_CONTRACT_ID;
326
327    use super::*;
328    use crate::{rpc::DarkfidRpcClient, test_utils::init_logger};
329
330    /// Tests the adding of [`ContractMetaData`] to the store by adding
331    /// metadata, and verifying the inserted data matches the expected results.
332    #[test]
333    fn test_add_metadata() -> Result<()> {
334        // Setup test, returning initialized service
335        let service = setup()?;
336
337        // Unique identifier for contracts in tests
338        let contract_id: ContractId = *MONEY_CONTRACT_ID;
339
340        // Declare expected metadata used for test
341        let expected_metadata: ContractMetaData = ContractMetaData::new(
342            "Money Contract".to_string(),
343            "Money Contract Description".to_string(),
344        );
345
346        // Add the metadata
347        service.add_contract_metadata(&[contract_id], &[expected_metadata.clone()])?;
348
349        // Get the metadata that was loaded as actual results
350        let actual_metadata = service.get_contract_metadata(&contract_id)?;
351
352        // Verify existence of loaded metadata
353        assert!(actual_metadata.is_some());
354
355        // Confirm actual metadata match expected results
356        assert_eq!(actual_metadata.unwrap(), expected_metadata.clone());
357
358        Ok(())
359    }
360
361    /// This test validates the loading and retrieval of native contract metadata. It sets up the
362    /// explorer service, loads native contract metadata, and then verifies metadata retrieval
363    /// for each native contract.
364    #[test]
365    fn test_load_native_contract_metadata() -> Result<()> {
366        // Setup test, returning initialized service
367        let service = setup()?;
368
369        // Load native contract metadata
370        service.load_native_contract_metadata()?;
371
372        // Define Contract IDs used to retrieve loaded metadata
373        let native_contract_ids = [*DAO_CONTRACT_ID, *DEPLOYOOOR_CONTRACT_ID, *MONEY_CONTRACT_ID];
374
375        // For each native contract, verify metadata was loaded
376        for contract_id in native_contract_ids.iter() {
377            let metadata = service.get_contract_metadata(contract_id)?;
378            assert!(metadata.is_some());
379        }
380
381        Ok(())
382    }
383
384    /// This test validates the loading, storage, and retrieval of native contract source code. It sets up the
385    /// explorer service, loads native contract sources, and then verifies both the source paths and content
386    /// for each native contract. The test compares the retrieved source paths and content against the expected
387    /// results from the corresponding tar archives.
388    #[test]
389    fn test_load_native_contracts() -> Result<()> {
390        // Setup test, returning initialized service
391        let service = setup()?;
392
393        // Load native contracts
394        service.load_native_contract_sources()?;
395
396        // Define contract archive paths
397        let native_contract_tars = [
398            "native_contracts_src/dao_contract_src.tar",
399            "native_contracts_src/deployooor_contract_src.tar",
400            "native_contracts_src/money_contract_src.tar",
401        ];
402
403        // Define Contract IDs to associate with each contract source archive
404        let native_contract_ids = [*DAO_CONTRACT_ID, *DEPLOYOOOR_CONTRACT_ID, *MONEY_CONTRACT_ID];
405
406        // Iterate archive and verify actual match expected results
407        for (&tar_file, &contract_id) in native_contract_tars.iter().zip(&native_contract_ids) {
408            // Verify that source paths match
409            verify_source_paths(&service, tar_file, contract_id)?;
410
411            // Verify that source content match
412            verify_source_content(&service, tar_file, contract_id)?;
413        }
414
415        Ok(())
416    }
417
418    /// This test validates the transformation of a [`ContractId`] into a [`ContractRecord`].
419    /// It sets up the explorer service, adds test metadata for a specific Contract ID, and then verifies the
420    /// correct transformation of this Contract ID into a ContractRecord.
421    #[test]
422    fn test_to_contract_record() -> Result<()> {
423        // Setup test, returning initialized service
424        let service = setup()?;
425
426        // Unique identifier for contracts in tests
427        let contract_id: ContractId = *MONEY_CONTRACT_ID;
428
429        // Declare expected metadata used for test
430        let expected_metadata: ContractMetaData = ContractMetaData::new(
431            "Money Contract".to_string(),
432            "Money Contract Description".to_string(),
433        );
434
435        // Load contract metadata used for test
436        service.add_contract_metadata(&[contract_id], &[expected_metadata.clone()])?;
437
438        // Transform Contract ID to a `ContractRecord`
439        let contract_record = service.to_contract_record(&contract_id)?;
440
441        // Verify that name and description exist
442        assert!(
443            contract_record.name.is_some(),
444            "Expected to_contract_record to return a contract with name"
445        );
446        assert!(
447            contract_record.description.is_some(),
448            "Expected to_contract_record to return a contract with description"
449        );
450
451        // Verify that id, name, and description match expected results
452        assert_eq!(contract_id.to_string(), contract_record.id);
453        assert_eq!(expected_metadata.name, contract_record.name.unwrap());
454        assert_eq!(expected_metadata.description, contract_record.description.unwrap());
455
456        Ok(())
457    }
458
459    /// Sets up a test case for contract metadata store testing by initializing the logger
460    /// and returning an initialized [`ExplorerService`].
461    fn setup() -> Result<ExplorerService> {
462        // Initialize logger to show execution output
463        init_logger(simplelog::LevelFilter::Off, vec!["sled", "runtime", "net"]);
464
465        // Create a temporary directory for sled DB
466        let temp_dir = TempDir::new("test")?;
467
468        // Initialize a sled DB instance using the temporary directory's path
469        let db_path = temp_dir.path().join("sled_db");
470
471        // Initialize the explorer service
472        ExplorerService::new(
473            db_path.to_string_lossy().into_owned(),
474            Arc::new(DarkfidRpcClient::new()),
475        )
476    }
477
478    /// This Auxiliary function verifies that the loaded native contract source paths match the expected results
479    /// from a given contract archive. This function extracts source paths from the specified `tar_file`, retrieves
480    /// the actual paths for the [`ContractId`] from the ExplorerService, and compares them to ensure they match.
481    fn verify_source_paths(
482        service: &ExplorerService,
483        tar_file: &str,
484        contract_id: ContractId,
485    ) -> Result<()> {
486        // Read the tar file and extract source paths
487        let tar_bytes = std::fs::read(tar_file)?;
488        let mut expected_source_paths = extract_file_paths_from_tar(&tar_bytes)?;
489
490        // Retrieve and sort actual source paths for the provided Contract ID
491        let mut actual_source_paths = service.get_contract_source_paths(&contract_id)?;
492
493        // Sort paths to ensure they are in the same order needed for assert
494        expected_source_paths.sort();
495        actual_source_paths.sort();
496
497        // Verify actual source matches expected result
498        assert_eq!(
499            expected_source_paths, actual_source_paths,
500            "Mismatch between expected and actual source paths for tar file: {tar_file}"
501        );
502
503        Ok(())
504    }
505
506    /// This auxiliary function verifies that the loaded native contract source content matches the
507    /// expected results from a given contract source archive. It extracts source files from the specified
508    /// `tar_file`, retrieves the actual content for each file path using the [`ContractId`] from the
509    /// ExplorerService, and compares them to ensure the content match.
510    fn verify_source_content(
511        service: &ExplorerService,
512        tar_file: &str,
513        contract_id: ContractId,
514    ) -> Result<()> {
515        // Read the tar file
516        let tar_bytes = std::fs::read(tar_file)?;
517        let expected_source_paths = extract_file_paths_from_tar(&tar_bytes)?;
518
519        // Validate contents of tar archive source code content
520        for file_path in expected_source_paths {
521            // Get the source code content
522            let actual_source = service.get_contract_source_content(&contract_id, &file_path)?;
523
524            // Verify source content exists
525            assert!(
526                actual_source.is_some(),
527                "Actual source `{file_path}` is missing in the store."
528            );
529
530            // Read the source content from the tar archive
531            let expected_source = read_file_from_tar(tar_file, &file_path)?;
532
533            // Verify actual source matches expected results
534            assert_eq!(
535                actual_source.unwrap(),
536                expected_source,
537                "Actual source does not match expected results `{file_path}`."
538            );
539        }
540
541        Ok(())
542    }
543
544    /// Auxiliary function that reads the contents of specified `file_path` within a tar archive.
545    fn read_file_from_tar(tar_path: &str, file_path: &str) -> Result<String> {
546        let file = File::open(tar_path)?;
547        let mut archive = Archive::new(file);
548        for entry in archive.entries()? {
549            let mut entry = entry?;
550            if let Ok(path) = entry.path() {
551                if path == Path::new(file_path) {
552                    let mut content = String::new();
553                    entry.read_to_string(&mut content)?;
554                    return Ok(content);
555                }
556            }
557        }
558
559        Err(Custom(format!("File {file_path} not found in tar archive.")))
560    }
561
562    /// Auxiliary function that extracts all file paths from the given `tar_bytes` tar archive.
563    pub fn extract_file_paths_from_tar(tar_bytes: &[u8]) -> Result<Vec<String>> {
564        let cursor = Cursor::new(tar_bytes);
565        let mut archive = Archive::new(cursor);
566
567        // Collect paths from the tar archive
568        let mut file_paths = Vec::new();
569        for entry in archive.entries()? {
570            let entry = entry?;
571            let path = entry.path()?;
572
573            // Skip directories and only include files
574            if entry.header().entry_type().is_file() {
575                file_paths.push(path.to_string_lossy().to_string());
576            }
577        }
578
579        Ok(file_paths)
580    }
581}