massa_grpc/stream/
send_endorsements.rs

1// Copyright (c) 2023 MASSA LABS <info@massa.net>
2
3use crate::error::{match_for_io_error, GrpcError};
4use crate::server::MassaPublicGrpc;
5use futures_util::StreamExt;
6use massa_models::endorsement::{EndorsementDeserializer, SecureShareEndorsement};
7use massa_models::secure_share::SecureShareDeserializer;
8use massa_pos_exports::SelectorController;
9use massa_proto_rs::massa::api::v1 as grpc_api;
10use massa_proto_rs::massa::model::v1 as grpc_model;
11use massa_serialization::{DeserializeError, Deserializer};
12use std::collections::HashMap;
13use std::io::ErrorKind;
14use std::pin::Pin;
15use tracing::{error, warn};
16
17/// Type declaration for SendEndorsements
18pub type SendEndorsementsStreamType = Pin<
19    Box<
20        dyn futures_util::Stream<Item = Result<grpc_api::SendEndorsementsResponse, tonic::Status>>
21            + Send
22            + 'static,
23    >,
24>;
25
26/// This function takes a streaming request of endorsements messages,
27/// verifies, saves and propagates the endorsements received in each message, and sends back a stream of
28/// endorsements ids messages
29pub(crate) async fn send_endorsements(
30    grpc: &MassaPublicGrpc,
31    request: tonic::Request<tonic::Streaming<grpc_api::SendEndorsementsRequest>>,
32) -> Result<SendEndorsementsStreamType, GrpcError> {
33    let mut pool_command_sender = grpc.pool_controller.clone();
34    let protocol_command_sender = grpc.protocol_controller.clone();
35    let selector_controller = grpc.selector_controller.clone();
36    let config = grpc.grpc_config.clone();
37    let storage = grpc.storage.clone_without_refs();
38
39    // Create a channel to handle communication with the client
40    let (tx, rx) = tokio::sync::mpsc::channel(config.max_channel_size);
41    // Extract the incoming stream of endorsements messages
42    let mut in_stream = request.into_inner();
43
44    // Spawn a task that reads incoming messages and processes the endorsements in each message
45    tokio::spawn(async move {
46        while let Some(result) = in_stream.next().await {
47            match result {
48                Ok(req_content) => {
49                    // If the incoming message has no endorsements, send an error message back to the client
50                    if req_content.endorsements.is_empty() {
51                        report_error(
52                            tx.clone(),
53                            tonic::Code::InvalidArgument,
54                            "the request payload is empty".to_owned(),
55                        )
56                        .await;
57                    } else {
58                        // If there are too many endorsements in the incoming message, send an error message back to the client
59                        let proto_endorsement = req_content.endorsements;
60                        if proto_endorsement.len() as u32 > config.max_endorsements_per_message {
61                            report_error(
62                                tx.clone(),
63                                tonic::Code::InvalidArgument,
64                                "too many endorsements per message".to_owned(),
65                            )
66                            .await;
67                        } else {
68                            // Deserialize and verify each endorsement in the incoming message
69                            let endorsement_deserializer = SecureShareDeserializer::new(
70                                EndorsementDeserializer::new(
71                                    config.thread_count,
72                                    config.endorsement_count,
73                                ),
74                                config.chain_id,
75                            );
76                            let verified_eds_res: Result<HashMap<String, SecureShareEndorsement>, GrpcError> = proto_endorsement
77                                .into_iter()
78                                .map(|proto_endorsement| {
79                                    let verified_op = match endorsement_deserializer.deserialize::<DeserializeError>(&proto_endorsement) {
80                                        Ok(tuple) => {
81                                            // Deserialize the endorsement and verify its signature
82                                            let (rest, res_endorsement): (&[u8], SecureShareEndorsement) = tuple;
83                                            if rest.is_empty() {
84                                                res_endorsement.verify_signature(config.chain_id)
85                                                    .map(|_| (res_endorsement.id.to_string(), res_endorsement))
86                                                    .map_err(|e| e.into())
87                                            } else {
88                                                Err(GrpcError::InternalServerError(
89                                                    "there is data left after endorsement deserialization".to_owned()
90                                                ))
91                                            }
92                                        }
93                                        Err(e) => {
94                                            Err(GrpcError::InternalServerError(format!("failed to deserialize endorsement: {}", e)
95                                            ))
96                                        }
97                                    };
98                                    verified_op
99                                })
100                                .collect();
101
102                            match verified_eds_res {
103                                // If all endorsements in the incoming message are valid, store and propagate them
104                                Ok(verified_eds) => {
105                                    // Check the PoS draws before letting those endorsements in: the
106                                    // propagation flow trusts its caller and does not check them again.
107                                    if let Err(error) = check_endorsement_draws(
108                                        &verified_eds,
109                                        selector_controller.as_ref(),
110                                    ) {
111                                        report_error(
112                                            tx.clone(),
113                                            tonic::Code::InvalidArgument,
114                                            error,
115                                        )
116                                        .await;
117                                        continue;
118                                    }
119
120                                    let mut endorsement_storage = storage.clone_without_refs();
121                                    endorsement_storage.store_endorsements(
122                                        verified_eds.values().cloned().collect(),
123                                    );
124                                    // Add the received endorsements to the endorsements pool
125                                    pool_command_sender
126                                        .add_endorsements(endorsement_storage.clone());
127
128                                    // Propagate the endorsements to the network
129                                    if let Err(e) = protocol_command_sender
130                                        .propagate_endorsements(endorsement_storage)
131                                    {
132                                        // If propagation failed, send an error message back to the client
133                                        let error =
134                                            format!("failed to propagate endorsement: {}", e);
135                                        report_error(
136                                            tx.clone(),
137                                            tonic::Code::Internal,
138                                            error.to_owned(),
139                                        )
140                                        .await;
141                                    };
142
143                                    // Build the response message
144                                    let result = grpc_model::EndorsementIds {
145                                        endorsement_ids: verified_eds.keys().cloned().collect(),
146                                    };
147                                    // Send the response message back to the client
148                                    if let Err(e) = tx
149                                        .send(Ok(grpc_api::SendEndorsementsResponse {
150                                            result: Some(
151                                                grpc_api::send_endorsements_response::Result::EndorsementIds(
152                                                    result,
153                                                ),
154                                            ),
155                                        }))
156                                        .await
157                                    {
158                                        error!("failed to send back endorsement response: {}", e)
159                                    };
160                                }
161                                // If the verification failed, send an error message back to the client
162                                Err(e) => {
163                                    let error = format!("invalid endorsement(s): {}", e);
164                                    report_error(
165                                        tx.clone(),
166                                        tonic::Code::InvalidArgument,
167                                        error.to_owned(),
168                                    )
169                                    .await;
170                                }
171                            }
172                        }
173                    }
174                }
175                // Handles errors that occur while sending a response back to a client
176                Err(err) => {
177                    // Check if the error matches any IO errors
178                    if let Some(io_err) = match_for_io_error(&err) {
179                        if io_err.kind() == ErrorKind::BrokenPipe {
180                            warn!("client disconnected, broken pipe: {}", io_err);
181                            break;
182                        }
183                    }
184                    error!("{}", err);
185                    // Send the error response back to the client
186                    if let Err(e) = tx.send(Err(err)).await {
187                        error!(
188                            "failed to send back send_endorsements error response: {}",
189                            e
190                        );
191                        break;
192                    }
193                }
194            }
195        }
196    });
197
198    let out_stream = tokio_stream::wrappers::ReceiverStream::new(rx);
199    Ok(Box::pin(out_stream) as SendEndorsementsStreamType)
200}
201
202/// Checks that each endorsement was created by the endorser drawn for its `(slot, index)` pair.
203///
204/// This mirrors the check done on endorsements received from peers (`note_endorsements_from_peer`)
205/// and in the endorsement pool: endorsements accepted here are handed over to the protocol
206/// propagation flow, which trusts its caller and rebroadcasts them without further validation.
207fn check_endorsement_draws(
208    endorsements: &HashMap<String, SecureShareEndorsement>,
209    selector_controller: &dyn SelectorController,
210) -> Result<(), String> {
211    for endorsement in endorsements.values() {
212        let selection = selector_controller
213            .get_selection(endorsement.content.slot)
214            .map_err(|e| {
215                format!(
216                    "failed to get the PoS draw at slot {}: {}",
217                    endorsement.content.slot, e
218                )
219            })?
220            .endorsements;
221        let Some(address) = selection.get(endorsement.content.index as usize) else {
222            return Err(format!(
223                "no selection at slot {} for index {}",
224                endorsement.content.slot, endorsement.content.index
225            ));
226        };
227        if address != &endorsement.content_creator_address {
228            return Err(format!(
229                "invalid endorsement producer selection at slot {} index {}: expected address {}, got {}",
230                endorsement.content.slot,
231                endorsement.content.index,
232                address,
233                endorsement.content_creator_address
234            ));
235        }
236    }
237    Ok(())
238}
239
240// This function reports an error to the sender by sending a gRPC response message to the client
241async fn report_error(
242    sender: tokio::sync::mpsc::Sender<Result<grpc_api::SendEndorsementsResponse, tonic::Status>>,
243    code: tonic::Code,
244    error: String,
245) {
246    error!("{}", error);
247    // Attempt to send the error response message to the sender
248    if let Err(e) = sender
249        .send(Ok(grpc_api::SendEndorsementsResponse {
250            result: Some(grpc_api::send_endorsements_response::Result::Error(
251                massa_proto_rs::massa::model::v1::Error {
252                    code: code.into(),
253                    message: error,
254                },
255            )),
256        }))
257        .await
258    {
259        // If sending the message fails, log the error message
260        error!(
261            "failed to send back send_endorsements error response: {}",
262            e
263        );
264    }
265}