oasis_core_runtime/transaction/
rwset.rs

1//! Read/write set.
2
3/// A coarsened key prefix that represents any key that starts with
4/// this prefix.
5#[derive(Clone, Debug, Default, PartialEq, Eq, cbor::Encode, cbor::Decode)]
6#[cbor(transparent)]
7pub struct CoarsenedKey(pub Vec<u8>);
8
9impl AsRef<[u8]> for CoarsenedKey {
10    fn as_ref(&self) -> &[u8] {
11        &self.0
12    }
13}
14
15impl From<CoarsenedKey> for Vec<u8> {
16    fn from(val: CoarsenedKey) -> Self {
17        val.0
18    }
19}
20
21impl From<Vec<u8>> for CoarsenedKey {
22    fn from(v: Vec<u8>) -> CoarsenedKey {
23        CoarsenedKey(v)
24    }
25}
26
27/// A set of coarsened keys.
28pub type CoarsenedSet = Vec<CoarsenedKey>;
29
30/// A read/write set.
31#[derive(Clone, Debug, Default, PartialEq, Eq, cbor::Encode, cbor::Decode)]
32pub struct ReadWriteSet {
33    /// Size of the key prefixes (in bytes) used for coarsening the keys.
34    pub granularity: u16,
35    /// The read set.
36    pub read_set: CoarsenedSet,
37    /// The write set.
38    pub write_set: CoarsenedSet,
39}
40
41#[cfg(test)]
42mod test {
43    use super::*;
44
45    #[test]
46    fn test_serialization() {
47        let rw_set = ReadWriteSet {
48            granularity: 3,
49            read_set: vec![b"foo".to_vec().into(), b"bar".to_vec().into()],
50            write_set: vec![b"moo".to_vec().into()],
51        };
52
53        let enc = cbor::to_vec(rw_set.clone());
54
55        let dec_rw_set: ReadWriteSet = cbor::from_slice(&enc).unwrap();
56        assert_eq!(rw_set, dec_rw_set, "serialization should round-trip");
57    }
58}