-
Notifications
You must be signed in to change notification settings - Fork 39
feat: Validator trait + implementation for USV2 #430
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
Open
tamaralipows
wants to merge
12
commits into
main
Choose a base branch
from
testing-sdk/tnl/ENG-5267-add-validators
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e8614ac
feat: Validator trait + implementation for USV2
8978571
fix: gate the validation module behind the evm feature
39a0e78
feat: Split validation in two steps for batching
faf81b3
chore: Move validation into tycho-test
6ab9e6e
feat: Metric for failed validations
a3dbe15
refactor(validation): centralize protocol downcasting with get_validator
1f26215
refactor(validation): don't pass to process_state
dc6a784
refactor(validation): call getReserves instead of balanceOf
d95fd37
chore: validator type aliases
2d60478
Merge branch 'main' into testing-sdk/tnl/ENG-5267-add-validators
tamaralipows 0e2196c
chore: put back accidentally deleted logs
fd03439
chore: put back test ignore
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ mod metrics; | |
| use std::{ | ||
| collections::{HashMap, HashSet}, | ||
| fmt::Debug, | ||
| str::FromStr, | ||
| sync::{Arc, RwLock}, | ||
| time::Duration, | ||
| }; | ||
|
|
@@ -35,6 +36,7 @@ use tycho_test::{ | |
| protocol_stream_processor::ProtocolStreamProcessor, | ||
| rfq_stream_processor::RFQStreamProcessor, StreamUpdate, UpdateType, | ||
| }, | ||
| validation::{batch_validate_components, get_validator, Validator}, | ||
| }; | ||
|
|
||
| #[derive(Parser, Clone)] | ||
|
|
@@ -313,10 +315,11 @@ async fn process_update( | |
| metrics::record_protocol_sync_state(protocol, sync_state); | ||
| } | ||
|
|
||
| // Process updated states in parallel | ||
| let semaphore = Arc::new(Semaphore::new(cli.parallel_simulations as usize)); | ||
| let mut tasks = Vec::new(); | ||
| // Collect all components to process (both updated and stale) for batch validation | ||
| let mut components_to_process: Vec<(String, ProtocolComponent, Box<dyn ProtocolSim>)> = | ||
| Vec::new(); | ||
|
|
||
| // Collect updated components | ||
| for (id, state) in update | ||
| .update | ||
| .states | ||
|
|
@@ -332,55 +335,39 @@ async fn process_update( | |
| match states.get(id) { | ||
| Some(comp) => comp.clone(), | ||
| None => { | ||
| warn!(id=%id, "Component not found in cached protocol pairs. Potential causes: \ | ||
| there was an error decoding the component, the component was evicted from the cache, \ | ||
| or the component was never added to the cache. Skipping..."); | ||
| warn!(id=%id, "Component not found in cached protocol pairs. Skipping..."); | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
| UpdateType::Rfq => match update.update.new_pairs.get(id) { | ||
| Some(comp) => comp.clone(), | ||
| None => { | ||
| warn!(id=%id, "Component not found in update's new pairs. Potential cause: \ | ||
| the `states` and `new_pairs` lists don't contain the same items. Skipping..."); | ||
| warn!(id=%id, "Component not found in update's new pairs. Skipping..."); | ||
| continue; | ||
| } | ||
| }, | ||
| }; | ||
| let block = block.clone(); | ||
| let state_id = id.clone(); | ||
| let state = state.clone_box(); | ||
| let permit = semaphore | ||
| .clone() | ||
| .acquire_owned() | ||
| .await | ||
| .into_diagnostic() | ||
| .wrap_err("Failed to acquire permit")?; | ||
|
|
||
| let task = tokio::spawn(async move { | ||
| let simulation_id = generate_simulation_id(&component.protocol_system, &state_id); | ||
| let result = | ||
| process_state(&simulation_id, chain, component, &block, state_id, state).await; | ||
| drop(permit); | ||
| result | ||
| }); | ||
| tasks.push(task); | ||
| components_to_process.push((id.clone(), component, state.clone_box())); | ||
| } | ||
|
|
||
| // Select states that were not updated in this block to test simulation and execution | ||
| // Collect stale components (not updated in this block) | ||
| let selected_ids = { | ||
| let current_state = tycho_state | ||
| .read() | ||
| .map_err(|e| miette!("Failed to acquire write lock on Tycho state: {e}"))?; | ||
|
|
||
| let mut all_selected_ids = Vec::new(); | ||
|
|
||
| // Add component IDs from always_test_components that are not in the current update | ||
| for component_id in &cli.always_test_components { | ||
| if !update.update.states.keys().contains(component_id) | ||
| // Ensure that the component exists in the Tycho DB | ||
| && current_state.components.contains_key(component_id) | ||
| if !update | ||
| .update | ||
| .states | ||
| .keys() | ||
| .contains(component_id) && | ||
| current_state | ||
| .components | ||
| .contains_key(component_id) | ||
| { | ||
| all_selected_ids.push(component_id.clone()); | ||
| } | ||
|
|
@@ -390,7 +377,6 @@ async fn process_update( | |
| .component_ids_by_protocol | ||
| .values() | ||
| { | ||
| // Filter out IDs that are in the current update or already in all_selected_ids | ||
| let available_ids: Vec<_> = component_ids | ||
| .iter() | ||
| .filter(|id| { | ||
|
|
@@ -412,13 +398,13 @@ async fn process_update( | |
| all_selected_ids | ||
| }; | ||
|
|
||
| for id in selected_ids { | ||
| for id in &selected_ids { | ||
| let (component, state) = { | ||
| let current_state = tycho_state | ||
| .read() | ||
| .map_err(|e| miette!("Failed to acquire read lock on Tycho state: {e}"))?; | ||
|
|
||
| match (current_state.components.get(&id), current_state.states.get(&id)) { | ||
| match (current_state.components.get(id), current_state.states.get(id)) { | ||
| (Some(comp), Some(state)) => (comp.clone(), state.clone()), | ||
| (None, _) => { | ||
| error!(id=%id, "Component not found in saved protocol components."); | ||
|
|
@@ -430,10 +416,79 @@ async fn process_update( | |
| } | ||
| } | ||
| }; | ||
| components_to_process.push((id.clone(), component, state.clone_box())); | ||
| } | ||
|
|
||
| // Collect components that implement Validator for batch validation | ||
| let mut validator_components: Vec<( | ||
| &dyn Validator, | ||
| tycho_common::Bytes, | ||
| Vec<tycho_common::models::token::Token>, | ||
| String, // protocol_system | ||
| )> = Vec::new(); | ||
|
|
||
| for (id, component, state) in &components_to_process { | ||
| let component_id = tycho_common::Bytes::from_str(id) | ||
| .unwrap_or_else(|_| tycho_common::Bytes::from(id.as_bytes())); | ||
|
|
||
| if let Some(validator) = get_validator(&component.protocol_system, state.as_ref()) { | ||
| validator_components.push(( | ||
| validator, | ||
| component_id, | ||
| component.tokens.clone(), | ||
| component.protocol_system.clone(), | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| // Batch validate all components of this block in a single call | ||
| if !validator_components.is_empty() { | ||
| // Extract just the validator data (without protocol_system) for batch_validate_components | ||
| // TODO do this neater | ||
| let validator_data: Vec<_> = validator_components | ||
| .iter() | ||
| .map(|(validator, id, tokens, _protocol)| (*validator, id.clone(), tokens.clone())) | ||
| .collect(); | ||
|
|
||
| let results = | ||
| batch_validate_components(&cli.rpc_url, &validator_data, block.header.number).await; | ||
|
|
||
| for (i, result) in results.iter().enumerate() { | ||
| let component_id = &validator_components[i].1; | ||
| let protocol = &validator_components[i].3; | ||
| match result { | ||
| Ok(passed) => { | ||
| if *passed { | ||
| info!( | ||
| component_id = %component_id, | ||
| "State validation passed" | ||
| ); | ||
| } else { | ||
| error!( | ||
| component_id = %component_id, | ||
| "State validation failed" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we log here the delta message? 🤔 |
||
| ); | ||
| metrics::record_validation_failure(protocol); | ||
| } | ||
| } | ||
| Err(e) => { | ||
| error!( | ||
| component_id = %component_id, | ||
| error = %e, | ||
| "Error validating component" | ||
| ); | ||
| metrics::record_validation_failure(protocol); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Process all components (updated and stale) in parallel | ||
| let semaphore = Arc::new(Semaphore::new(cli.parallel_simulations as usize)); | ||
| let mut tasks = Vec::new(); | ||
|
|
||
| for (id, component, state) in components_to_process { | ||
| let block = block.clone(); | ||
| let state_id = id.clone(); | ||
| let state = state.clone_box(); | ||
| let permit = semaphore | ||
| .clone() | ||
| .acquire_owned() | ||
|
|
@@ -442,9 +497,8 @@ async fn process_update( | |
| .wrap_err("Failed to acquire permit")?; | ||
|
|
||
| let task = tokio::spawn(async move { | ||
| let simulation_id = generate_simulation_id(&component.protocol_system, &state_id); | ||
| let result = | ||
| process_state(&simulation_id, chain, component, &block, state_id, state).await; | ||
| let simulation_id = generate_simulation_id(&component.protocol_system, &id); | ||
| let result = process_state(&simulation_id, chain, component, &block, id, state).await; | ||
| drop(permit); | ||
| result | ||
| }); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| pub mod execution; | ||
| pub mod rpc_tools; | ||
| pub mod stream_processor; | ||
| pub mod validation; | ||
| pub use rpc_tools::RPCTools; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we put all of this component selecting in one big method? I think it fits nicely together now!