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
170
171
172
173
174
175
176
use crate::{
database::{columns::CONTRACTS_STATE, Database},
state::{Error, IterDirection, MultiKey},
};
use fuel_vm::prelude::MerkleRoot;
use fuel_vm::{
crypto,
prelude::{Bytes32, ContractId, MerkleStorage},
};
use itertools::Itertools;
use std::borrow::Cow;
impl MerkleStorage<ContractId, Bytes32, Bytes32> for Database {
type Error = Error;
fn insert(
&mut self,
parent: &ContractId,
key: &Bytes32,
value: &Bytes32,
) -> Result<Option<Bytes32>, Error> {
let key = MultiKey::new((parent, key));
Database::insert(self, key.as_ref().to_vec(), CONTRACTS_STATE, *value).map_err(Into::into)
}
fn remove(&mut self, parent: &ContractId, key: &Bytes32) -> Result<Option<Bytes32>, Error> {
let key = MultiKey::new((parent, key));
Database::remove(self, key.as_ref(), CONTRACTS_STATE).map_err(Into::into)
}
fn get(&self, parent: &ContractId, key: &Bytes32) -> Result<Option<Cow<Bytes32>>, Error> {
let key = MultiKey::new((parent, key));
self.get(key.as_ref(), CONTRACTS_STATE).map_err(Into::into)
}
fn contains_key(&self, parent: &ContractId, key: &Bytes32) -> Result<bool, Error> {
let key = MultiKey::new((parent, key));
self.exists(key.as_ref(), CONTRACTS_STATE)
.map_err(Into::into)
}
fn root(&mut self, parent: &ContractId) -> Result<MerkleRoot, Error> {
let items: Vec<_> = Database::iter_all::<Vec<u8>, Bytes32>(
self,
CONTRACTS_STATE,
Some(parent.as_ref().to_vec()),
None,
Some(IterDirection::Forward),
)
.try_collect()?;
let root = items
.iter()
.filter_map(|(key, value)| {
(&key[..parent.len()] == parent.as_ref()).then(|| (key, value))
})
.sorted_by_key(|t| t.0)
.map(|(_, value)| value);
Ok(crypto::ephemeral_merkle_root(root).into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get() {
let storage_id: (ContractId, Bytes32) =
(ContractId::from([1u8; 32]), Bytes32::from([1u8; 32]));
let stored_value: Bytes32 = Bytes32::from([2u8; 32]);
let database = Database::default();
database
.insert(MultiKey::new(storage_id), CONTRACTS_STATE, stored_value)
.unwrap();
assert_eq!(
MerkleStorage::<ContractId, Bytes32, Bytes32>::get(
&database,
&storage_id.0,
&storage_id.1
)
.unwrap()
.unwrap()
.into_owned(),
stored_value
);
}
#[test]
fn put() {
let storage_id: (ContractId, Bytes32) =
(ContractId::from([1u8; 32]), Bytes32::from([1u8; 32]));
let stored_value: Bytes32 = Bytes32::from([2u8; 32]);
let mut database = Database::default();
MerkleStorage::<ContractId, Bytes32, Bytes32>::insert(
&mut database,
&storage_id.0,
&storage_id.1,
&stored_value,
)
.unwrap();
let returned: Bytes32 = database
.get(MultiKey::new(storage_id).as_ref(), CONTRACTS_STATE)
.unwrap()
.unwrap();
assert_eq!(returned, stored_value);
}
#[test]
fn remove() {
let storage_id: (ContractId, Bytes32) =
(ContractId::from([1u8; 32]), Bytes32::from([1u8; 32]));
let stored_value: Bytes32 = Bytes32::from([2u8; 32]);
let mut database = Database::default();
database
.insert(MultiKey::new(storage_id), CONTRACTS_STATE, stored_value)
.unwrap();
MerkleStorage::<ContractId, Bytes32, Bytes32>::remove(
&mut database,
&storage_id.0,
&storage_id.1,
)
.unwrap();
assert!(!database
.exists(MultiKey::new(storage_id).as_ref(), CONTRACTS_STATE)
.unwrap());
}
#[test]
fn exists() {
let storage_id: (ContractId, Bytes32) =
(ContractId::from([1u8; 32]), Bytes32::from([1u8; 32]));
let stored_value: Bytes32 = Bytes32::from([2u8; 32]);
let database = Database::default();
database
.insert(MultiKey::new(storage_id), CONTRACTS_STATE, stored_value)
.unwrap();
assert!(MerkleStorage::<ContractId, Bytes32, Bytes32>::contains_key(
&database,
&storage_id.0,
&storage_id.1
)
.unwrap());
}
#[test]
fn root() {
let storage_id: (ContractId, Bytes32) =
(ContractId::from([1u8; 32]), Bytes32::from([1u8; 32]));
let stored_value: Bytes32 = Bytes32::from([2u8; 32]);
let mut database = Database::default();
MerkleStorage::<ContractId, Bytes32, Bytes32>::insert(
&mut database,
&storage_id.0,
&storage_id.1,
&stored_value,
)
.unwrap();
let root =
MerkleStorage::<ContractId, Bytes32, Bytes32>::root(&mut database, &storage_id.0);
assert!(root.is_ok())
}
}