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
use crate::{
    commit::{CommitHash, CommitProof, CommitState, Comparison},
    encoding::encoding_error,
};
use async_trait::async_trait;
use binary_stream::futures::{
    BinaryReader, BinaryWriter, Decodable, Encodable,
};
use futures::io::{AsyncRead, AsyncSeek, AsyncWrite};
use rs_merkle::{algorithms::Sha256, MerkleProof};
use std::io::{Error, ErrorKind, Result};

#[async_trait]
impl Encodable for CommitHash {
    async fn encode<W: AsyncWrite + AsyncSeek + Unpin + Send>(
        &self,
        writer: &mut BinaryWriter<W>,
    ) -> Result<()> {
        writer.write_bytes(self.as_ref()).await?;
        Ok(())
    }
}

#[async_trait]
impl Decodable for CommitHash {
    async fn decode<R: AsyncRead + AsyncSeek + Unpin + Send>(
        &mut self,
        reader: &mut BinaryReader<R>,
    ) -> Result<()> {
        let commit: [u8; 32] = reader
            .read_bytes(32)
            .await?
            .as_slice()
            .try_into()
            .map_err(encoding_error)?;
        *self = CommitHash(commit);
        Ok(())
    }
}

#[async_trait]
impl Encodable for CommitState {
    async fn encode<W: AsyncWrite + AsyncSeek + Unpin + Send>(
        &self,
        writer: &mut BinaryWriter<W>,
    ) -> Result<()> {
        self.0.encode(&mut *writer).await?;
        self.1.encode(&mut *writer).await?;
        Ok(())
    }
}

#[async_trait]
impl Decodable for CommitState {
    async fn decode<R: AsyncRead + AsyncSeek + Unpin + Send>(
        &mut self,
        reader: &mut BinaryReader<R>,
    ) -> Result<()> {
        self.0.decode(&mut *reader).await?;
        self.1.decode(&mut *reader).await?;
        Ok(())
    }
}

#[async_trait]
impl Encodable for CommitProof {
    async fn encode<W: AsyncWrite + AsyncSeek + Unpin + Send>(
        &self,
        writer: &mut BinaryWriter<W>,
    ) -> Result<()> {
        writer.write_bytes(self.root.as_ref()).await?;
        let proof_bytes = self.proof.to_bytes();
        writer.write_u32(proof_bytes.len() as u32).await?;
        writer.write_bytes(&proof_bytes).await?;

        self.length.encode(&mut *writer).await?;
        self.indices.encode(&mut *writer).await?;

        Ok(())
    }
}

#[async_trait]
impl Decodable for CommitProof {
    async fn decode<R: AsyncRead + AsyncSeek + Unpin + Send>(
        &mut self,
        reader: &mut BinaryReader<R>,
    ) -> Result<()> {
        let root_hash: [u8; 32] = reader
            .read_bytes(32)
            .await?
            .as_slice()
            .try_into()
            .map_err(encoding_error)?;
        self.root = CommitHash(root_hash);

        let length = reader.read_u32().await?;
        let proof_bytes = reader.read_bytes(length as usize).await?;
        let proof = MerkleProof::<Sha256>::from_bytes(&proof_bytes)
            .map_err(encoding_error)?;
        self.proof = proof;

        self.length.decode(&mut *reader).await?;
        self.indices.decode(&mut *reader).await?;
        Ok(())
    }
}

#[async_trait]
impl Encodable for Comparison {
    async fn encode<W: AsyncWrite + AsyncSeek + Unpin + Send>(
        &self,
        writer: &mut BinaryWriter<W>,
    ) -> Result<()> {
        match self {
            Self::Equal => {
                writer.write_u8(1).await?;
            }
            Self::Contains(indices) => {
                writer.write_u8(2).await?;
                writer.write_u32(indices.len() as u32).await?;
                for i in indices {
                    writer.write_u64(*i as u64).await?;
                }
            }
            Self::Unknown => {
                writer.write_u8(3).await?;
            }
        }
        Ok(())
    }
}

#[async_trait]
impl Decodable for Comparison {
    async fn decode<R: AsyncRead + AsyncSeek + Unpin + Send>(
        &mut self,
        reader: &mut BinaryReader<R>,
    ) -> Result<()> {
        let kind = reader.read_u8().await?;
        match kind {
            1 => {
                *self = Self::Equal;
            }
            2 => {
                let indices_len = reader.read_u32().await? as usize;
                let mut indices = Vec::with_capacity(indices_len);
                for _ in 0..indices_len {
                    indices.push(reader.read_u64().await? as usize);
                }

                *self = Self::Contains(indices);
            }
            3 => {
                *self = Self::Unknown;
            }
            _ => {
                return Err(Error::new(
                    ErrorKind::Other,
                    format!("unknown comparison variant kind {}", kind),
                ));
            }
        }
        Ok(())
    }
}