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
#![allow(dead_code)]
#![allow(unused_imports)]
pub mod env;
pub mod executor;
pub mod ext;
pub mod funcs;
pub mod registry;
pub mod result;
mod tests;
use crate::{
api::types::GearPages,
metadata::{registry::LocalRegistry, result::Result},
};
pub use result::Error;
use scale_info::{form::PortableForm, PortableRegistry};
use std::fmt;
use subxt::sp_runtime::traits::Saturating;
use wasmtime::AsContextMut;
pub type StoreData = ext::Ext;
const PAGE_SIZE: usize = 4096;
macro_rules! construct_metadata {
($($meta:ident),+) => {
#[derive(Debug, Eq)]
pub struct Metadata {
$(
pub $meta: Option<String>,
)+
}
impl PartialEq for Metadata {
fn eq(&self, other: &Self) -> bool {
$(
if self.$meta != other.$meta && stringify!($meta) != "meta_registry"{
return false;
}
)+
true
}
}
impl Metadata {
pub fn of(bin: &[u8]) -> Result<Self> {
executor::execute(bin, |mut reader| -> Result<Self> {
let memory = reader.memory()?;
Ok(Self {
$(
$meta: reader.meta(&memory, stringify!($meta))
.map(|b|String::from_utf8_lossy(&b).to_string())
.ok(),
)+
})
})
}
fn format(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let registry = self.registry().map_err(|_|fmt::Error)?;
let mut display = fmt.debug_struct("Metadata");
$(
if let Some(type_name) = &self.$meta {
if let Ok(ty) = registry.derive_name(&type_name) {
display.field(stringify!($meta), &ty);
}
else if stringify!($meta) != "meta_registry" {
display.field(stringify!($meta), &type_name);
}
}
)+
display.finish()
}
}
};
}
construct_metadata![
meta_title,
meta_init_input,
meta_init_output,
meta_async_init_input,
meta_async_init_output,
meta_handle_input,
meta_handle_output,
meta_async_handle_input,
meta_async_handle_output,
meta_state_input,
meta_state_output,
meta_registry
];
impl Metadata {
pub fn read(
bin: &[u8],
initial_size: u64,
pages: GearPages,
msg: Vec<u8>,
timestamp: u64,
height: u64,
) -> Result<Vec<u8>> {
executor::execute(bin, move |mut reader| -> Result<Vec<u8>> {
reader.state(initial_size, pages.clone(), msg.clone(), timestamp, height)
})
}
pub fn registry(&self) -> Result<PortableRegistry> {
PortableRegistry::from_hex(self.meta_registry.as_ref().ok_or(Error::RegistryNotFound)?)
}
}
impl fmt::Display for Metadata {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.format(fmt)
}
}