forc_debug/
types.rs

1use crate::error::{Error, Result};
2use dap::types::Breakpoint;
3use fuel_types::ContractId;
4use std::{
5    collections::HashMap,
6    ops::{Deref, DerefMut},
7    path::PathBuf,
8};
9use sway_core::asm_generation::ProgramABI;
10
11pub type ExitCode = i64;
12pub type Instruction = u64;
13pub type Breakpoints = HashMap<PathBuf, Vec<Breakpoint>>;
14
15/// A map storing ABIs for contracts, capable of fetching ABIs from the registry for unknown contracts.
16#[derive(Debug, Default)]
17pub struct AbiMap(HashMap<ContractId, ProgramABI>);
18
19impl AbiMap {
20    /// Registers the given ABI for the given contract ID.
21    pub fn register_abi(&mut self, contract_id: ContractId, abi: ProgramABI) {
22        self.insert(contract_id, abi);
23    }
24
25    /// Either fetches the ABI from the Sway ABI Registry or returns it from the cache if it's already known.
26    pub fn get_or_fetch_abi(&mut self, contract_id: &ContractId) -> Option<&ProgramABI> {
27        // If we already have it, return it
28        if self.contains_key(contract_id) {
29            return self.get(contract_id);
30        }
31
32        // Try to fetch from ABI Registry
33        match fetch_abi_from_registry(contract_id) {
34            Ok(abi) => {
35                self.register_abi(*contract_id, abi);
36                self.get(contract_id)
37            }
38            Err(_) => None,
39        }
40    }
41}
42
43impl Deref for AbiMap {
44    type Target = HashMap<ContractId, ProgramABI>;
45    fn deref(&self) -> &Self::Target {
46        &self.0
47    }
48}
49
50impl DerefMut for AbiMap {
51    fn deref_mut(&mut self) -> &mut Self::Target {
52        &mut self.0
53    }
54}
55
56/// Fetches the ABI for the given contract ID from the Sway ABI Registry.
57fn fetch_abi_from_registry(_contract_id: &ContractId) -> Result<ProgramABI> {
58    // TODO: Implement this once the Sway ABI Registry is available
59    // See this github issue: https://github.com/FuelLabs/sway/issues/6862
60    Err(Error::AbiError("Not implemented yet".to_string()))
61}