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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! Hash-pinned, worker-produced state ready for serialized runtime commit.
use std::collections::BTreeSet;
use std::fmt;
use alloy_primitives::{Address, U256};
use evm_fork_cache::PreparedAccountPatch;
use super::{AmmStatePoint, DeferredWork, PoolInstanceId, PoolRegistration, RuntimeWorkId};
/// One authoritative storage value fetched for a prepared pool.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct AmmPreparedStorage {
address: Address,
slot: U256,
value: U256,
}
impl AmmPreparedStorage {
/// Construct one prepared storage value.
pub const fn new(address: Address, slot: U256, value: U256) -> Self {
Self {
address,
slot,
value,
}
}
/// Storage-owning contract.
pub const fn address(self) -> Address {
self.address
}
/// Storage key.
pub const fn slot(self) -> U256 {
self.slot
}
/// Authoritative value at the prepared baseline.
pub const fn value(self) -> U256 {
self.value
}
}
/// Worker-produced pool metadata, exact state, and optional deferred follow-up
/// work pinned to one canonical point.
///
/// The public constructor is a trusted integration seam: the caller attests
/// that every value was fetched from the advertised hash. Runtime-owned
/// background jobs add an unforgeable work/generation claim internally before
/// they may use the scheduled commit path. Applications should normally queue
/// cold-start work through [`AmmRuntimeHandle::queue_cold_start`](super::AmmRuntimeHandle::queue_cold_start)
/// instead of constructing this artifact directly.
#[derive(Clone, Debug)]
pub struct AmmPreparedPoolState {
registration: PoolRegistration,
baseline: AmmStatePoint,
storage: Vec<AmmPreparedStorage>,
accounts: Option<PreparedAccountPatch>,
deferred: Vec<DeferredWork>,
schedule: Option<(RuntimeWorkId, PoolInstanceId)>,
}
pub(crate) type AmmPreparedPoolParts = (
PoolRegistration,
AmmStatePoint,
Vec<AmmPreparedStorage>,
Option<PreparedAccountPatch>,
Vec<DeferredWork>,
Option<(RuntimeWorkId, PoolInstanceId)>,
);
impl AmmPreparedPoolState {
/// Construct a caller-attested prepared pool, rejecting duplicate storage identities.
///
/// This validates the artifact's shape, not its external RPC provenance.
/// Passing values that were not fetched at `baseline` violates this API's
/// trust contract. The runtime still rejects a baseline that is no longer
/// current and validates the adapter's declared storage dependencies.
pub fn new(
registration: PoolRegistration,
baseline: AmmStatePoint,
storage: impl IntoIterator<Item = AmmPreparedStorage>,
) -> Result<Self, AmmPreparedStateError> {
let mut storage: Vec<_> = storage.into_iter().collect();
storage.sort_unstable_by_key(|entry| (entry.address, entry.slot));
let mut identities = BTreeSet::new();
for entry in &storage {
if !identities.insert((entry.address, entry.slot)) {
return Err(AmmPreparedStateError::DuplicateStorage {
address: entry.address,
slot: entry.slot,
});
}
}
Ok(Self {
registration,
baseline,
storage,
accounts: None,
deferred: Vec::new(),
schedule: None,
})
}
/// Prepared registration metadata.
pub const fn registration(&self) -> &PoolRegistration {
&self.registration
}
/// Exact post-block point used for every fetched value.
pub const fn baseline(&self) -> AmmStatePoint {
self.baseline
}
/// Canonically ordered prepared storage.
pub fn storage(&self) -> &[AmmPreparedStorage] {
&self.storage
}
/// Exact-hash account/code proof patch, when the worker verified code seeds.
pub const fn accounts(&self) -> Option<&PreparedAccountPatch> {
self.accounts.as_ref()
}
/// Optional adapter work intentionally left until after the pool is searchable.
pub fn deferred(&self) -> &[DeferredWork] {
&self.deferred
}
/// Scheduled work identity, when produced by the runtime worker.
pub fn work(&self) -> Option<&RuntimeWorkId> {
self.schedule.as_ref().map(|(work, _)| work)
}
/// Reserved pool generation, when produced by the runtime worker.
pub fn pool_instance(&self) -> Option<&PoolInstanceId> {
self.schedule.as_ref().map(|(_, pool)| pool)
}
#[allow(dead_code)]
pub(crate) fn with_schedule(mut self, work: RuntimeWorkId, pool: PoolInstanceId) -> Self {
self.schedule = Some((work, pool));
self
}
pub(crate) fn with_accounts(mut self, accounts: PreparedAccountPatch) -> Self {
self.accounts = Some(accounts);
self
}
pub(crate) fn with_deferred(mut self, deferred: Vec<DeferredWork>) -> Self {
self.deferred = deferred;
self
}
pub(crate) fn into_parts(self) -> AmmPreparedPoolParts {
(
self.registration,
self.baseline,
self.storage,
self.accounts,
self.deferred,
self.schedule,
)
}
}
/// Invalid worker-produced prepared state.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum AmmPreparedStateError {
/// The artifact carried more than one value for the same storage identity.
DuplicateStorage {
/// Storage-owning contract.
address: Address,
/// Duplicated storage key.
slot: U256,
},
}
impl fmt::Display for AmmPreparedStateError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DuplicateStorage { address, slot } => {
write!(
f,
"duplicate prepared storage value for ({address}, {slot})"
)
}
}
}
}
impl std::error::Error for AmmPreparedStateError {}