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
//! Storage backend abstraction for contract state persistence.
//!
//! This module provides a trait-based abstraction over storage operations,
//! enabling both production (on-chain) and testing (in-memory) backends.
//!
//! # Storage Backends
//!
//! - **`HostStorage`**: Production backend that delegates to WASM host imports.
//! Used when contracts run on-chain.
//! - **`MemoryStorage`**: In-memory backend using `BTreeMap` for testing.
//! Enables fast, isolated unit tests without WASM runtime.
//!
//! # Example
//!
//! ```ignore
//! use truthlinked_sdk::backend::{StorageBackend, MemoryStorage};
//!
//! // Testing with in-memory storage
//! let mut storage = MemoryStorage::new();
//! let slot = [1u8; 32];
//! storage.write_32(slot, [42u8; 32])?;
//! let value = storage.read_32(&slot)?;
//! assert_eq!(value[0], 42);
//! ```
extern crate alloc;
use BTreeMap;
use crateenv;
use crateResult;
/// Trait for storage backends that support 32-byte slot read/write operations.
///
/// All storage in TruthLinked contracts operates on fixed 32-byte slots.
/// This trait abstracts the underlying storage mechanism, allowing contracts
/// to work with both on-chain storage (via WASM host) and in-memory storage (for testing).
/// Storage backend that delegates to WASM host imports.
///
/// This is a zero-sized type that forwards all storage operations to the
/// TruthLinked runtime via host function calls. Used when contracts execute on-chain.
///
/// # Example
///
/// ```ignore
/// use truthlinked_sdk::backend::{HostStorage, StorageBackend};
///
/// let storage = HostStorage;
/// let value = storage.read_32(&slot)?;
/// ```
;
/// In-memory storage backend for testing and development.
///
/// Uses a `BTreeMap` to store slot values in memory. This enables fast,
/// isolated unit tests without requiring a WASM runtime or blockchain.
///
/// # Example
///
/// ```ignore
/// use truthlinked_sdk::backend::MemoryStorage;
///
/// let mut storage = MemoryStorage::new()
/// .with_slot([1u8; 32], [42u8; 32])
/// .with_slot([2u8; 32], [99u8; 32]);
///
/// let snapshot = storage.snapshot(); // Clone current state
/// ```