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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use crate::jormungandr::{
    grpc::{
        node::{
            node_server::{Node, NodeServer},
            BlockEvent, ClientAuthRequest, ClientAuthResponse, Gossip, HandshakeRequest,
            HandshakeResponse, PeersRequest, PeersResponse, PullBlocksRequest,
            PullBlocksToTipRequest, PullHeadersRequest, PushHeadersResponse, TipRequest,
            TipResponse, UploadBlocksResponse,
        },
        types::{Block, BlockIds, Fragment, FragmentIds, Header},
    },
    Block0ConfigurationBuilder,
};
use chain_core::{
    packer::Codec,
    property::{DeserializeFromSlice, Header as BlockHeader, Serialize},
};
use chain_impl_mockchain::{block::BlockVersion, chaintypes::ConsensusVersion, key::Hash};
use std::{
    fmt,
    sync::{Arc, RwLock},
};
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;
use tonic::{Request, Response, Status};
use tracing::info;

mod builder;
mod controller;
mod data;
mod logger;
mod verifier;

pub use builder::{start_thread, MockBuilder};
pub use controller::MockController;
pub use data::MockServerData;
pub use logger::{MethodType, MockLogger};
pub use verifier::MockVerifier;

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MockExitCode {
    Timeout,
    Success,
}

#[derive(Clone, Debug)]
pub enum ProtocolVersion {
    Bft = 0,
    GenesisPraos = 1,
}

impl From<ConsensusVersion> for ProtocolVersion {
    fn from(from: ConsensusVersion) -> Self {
        match from {
            ConsensusVersion::Bft => Self::Bft,
            ConsensusVersion::GenesisPraos => Self::GenesisPraos,
        }
    }
}

impl fmt::Display for ProtocolVersion {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

pub struct JormungandrServerImpl {
    data: Arc<RwLock<MockServerData>>,
}

impl JormungandrServerImpl {
    pub fn new(data: Arc<RwLock<MockServerData>>) -> Self {
        info!(
            method = %MethodType::Init.to_string(),
            "mock node started on {}", data.read().unwrap().profile().address()
        );
        JormungandrServerImpl { data }
    }
}

#[tonic::async_trait]
impl Node for JormungandrServerImpl {
    type PullBlocksStream = ReceiverStream<Result<Block, Status>>;
    type PullBlocksToTipStream = ReceiverStream<Result<Block, Status>>;
    type GetBlocksStream = ReceiverStream<Result<Block, Status>>;
    type PullHeadersStream = ReceiverStream<Result<Header, Status>>;
    type GetHeadersStream = ReceiverStream<Result<Header, Status>>;
    type GetFragmentsStream = ReceiverStream<Result<Fragment, Status>>;
    type BlockSubscriptionStream = ReceiverStream<Result<BlockEvent, Status>>;
    type FragmentSubscriptionStream = ReceiverStream<Result<Fragment, Status>>;
    type GossipSubscriptionStream = ReceiverStream<Result<Gossip, Status>>;

    async fn handshake(
        &self,
        request: Request<HandshakeRequest>,
    ) -> Result<Response<HandshakeResponse>, Status> {
        info!(method = %MethodType::Handshake, "Handshake method received",);

        let request = request.into_inner();
        let client_nonce = &request.nonce;

        let mut data = self.data.write().unwrap();
        let signature = data.node_signature(client_nonce);
        let nonce = data.generate_auth_nonce().to_vec();

        let reply = HandshakeResponse {
            version: data.protocol().clone() as u32,
            block0: data.genesis_hash().as_ref().to_vec(),
            node_id: data.node_id().to_vec(),
            signature,
            nonce,
        };
        Ok(Response::new(reply))
    }

    async fn client_auth(
        &self,
        request: tonic::Request<ClientAuthRequest>,
    ) -> Result<tonic::Response<ClientAuthResponse>, tonic::Status> {
        let request = request.into_inner();
        info!(
            method = %MethodType::ClientAuth,
            "ClientAuth request received",
        );
        let data = self.data.read().unwrap();
        if !data.validate_peer_node_id(&request.node_id, &request.signature) {
            return Err(Status::invalid_argument("invalid node ID or signature"));
        }
        let response = ClientAuthResponse {};
        Ok(Response::new(response))
    }

    async fn tip(
        &self,
        _request: tonic::Request<TipRequest>,
    ) -> Result<tonic::Response<TipResponse>, tonic::Status> {
        info!(method = %MethodType::Tip, "Tip request received");
        let tip_response = TipResponse {
            block_header: self
                .data
                .read()
                .unwrap()
                .tip()
                .map_err(|e| tonic::Status::internal(format!("invalid tip {}", e)))?
                .serialize_as_vec()
                .map_err(|e| tonic::Status::internal(format!("cannot serialize header {}", e)))?,
        };
        Ok(Response::new(tip_response))
    }

    async fn peers(
        &self,
        _request: tonic::Request<PeersRequest>,
    ) -> Result<tonic::Response<PeersResponse>, tonic::Status> {
        info!(method = %MethodType::GetPeers, "Get peers request received");
        let data = self.data.read().unwrap();
        // Gossip struct serde, jormungandr/src/topology/gossip.rs
        let mut codec = chain_core::packer::Codec::new(Vec::new());
        let bytes = data.profile().gossip().as_ref();
        if bytes.len() > 512 {
            panic!("gossip size overflow");
        }
        codec.put_be_u16(bytes.len() as u16).unwrap();
        codec.put_bytes(bytes).unwrap();
        Ok(Response::new(PeersResponse {
            peers: vec![codec.into_inner()],
        }))
    }
    async fn get_blocks(
        &self,
        request: tonic::Request<BlockIds>,
    ) -> Result<tonic::Response<Self::GetBlocksStream>, tonic::Status> {
        info!(
            method = %MethodType::GetBlocks,
            "Get blocks request received"
        );

        let block_ids = request.into_inner();

        let mut blocks = vec![];

        for block_id in block_ids.ids.iter() {
            let block_hash =
                Hash::deserialize_from_slice(&mut Codec::new(block_id.as_slice())).unwrap();

            let mut block = self
                .data
                .read()
                .unwrap()
                .get_block(block_hash)
                .map_err(|_| tonic::Status::not_found(format!("{} not available", block_hash)));

            if self.data.read().unwrap().invalid_block0_hash()
                && block
                    .as_ref()
                    .map(|b| b.header().version() == BlockVersion::Genesis)
                    .unwrap_or(false)
            {
                block = Ok(Block0ConfigurationBuilder::default().build().to_block());
            }

            blocks.push(block);
        }

        let (tx, rx) = mpsc::channel(blocks.len());

        for block in blocks {
            tx.send(block.map(|b| {
                let mut codec = Codec::new(vec![]);
                b.serialize(&mut codec).unwrap();
                Block {
                    content: codec.into_inner(),
                }
            }))
            .await
            .unwrap();
        }

        Ok(Response::new(ReceiverStream::new(rx)))
    }
    async fn get_headers(
        &self,
        _request: tonic::Request<BlockIds>,
    ) -> Result<tonic::Response<Self::GetHeadersStream>, tonic::Status> {
        info!(
            method = %MethodType::GetHeaders,
            "Get headers request received",
        );
        let (_tx, rx) = mpsc::channel(1);
        Ok(Response::new(ReceiverStream::new(rx)))
    }
    async fn get_fragments(
        &self,
        _request: tonic::Request<FragmentIds>,
    ) -> Result<tonic::Response<Self::GetFragmentsStream>, tonic::Status> {
        info!(
            method = %MethodType::GetFragments,
            "Get fragments request received",
        );
        let (_tx, rx) = mpsc::channel(1);
        Ok(Response::new(ReceiverStream::new(rx)))
    }
    async fn pull_headers(
        &self,
        _request: tonic::Request<PullHeadersRequest>,
    ) -> Result<tonic::Response<Self::PullHeadersStream>, tonic::Status> {
        info!(
            method = %MethodType::PullHeaders,
            "Pull Headers request received",
        );
        let (_tx, rx) = mpsc::channel(1);
        Ok(Response::new(ReceiverStream::new(rx)))
    }
    async fn pull_blocks(
        &self,
        request: tonic::Request<PullBlocksRequest>,
    ) -> Result<tonic::Response<Self::PullBlocksStream>, tonic::Status> {
        info!(
            method = %MethodType::PullBlocks,
            "PullBlocks request received",
        );
        let request = request.into_inner();
        let (distance, block_iter) = {
            let data = self.data.read().unwrap();

            let (from, to) = (request.from[0].as_ref(), request.to.as_ref());

            let distance = data
                .storage()
                // This ignores all checkpoints except the first one, we don't need it for now
                .is_ancestor(from, to)
                .map_err(|e| tonic::Status::not_found(e.to_string()))?
                .ok_or_else(|| tonic::Status::invalid_argument("from is not an ancestor of to"))?;
            let iter = data.storage().iter(request.to.as_ref(), distance).unwrap();
            (distance, iter)
        };

        let (tx, rx) = mpsc::channel(distance as usize);
        for block in block_iter {
            tx.send(
                block
                    .map(|b| Block {
                        content: b.as_ref().into(),
                    })
                    .map_err(|e| tonic::Status::aborted(e.to_string())),
            )
            .await
            .unwrap();
        }
        Ok(Response::new(ReceiverStream::new(rx)))
    }
    async fn pull_blocks_to_tip(
        &self,
        _request: tonic::Request<PullBlocksToTipRequest>,
    ) -> Result<tonic::Response<Self::PullBlocksToTipStream>, tonic::Status> {
        info!(
            method = %MethodType::PullBlocksToTip,
            "PullBlocksToTip request received",
        );
        let (_tx, rx) = mpsc::channel(1);
        Ok(Response::new(ReceiverStream::new(rx)))
    }
    async fn push_headers(
        &self,
        _request: tonic::Request<tonic::Streaming<Header>>,
    ) -> Result<tonic::Response<PushHeadersResponse>, tonic::Status> {
        info!(
            method = %MethodType::PushHeaders,
            "Push headers method received",
        );
        Ok(Response::new(PushHeadersResponse::default()))
    }
    async fn upload_blocks(
        &self,
        _request: tonic::Request<tonic::Streaming<Block>>,
    ) -> Result<tonic::Response<UploadBlocksResponse>, tonic::Status> {
        info!(
            method = %MethodType::UploadBlocks,
            "Upload blocks method received",
        );
        Ok(Response::new(UploadBlocksResponse::default()))
    }

