1use std::{
2 any::Any,
3 collections::{hash_map::Entry, HashMap, HashSet},
4 future::Future,
5 pin::Pin,
6 sync::Arc,
7};
8
9use alloy::primitives::{Address, U256};
10use thiserror::Error;
11use tokio::sync::{watch, RwLock, RwLockReadGuard};
12use tracing::{debug, error, info, warn};
13use tycho_client::feed::{synchronizer::ComponentWithState, BlockHeader, FeedMessage, HeaderLike};
14use tycho_common::{
15 dto::{ChangeType, ProtocolStateDelta},
16 models::{blockchain::BlockAggregatedChanges, token::Token, Chain},
17 simulation::protocol_sim::{Balances, BlockContext, ProtocolSim},
18 Bytes,
19};
20#[cfg(test)]
21use {
22 mockall::mock,
23 num_bigint::BigUint,
24 tycho_common::simulation::{
25 errors::{SimulationError, TransitionError},
26 protocol_sim::GetAmountOutResult,
27 },
28};
29
30use crate::{
31 evm::{
32 engine_db::{update_engine, SHARED_TYCHO_DB},
33 override_stream::{OverrideSnapshot, StateOverrideProvider},
34 protocol::{
35 utils::bytes_to_address,
36 vm::{constants::ERC20_PROXY_BYTECODE, erc20_token::IMPLEMENTATION_SLOT},
37 },
38 simulation::{BlockEnvOverrides, PendingOverrides},
39 tycho_models::{AccountUpdate, ResponseAccount},
40 },
41 protocol::{
42 errors::InvalidSnapshotError,
43 models::{DecoderContext, ProtocolComponent, TryFromWithBlock, Update},
44 },
45};
46
47#[derive(Error, Debug)]
48pub enum StreamDecodeError {
49 #[error("{0}")]
50 Fatal(String),
51}
52
53#[derive(Default)]
54struct DecoderState {
55 tokens: HashMap<Bytes, Token>,
56 states: HashMap<String, Box<dyn ProtocolSim>>,
57 components: HashMap<String, ProtocolComponent>,
58 contracts_map: HashMap<Bytes, HashSet<String>>,
60 proxy_token_addresses: HashMap<Address, Address>,
62 failed_components: HashSet<String>,
66 current_block_number: u64,
68}
69
70type DecodeFut =
71 Pin<Box<dyn Future<Output = Result<Box<dyn ProtocolSim>, InvalidSnapshotError>> + Send + Sync>>;
72type AccountBalances = HashMap<Bytes, HashMap<Bytes, Bytes>>;
73type RegistryFn<H> = dyn Fn(
74 ComponentWithState,
75 H,
76 AccountBalances,
77 Arc<RwLock<DecoderState>>,
78 Option<watch::Receiver<OverrideSnapshot>>,
79 ) -> DecodeFut
80 + Send
81 + Sync;
82type FilterFn = fn(&ComponentWithState) -> bool;
83
84pub struct TychoStreamDecoder<H>
97where
98 H: HeaderLike,
99{
100 state: Arc<RwLock<DecoderState>>,
101 skip_state_decode_failures: bool,
102 min_token_quality: u32,
103 registry: HashMap<String, Box<RegistryFn<H>>>,
104 inclusion_filters: HashMap<String, Vec<FilterFn>>,
105 protocols_skip_unknown_tokens: HashSet<String>,
107 override_providers: HashMap<String, Arc<dyn StateOverrideProvider>>,
110 block_time_secs: u64,
113 chain: Chain,
116}
117
118fn is_deprecated_curve_registration<T: 'static>(exchange: &str) -> bool {
122 exchange == "vm:curve" &&
123 std::any::type_name::<T>() !=
124 std::any::type_name::<crate::evm::protocol::curve::CurveState>()
125}
126
127impl<H> TychoStreamDecoder<H>
128where
129 H: HeaderLike + Clone + Sync + Send + 'static + std::fmt::Debug,
130{
131 pub fn new(chain: Chain) -> Self {
137 Self {
138 state: Arc::new(RwLock::new(DecoderState::default())),
139 skip_state_decode_failures: false,
140 min_token_quality: 100,
141 registry: HashMap::new(),
142 inclusion_filters: HashMap::new(),
143 protocols_skip_unknown_tokens: HashSet::new(),
144 override_providers: HashMap::new(),
145 block_time_secs: chain.block_time_secs(),
146 chain,
147 }
148 }
149
150 fn execution_block(&self, header: &BlockHeader) -> BlockContext {
155 if header.partial_block_index.is_some() {
156 BlockContext::new(header.number, header.timestamp)
157 } else {
158 BlockContext::new(header.number + 1, header.timestamp + self.block_time_secs)
159 }
160 }
161
162 fn refresh_execution_block<C>(
171 updated_states: &mut HashMap<String, Box<dyn ProtocolSim>>,
172 stored_states: &mut HashMap<String, Box<dyn ProtocolSim>>,
173 failed_components: &HashSet<String>,
174 removed_components: &HashMap<String, C>,
175 execution_block: &BlockContext,
176 ) {
177 for state in updated_states.values_mut() {
178 state.apply_block(execution_block);
179 }
180 for (id, state) in stored_states.iter_mut() {
181 if failed_components.contains(id) ||
182 removed_components.contains_key(id) ||
183 updated_states.contains_key(id)
184 {
185 continue;
186 }
187 if state.apply_block(execution_block) {
188 updated_states.insert(id.clone(), state.clone_box());
189 }
190 }
191 }
192
193 pub fn set_override_provider(
199 &mut self,
200 protocol_system: String,
201 provider: Arc<dyn StateOverrideProvider>,
202 ) {
203 self.override_providers
204 .insert(protocol_system, provider);
205 }
206
207 pub async fn set_tokens(&self, tokens: HashMap<Bytes, Token>) {
212 let mut guard = self.state.write().await;
213 guard.tokens = tokens;
214 }
215
216 pub fn skip_state_decode_failures(&mut self, skip: bool) {
217 self.skip_state_decode_failures = skip;
218 }
219
220 pub fn min_token_quality(&mut self, quality: u32) {
226 self.min_token_quality = quality;
227 }
228
229 pub fn register_decoder_with_context<T>(&mut self, exchange: &str, mut context: DecoderContext)
244 where
245 T: ProtocolSim
246 + TryFromWithBlock<ComponentWithState, H, Error = InvalidSnapshotError>
247 + Send
248 + 'static,
249 {
250 if let Some(requested) = context
251 .chain
252 .filter(|requested| *requested != self.chain)
253 {
254 warn!(
255 exchange,
256 requested_chain = %requested,
257 decoder_chain = %self.chain,
258 "DecoderContext declares a different chain than the decoder; using the decoder's \
259 chain"
260 );
261 }
262 context.chain = Some(self.chain);
263
264 if is_deprecated_curve_registration::<T>(exchange) {
265 warn!(
266 registered_type = std::any::type_name::<T>(),
267 "Registering \"vm:curve\" with the generic VM adapter is deprecated; register the \
268 native `CurveState` decoder instead (`exchange::<CurveState>(\"vm:curve\", ...)`). \
269 The VM-adapter path still works but will be removed in a future release."
270 );
271 }
272 let decoder = Box::new(
273 move |component: ComponentWithState,
274 header: H,
275 account_balances: AccountBalances,
276 state: Arc<RwLock<DecoderState>>,
277 live_override: Option<watch::Receiver<OverrideSnapshot>>| {
278 let mut context = context.clone();
279 context.live_override = live_override;
280 Box::pin(async move {
281 let guard = state.read().await;
282 T::try_from_with_header(
283 component,
284 header,
285 &account_balances,
286 &guard.tokens,
287 &context,
288 )
289 .await
290 .map(|c| Box::new(c) as Box<dyn ProtocolSim>)
291 }) as DecodeFut
292 },
293 );
294 self.registry
295 .insert(exchange.to_string(), decoder);
296 }
297
298 pub fn register_decoder<T>(&mut self, exchange: &str)
310 where
311 T: ProtocolSim
312 + TryFromWithBlock<ComponentWithState, H, Error = InvalidSnapshotError>
313 + Send
314 + 'static,
315 {
316 let context = DecoderContext::new();
317 self.register_decoder_with_context::<T>(exchange, context);
318 }
319
320 pub fn register_decoder_dropping_unknown_tokens<T>(&mut self, exchange: &str)
324 where
325 T: ProtocolSim
326 + TryFromWithBlock<ComponentWithState, H, Error = InvalidSnapshotError>
327 + Send
328 + 'static,
329 {
330 self.register_decoder::<T>(exchange);
331 self.protocols_skip_unknown_tokens
332 .insert(exchange.to_string());
333 }
334
335 pub fn register_filter(&mut self, exchange: &str, predicate: FilterFn) {
354 self.inclusion_filters
355 .entry(exchange.to_string())
356 .or_default()
357 .push(predicate);
358 }
359
360 fn admits(&self, exchange: &str, snapshot: &ComponentWithState) -> bool {
363 let Some(predicates) = self.inclusion_filters.get(exchange) else { return true };
364 predicates
365 .iter()
366 .all(|predicate| predicate(snapshot))
367 }
368
369 pub async fn decode(&self, msg: &FeedMessage<H>) -> Result<Update, StreamDecodeError> {
372 let mut updated_states = HashMap::new();
374 let mut new_pairs = HashMap::new();
375 let mut removed_pairs = HashMap::new();
376 let mut contracts_map = HashMap::new();
377 let mut msg_failed_components = HashSet::new();
378
379 let header = msg
380 .state_msgs
381 .values()
382 .next()
383 .ok_or_else(|| StreamDecodeError::Fatal("Missing block!".into()))?
384 .header
385 .clone();
386
387 let block_number = header
388 .clone()
389 .block_number_or_timestamp();
390 let current_block = header.clone().block();
391 let is_partial = current_block
392 .as_ref()
393 .map(|h| h.partial_block_index.is_some())
394 .unwrap_or(false);
395
396 for (protocol, protocol_msg) in msg.state_msgs.iter() {
397 let drops_unknown_tokens = self
398 .protocols_skip_unknown_tokens
399 .contains(protocol.as_str());
400 if let Some(deltas) = protocol_msg.deltas.as_ref() {
402 let mut state_guard = self.state.write().await;
403
404 let new_tokens = deltas
405 .new_tokens
406 .iter()
407 .filter(|(addr, t)| {
408 t.quality >= self.min_token_quality &&
409 !state_guard.tokens.contains_key(*addr)
410 })
411 .map(|(addr, t)| (addr.clone(), t.clone()))
412 .collect::<HashMap<Bytes, Token>>();
413
414 if !new_tokens.is_empty() {
415 debug!(n = new_tokens.len(), "NewTokens");
416 state_guard.tokens.extend(new_tokens);
417 }
418 }
419
420 {
422 let mut state_guard = self.state.write().await;
423 let removed_components: Vec<(String, ProtocolComponent)> = protocol_msg
424 .removed_components
425 .iter()
426 .map(|(id, comp)| {
427 if *id != comp.id {
428 error!(
429 "Component id mismatch in removed components {id} != {}",
430 comp.id
431 );
432 return Err(StreamDecodeError::Fatal("Component id mismatch".into()));
433 }
434
435 let tokens = comp
436 .tokens
437 .iter()
438 .flat_map(|addr| state_guard.tokens.get(addr).cloned())
439 .collect::<Vec<_>>();
440
441 if tokens.len() == comp.tokens.len() {
442 Ok(Some((
443 id.clone(),
444 ProtocolComponent::from_with_tokens(comp.clone(), tokens),
445 )))
446 } else {
447 Ok(None)
448 }
449 })
450 .collect::<Result<Vec<Option<(String, ProtocolComponent)>>, StreamDecodeError>>(
451 )?
452 .into_iter()
453 .flatten()
454 .collect();
455
456 for (id, component) in removed_components {
458 state_guard.components.remove(&id);
459 state_guard.states.remove(&id);
460 removed_pairs.insert(id, component);
461 }
462
463 info!(
465 "Processing {} contracts from snapshots",
466 protocol_msg
467 .snapshots
468 .get_vm_storage()
469 .len()
470 );
471
472 let mut proxy_token_accounts: HashMap<Address, AccountUpdate> = HashMap::new();
473 let mut storage_by_address: HashMap<Address, ResponseAccount> = HashMap::new();
474 for (key, value) in protocol_msg
475 .snapshots
476 .get_vm_storage()
477 .iter()
478 {
479 let account: ResponseAccount = value.clone().into();
480
481 if state_guard.tokens.contains_key(key) {
482 let original_address = account.address;
483 let (impl_addr, proxy_state) = match state_guard
492 .proxy_token_addresses
493 .get(&original_address)
494 {
495 Some(impl_addr) => {
496 let proxy_state = AccountUpdate::new(
503 original_address,
504 value.chain,
505 account.slots.clone(),
506 Some(account.native_balance),
507 None,
508 ChangeType::Update,
509 );
510 (*impl_addr, proxy_state)
511 }
512 None => {
513 let impl_addr = generate_proxy_token_address(
517 state_guard.proxy_token_addresses.len(),
518 )?;
519 state_guard
520 .proxy_token_addresses
521 .insert(original_address, impl_addr);
522
523 let proxy_state = create_proxy_token_account(
525 original_address,
526 Some(impl_addr),
527 &account.slots,
528 value.chain,
529 Some(account.native_balance),
530 );
531
532 (impl_addr, proxy_state)
533 }
534 };
535
536 proxy_token_accounts.insert(original_address, proxy_state);
537
538 let impl_update = ResponseAccount {
540 address: impl_addr,
541 slots: HashMap::new(),
542 ..account.clone()
543 };
544 storage_by_address.insert(impl_addr, impl_update);
545 } else {
546 storage_by_address.insert(account.address, account);
548 }
549 }
550
551 let mut proxy_creates: Vec<AccountUpdate> = Vec::new();
555 let mut proxy_updates: HashMap<Address, AccountUpdate> = HashMap::new();
556 for (addr, update) in proxy_token_accounts {
557 if matches!(update.change, ChangeType::Creation) {
558 proxy_creates.push(update);
559 } else {
560 proxy_updates.insert(addr, update);
561 }
562 }
563
564 info!("Updating engine with {} contracts from snapshots", storage_by_address.len());
565 update_engine(
566 SHARED_TYCHO_DB.clone(),
567 header.clone().block(),
568 Some(storage_by_address),
569 proxy_updates,
570 )
571 .map_err(|e| StreamDecodeError::Fatal(e.to_string()))?;
572
573 if !proxy_creates.is_empty() {
577 SHARED_TYCHO_DB
578 .force_update_accounts(proxy_creates)
579 .map_err(|e| StreamDecodeError::Fatal(e.to_string()))?;
580 }
581 info!("Engine updated");
582 drop(state_guard);
583 }
584
585 let account_balances = protocol_msg
588 .clone()
589 .snapshots
590 .get_vm_storage()
591 .iter()
592 .filter_map(|(addr, acc)| {
593 if acc.token_balances.is_empty() {
594 return None;
595 }
596 let balances = acc
597 .token_balances
598 .iter()
599 .map(|(token_addr, ab)| (token_addr.clone(), ab.balance.clone()))
600 .collect::<HashMap<Bytes, Bytes>>();
601 Some((addr.clone(), balances))
602 })
603 .collect::<AccountBalances>();
604
605 let mut new_components = HashMap::new();
606 let mut count_token_skips = 0;
607 let mut components_to_store = HashMap::new();
608 {
609 let state_guard = self.state.read().await;
610
611 'snapshot_loop: for (id, snapshot) in protocol_msg
613 .snapshots
614 .get_states()
615 .clone()
616 {
617 if !self.admits(protocol.as_str(), &snapshot) {
619 continue;
620 }
621
622 let mut component_tokens = Vec::new();
624 let mut new_tokens_accounts = HashMap::new();
625 for token in snapshot.component.tokens.clone() {
626 match state_guard.tokens.get(&token) {
627 Some(token) => {
628 let token_address = match bytes_to_address(&token.address) {
631 Ok(addr) => addr,
632 Err(_) if drops_unknown_tokens => {
633 warn!(
634 "Token address could not be decoded {}, ignoring the token in component {:x?}",
635 token.address, id
636 );
637 continue;
638 }
639 Err(_) => {
640 count_token_skips += 1;
641 msg_failed_components.insert(id.clone());
642 warn!(
643 "Token address could not be decoded {}, ignoring pool {:x?}",
644 token.address, id
645 );
646 continue 'snapshot_loop;
647 }
648 };
649 component_tokens.push(token.clone());
650 if !state_guard
652 .proxy_token_addresses
653 .contains_key(&token_address)
654 {
655 new_tokens_accounts.insert(
656 token_address,
657 create_proxy_token_account(
658 token_address,
659 None,
660 &HashMap::new(),
661 snapshot.component.chain,
662 None,
663 ),
664 );
665 }
666 }
667 None if drops_unknown_tokens => {
668 warn!(
669 "Token not found {}, ignoring the token in component {:x?}",
670 token, id
671 );
672 }
673 None => {
674 count_token_skips += 1;
675 msg_failed_components.insert(id.clone());
676 debug!("Token not found {}, ignoring pool {:x?}", token, id);
677 continue 'snapshot_loop;
678 }
679 }
680 }
681 let component = ProtocolComponent::from_with_tokens(
682 snapshot.component.clone(),
683 component_tokens,
684 );
685
686 if !new_tokens_accounts.is_empty() {
688 update_engine(
689 SHARED_TYCHO_DB.clone(),
690 header.clone().block(),
691 None,
692 new_tokens_accounts,
693 )
694 .map_err(|e| StreamDecodeError::Fatal(e.to_string()))?;
695 }
696
697 if !component
700 .static_attributes
701 .contains_key("manual_updates")
702 {
703 for contract in &component.contract_ids {
704 contracts_map
705 .entry(contract.clone())
706 .or_insert_with(HashSet::new)
707 .insert(id.clone());
708 }
709 for (_, tracing) in snapshot.entrypoints.iter() {
712 for contract in tracing.accessed_slots.keys().cloned() {
713 contracts_map
714 .entry(contract)
715 .or_insert_with(HashSet::new)
716 .insert(id.clone());
717 }
718 }
719 }
720
721 new_pairs.insert(id.clone(), component.clone());
723
724 components_to_store.insert(id.clone(), component);
726
727 if let Some(state_decode_f) = self.registry.get(protocol.as_str()) {
729 let live_override = self
730 .override_providers
731 .get(protocol.as_str())
732 .and_then(|provider| provider.subscribe(protocol.as_str()));
733 match state_decode_f(
734 snapshot,
735 header.clone(),
736 account_balances.clone(),
737 self.state.clone(),
738 live_override,
739 )
740 .await
741 {
742 Ok(state) => {
743 new_components.insert(id.clone(), state);
744 }
745 Err(e) => {
746 if self.skip_state_decode_failures {
747 warn!(pool = id, error = %e, "StateDecodingFailure");
748 msg_failed_components.insert(id.clone());
749 continue 'snapshot_loop;
750 }
751 error!(pool = id, error = %e, "StateDecodingFailure");
752 return Err(StreamDecodeError::Fatal(format!("{e}")));
753 }
754 }
755 } else if self.skip_state_decode_failures {
756 warn!(pool = id, "MissingDecoderRegistration");
757 msg_failed_components.insert(id.clone());
758 } else {
759 error!(pool = id, "MissingDecoderRegistration");
760 return Err(StreamDecodeError::Fatal(format!(
761 "Missing decoder registration for: {id}"
762 )));
763 }
764 }
765 }
766
767 if !components_to_store.is_empty() {
769 let mut state_guard = self.state.write().await;
770 for (id, component) in components_to_store {
771 state_guard
772 .components
773 .insert(id, component);
774 }
775 }
776
777 if !protocol_msg.snapshots.states.is_empty() {
778 info!("Decoded {} snapshots for protocol {protocol}", new_components.len());
779 }
780 if count_token_skips > 0 {
781 info!("Skipped {count_token_skips} pools due to missing tokens");
782 }
783
784 updated_states.extend(new_components);
786
787 if let Some(deltas) = protocol_msg.deltas.clone() {
789 let mut state_guard = self.state.write().await;
791
792 let mut account_update_by_address: HashMap<Address, AccountUpdate> = HashMap::new();
793 let mut new_proxy_accounts: Vec<AccountUpdate> = Vec::new();
795 for (key, value) in deltas.account_deltas.iter() {
796 let mut update: AccountUpdate = value.clone().into();
797
798 if update.code.is_none() && matches!(update.change, ChangeType::Creation) {
804 error!(
805 update = ?update,
806 "FaultyCreationDelta"
807 );
808 update.code = Some(vec![]);
809 }
810
811 if state_guard.tokens.contains_key(key) {
812 let original_address = update.address;
813 let impl_addr = match state_guard
820 .proxy_token_addresses
821 .get(&original_address)
822 {
823 Some(impl_addr) => {
824 let proxy_update = AccountUpdate {
829 code: None,
830 change: ChangeType::Update,
831 ..update.clone()
832 };
833 account_update_by_address.insert(original_address, proxy_update);
834
835 *impl_addr
836 }
837 None => {
838 let impl_addr = generate_proxy_token_address(
843 state_guard.proxy_token_addresses.len(),
844 )?;
845 state_guard
846 .proxy_token_addresses
847 .insert(original_address, impl_addr);
848
849 let proxy_state = create_proxy_token_account(
854 original_address,
855 Some(impl_addr),
856 &update.slots,
857 update.chain,
858 update.balance,
859 );
860 new_proxy_accounts.push(proxy_state);
861
862 impl_addr
863 }
864 };
865
866 if update.code.is_some() {
868 let impl_update = AccountUpdate {
869 address: impl_addr,
870 slots: HashMap::new(),
871 ..update.clone()
872 };
873 account_update_by_address.insert(impl_addr, impl_update);
874 }
875 } else {
876 account_update_by_address.insert(update.address, update);
878 }
879 }
880 drop(state_guard);
881
882 let state_guard = self.state.read().await;
883 info!("Updating engine with {} contract deltas", deltas.account_deltas.len());
884 update_engine(
885 SHARED_TYCHO_DB.clone(),
886 header.clone().block(),
887 None,
888 account_update_by_address,
889 )
890 .map_err(|e| StreamDecodeError::Fatal(e.to_string()))?;
891
892 if !new_proxy_accounts.is_empty() {
895 SHARED_TYCHO_DB
896 .force_update_accounts(new_proxy_accounts)
897 .map_err(|e| StreamDecodeError::Fatal(e.to_string()))?;
898 }
899 info!("Engine updated");
900
901 let mut pools_to_update = HashSet::new();
903 for (account, _update) in deltas.account_deltas {
904 pools_to_update.extend(
906 contracts_map
907 .get(&account)
908 .cloned()
909 .unwrap_or_default(),
910 );
911 pools_to_update.extend(
913 state_guard
914 .contracts_map
915 .get(&account)
916 .cloned()
917 .unwrap_or_default(),
918 );
919 }
920
921 let all_balances = Balances {
923 component_balances: deltas
924 .component_balances
925 .iter()
926 .map(|(pool_id, bals)| {
927 let mut balances = HashMap::new();
928 for (t, b) in bals {
929 balances.insert(t.clone(), b.balance.clone());
930 }
931 pools_to_update.insert(pool_id.clone());
932 (pool_id.clone(), balances)
933 })
934 .collect(),
935 account_balances: deltas
936 .account_balances
937 .iter()
938 .map(|(account, bals)| {
939 let mut balances = HashMap::new();
940 for (t, b) in bals {
941 balances.insert(t.clone(), b.balance.clone());
942 }
943 pools_to_update.extend(
944 contracts_map
945 .get(account)
946 .cloned()
947 .unwrap_or_default(),
948 );
949 (account.clone(), balances)
950 })
951 .collect(),
952 };
953
954 for (id, update) in deltas.state_deltas {
956 let update_with_block = Self::add_block_info_to_delta(
958 ProtocolStateDelta::from(update),
959 current_block.clone(),
960 );
961 match Self::apply_update(
962 &id,
963 update_with_block,
964 &mut updated_states,
965 &state_guard,
966 &all_balances,
967 ) {
968 Ok(_) => {
969 pools_to_update.remove(&id);
970 }
971 Err(e) => {
972 if self.skip_state_decode_failures {
973 warn!(pool = id, error = %e, "Failed to apply state update, marking component as removed");
974 updated_states.remove(&id);
976 if let Some(component) = new_pairs.remove(&id) {
978 removed_pairs.insert(id.clone(), component);
979 } else if let Some(component) = state_guard.components.get(&id) {
980 removed_pairs.insert(id.clone(), component.clone());
981 } else {
982 warn!(pool = id, "Component not found in new_pairs or state, cannot add to removed_pairs");
985 }
986 pools_to_update.remove(&id);
987
988 msg_failed_components.insert(id.clone());
990 } else {
991 return Err(e);
992 }
993 }
994 }
995 }
996
997 for pool in pools_to_update {
999 let default_delta_with_block = Self::add_block_info_to_delta(
1001 ProtocolStateDelta::default(),
1002 current_block.clone(),
1003 );
1004 match Self::apply_update(
1005 &pool,
1006 default_delta_with_block,
1007 &mut updated_states,
1008 &state_guard,
1009 &all_balances,
1010 ) {
1011 Ok(_) => {}
1012 Err(e) => {
1013 if self.skip_state_decode_failures {
1014 warn!(pool = pool, error = %e, "Failed to apply contract/balance update, marking component as removed");
1015 updated_states.remove(&pool);
1017 if let Some(component) = new_pairs.remove(&pool) {
1019 removed_pairs.insert(pool.clone(), component);
1020 } else if let Some(component) = state_guard.components.get(&pool) {
1021 removed_pairs.insert(pool.clone(), component.clone());
1022 } else {
1023 warn!(pool = pool, "Component not found in new_pairs or state, cannot add to removed_pairs");
1026 }
1027
1028 msg_failed_components.insert(pool.clone());
1030 } else {
1031 return Err(e);
1032 }
1033 }
1034 }
1035 }
1036 };
1037 }
1038
1039 let mut state_guard = self.state.write().await;
1041
1042 state_guard
1044 .failed_components
1045 .extend(msg_failed_components);
1046
1047 updated_states.retain(|id, _| {
1051 !state_guard
1052 .failed_components
1053 .contains(id)
1054 });
1055 new_pairs.retain(|id, _| {
1056 !state_guard
1057 .failed_components
1058 .contains(id)
1059 });
1060
1061 if let Some(header) = current_block.as_ref() {
1062 let execution_block = self.execution_block(header);
1063 let decoder_state = &mut *state_guard;
1064 Self::refresh_execution_block(
1065 &mut updated_states,
1066 &mut decoder_state.states,
1067 &decoder_state.failed_components,
1068 &removed_pairs,
1069 &execution_block,
1070 );
1071 }
1072
1073 state_guard
1074 .states
1075 .extend(updated_states.clone());
1076
1077 state_guard.current_block_number = block_number;
1078
1079 for (id, component) in new_pairs.iter() {
1081 state_guard
1082 .components
1083 .insert(id.clone(), component.clone());
1084 }
1085
1086 for id in removed_pairs.keys() {
1088 state_guard.components.remove(id);
1089 }
1090
1091 for (key, values) in contracts_map {
1092 state_guard
1093 .contracts_map
1094 .entry(key)
1095 .or_insert_with(HashSet::new)
1096 .extend(values);
1097 }
1098
1099 Ok(Update::new(block_number, updated_states, new_pairs)
1101 .set_is_partial(is_partial)
1102 .set_removed_pairs(removed_pairs)
1103 .set_sync_states(msg.sync_states.clone()))
1104 }
1105
1106 pub async fn apply_deltas_ephemeral(
1126 &self,
1127 pending_deltas: &HashMap<String, BlockAggregatedChanges>,
1128 header: H,
1129 ) -> Result<Update, StreamDecodeError> {
1130 let block_number = header
1131 .clone()
1132 .block_number_or_timestamp();
1133 let current_block = header.block();
1134 let state_guard = self.state.read().await;
1135
1136 let mut updated_states: HashMap<String, Box<dyn ProtocolSim>> = HashMap::new();
1137
1138 for deltas in pending_deltas.values() {
1139 let all_balances = Balances {
1140 component_balances: deltas
1141 .component_balances
1142 .iter()
1143 .map(|(pool_id, bals)| {
1144 let balances = bals
1145 .iter()
1146 .map(|(t, b)| (t.clone(), b.balance.clone()))
1147 .collect();
1148 (pool_id.clone(), balances)
1149 })
1150 .collect(),
1151 account_balances: HashMap::new(),
1152 };
1153
1154 for (id, state_delta) in &deltas.state_deltas {
1155 let dto_delta = Self::add_block_info_to_delta(
1156 ProtocolStateDelta::from(state_delta.clone()),
1157 current_block.clone(),
1158 );
1159 if let Err(e) = Self::apply_update(
1160 id,
1161 dto_delta,
1162 &mut updated_states,
1163 &state_guard,
1164 &all_balances,
1165 ) {
1166 warn!(pool = id, error = %e, "EphemeralDeltaTransitionError");
1167 }
1168 }
1169
1170 if !deltas.account_deltas.is_empty() {
1171 Self::apply_pending_overrides(
1172 deltas,
1173 current_block.as_ref(),
1174 &mut updated_states,
1175 &state_guard,
1176 &all_balances,
1177 )?;
1178 }
1179 }
1180
1181 if let Some(header) = current_block.as_ref() {
1186 let execution_block = BlockContext::new(header.number, header.timestamp);
1187 for state in updated_states.values_mut() {
1188 state.apply_block(&execution_block);
1189 }
1190 }
1191
1192 Ok(Update::new(block_number, updated_states, HashMap::new()))
1193 }
1194
1195 fn apply_pending_overrides(
1205 deltas: &BlockAggregatedChanges,
1206 current_block: Option<&BlockHeader>,
1207 updated_states: &mut HashMap<String, Box<dyn ProtocolSim>>,
1208 state_guard: &RwLockReadGuard<'_, DecoderState>,
1209 all_balances: &Balances,
1210 ) -> Result<(), StreamDecodeError> {
1211 let mut linked_pools: HashSet<&String> = HashSet::new();
1212 for account in deltas.account_deltas.keys() {
1213 if let Some(pools) = state_guard.contracts_map.get(account) {
1214 linked_pools.extend(pools);
1215 }
1216 }
1217
1218 let block = current_block
1219 .map(|h| BlockEnvOverrides { number: Some(h.number), timestamp: Some(h.timestamp) });
1220 let overrides: Arc<dyn Any + Send + Sync> =
1221 Arc::new(PendingOverrides::from_account_deltas(&deltas.account_deltas, block));
1222 for id in linked_pools {
1223 if !updated_states.contains_key(id) {
1227 let empty_delta = Self::add_block_info_to_delta(
1228 ProtocolStateDelta { component_id: id.clone(), ..Default::default() },
1229 current_block.cloned(),
1230 );
1231 if let Err(e) =
1232 Self::apply_update(id, empty_delta, updated_states, state_guard, all_balances)
1233 {
1234 warn!(pool = id, error = %e, "EphemeralDeltaTransitionError");
1235 continue;
1236 }
1237 }
1238 if let Some(state) = updated_states.get_mut(id) {
1239 state
1240 .set_pending_overrides(Arc::clone(&overrides))
1241 .map_err(|e| {
1242 StreamDecodeError::Fatal(format!("Pending overrides for pool {id}: {e}"))
1243 })?;
1244 }
1245 }
1246 Ok(())
1247 }
1248
1249 fn add_block_info_to_delta(
1251 mut delta: ProtocolStateDelta,
1252 block_header_opt: Option<BlockHeader>,
1253 ) -> ProtocolStateDelta {
1254 if let Some(header) = block_header_opt {
1255 delta.updated_attributes.insert(
1258 "block_number".to_string(),
1259 Bytes::from(header.number.to_be_bytes().to_vec()),
1260 );
1261 delta.updated_attributes.insert(
1262 "block_timestamp".to_string(),
1263 Bytes::from(header.timestamp.to_be_bytes().to_vec()),
1264 );
1265 }
1266 delta
1267 }
1268
1269 fn apply_update(
1270 id: &String,
1271 update: ProtocolStateDelta,
1272 updated_states: &mut HashMap<String, Box<dyn ProtocolSim>>,
1273 state_guard: &RwLockReadGuard<'_, DecoderState>,
1274 all_balances: &Balances,
1275 ) -> Result<(), StreamDecodeError> {
1276 match updated_states.entry(id.clone()) {
1277 Entry::Occupied(mut entry) => {
1278 let state: &mut Box<dyn ProtocolSim> = entry.get_mut();
1280 state
1281 .delta_transition(update, &state_guard.tokens, all_balances)
1282 .map_err(|e| {
1283 error!(pool = id, error = ?e, "DeltaTransitionError");
1284 StreamDecodeError::Fatal(format!("TransitionFailure: {e:?}"))
1285 })?;
1286 }
1287 Entry::Vacant(_) => {
1288 match state_guard.states.get(id) {
1289 Some(stored_state) => {
1292 let mut state = stored_state.clone();
1293 state
1294 .delta_transition(update, &state_guard.tokens, all_balances)
1295 .map_err(|e| {
1296 error!(pool = id, error = ?e, "DeltaTransitionError");
1297 StreamDecodeError::Fatal(format!("TransitionFailure: {e:?}"))
1298 })?;
1299 updated_states.insert(id.clone(), state);
1300 }
1301 None => debug!(pool = id, reason = "MissingState", "DeltaTransitionError"),
1302 }
1303 }
1304 }
1305 Ok(())
1306 }
1307}
1308
1309fn generate_proxy_token_address(idx: usize) -> Result<Address, StreamDecodeError> {
1311 let padded_idx = format!("{idx:x}");
1312 let padded_zeroes = "0".repeat(33 - padded_idx.len());
1313 let proxy_token_address = format!("{padded_zeroes}{padded_idx}BAdbaBe");
1314 let decoded = hex::decode(proxy_token_address).map_err(|e| {
1315 StreamDecodeError::Fatal(format!("Invalid proxy token address encoding: {e}"))
1316 })?;
1317
1318 const ADDRESS_LENGTH: usize = 20;
1319 if decoded.len() != ADDRESS_LENGTH {
1320 return Err(StreamDecodeError::Fatal(format!(
1321 "Invalid proxy token address length: expected {}, got {}",
1322 ADDRESS_LENGTH,
1323 decoded.len(),
1324 )));
1325 }
1326
1327 Ok(Address::from_slice(&decoded))
1328}
1329
1330fn create_proxy_token_account(
1335 addr: Address,
1336 new_address: Option<Address>,
1337 storage: &HashMap<U256, U256>,
1338 chain: Chain,
1339 balance: Option<U256>,
1340) -> AccountUpdate {
1341 let mut slots = storage.clone();
1342 if let Some(new_address) = new_address {
1343 slots.insert(*IMPLEMENTATION_SLOT, U256::from_be_slice(new_address.as_slice()));
1344 }
1345
1346 AccountUpdate {
1347 address: addr,
1348 chain,
1349 slots,
1350 balance,
1351 code: Some(ERC20_PROXY_BYTECODE.to_vec()),
1352 change: ChangeType::Creation,
1353 }
1354}
1355
1356#[cfg(test)]
1357#[expect(
1358 clippy::same_name_method,
1359 reason = "mockall generates the inherent methods the ProtocolSim impl forwards to"
1360)]
1361mod protocol_sim_mock {
1362 use super::*;
1363
1364 mock! {
1365 #[derive(Debug)]
1366 pub ProtocolSim {
1367 pub fn fee(&self) -> f64;
1368 pub fn spot_price(&self, base: &Token, quote: &Token) -> Result<f64, SimulationError>;
1369 pub fn get_amount_out(
1370 &self,
1371 amount_in: BigUint,
1372 token_in: &Token,
1373 token_out: &Token,
1374 ) -> Result<GetAmountOutResult, SimulationError>;
1375 pub fn get_limits(
1376 &self,
1377 sell_token: Bytes,
1378 buy_token: Bytes,
1379 ) -> Result<(BigUint, BigUint), SimulationError>;
1380 pub fn delta_transition(
1381 &mut self,
1382 delta: ProtocolStateDelta,
1383 tokens: &HashMap<Bytes, Token>,
1384 balances: &Balances,
1385 ) -> Result<(), TransitionError>;
1386 pub fn clone_box(&self) -> Box<dyn ProtocolSim>;
1387 pub fn eq(&self, other: &dyn ProtocolSim) -> bool;
1388 pub fn base_token(&self) -> &Token;
1389 pub fn quote_token(&self) -> &Token;
1390 pub fn directions(&self) -> Vec<(Bytes, Bytes)>;
1391 }
1392 }
1393}
1394
1395#[cfg(test)]
1396pub use protocol_sim_mock::MockProtocolSim;
1397
1398#[cfg(test)]
1399crate::impl_non_serializable_protocol!(MockProtocolSim, "test protocol");
1400
1401#[cfg(test)]
1402impl ProtocolSim for MockProtocolSim {
1403 fn fee(&self) -> f64 {
1404 self.fee()
1405 }
1406
1407 fn spot_price(&self, base: &Token, quote: &Token) -> Result<f64, SimulationError> {
1408 self.spot_price(base, quote)
1409 }
1410
1411 fn get_amount_out(
1412 &self,
1413 amount_in: BigUint,
1414 token_in: &Token,
1415 token_out: &Token,
1416 ) -> Result<GetAmountOutResult, SimulationError> {
1417 self.get_amount_out(amount_in, token_in, token_out)
1418 }
1419
1420 fn get_limits(
1421 &self,
1422 sell_token: Bytes,
1423 buy_token: Bytes,
1424 ) -> Result<(BigUint, BigUint), SimulationError> {
1425 self.get_limits(sell_token, buy_token)
1426 }
1427
1428 fn delta_transition(
1429 &mut self,
1430 delta: ProtocolStateDelta,
1431 tokens: &HashMap<Bytes, Token>,
1432 balances: &Balances,
1433 ) -> Result<(), TransitionError> {
1434 self.delta_transition(delta, tokens, balances)
1435 }
1436
1437 fn clone_box(&self) -> Box<dyn ProtocolSim> {
1438 self.clone_box()
1439 }
1440
1441 fn as_any(&self) -> &dyn Any {
1442 panic!("MockProtocolSim does not support as_any")
1443 }
1444
1445 fn as_any_mut(&mut self) -> &mut dyn Any {
1446 panic!("MockProtocolSim does not support as_any_mut")
1447 }
1448
1449 fn eq(&self, other: &dyn ProtocolSim) -> bool {
1450 self.eq(other)
1451 }
1452
1453 fn typetag_name(&self) -> &'static str {
1454 "MockProtocolSim"
1456 }
1457
1458 #[expect(clippy::unreachable, reason = "the mock is never deserialized through its typetag")]
1459 fn typetag_deserialize(&self) {
1460 unreachable!()
1461 }
1462}
1463
1464#[cfg(test)]
1467impl crate::book::component::PairState for MockProtocolSim {
1468 fn base_token(&self) -> &Token {
1469 self.base_token()
1470 }
1471
1472 fn quote_token(&self) -> &Token {
1473 self.quote_token()
1474 }
1475
1476 fn directions(&self) -> Vec<(Bytes, Bytes)> {
1477 self.directions()
1478 }
1479}
1480
1481#[cfg(test)]
1482mod tests {
1483 use std::str::FromStr;
1484
1485 use alloy::primitives::address;
1486 use mockall::predicate::*;
1487 use rstest::*;
1488 use tycho_client::feed::BlockHeader;
1489 use tycho_common::{models::Chain, Bytes};
1490
1491 use super::*;
1492
1493 fn header_at(number: u64, timestamp: u64, partial: Option<u32>) -> BlockHeader {
1494 BlockHeader {
1495 hash: Bytes::from([0u8; 32]),
1496 number,
1497 parent_hash: Bytes::from([0u8; 32]),
1498 revert: false,
1499 timestamp,
1500 partial_block_index: partial,
1501 }
1502 }
1503
1504 fn block_sensitive_state() -> Box<dyn ProtocolSim> {
1506 use crate::evm::protocol::{
1507 aerodrome_slipstreams::state::AerodromeSlipstreamsState,
1508 utils::{
1509 slipstreams::{dynamic_fee_module::DynamicFeeConfig, observations::Observation},
1510 uniswap::{tick_list::TickInfo, tick_math::get_sqrt_ratio_at_tick},
1511 },
1512 };
1513
1514 Box::new(
1515 AerodromeSlipstreamsState::new(
1516 "block-sensitive".to_string(),
1517 0,
1518 1_000_000_000_000_000_000,
1519 get_sqrt_ratio_at_tick(0).unwrap(),
1520 0,
1521 1,
1522 3000,
1523 1,
1524 0,
1525 vec![TickInfo::new(-120, 0).unwrap(), TickInfo::new(120, 0).unwrap()],
1526 vec![Observation { block_timestamp: 500, initialized: true, ..Default::default() }],
1527 DynamicFeeConfig::new(2700, 30_000, 0, true, 750),
1528 )
1529 .expect("state should build")
1530 .with_position_assumption(crate::protocol::models::BlockPositionAssumption::First),
1533 )
1534 }
1535
1536 #[test]
1537 fn confirmed_header_targets_the_next_block() {
1538 let decoder = TychoStreamDecoder::<BlockHeader>::new(Chain::Base);
1539
1540 let execution_block = decoder.execution_block(&header_at(100, 1_000, None));
1541
1542 assert_eq!(execution_block.number(), 101);
1543 assert_eq!(execution_block.timestamp(), 1_000 + Chain::Base.block_time_secs());
1544 }
1545
1546 #[test]
1547 fn partial_header_targets_the_block_that_is_still_open() {
1548 let decoder = TychoStreamDecoder::<BlockHeader>::new(Chain::Ethereum);
1549
1550 let execution_block = decoder.execution_block(&header_at(100, 1_000, Some(3)));
1551
1552 assert_eq!(execution_block.number(), 100);
1553 assert_eq!(execution_block.timestamp(), 1_000);
1554 }
1555
1556 #[test]
1557 fn refresh_re_emits_a_state_whose_fee_flipped_without_a_delta() {
1558 let mut stored = HashMap::from([("block-sensitive".to_string(), {
1562 let mut state = block_sensitive_state();
1563 state.apply_block(&BlockContext::new(100, 500));
1564 state
1565 })]);
1566 let mut updated: HashMap<String, Box<dyn ProtocolSim>> = HashMap::new();
1567
1568 TychoStreamDecoder::<BlockHeader>::refresh_execution_block(
1569 &mut updated,
1570 &mut stored,
1571 &HashSet::new(),
1572 &HashMap::<String, ()>::new(),
1573 &BlockContext::new(101, 502),
1574 );
1575
1576 let emitted = updated
1577 .get("block-sensitive")
1578 .expect("a fee flip must be emitted even without a delta");
1579 assert_eq!(emitted.fee(), 750.0 / 1_000_000.0);
1580 assert_eq!(stored["block-sensitive"].fee(), 750.0 / 1_000_000.0);
1582 }
1583
1584 #[test]
1585 fn refresh_never_re_emits_failed_components() {
1586 let mut stored = HashMap::from([("zombie".to_string(), {
1589 let mut state = block_sensitive_state();
1590 state.apply_block(&BlockContext::new(100, 500));
1591 state
1592 })]);
1593 let failed = HashSet::from(["zombie".to_string()]);
1594 let mut updated: HashMap<String, Box<dyn ProtocolSim>> = HashMap::new();
1595
1596 TychoStreamDecoder::<BlockHeader>::refresh_execution_block(
1597 &mut updated,
1598 &mut stored,
1599 &failed,
1600 &HashMap::<String, ()>::new(),
1601 &BlockContext::new(101, 502),
1602 );
1603
1604 assert!(updated.is_empty());
1605 }
1606
1607 #[test]
1608 fn refresh_never_re_emits_removed_components() {
1609 let mut stored = HashMap::from([("gone".to_string(), {
1612 let mut state = block_sensitive_state();
1613 state.apply_block(&BlockContext::new(100, 500));
1614 state
1615 })]);
1616 let removed = HashMap::from([("gone".to_string(), ())]);
1617 let mut updated: HashMap<String, Box<dyn ProtocolSim>> = HashMap::new();
1618
1619 TychoStreamDecoder::<BlockHeader>::refresh_execution_block(
1620 &mut updated,
1621 &mut stored,
1622 &HashSet::new(),
1623 &removed,
1624 &BlockContext::new(101, 502),
1625 );
1626
1627 assert!(updated.is_empty());
1628 }
1629
1630 #[test]
1631 fn refresh_stays_quiet_when_no_fee_changed() {
1632 let mut stored: HashMap<String, Box<dyn ProtocolSim>> = HashMap::from([
1635 ("idle-sensitive".to_string(), {
1636 let mut state = block_sensitive_state();
1637 state.apply_block(&BlockContext::new(101, 502));
1638 state
1639 }),
1640 (
1641 "univ2".to_string(),
1642 Box::new(crate::evm::protocol::uniswap_v2::state::UniswapV2State::new(
1643 U256::from(1_000_000u64),
1644 U256::from(1_000_000u64),
1645 )) as Box<dyn ProtocolSim>,
1646 ),
1647 ]);
1648 let mut updated: HashMap<String, Box<dyn ProtocolSim>> = HashMap::new();
1649
1650 TychoStreamDecoder::<BlockHeader>::refresh_execution_block(
1651 &mut updated,
1652 &mut stored,
1653 &HashSet::new(),
1654 &HashMap::<String, ()>::new(),
1655 &BlockContext::new(102, 504),
1656 );
1657
1658 assert!(updated.is_empty());
1659 }
1660 use crate::evm::protocol::{curve::CurveState, uniswap_v2::state::UniswapV2State};
1661
1662 #[test]
1663 fn curve_vm_adapter_registration_flagged_deprecated() {
1664 assert!(!is_deprecated_curve_registration::<CurveState>("vm:curve"));
1666 assert!(is_deprecated_curve_registration::<UniswapV2State>("vm:curve"));
1668 assert!(!is_deprecated_curve_registration::<UniswapV2State>("uniswap_v2"));
1670 }
1671
1672 async fn setup_decoder(set_tokens: bool) -> TychoStreamDecoder<BlockHeader> {
1673 let mut decoder = TychoStreamDecoder::new(Chain::Ethereum);
1674 decoder.register_decoder::<UniswapV2State>("uniswap_v2");
1675 if set_tokens {
1676 let tokens = [
1677 Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").lpad(20, 0),
1678 Bytes::from("0xdac17f958d2ee523a2206206994597c13d831ec7").lpad(20, 0),
1679 ]
1680 .iter()
1681 .map(|addr| {
1682 let addr_str = format!("{addr:x}");
1683 (
1684 addr.clone(),
1685 Token::new(addr, &addr_str, 18, 100, &[Some(100_000)], Chain::Ethereum, 100),
1686 )
1687 })
1688 .collect();
1689 decoder.set_tokens(tokens).await;
1690 }
1691 decoder
1692 }
1693
1694 fn load_test_msg(name: &str) -> FeedMessage<BlockHeader> {
1695 use std::{fs, path::Path};
1696
1697 use tycho_client::feed::dto;
1698 let project_root = env!("CARGO_MANIFEST_DIR");
1699 let asset_path = Path::new(project_root).join(format!("tests/assets/decoder/{name}.json"));
1700 let json_data = fs::read_to_string(asset_path).expect("Failed to read test asset");
1701 let feed_msg: dto::FeedMessage<BlockHeader> =
1702 serde_json::from_str(&json_data).expect("Failed to deserialize FeedMsg json!");
1703 FeedMessage::from(feed_msg)
1704 }
1705
1706 #[tokio::test]
1707 async fn test_decode() {
1708 let decoder = setup_decoder(true).await;
1709
1710 let msg = load_test_msg("uniswap_v2_snapshot");
1711 let res1 = decoder
1712 .decode(&msg)
1713 .await
1714 .expect("decode failure");
1715 let msg = load_test_msg("uniswap_v2_delta");
1716 let res2 = decoder
1717 .decode(&msg)
1718 .await
1719 .expect("decode failure");
1720
1721 assert_eq!(res1.states.len(), 1);
1722 assert_eq!(res2.states.len(), 1);
1723 assert_eq!(res1.sync_states.len(), 1);
1724 assert_eq!(res2.sync_states.len(), 1);
1725 }
1726
1727 #[tokio::test]
1728 async fn test_decode_token_creation_delta_with_existing_proxy() {
1729 let decoder = setup_decoder(true).await;
1730 let msg = load_test_msg("uniswap_v2_delta_token_creation");
1731
1732 decoder
1735 .decode(&msg)
1736 .await
1737 .expect("first decode (proxy creation) failed");
1738
1739 decoder
1743 .decode(&msg)
1744 .await
1745 .expect("decode of a token Creation delta with an existing proxy failed");
1746 }
1747
1748 #[tokio::test]
1749 async fn test_decode_component_missing_token() {
1750 let decoder = setup_decoder(false).await;
1751 let tokens = [Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").lpad(20, 0)]
1752 .iter()
1753 .map(|addr| {
1754 let addr_str = format!("{addr:x}");
1755 (
1756 addr.clone(),
1757 Token::new(addr, &addr_str, 18, 100, &[Some(100_000)], Chain::Ethereum, 100),
1758 )
1759 })
1760 .collect();
1761 decoder.set_tokens(tokens).await;
1762
1763 let msg = load_test_msg("uniswap_v2_snapshot");
1764 let res1 = decoder
1765 .decode(&msg)
1766 .await
1767 .expect("decode failure");
1768
1769 assert_eq!(res1.states.len(), 0);
1770 }
1771
1772 #[rstest]
1773 #[case::unknown_token(false)]
1774 #[case::undecodable_address(true)]
1775 #[tokio::test]
1776 async fn test_dropping_decoder_keeps_component(#[case] bad_address: bool) {
1777 let weth = Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").lpad(20, 0);
1778 let other = match bad_address {
1780 true => Bytes::from([1u8; 21].to_vec()),
1781 false => Bytes::from("0xdac17f958d2ee523a2206206994597c13d831ec7").lpad(20, 0),
1782 };
1783 let mut decoder = TychoStreamDecoder::new(Chain::Ethereum);
1784 decoder.register_decoder_dropping_unknown_tokens::<UniswapV2State>("uniswap_v2");
1785 let token = |address: &Bytes, symbol| {
1786 let token =
1787 Token::new(address, symbol, 18, 100, &[Some(100_000)], Chain::Ethereum, 100);
1788 (address.clone(), token)
1789 };
1790 let mut known = HashMap::from([token(&weth, "WETH")]);
1791 if bad_address {
1792 known.extend([token(&other, "OTHER")]);
1793 }
1794 decoder.set_tokens(known).await;
1795 let mut msg = load_test_msg("uniswap_v2_snapshot");
1796 let (id, snapshot) = msg
1797 .state_msgs
1798 .get_mut("uniswap_v2")
1799 .unwrap()
1800 .snapshots
1801 .states
1802 .iter_mut()
1803 .next()
1804 .unwrap();
1805 snapshot.component.tokens = vec![weth, other];
1806 let id = id.clone();
1807
1808 let update = decoder.decode(&msg).await.unwrap();
1809
1810 let symbols: Vec<_> = update.new_pairs[&id]
1811 .tokens
1812 .iter()
1813 .map(|token| token.symbol.as_str())
1814 .collect();
1815 assert_eq!(symbols, ["WETH"]);
1816 assert_eq!(update.states.len(), 1);
1817 }
1818
1819 #[tokio::test]
1820 async fn test_decode_component_bad_id() {
1821 let decoder = setup_decoder(true).await;
1822 let msg = load_test_msg("uniswap_v2_snapshot_broken_id");
1823
1824 match decoder.decode(&msg).await {
1825 Err(StreamDecodeError::Fatal(msg)) => {
1826 assert_eq!(msg, "Component id mismatch");
1827 }
1828 Ok(_) => {
1829 panic!("Expected failures to be raised")
1830 }
1831 }
1832 }
1833
1834 #[rstest]
1835 #[case(true)]
1836 #[case(false)]
1837 #[tokio::test]
1838 async fn test_decode_component_bad_state(#[case] skip_failures: bool) {
1839 let mut decoder = setup_decoder(true).await;
1840 decoder.skip_state_decode_failures = skip_failures;
1841
1842 let msg = load_test_msg("uniswap_v2_snapshot_broken_state");
1843 match decoder.decode(&msg).await {
1844 Err(StreamDecodeError::Fatal(msg)) => {
1845 if !skip_failures {
1846 assert_eq!(msg, "Missing attributes reserve0");
1847 } else {
1848 panic!("Expected failures to be ignored. Err: {msg}")
1849 }
1850 }
1851 Ok(res) => {
1852 if !skip_failures {
1853 panic!("Expected failures to be raised")
1854 } else {
1855 assert_eq!(res.states.len(), 0);
1856 }
1857 }
1858 }
1859 }
1860
1861 #[tokio::test]
1862 async fn test_decode_updates_state_on_contract_change() {
1863 let decoder = setup_decoder(true).await;
1864
1865 let mut mock_state = MockProtocolSim::new();
1867
1868 mock_state
1869 .expect_clone_box()
1870 .times(1)
1871 .returning(|| {
1872 let mut cloned_mock_state = MockProtocolSim::new();
1873 cloned_mock_state
1875 .expect_delta_transition()
1876 .times(1)
1877 .returning(|_, _, _| Ok(()));
1878 cloned_mock_state
1879 .expect_clone_box()
1880 .times(1)
1881 .returning(|| Box::new(MockProtocolSim::new()));
1882 Box::new(cloned_mock_state)
1883 });
1884
1885 let pool_id =
1887 "0x93d199263632a4ef4bb438f1feb99e57b4b5f0bd0000000000000000000005c2".to_string();
1888 decoder
1889 .state
1890 .write()
1891 .await
1892 .states
1893 .insert(pool_id.clone(), Box::new(mock_state) as Box<dyn ProtocolSim>);
1894 decoder
1895 .state
1896 .write()
1897 .await
1898 .contracts_map
1899 .insert(
1900 Bytes::from("0xba12222222228d8ba445958a75a0704d566bf2c8").lpad(20, 0),
1901 HashSet::from([pool_id.clone()]),
1902 );
1903
1904 let msg = load_test_msg("balancer_v2_delta");
1906
1907 let _ = decoder
1909 .decode(&msg)
1910 .await
1911 .expect("decode failure");
1912
1913 }
1915
1916 #[tokio::test]
1919 async fn test_apply_deltas_ephemeral_sets_pending_overrides_on_the_clone_only() {
1920 use tycho_common::models::{
1921 blockchain::BlockAggregatedChanges, contract::AccountDelta,
1922 protocol::ProtocolComponentStateDelta, ChangeType as ModelChangeType,
1923 };
1924
1925 use crate::evm::protocol::uniswap_v4::state::{UniswapV4Fees, UniswapV4State};
1926
1927 let decoder = TychoStreamDecoder::<BlockHeader>::new(Chain::Ethereum);
1928 let pool_id = "0xhooked".to_string();
1929 let quiet_pool_id = "0xhooked-no-log".to_string();
1930 let pool = UniswapV4State::new(
1931 1000,
1932 U256::from(1u8) << 96,
1933 UniswapV4Fees::new(0, 0, 3000),
1934 0,
1935 60,
1936 vec![],
1937 )
1938 .unwrap();
1939 let hook = Bytes::from(vec![7u8; 20]);
1940 {
1941 let mut state = decoder.state.write().await;
1942 state
1943 .states
1944 .insert(pool_id.clone(), Box::new(pool.clone()));
1945 state
1946 .states
1947 .insert(quiet_pool_id.clone(), Box::new(pool));
1948 state
1949 .contracts_map
1950 .insert(hook.clone(), HashSet::from([pool_id.clone(), quiet_pool_id.clone()]));
1951 }
1952
1953 let deltas = BlockAggregatedChanges {
1954 extractor: "uniswap_v4_hooks".to_string(),
1955 state_deltas: HashMap::from([(
1956 pool_id.clone(),
1957 ProtocolComponentStateDelta {
1958 component_id: pool_id.clone(),
1959 updated_attributes: HashMap::from([(
1960 "liquidity".to_string(),
1961 Bytes::from(2000_u64.to_be_bytes().to_vec()),
1962 )]),
1963 deleted_attributes: HashSet::new(),
1964 created_attributes: HashSet::new(),
1965 },
1966 )]),
1967 account_deltas: HashMap::from([(
1968 hook.clone(),
1969 AccountDelta::new(
1970 Chain::Ethereum,
1971 hook.clone(),
1972 HashMap::from([(Bytes::from(vec![0u8]), Some(Bytes::from(vec![9u8])))]),
1973 None,
1974 None,
1975 ModelChangeType::Update,
1976 ),
1977 )]),
1978 ..Default::default()
1979 };
1980 let header = BlockHeader { number: 10, timestamp: 20, ..Default::default() };
1981
1982 let update = decoder
1983 .apply_deltas_ephemeral(
1984 &HashMap::from([("uniswap_v4_hooks".to_string(), deltas)]),
1985 header,
1986 )
1987 .await
1988 .unwrap();
1989
1990 let clone = update.states[&pool_id]
1991 .as_any()
1992 .downcast_ref::<UniswapV4State>()
1993 .unwrap();
1994 let pending = clone
1995 .pending_overrides()
1996 .expect("the clone carries the pending block's overrides");
1997 assert_eq!(
1998 pending.block,
1999 Some(BlockEnvOverrides { number: Some(10), timestamp: Some(20) })
2000 );
2001 assert_eq!(
2002 pending.storage.as_ref().unwrap()[&Address::from_slice(&hook)][&U256::ZERO],
2003 U256::from(9)
2004 );
2005 let quiet = update.states[&quiet_pool_id]
2006 .as_any()
2007 .downcast_ref::<UniswapV4State>()
2008 .expect("a pool whose hook was written is cloned without a delta of its own");
2009 assert!(quiet.pending_overrides().is_some());
2010
2011 let stored = decoder.state.read().await;
2012 for id in [&pool_id, &quiet_pool_id] {
2013 let stored = stored.states[id]
2014 .as_any()
2015 .downcast_ref::<UniswapV4State>()
2016 .unwrap();
2017 assert!(stored.pending_overrides().is_none(), "confirmed state is never overlaid");
2018 }
2019 }
2020
2021 #[test]
2022 fn test_generate_proxy_token_address() {
2023 let idx = 1;
2024 let generated_address =
2025 generate_proxy_token_address(idx).expect("proxy token address should be valid");
2026 assert_eq!(generated_address, address!("000000000000000000000000000000001badbabe"));
2027
2028 let idx = 123456;
2029 let generated_address =
2030 generate_proxy_token_address(idx).expect("proxy token address should be valid");
2031 assert_eq!(generated_address, address!("00000000000000000000000000001e240badbabe"));
2032 }
2033
2034 fn hooked_v4_msg() -> FeedMessage<BlockHeader> {
2040 use tycho_client::feed::synchronizer::{Snapshot, StateSyncMessage};
2041 use tycho_common::models::protocol::{ProtocolComponent, ProtocolComponentState};
2042
2043 let id = "0x00000000000000000000000000000000000000000000000000000000000000c4";
2044 let static_attributes = HashMap::from([
2045 ("key_lp_fee".to_string(), Bytes::from(500_i32.to_be_bytes().to_vec())),
2046 ("tick_spacing".to_string(), Bytes::from(60_i32.to_be_bytes().to_vec())),
2047 (
2048 "hooks".to_string(),
2049 Bytes::from_str("0x00000000000000000000000000000000000000c4").unwrap(),
2050 ),
2051 ]);
2052 let attributes = HashMap::from([
2053 ("liquidity".to_string(), Bytes::from(100_u64.to_be_bytes().to_vec())),
2054 ("tick".to_string(), Bytes::from(300_i32.to_be_bytes().to_vec())),
2055 (
2056 "sqrt_price_x96".to_string(),
2057 Bytes::from(
2058 79228162514264337593543950336_u128
2059 .to_be_bytes()
2060 .to_vec(),
2061 ),
2062 ),
2063 ("protocol_fees/zero2one".to_string(), Bytes::from(0_u32.to_be_bytes().to_vec())),
2064 ("protocol_fees/one2zero".to_string(), Bytes::from(0_u32.to_be_bytes().to_vec())),
2065 ("ticks/60/net_liquidity".to_string(), Bytes::from(400_i128.to_be_bytes().to_vec())),
2066 ]);
2067
2068 let snapshot = ComponentWithState {
2069 state: ProtocolComponentState::new(id, attributes, HashMap::new()),
2070 component: ProtocolComponent {
2071 id: id.to_string(),
2072 static_attributes,
2073 ..Default::default()
2074 },
2075 component_tvl: None,
2076 entrypoints: Vec::new(),
2077 };
2078
2079 FeedMessage {
2080 state_msgs: HashMap::from([(
2081 "uniswap_v4_hooks".to_string(),
2082 StateSyncMessage {
2083 header: header_at(1, 1_000, None),
2084 snapshots: Snapshot {
2085 states: HashMap::from([(id.to_string(), snapshot)]),
2086 vm_storage: HashMap::new(),
2087 },
2088 deltas: None,
2089 removed_components: HashMap::new(),
2090 },
2091 )]),
2092 sync_states: HashMap::new(),
2093 }
2094 }
2095
2096 fn hooked_v4_decoder(chain: Chain, context: DecoderContext) -> TychoStreamDecoder<BlockHeader> {
2098 let mut decoder = TychoStreamDecoder::new(chain);
2099 decoder.register_decoder_with_context::<crate::evm::protocol::uniswap_v4::state::UniswapV4State>(
2100 "uniswap_v4_hooks", context
2101 );
2102 decoder
2103 }
2104
2105 #[tokio::test]
2106 async fn test_unregistered_hook_is_rejected_off_the_generic_vm_chains() {
2107 let decoder = hooked_v4_decoder(Chain::Robinhood, DecoderContext::new());
2108
2109 let Err(StreamDecodeError::Fatal(message)) = decoder.decode(&hooked_v4_msg()).await else {
2110 panic!("a hooked pool on robinhood must not decode");
2111 };
2112
2113 assert!(message.contains("unsupported uniswap v4 hook"), "{message}");
2114 assert!(message.contains("robinhood"), "{message}");
2115 }
2116
2117 #[tokio::test]
2118 async fn test_decoder_chain_overrides_the_registered_context_chain() {
2119 let decoder =
2120 hooked_v4_decoder(Chain::Robinhood, DecoderContext::new().chain(Chain::Ethereum));
2121
2122 let Err(StreamDecodeError::Fatal(message)) = decoder.decode(&hooked_v4_msg()).await else {
2123 panic!("the decoder's chain must win over the context's");
2124 };
2125
2126 assert!(message.contains("robinhood"), "{message}");
2127 }
2128
2129 #[tokio::test]
2130 async fn test_unregistered_hook_takes_the_generic_path_on_ethereum() {
2131 let decoder = hooked_v4_decoder(Chain::Ethereum, DecoderContext::new());
2132
2133 let Err(StreamDecodeError::Fatal(message)) = decoder.decode(&hooked_v4_msg()).await else {
2134 panic!("the generic VM creator has no balance_owner attribute to work from");
2135 };
2136
2137 assert!(!message.contains("unsupported uniswap v4 hook"), "{message}");
2138 assert!(message.contains("balance_owner"), "{message}");
2139 }
2140
2141 #[tokio::test(flavor = "multi_thread")]
2142 async fn test_euler_hook_low_pool_manager_balance() {
2143 let mut decoder = TychoStreamDecoder::new(Chain::Ethereum);
2144
2145 decoder.register_decoder_with_context::<crate::evm::protocol::uniswap_v4::state::UniswapV4State>(
2146 "uniswap_v4_hooks", DecoderContext::new().vm_traces(true)
2147 );
2148
2149 let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
2150 let teth = Bytes::from_str("0xd11c452fc99cf405034ee446803b6f6c1f6d5ed8").unwrap();
2151 let tokens = HashMap::from([
2152 (
2153 weth.clone(),
2154 Token::new(&weth, "WETH", 18, 100, &[Some(100_000)], Chain::Ethereum, 100),
2155 ),
2156 (
2157 teth.clone(),
2158 Token::new(&teth, "tETH", 18, 100, &[Some(100_000)], Chain::Ethereum, 100),
2159 ),
2160 ]);
2161
2162 decoder.set_tokens(tokens.clone()).await;
2163
2164 let msg = load_test_msg("euler_hook_snapshot");
2165 let res = decoder
2166 .decode(&msg)
2167 .await
2168 .expect("decode failure");
2169
2170 let pool_state = res
2171 .states
2172 .get("0xc70d7fbd7fcccdf726e02fed78548b40dc52502b097c7a1ee7d995f4d4396134")
2173 .expect("Couldn't find target pool");
2174 let amount_out = pool_state
2175 .get_amount_out(
2176 BigUint::from_str("1000000000000000000").unwrap(),
2177 tokens.get(&teth).unwrap(),
2178 tokens.get(&weth).unwrap(),
2179 )
2180 .expect("Get amount out failed");
2181
2182 assert_eq!(amount_out.amount, BigUint::from_str("1216190190361759119").unwrap());
2183 }
2184
2185 fn pons_feed_message(snapshot: ComponentWithState) -> FeedMessage<BlockHeader> {
2187 use tycho_client::feed::synchronizer::{Snapshot, StateSyncMessage};
2188
2189 use crate::evm::protocol::uniswap_v4::pons_fixture;
2190
2191 FeedMessage {
2192 state_msgs: HashMap::from([(
2193 "uniswap_v4_hooks".to_string(),
2194 StateSyncMessage {
2195 header: pons_fixture::header(),
2196 snapshots: Snapshot {
2197 states: HashMap::from([(pons_fixture::POOL_ID.to_string(), snapshot)]),
2198 vm_storage: HashMap::new(),
2199 },
2200 deltas: None,
2201 removed_components: HashMap::new(),
2202 },
2203 )]),
2204 sync_states: HashMap::new(),
2205 }
2206 }
2207
2208 async fn pons_stream_decoder() -> TychoStreamDecoder<BlockHeader> {
2210 use crate::evm::protocol::uniswap_v4::{
2211 hooks::hook_handler_creator::initialize_hook_handlers, pons_fixture,
2212 state::UniswapV4State,
2213 };
2214
2215 initialize_hook_handlers().expect("hook handler registration should succeed");
2216 let mut decoder = TychoStreamDecoder::new(Chain::Robinhood);
2217 decoder.register_decoder::<UniswapV4State>("uniswap_v4_hooks");
2218 decoder
2219 .set_tokens(pons_fixture::tokens())
2220 .await;
2221 decoder
2222 }
2223
2224 #[tokio::test]
2228 async fn test_pons_pool_decodes_through_the_stream_decoder_on_robinhood() {
2229 use crate::evm::protocol::uniswap_v4::{
2230 hooks::pons_v2::hook_handler::PonsV2HookHandler, pons_fixture, state::UniswapV4State,
2231 };
2232
2233 let decoder = pons_stream_decoder().await;
2234
2235 let result = decoder
2236 .decode(&pons_feed_message(pons_fixture::snapshot()))
2237 .await
2238 .expect("decode failure");
2239
2240 let emitted = result
2241 .states
2242 .get(pons_fixture::POOL_ID)
2243 .expect("the Pons pool must reach consumers");
2244 let pool = emitted
2245 .as_any()
2246 .downcast_ref::<UniswapV4State>()
2247 .expect("a uniswap_v4_hooks component decodes into a UniswapV4State");
2248 let handler = pool
2249 .hook
2250 .as_ref()
2251 .expect("a Pons pool must carry a hook handler");
2252 let pons = handler
2253 .as_any()
2254 .downcast_ref::<PonsV2HookHandler>()
2255 .expect("the registry must hand back the native Pons handler");
2256 assert_eq!(u32::from(pons.hook_fee_bps()), pons_fixture::HOOK_FEE_BPS);
2257 assert_eq!(u32::from(pons.creator_tax_bps()), pons_fixture::CREATOR_TAX_BPS);
2258
2259 let core = pons_fixture::decode_on(pons_fixture::hookless_snapshot(), Chain::Robinhood)
2260 .await
2261 .expect("the same pool with no hook must decode too");
2262 let (xlg, nvda) = (pons_fixture::xlg(), pons_fixture::nvda());
2263 let amount_in = BigUint::from(10u64).pow(18);
2264 for (token_in, token_out) in [(&xlg, &nvda), (&nvda, &xlg)] {
2265 let core_out = core
2266 .get_amount_out(amount_in.clone(), token_in, token_out)
2267 .expect("the reference pool quotes one whole token")
2268 .amount;
2269 let hooked_out = emitted
2270 .get_amount_out(amount_in.clone(), token_in, token_out)
2271 .expect("the emitted pool quotes one whole token")
2272 .amount;
2273
2274 let expected = pons_fixture::net_of_hook_take(&core_out);
2275 assert!(expected < core_out, "the hook must take something out of {core_out}");
2276 assert_eq!(hooked_out, expected, "{} -> {}", token_in.symbol, token_out.symbol);
2277 }
2278 }
2279
2280 #[tokio::test]
2283 async fn test_unknown_hook_component_is_not_emitted_on_robinhood() {
2284 use crate::evm::protocol::uniswap_v4::pons_fixture;
2285
2286 let mut decoder = pons_stream_decoder().await;
2287 decoder.skip_state_decode_failures(true);
2288
2289 let result = decoder
2290 .decode(&pons_feed_message(pons_fixture::unknown_hook_snapshot()))
2291 .await
2292 .expect("a skipped decode failure is not fatal");
2293
2294 assert!(
2295 !result
2296 .states
2297 .contains_key(pons_fixture::POOL_ID),
2298 "a pool whose hook cannot be modelled must not be quoted"
2299 );
2300 assert!(result.states.is_empty());
2301 }
2302
2303 fn component_with_id(id: &str) -> ComponentWithState {
2304 use tycho_common::models::protocol::{ProtocolComponent, ProtocolComponentState};
2305
2306 ComponentWithState {
2307 state: ProtocolComponentState::new(id, HashMap::new(), HashMap::new()),
2308 component: ProtocolComponent { id: id.to_string(), ..Default::default() },
2309 component_tvl: None,
2310 entrypoints: Vec::new(),
2311 }
2312 }
2313
2314 fn rejects_a(component: &ComponentWithState) -> bool {
2315 component.component.id != "a"
2316 }
2317
2318 fn rejects_b(component: &ComponentWithState) -> bool {
2319 component.component.id != "b"
2320 }
2321
2322 #[test]
2323 fn test_admits_requires_every_registered_filter() {
2324 let mut decoder = TychoStreamDecoder::<BlockHeader>::new(Chain::Ethereum);
2327 decoder.register_filter("x", rejects_a);
2328 decoder.register_filter("x", rejects_b);
2329
2330 assert!(!decoder.admits("x", &component_with_id("a")));
2331 assert!(!decoder.admits("x", &component_with_id("b")));
2332 assert!(decoder.admits("x", &component_with_id("c")));
2333 assert!(decoder.admits("y", &component_with_id("a")));
2334 }
2335}