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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use sui_sdk_types::Address;
use sui_sdk_types::ObjectReference;
use sui_sdk_types::framework::ApplyStreamError;
use sui_sdk_types::framework::EventStreamHead;
use sui_sdk_types::proof::ProofError;
use crate::proto::TryFromProtoError;
/// Errors returned by the [`crate::light_client`] APIs.
#[derive(Debug)]
#[non_exhaustive]
pub enum LightClientError {
/// A gRPC call returned a non-success status.
Rpc(tonic::Status),
/// A protobuf message returned by the server was malformed.
Proto(TryFromProtoError),
/// BCS decoding of a wire-format payload (e.g. the response's
/// `checkpoint_summary` bytes) failed.
Bcs(bcs::Error),
/// The BLS aggregate signature on a checkpoint summary failed to
/// verify against the cached validator committee.
InvalidSignature(sui_crypto::SignatureError),
/// The OCS proof failed to verify against the trusted checkpoint
/// summary.
InvalidProof(ProofError),
/// The cache was asked to apply a ratchet update with a new committee
/// whose `epoch` was not exactly one greater than the current epoch.
InvalidEpochAdvance {
/// The current epoch held by the cache.
current: u64,
/// The advertised epoch of the new committee.
provided: u64,
},
/// A checkpoint summary advertised as the last of an epoch had no
/// `end_of_epoch_data` payload — the next epoch's committee cannot
/// be extracted.
MissingEndOfEpochData {
/// The checkpoint sequence number whose summary was missing the
/// payload.
checkpoint: u64,
},
/// The server returned a proof anchored at a different checkpoint
/// than the one the caller requested.
CheckpointMismatch {
/// The checkpoint sequence number the caller asked for.
requested: u64,
/// The checkpoint sequence number on the returned summary.
returned: u64,
},
/// After ratcheting forward, the cache still did not have a
/// committee on file for the relevant epoch. This should not
/// happen in practice and indicates either a bug or a malicious
/// server response.
NoCommitteeForEpoch {
/// The epoch number that has no committee on file.
epoch: u64,
},
/// The ratchet driver tried to advance through more epochs than
/// the configured cap (see
/// [`crate::light_client::RatchetConfig::max_ratchet_gap`]) allows
/// in a single call. This guard exists to bound the work the
/// client will do in response to a server that keeps reporting
/// `last_checkpoint < target_seq` for every epoch, never letting
/// the discovery walk terminate.
RatchetGapTooLarge {
/// The cache's current epoch when the ratchet began.
current: u64,
/// The epoch the discovery walk had reached when the cap was
/// hit. The actual target epoch may be even further out.
target: u64,
/// The configured maximum gap.
max: u64,
},
/// The fullnode reported `NotFound` for an epoch the ratchet
/// needed to advance through. Distinguished from a generic
/// `Rpc(Status)` so callers can differentiate "the network does
/// not know about this epoch" (likely a bug or a request for an
/// epoch past the chain's tip) from a transport-level failure.
EpochNotFound {
/// The epoch number that the fullnode could not produce.
epoch: u64,
},
/// The server returned an inclusion proof for a different object
/// id than the caller asked about.
ObjectIdMismatch {
/// The object id the caller asked about.
requested: Address,
/// The object id on the returned object reference.
returned: Address,
},
/// The `object_data` bytes the server returned did not BCS-decode to
/// an [`Object`] whose `(id, version, digest)` match the
/// authenticated `ObjectReference`. Without this check, a server
/// could pass the proof verification step while still returning
/// arbitrary bytes for the object's contents.
///
/// Boxed because `ObjectReference` carries a 32-byte digest and a
/// 32-byte address; keeping two of them inline would inflate every
/// `Result<_, LightClientError>` slot by ~144 bytes for a case
/// that's exceedingly rare in practice.
///
/// [`Object`]: sui_sdk_types::Object
ObjectDataMismatch(Box<ObjectDataMismatch>),
/// The locally-replayed [`EventStreamHead`] disagreed with the
/// authenticated head fetched from chain at the given checkpoint.
/// This is the streaming client's terminal failure mode — once the
/// MMRs diverge, every subsequent event would compound the
/// divergence, so the stream is aborted with this error.
///
/// Boxed because `EventStreamHead` carries a `Vec<U256>` (each
/// `U256` is 32 bytes) and grows unbounded with checkpoint count;
/// keeping two of them inline would inflate every
/// `Result<_, LightClientError>` slot returned from the streaming
/// task.
MmrMismatch(Box<MmrMismatch>),
/// Folding a batch of received events into the local MMR violated
/// the [`apply_stream_updates`] contract — empty batch, mismatched
/// `checkpoint_seq` within a batch, or non-monotonic batch
/// ordering. Surfaces a malformed server response that slipped
/// past the per-event decode.
///
/// [`apply_stream_updates`]: sui_sdk_types::framework::apply_stream_updates
InvalidEventBatch(ApplyStreamError),
/// An object returned by the OCS inclusion proof flow did not
/// match the shape the caller expected — for example, an
/// `EventStreamHead` fetch returned a package instead of a Move
/// struct, or the dynamic-field contents were too short to
/// contain the expected value.
UnexpectedObjectShape {
/// A short, human-readable description of what the caller was
/// expecting versus what the object actually was.
reason: &'static str,
},
}
/// Payload for [`LightClientError::ObjectDataMismatch`].
#[derive(Debug)]
pub struct ObjectDataMismatch {
/// The authenticated reference from the verified inclusion proof.
pub expected: ObjectReference,
/// The reference reconstructed from the returned `object_data`
/// bytes.
pub returned: ObjectReference,
}
/// Payload for [`LightClientError::MmrMismatch`].
#[derive(Debug)]
pub struct MmrMismatch {
/// The checkpoint at which the reconciliation was attempted.
pub checkpoint: u64,
/// The head fetched from chain via an OCS inclusion proof — the
/// truth against which the local replay was compared.
pub expected: EventStreamHead,
/// The head the streaming client computed by folding received
/// events into its local MMR.
pub actual: EventStreamHead,
}
impl std::fmt::Display for LightClientError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Rpc(status) => write!(f, "gRPC error: {status}"),
Self::Proto(e) => write!(f, "malformed protobuf message: {e}"),
Self::Bcs(e) => write!(f, "BCS decoding failed: {e}"),
Self::InvalidSignature(e) => {
write!(f, "checkpoint summary signature did not verify: {e}")
}
Self::InvalidProof(e) => write!(f, "OCS proof did not verify: {e}"),
Self::InvalidEpochAdvance { current, provided } => write!(
f,
"cannot advance epoch cache: current epoch is {current}, but provided committee is for epoch {provided}"
),
Self::MissingEndOfEpochData { checkpoint } => write!(
f,
"checkpoint {checkpoint} was advertised as end-of-epoch but its summary has no `end_of_epoch_data` payload"
),
Self::CheckpointMismatch {
requested,
returned,
} => write!(
f,
"proof was returned for checkpoint {returned} but caller requested checkpoint {requested}"
),
Self::NoCommitteeForEpoch { epoch } => write!(
f,
"no validator committee on file for epoch {epoch} after ratchet"
),
Self::RatchetGapTooLarge {
current,
target,
max,
} => write!(
f,
"ratchet would advance from epoch {current} past epoch {target}, exceeding configured max gap of {max}"
),
Self::EpochNotFound { epoch } => {
write!(f, "fullnode reported NotFound for epoch {epoch}")
}
Self::ObjectIdMismatch {
requested,
returned,
} => write!(
f,
"proof was returned for object {returned} but caller requested object {requested}"
),
Self::ObjectDataMismatch(boxed) => {
let ObjectDataMismatch { expected, returned } = boxed.as_ref();
write!(
f,
"object_data bytes hash to {} version {} but the verified leaf attests to {} version {}",
returned.digest(),
returned.version(),
expected.digest(),
expected.version(),
)
}
Self::MmrMismatch(boxed) => {
let MmrMismatch {
checkpoint,
expected,
actual,
} = boxed.as_ref();
write!(
f,
"local MMR diverged from on-chain EventStreamHead at checkpoint {checkpoint}: \
locally replayed {} events to checkpoint {}, chain attests to {} events at \
checkpoint {}",
actual.num_events,
actual.checkpoint_seq,
expected.num_events,
expected.checkpoint_seq,
)
}
Self::InvalidEventBatch(e) => write!(f, "invalid event batch: {e}"),
Self::UnexpectedObjectShape { reason } => {
write!(f, "unexpected object shape: {reason}")
}
}
}
}
impl std::error::Error for LightClientError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Rpc(e) => Some(e),
Self::Proto(e) => Some(e),
Self::Bcs(e) => Some(e),
Self::InvalidSignature(e) => Some(e),
Self::InvalidProof(e) => Some(e),
Self::InvalidEventBatch(e) => Some(e),
_ => None,
}
}
}
impl From<tonic::Status> for LightClientError {
fn from(value: tonic::Status) -> Self {
Self::Rpc(value)
}
}
impl From<TryFromProtoError> for LightClientError {
fn from(value: TryFromProtoError) -> Self {
Self::Proto(value)
}
}
impl From<bcs::Error> for LightClientError {
fn from(value: bcs::Error) -> Self {
Self::Bcs(value)
}
}
impl From<sui_crypto::SignatureError> for LightClientError {
fn from(value: sui_crypto::SignatureError) -> Self {
Self::InvalidSignature(value)
}
}
impl From<ProofError> for LightClientError {
fn from(value: ProofError) -> Self {
Self::InvalidProof(value)
}
}
impl From<ApplyStreamError> for LightClientError {
fn from(value: ApplyStreamError) -> Self {
Self::InvalidEventBatch(value)
}
}