Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create message_validator crate #152

Open
wants to merge 11 commits into
base: unstable
Choose a base branch
from

Conversation

diegomrsantos
Copy link

@diegomrsantos diegomrsantos commented Feb 20, 2025

Issue Addressed

Extracts validation from #147.
Related to #161.

Proposed Changes

Create a crate for validation that doesn't depend on other crates.

Additional Info

I added in common for now, but not sure it's necessary.

@diegomrsantos diegomrsantos changed the title creaate message_validator crate Create message_validator crate Feb 20, 2025
@@ -0,0 +1 @@
mod validation;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why a submodule?

pub(crate) fn validate(
&mut self,
_message: SignedSSVMessage,
) -> std::result::Result<Result, Error> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no need to specify the full type path


pub struct Validator;

pub enum Error {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a separate error instead of just returning the ValidationFailure?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to return ValidationFailure? We return the local Result and Error is an error in the validation process. Maybe we don't need the Error, I don't know if it can fail yet.

Copy link
Author

@diegomrsantos diegomrsantos Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it should be like this?

pub enum Result {
    Accept,
    Reject(ValidationFailure),
    Ignore,
}

impl Validator {
    pub(crate) fn validate(
        &mut self,
        _message: SignedSSVMessage,
    ) -> Result {

        Accept
    }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaah, now I also understand why you specified the path for the std Result type.

Well, we will need to report the validation result to the network crate via a queue. So validate should not return anything, right?

Nesting a validation failure into the Result also feels incorrect: we have a specific validation failure, and this maps to a way we handle the message. And this will be what we report to network, right?

Copy link
Author

@diegomrsantos diegomrsantos Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like this?

pub fn start_validator_service(validator: Validator) -> (Sender<SignedSSVMessage>, Receiver<ValidationResult>) {
    // Channel for receiving messages to validate.
    let (msg_tx, msg_rx) = mpsc::channel::<SignedSSVMessage>();
    // Channel for sending back validation results.
    let (result_tx, result_rx) = mpsc::channel::<ValidationResult>();

    spawn(move || {
       
        for signed_msg in msg_rx {
           
            let validation_result = validator.validate(signed_msg);
            
            if result_tx.send(validation_result).is_err() {
                break;
            }
        }
    });

    (msg_tx, result_rx)
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I believe we'd need to adapt it to go through the processor as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need a centralized validator task, as there are no central resources to manage. Just create a closure, and send it to a processor queue via send_blocking.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nesting a validation failure into the Result also feels incorrect

We might need it, cause the result can also be Accept. There's also a difference between Reject and Ignore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we need it, but we do not need to embed the validation failure into it, because we create the validation result (=the way we treat the message) from the validation failure, right? In other words, the failure variant determines whether to Ignore or Reject.

Copy link
Author

@diegomrsantos diegomrsantos Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send_blocking doesn't work:

61  |     pub fn send_blocking<F: FnOnce() + Send + 'static>(
    |                             ^^^^^^^^ required by this bound in `Sender::send_blocking`

How about (ignore the Result for now)?

pub fn start_validator_service(
    validator: Validator,
) -> (Sender<SignedSSVMessage>, Receiver<Result>) {
    let (msg_tx, mut msg_rx) = mpsc::channel::<SignedSSVMessage>(100);
    let (result_tx, result_rx) = mpsc::channel::<Result>(100);

    validator
        .processor
        .urgent_consensus
        .send_async(
            async move {
                while let Some(signed_msg) = msg_rx.recv().await {
                    let result = validate(signed_msg);
                    // If the send fails, we can assume the receiver is dropped.
                    if result_tx.send(result).await.is_err() {
                        break;
                    }
                }
            },
            "validator_service",
        )
        .unwrap();

    (msg_tx, result_rx)
}

@diegomrsantos diegomrsantos self-assigned this Feb 20, 2025
@diegomrsantos diegomrsantos force-pushed the msg-validator-crate branch 2 times, most recently from 5c476e3 to 84b2aa0 Compare February 20, 2025 19:14
@diegomrsantos diegomrsantos marked this pull request as ready for review February 27, 2025 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants