1use anyhow::Result;
4use async_trait::async_trait;
5use std::path::Path;
6
7use crate::config::RuntimeConfig;
8use crate::security::SecurityConfig;
9use crate::types::{
10 RuntimeEnvironment, ExecutionInputs, ExecutionResult, RuntimeCapabilities,
11 RuntimeMetricDefinition, RuntimeEvent, SecurityViolation
12};
13
14#[async_trait]
16pub trait BlockchainRuntime: Send + Sync {
17 fn blockchain_id(&self) -> &str;
19
20 async fn create_environment(&self, config: RuntimeConfig) -> Result<RuntimeEnvironment>;
22
23 async fn execute(
25 &self,
26 env: &RuntimeEnvironment,
27 code_path: &Path,
28 inputs: &ExecutionInputs,
29 ) -> Result<ExecutionResult>;
30
31 async fn deploy_contract(
33 &self,
34 env: &RuntimeEnvironment,
35 bytecode: &[u8],
36 constructor_args: &[u8],
37 ) -> Result<String>;
38
39 async fn call_function(
41 &self,
42 env: &RuntimeEnvironment,
43 contract_address: &str,
44 function: &str,
45 args: &[u8],
46 ) -> Result<Vec<u8>>;
47
48 fn metrics_definition(&self) -> Vec<RuntimeMetricDefinition>;
50
51 async fn monitor(
53 &self,
54 env: &RuntimeEnvironment,
55 execution_id: &str,
56 ) -> Result<Vec<RuntimeEvent>>;
57
58 async fn destroy(&self, env: RuntimeEnvironment) -> Result<()>;
60
61 async fn is_available(&self) -> bool;
63
64 fn capabilities(&self) -> RuntimeCapabilities;
66
67 async fn execute_secure(
69 &self,
70 env: &RuntimeEnvironment,
71 code_path: &Path,
72 inputs: &ExecutionInputs,
73 security_config: &SecurityConfig,
74 ) -> Result<ExecutionResult>;
75
76 async fn check_reentrancy(
78 &self,
79 env: &RuntimeEnvironment,
80 function_name: &str,
81 caller: &str,
82 call_stack: &[String],
83 ) -> Result<bool>;
84
85 async fn detect_overflow(
87 &self,
88 env: &RuntimeEnvironment,
89 operation: &str,
90 operands: &[i64],
91 ) -> Result<bool>;
92
93 async fn verify_access_control(
95 &self,
96 env: &RuntimeEnvironment,
97 function_name: &str,
98 caller: &str,
99 required_role: Option<&str>,
100 ) -> Result<bool>;
101
102 async fn enforce_resource_limits(
104 &self,
105 env: &RuntimeEnvironment,
106 gas_used: u64,
107 memory_used: u64,
108 call_depth: u32,
109 external_calls: u32,
110 security_config: &SecurityConfig,
111 ) -> Result<Vec<SecurityViolation>>;
112
113 async fn get_security_report(
115 &self,
116 env: &RuntimeEnvironment,
117 execution_id: &str,
118 ) -> Result<std::collections::HashMap<String, serde_json::Value>>;
119}
120
121pub struct DefaultBlockchainRuntime {
123 blockchain_id: String,
124 capabilities: RuntimeCapabilities,
125}
126
127impl DefaultBlockchainRuntime {
128 pub fn new(blockchain_id: String) -> Self {
130 Self {
131 blockchain_id,
132 capabilities: RuntimeCapabilities::default(),
133 }
134 }
135
136 pub fn with_capabilities(blockchain_id: String, capabilities: RuntimeCapabilities) -> Self {
138 Self {
139 blockchain_id,
140 capabilities,
141 }
142 }
143}
144
145#[async_trait]
146impl BlockchainRuntime for DefaultBlockchainRuntime {
147 fn blockchain_id(&self) -> &str {
148 &self.blockchain_id
149 }
150
151 async fn create_environment(&self, config: RuntimeConfig) -> Result<RuntimeEnvironment> {
152 Ok(RuntimeEnvironment {
154 environment_id: format!("env_{}", std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_millis()),
155 blockchain_id: self.blockchain_id.clone(),
156 runtime_type: crate::types::RuntimeType::LocalProcess,
157 endpoint_url: "http://localhost:8545".to_string(),
158 state: crate::types::EnvironmentState::Ready,
159 metadata: std::collections::HashMap::new(),
160 })
161 }
162
163 async fn execute(
164 &self,
165 _env: &RuntimeEnvironment,
166 _code_path: &Path,
167 _inputs: &ExecutionInputs,
168 ) -> Result<ExecutionResult> {
169 Ok(ExecutionResult::new("exec_123".to_string(), true))
171 }
172
173 async fn deploy_contract(
174 &self,
175 _env: &RuntimeEnvironment,
176 _bytecode: &[u8],
177 _constructor_args: &[u8],
178 ) -> Result<String> {
179 Ok("0x1234567890abcdef".to_string())
181 }
182
183 async fn call_function(
184 &self,
185 _env: &RuntimeEnvironment,
186 _contract_address: &str,
187 _function: &str,
188 _args: &[u8],
189 ) -> Result<Vec<u8>> {
190 Ok(vec![0x01, 0x02, 0x03])
192 }
193
194 fn metrics_definition(&self) -> Vec<RuntimeMetricDefinition> {
195 vec![
196 RuntimeMetricDefinition {
197 name: "gas_used".to_string(),
198 description: "Gas consumed during execution".to_string(),
199 unit: "gas".to_string(),
200 metric_type: crate::types::MetricType::Gas,
201 },
202 RuntimeMetricDefinition {
203 name: "execution_time".to_string(),
204 description: "Time taken to execute".to_string(),
205 unit: "ms".to_string(),
206 metric_type: crate::types::MetricType::Time,
207 },
208 ]
209 }
210
211 async fn monitor(
212 &self,
213 _env: &RuntimeEnvironment,
214 _execution_id: &str,
215 ) -> Result<Vec<RuntimeEvent>> {
216 Ok(vec![])
218 }
219
220 async fn destroy(&self, _env: RuntimeEnvironment) -> Result<()> {
221 Ok(())
223 }
224
225 async fn is_available(&self) -> bool {
226 true
228 }
229
230 fn capabilities(&self) -> RuntimeCapabilities {
231 self.capabilities.clone()
232 }
233
234 async fn execute_secure(
235 &self,
236 env: &RuntimeEnvironment,
237 code_path: &Path,
238 inputs: &ExecutionInputs,
239 _security_config: &SecurityConfig,
240 ) -> Result<ExecutionResult> {
241 let mut result = self.execute(env, code_path, inputs).await?;
243
244 result.security_context = crate::types::SecureExecutionContext::default();
246
247 Ok(result)
248 }
249
250 async fn check_reentrancy(
251 &self,
252 _env: &RuntimeEnvironment,
253 _function_name: &str,
254 _caller: &str,
255 _call_stack: &[String],
256 ) -> Result<bool> {
257 Ok(false)
259 }
260
261 async fn detect_overflow(
262 &self,
263 _env: &RuntimeEnvironment,
264 _operation: &str,
265 _operands: &[i64],
266 ) -> Result<bool> {
267 Ok(false)
269 }
270
271 async fn verify_access_control(
272 &self,
273 _env: &RuntimeEnvironment,
274 _function_name: &str,
275 _caller: &str,
276 _required_role: Option<&str>,
277 ) -> Result<bool> {
278 Ok(true)
280 }
281
282 async fn enforce_resource_limits(
283 &self,
284 _env: &RuntimeEnvironment,
285 _gas_used: u64,
286 _memory_used: u64,
287 _call_depth: u32,
288 _external_calls: u32,
289 _security_config: &SecurityConfig,
290 ) -> Result<Vec<SecurityViolation>> {
291 Ok(vec![])
293 }
294
295 async fn get_security_report(
296 &self,
297 _env: &RuntimeEnvironment,
298 _execution_id: &str,
299 ) -> Result<std::collections::HashMap<String, serde_json::Value>> {
300 Ok(std::collections::HashMap::new())
302 }
303}