-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmemory.rs
More file actions
169 lines (158 loc) · 6.02 KB
/
memory.rs
File metadata and controls
169 lines (158 loc) · 6.02 KB
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
//! Internally used functionality to interact between WASM and the host environment.
//! Most of the usage of types is unsafe and requires knowledge on how
//! the WASM runtime is set and used. Use with caution.
//!
//! End users should be using higher levels of abstraction to write contracts
//! and shouldn't need to manipulate functions and types in this module directly.
//! Use with caution.
pub mod buf;
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub struct WasmLinearMem {
pub start_ptr: *const u8,
pub size: u64,
}
impl WasmLinearMem {
/// # Safety
/// Ensure that the passed pointer is a valid pointer to the start of
/// the WASM linear memory
pub unsafe fn new(start_ptr: *const u8, size: u64) -> Self {
Self { start_ptr, size }
}
}
#[cfg(feature = "contract")]
pub mod wasm_interface {
use crate::prelude::*;
fn set_logger() -> Result<(), ContractInterfaceResult> {
#[cfg(feature = "trace")]
{
use crate::prelude::*;
use tracing_subscriber as tra;
if let Err(err) = tra::fmt()
.with_env_filter("warn,freenet_stdlib=trace")
.try_init()
{
return Err(ContractInterfaceResult::from(Err::<ValidateResult, _>(
ContractError::Other(format!("{}", err)),
)));
}
}
Ok(())
}
use std::io::Read;
/// Read all bytes from a streaming buffer into a Vec.
fn read_streaming_bytes(ptr: i64) -> Result<Vec<u8>, ContractInterfaceResult> {
let mut reader = unsafe { super::buf::StreamingBuffer::from_ptr(ptr) };
let mut bytes = Vec::with_capacity(reader.total_remaining());
reader.read_to_end(&mut bytes).map_err(|e| {
ContractInterfaceResult::from(Err::<ValidateResult, _>(ContractError::Other(format!(
"streaming read failed: {e}"
))))
})?;
Ok(bytes)
}
pub fn inner_validate_state<T: ContractInterface>(
parameters: i64,
state: i64,
related: i64,
) -> i64 {
if let Err(e) = set_logger().map_err(|e| e.into_raw()) {
return e;
}
let parameters = match read_streaming_bytes(parameters) {
Ok(bytes) => Parameters::from(bytes),
Err(e) => return e.into_raw(),
};
let state = match read_streaming_bytes(state) {
Ok(bytes) => State::from(bytes),
Err(e) => return e.into_raw(),
};
let related_bytes = match read_streaming_bytes(related) {
Ok(bytes) => bytes,
Err(e) => return e.into_raw(),
};
let related: RelatedContracts<'static> =
match bincode::deserialize::<RelatedContracts>(&related_bytes) {
Ok(v) => v.into_owned(),
Err(err) => {
return ContractInterfaceResult::from(Err::<::core::primitive::bool, _>(
ContractError::Deser(format!("{}", err)),
))
.into_raw()
}
};
let result = <T as ContractInterface>::validate_state(parameters, state, related);
ContractInterfaceResult::from(result).into_raw()
}
pub fn inner_update_state<T: ContractInterface>(
parameters: i64,
state: i64,
updates: i64,
) -> i64 {
if let Err(e) = set_logger().map_err(|e| e.into_raw()) {
return e;
}
let parameters = match read_streaming_bytes(parameters) {
Ok(bytes) => Parameters::from(bytes),
Err(e) => return e.into_raw(),
};
let state = match read_streaming_bytes(state) {
Ok(bytes) => State::from(bytes),
Err(e) => return e.into_raw(),
};
let updates_bytes = match read_streaming_bytes(updates) {
Ok(bytes) => bytes,
Err(e) => return e.into_raw(),
};
let updates: Vec<UpdateData<'static>> =
match bincode::deserialize::<Vec<UpdateData>>(&updates_bytes) {
Ok(v) => v.into_iter().map(|u| u.into_owned()).collect(),
Err(err) => {
return ContractInterfaceResult::from(Err::<ValidateResult, _>(
ContractError::Deser(format!("{}", err)),
))
.into_raw()
}
};
let result = <T as ContractInterface>::update_state(parameters, state, updates);
ContractInterfaceResult::from(result).into_raw()
}
pub fn inner_summarize_state<T: ContractInterface>(parameters: i64, state: i64) -> i64 {
if let Err(e) = set_logger().map_err(|e| e.into_raw()) {
return e;
}
let parameters = match read_streaming_bytes(parameters) {
Ok(bytes) => Parameters::from(bytes),
Err(e) => return e.into_raw(),
};
let state = match read_streaming_bytes(state) {
Ok(bytes) => State::from(bytes),
Err(e) => return e.into_raw(),
};
let summary = <T as ContractInterface>::summarize_state(parameters, state);
ContractInterfaceResult::from(summary).into_raw()
}
pub fn inner_get_state_delta<T: ContractInterface>(
parameters: i64,
state: i64,
summary: i64,
) -> i64 {
if let Err(e) = set_logger().map_err(|e| e.into_raw()) {
return e;
}
let parameters = match read_streaming_bytes(parameters) {
Ok(bytes) => Parameters::from(bytes),
Err(e) => return e.into_raw(),
};
let state = match read_streaming_bytes(state) {
Ok(bytes) => State::from(bytes),
Err(e) => return e.into_raw(),
};
let summary = match read_streaming_bytes(summary) {
Ok(bytes) => StateSummary::from(bytes),
Err(e) => return e.into_raw(),
};
let new_delta = <T as ContractInterface>::get_state_delta(parameters, state, summary);
ContractInterfaceResult::from(new_delta).into_raw()
}
}