1use std::sync::Arc;
2
3use borsh::{BorshDeserialize, BorshSerialize};
4use serde::de::DeserializeOwned;
5use serde::{Deserialize, Serialize};
6use sov_rollup_interface::rpc::{BatchResponse, TxIdentifier, TxResponse};
7use sov_rollup_interface::stf::{Event, EventKey, TransactionReceipt};
8
9#[derive(
17 Clone, PartialEq, PartialOrd, Eq, Ord, Debug, Default, BorshDeserialize, BorshSerialize,
18)]
19#[cfg_attr(feature = "arbitrary", derive(proptest_derive::Arbitrary))]
20pub struct DbBytes(Arc<Vec<u8>>);
21
22impl DbBytes {
23 pub fn new(contents: Vec<u8>) -> Self {
25 Self(Arc::new(contents))
26 }
27}
28
29impl From<Vec<u8>> for DbBytes {
30 fn from(value: Vec<u8>) -> Self {
31 Self(Arc::new(value))
32 }
33}
34
35impl AsRef<[u8]> for DbBytes {
36 fn as_ref(&self) -> &[u8] {
37 self.0.as_ref()
38 }
39}
40
41pub type AccessoryKey = Vec<u8>;
45pub type AccessoryStateValue = Option<Vec<u8>>;
49
50pub type DbHash = [u8; 32];
52pub type JmtValue = Option<Vec<u8>>;
54pub(crate) type StateKey = Vec<u8>;
55
56#[derive(Debug, PartialEq, BorshDeserialize, BorshSerialize)]
60#[cfg_attr(feature = "arbitrary", derive(proptest_derive::Arbitrary))]
61pub struct StoredSlot {
62 pub hash: DbHash,
64 pub extra_data: DbBytes,
66 pub batches: std::ops::Range<BatchNumber>,
68}
69
70#[derive(Debug, PartialEq, BorshDeserialize, BorshSerialize)]
73#[cfg_attr(feature = "arbitrary", derive(proptest_derive::Arbitrary))]
74pub struct StoredBatch {
75 pub hash: DbHash,
77 pub txs: std::ops::Range<TxNumber>,
79 pub custom_receipt: DbBytes,
81}
82
83impl<B: DeserializeOwned, T> TryFrom<StoredBatch> for BatchResponse<B, T> {
84 type Error = anyhow::Error;
85 fn try_from(value: StoredBatch) -> Result<Self, Self::Error> {
86 Ok(Self {
87 hash: value.hash,
88 custom_receipt: bincode::deserialize(&value.custom_receipt.0)?,
89 tx_range: value.txs.start.into()..value.txs.end.into(),
90 txs: None,
91 })
92 }
93}
94
95#[derive(Debug, PartialEq, BorshSerialize, BorshDeserialize, Clone)]
98pub struct StoredTransaction {
99 pub hash: DbHash,
101 pub events: std::ops::Range<EventNumber>,
103 pub body: Option<Vec<u8>>,
105 pub custom_receipt: DbBytes,
107}
108
109impl<R: DeserializeOwned> TryFrom<StoredTransaction> for TxResponse<R> {
110 type Error = anyhow::Error;
111 fn try_from(value: StoredTransaction) -> Result<Self, Self::Error> {
112 Ok(Self {
113 hash: value.hash,
114 event_range: value.events.start.into()..value.events.end.into(),
115 body: value.body,
116 custom_receipt: bincode::deserialize(&value.custom_receipt.0)?,
117 })
118 }
119}
120
121pub fn split_tx_for_storage<R: Serialize>(
123 tx: TransactionReceipt<R>,
124 event_offset: u64,
125) -> (StoredTransaction, Vec<Event>) {
126 let event_range = EventNumber(event_offset)..EventNumber(event_offset + tx.events.len() as u64);
127 let tx_for_storage = StoredTransaction {
128 hash: tx.tx_hash,
129 events: event_range,
130 body: tx.body_to_save,
131 custom_receipt: DbBytes::new(
132 bincode::serialize(&tx.receipt).expect("Serialization to vec is infallible"),
133 ),
134 };
135 (tx_for_storage, tx.events)
136}
137
138#[derive(Debug, PartialEq, Serialize, Deserialize)]
140pub enum EventIdentifier {
141 TxIdAndIndex((TxIdentifier, u64)),
143 TxIdAndKey((TxIdentifier, EventKey)),
145 Number(EventNumber),
149}
150
151#[derive(Debug, PartialEq, Serialize, Deserialize)]
153pub enum EventGroupIdentifier {
154 TxId(TxIdentifier),
156 Key(Vec<u8>),
159}
160
161macro_rules! u64_wrapper {
162 ($name:ident) => {
163 #[derive(
165 Clone,
166 Copy,
167 ::core::fmt::Debug,
168 Default,
169 PartialEq,
170 Eq,
171 PartialOrd,
172 Ord,
173 ::borsh::BorshDeserialize,
174 ::borsh::BorshSerialize,
175 ::serde::Serialize,
176 ::serde::Deserialize,
177 )]
178 #[cfg_attr(feature = "arbitrary", derive(proptest_derive::Arbitrary))]
179 pub struct $name(pub u64);
180
181 impl From<$name> for u64 {
182 fn from(value: $name) -> Self {
183 value.0
184 }
185 }
186
187 #[cfg(feature = "arbitrary")]
188 impl<'a> ::arbitrary::Arbitrary<'a> for $name {
189 fn arbitrary(u: &mut ::arbitrary::Unstructured<'a>) -> ::arbitrary::Result<Self> {
190 u.arbitrary().map($name)
191 }
192 }
193 };
194}
195
196u64_wrapper!(SlotNumber);
197u64_wrapper!(BatchNumber);
198u64_wrapper!(TxNumber);
199u64_wrapper!(EventNumber);
200
201#[cfg(feature = "arbitrary")]
202pub mod arbitrary {
203 use super::*;
206
207 impl<'a> ::arbitrary::Arbitrary<'a> for DbBytes {
208 fn arbitrary(u: &mut ::arbitrary::Unstructured<'a>) -> ::arbitrary::Result<Self> {
209 u.arbitrary().map(DbBytes::new)
210 }
211 }
212
213 impl<'a> ::arbitrary::Arbitrary<'a> for StoredTransaction {
214 fn arbitrary(u: &mut ::arbitrary::Unstructured<'a>) -> ::arbitrary::Result<Self> {
215 Ok(StoredTransaction {
216 hash: u.arbitrary()?,
217 events: u.arbitrary()?,
218 body: u.arbitrary()?,
219 custom_receipt: u.arbitrary()?,
220 })
221 }
222 }
223
224 impl<'a> ::arbitrary::Arbitrary<'a> for StoredBatch {
225 fn arbitrary(u: &mut ::arbitrary::Unstructured<'a>) -> ::arbitrary::Result<Self> {
226 Ok(StoredBatch {
227 hash: u.arbitrary()?,
228 txs: u.arbitrary()?,
229 custom_receipt: u.arbitrary()?,
230 })
231 }
232 }
233
234 impl<'a> ::arbitrary::Arbitrary<'a> for StoredSlot {
235 fn arbitrary(u: &mut ::arbitrary::Unstructured<'a>) -> ::arbitrary::Result<Self> {
236 Ok(StoredSlot {
237 hash: u.arbitrary()?,
238 extra_data: u.arbitrary()?,
239 batches: u.arbitrary()?,
240 })
241 }
242 }
243}