1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Built-in uses
use std::sync::{Arc, RwLock};

// External uses
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;

// Workspace uses

// Local uses
use crate::{
    node::InMemoryNodeInner,
    node::ShowCalls,
    node::ShowVMDetails,
    node::{ShowGasDetails, ShowStorageLogs},
};

pub struct ConfigurationApiNamespace<S> {
    node: Arc<RwLock<InMemoryNodeInner<S>>>,
}

impl<S> ConfigurationApiNamespace<S> {
    pub fn new(node: Arc<RwLock<InMemoryNodeInner<S>>>) -> Self {
        Self { node }
    }
}

#[rpc]
pub trait ConfigurationApiNamespaceT {
    /// Get the InMemoryNodeInner's show_calls property as a string
    ///
    /// # Returns
    /// The current `show_calls` value for the InMemoryNodeInner.
    #[rpc(name = "config_getShowCalls", returns = "String")]
    fn config_get_show_calls(&self) -> Result<String>;

    /// Get the InMemoryNodeInner's current_timestamp property
    ///
    /// # Returns
    /// The current `current_timestamp` value for the InMemoryNodeInner.
    #[rpc(name = "config_getCurrentTimestamp", returns = "u64")]
    fn config_get_current_timestamp(&self) -> Result<u64>;

    /// Set show_calls for the InMemoryNodeInner
    ///
    /// # Parameters
    /// - `value`: A ShowCalls enum to update show_calls to
    ///
    /// # Returns
    /// The updated/current `show_calls` value for the InMemoryNodeInner.
    #[rpc(name = "config_setShowCalls", returns = "String")]
    fn config_set_show_calls(&self, value: String) -> Result<String>;

    /// Set show_storage_logs for the InMemoryNodeInner
    ///
    /// # Parameters
    /// - `value`: A ShowStorageLogs enum to update show_storage_logs to
    ///
    /// # Returns
    /// The updated/current `show_storage_logs` value for the InMemoryNodeInner.
    #[rpc(name = "config_setShowStorageLogs", returns = "String")]
    fn config_set_show_storage_logs(&self, value: String) -> Result<String>;

    /// Set show_vm_details for the InMemoryNodeInner
    ///
    /// # Parameters
    /// - `value`: A ShowVMDetails enum to update show_vm_details to
    ///
    /// # Returns
    /// The updated/current `show_vm_details` value for the InMemoryNodeInner.
    #[rpc(name = "config_setShowVmDetails", returns = "String")]
    fn config_set_show_vm_details(&self, value: String) -> Result<String>;

    /// Set show_gas_details for the InMemoryNodeInner
    ///
    /// # Parameters
    /// - `value`: A ShowGasDetails enum to update show_gas_details to
    ///
    /// # Returns
    /// The updated/current `show_gas_details` value for the InMemoryNodeInner.
    #[rpc(name = "config_setShowGasDetails", returns = "String")]
    fn config_set_show_gas_details(&self, value: String) -> Result<String>;

    /// Set resolve_hashes for the InMemoryNodeInner
    ///
    /// # Parameters
    /// - `value`: A bool to update resolve_hashes to
    ///
    /// # Returns
    /// The updated `resolve_hashes` value for the InMemoryNodeInner.
    #[rpc(name = "config_setResolveHashes", returns = "bool")]
    fn config_set_resolve_hashes(&self, value: bool) -> Result<bool>;
}

impl<S: std::marker::Send + std::marker::Sync + 'static> ConfigurationApiNamespaceT
    for ConfigurationApiNamespace<S>
{
    fn config_get_show_calls(&self) -> Result<String> {
        let reader = self.node.read().unwrap();
        Ok(reader.show_calls.to_string())
    }

    fn config_get_current_timestamp(&self) -> Result<u64> {
        let reader = self.node.read().unwrap();
        Ok(reader.current_timestamp)
    }

    fn config_set_show_calls(&self, value: String) -> Result<String> {
        let show_calls = match value.parse::<ShowCalls>() {
            Ok(value) => value,
            Err(_) => {
                let reader = self.node.read().unwrap();
                return Ok(reader.show_calls.to_string());
            }
        };

        let mut inner = self.node.write().unwrap();
        inner.show_calls = show_calls;
        Ok(inner.show_calls.to_string())
    }

    fn config_set_show_storage_logs(&self, value: String) -> Result<String> {
        let show_storage_logs = match value.parse::<ShowStorageLogs>() {
            Ok(value) => value,
            Err(_) => {
                let reader = self.node.read().unwrap();
                return Ok(reader.show_storage_logs.to_string());
            }
        };

        let mut inner = self.node.write().unwrap();
        inner.show_storage_logs = show_storage_logs;
        Ok(inner.show_storage_logs.to_string())
    }

    fn config_set_show_vm_details(&self, value: String) -> Result<String> {
        let show_vm_details = match value.parse::<ShowVMDetails>() {
            Ok(value) => value,
            Err(_) => {
                let reader = self.node.read().unwrap();
                return Ok(reader.show_vm_details.to_string());
            }
        };

        let mut inner = self.node.write().unwrap();
        inner.show_vm_details = show_vm_details;
        Ok(inner.show_vm_details.to_string())
    }

    fn config_set_show_gas_details(&self, value: String) -> Result<String> {
        let show_gas_details = match value.parse::<ShowGasDetails>() {
            Ok(value) => value,
            Err(_) => {
                let reader = self.node.read().unwrap();
                return Ok(reader.show_gas_details.to_string());
            }
        };

        let mut inner = self.node.write().unwrap();
        inner.show_gas_details = show_gas_details;
        Ok(inner.show_gas_details.to_string())
    }

    fn config_set_resolve_hashes(&self, value: bool) -> Result<bool> {
        let mut inner = self.node.write().unwrap();
        inner.resolve_hashes = value;
        Ok(inner.resolve_hashes)
    }
}