    async fn block_subscription(
        &self,
        _request: tonic::Request<tonic::Streaming<Header>>,
    ) -> Result<tonic::Response<Self::BlockSubscriptionStream>, tonic::Status> {
        info!(
            method = %MethodType::BlockSubscription,
            "Block subscription event received",
        );
        let (_tx, rx) = mpsc::channel(1);
        Ok(Response::new(ReceiverStream::new(rx)))
    }

    async fn fragment_subscription(
        &self,
        _request: tonic::Request<tonic::Streaming<Fragment>>,
    ) -> Result<tonic::Response<Self::FragmentSubscriptionStream>, tonic::Status> {
        info!(
            method = %MethodType::FragmentSubscription,
            "Fragment subscription event received",
        );
        let (_tx, rx) = mpsc::channel(1);
        Ok(Response::new(ReceiverStream::new(rx)))
    }
    async fn gossip_subscription(
        &self,
        _request: tonic::Request<tonic::Streaming<Gossip>>,
    ) -> Result<tonic::Response<Self::GossipSubscriptionStream>, tonic::Status> {
        info!(
            method = %MethodType::GossipSubscription,
            "Gossip subscription event received",
        );
        let (_tx, rx) = mpsc::channel(1);
        Ok(Response::new(ReceiverStream::new(rx)))
    }
}