Skip to content

Commit e929d61

Browse files
author
TAMARA LIPOWSKI
committed
fix: error if block info not passed during delta transition
It's dangerous to allow this otherwise.
1 parent 0e9a3c3 commit e929d61

File tree

2 files changed

+64
-53
lines changed

2 files changed

+64
-53
lines changed

src/evm/protocol/uniswap_v4/state.rs

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -713,31 +713,37 @@ impl ProtocolSim for UniswapV4State {
713713
}
714714

715715
// Update block information if provided in the delta attributes
716-
if let Some(block_number_bytes) = delta
716+
let block_number_bytes = delta
717717
.updated_attributes
718718
.get("block_number")
719-
{
720-
self.block.number = u64::from_be_bytes(
721-
block_number_bytes[block_number_bytes.len() - 8..]
722-
.try_into()
723-
.map_err(|_| {
724-
TransitionError::DecodeError("Invalid block_number bytes".to_string())
725-
})?,
726-
);
727-
}
719+
.ok_or_else(|| {
720+
SimulationError::FatalError("block_number not found in updated attributes".into())
721+
})?;
728722

729-
if let Some(block_timestamp_bytes) = delta
723+
self.block.number = u64::from_be_bytes(
724+
block_number_bytes[block_number_bytes.len() - 8..]
725+
.try_into()
726+
.map_err(|_| {
727+
TransitionError::DecodeError("Invalid block_number bytes".to_string())
728+
})?,
729+
);
730+
731+
let block_timestamp_bytes = delta
730732
.updated_attributes
731733
.get("block_timestamp")
732-
{
733-
self.block.timestamp = u64::from_be_bytes(
734-
block_timestamp_bytes[block_timestamp_bytes.len() - 8..]
735-
.try_into()
736-
.map_err(|_| {
737-
TransitionError::DecodeError("Invalid block_timestamp bytes".to_string())
738-
})?,
739-
);
740-
}
734+
.ok_or_else(|| {
735+
SimulationError::FatalError(
736+
"block_timestamp not found in updated attributes".into(),
737+
)
738+
})?;
739+
740+
self.block.timestamp = u64::from_be_bytes(
741+
block_timestamp_bytes[block_timestamp_bytes.len() - 8..]
742+
.try_into()
743+
.map_err(|_| {
744+
TransitionError::DecodeError("Invalid block_timestamp bytes".to_string())
745+
})?,
746+
);
741747

742748
// Apply attribute changes
743749
if let Some(liquidity) = delta
@@ -890,7 +896,7 @@ mod tests {
890896
}
891897

892898
#[test]
893-
fn test_delta_transition_block_update() {
899+
fn test_delta_transition_missing_block_update() {
894900
let block = BlockHeader {
895901
number: 1000,
896902
hash: Bytes::from_str(
@@ -914,25 +920,19 @@ mod tests {
914920
assert_eq!(pool.block.number, 1000);
915921
assert_eq!(pool.block.timestamp, 1758201863);
916922

917-
let attributes: HashMap<String, Bytes> = [
918-
("block_number".to_string(), Bytes::from(2000_u64.to_be_bytes().to_vec())),
919-
("block_timestamp".to_string(), Bytes::from(1758201935_u64.to_be_bytes().to_vec())),
920-
]
921-
.into_iter()
922-
.collect();
923+
let attributes: HashMap<String, Bytes> =
924+
[("block_number".to_string(), Bytes::from(2000_u64.to_be_bytes().to_vec()))]
925+
.into_iter()
926+
.collect();
923927

924928
let delta = ProtocolStateDelta {
925929
component_id: "State1".to_owned(),
926930
updated_attributes: attributes,
927931
deleted_attributes: HashSet::new(),
928932
};
929933

930-
pool.delta_transition(delta, &HashMap::new(), &Balances::default())
931-
.unwrap();
932-
933-
// Verify block was updated
934-
assert_eq!(pool.block.number, 2000);
935-
assert_eq!(pool.block.timestamp, 1758201935);
934+
let result = pool.delta_transition(delta, &HashMap::new(), &Balances::default());
935+
assert!(result.is_err())
936936
}
937937

938938
#[test]
@@ -966,6 +966,8 @@ mod tests {
966966
("fee".to_string(), Bytes::from(100_u32.to_be_bytes().to_vec())),
967967
("ticks/-120/net_liquidity".to_string(), Bytes::from(10200_u64.to_be_bytes().to_vec())),
968968
("ticks/120/net_liquidity".to_string(), Bytes::from(9800_u64.to_be_bytes().to_vec())),
969+
("block_number".to_string(), Bytes::from(2000_u64.to_be_bytes().to_vec())),
970+
("block_timestamp".to_string(), Bytes::from(1758201935_u64.to_be_bytes().to_vec())),
969971
]
970972
.into_iter()
971973
.collect();
@@ -999,6 +1001,9 @@ mod tests {
9991001
.net_liquidity,
10001002
9800
10011003
);
1004+
// Verify block was updated
1005+
assert_eq!(pool.block.number, 2000);
1006+
assert_eq!(pool.block.timestamp, 1758201935);
10021007
}
10031008

10041009
#[tokio::test]

src/evm/protocol/vm/state.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -657,31 +657,37 @@ where
657657
balances: &Balances,
658658
) -> Result<(), TransitionError<String>> {
659659
// Update block information if provided in the delta attributes
660-
if let Some(block_number_bytes) = delta
660+
let block_number_bytes = delta
661661
.updated_attributes
662662
.get("block_number")
663-
{
664-
self.block.number = u64::from_be_bytes(
665-
block_number_bytes[block_number_bytes.len() - 8..]
666-
.try_into()
667-
.map_err(|_| {
668-
TransitionError::DecodeError("Invalid block_number bytes".to_string())
669-
})?,
670-
);
671-
}
663+
.ok_or_else(|| {
664+
SimulationError::FatalError("block_number not found in updated attributes".into())
665+
})?;
666+
667+
self.block.number = u64::from_be_bytes(
668+
block_number_bytes[block_number_bytes.len() - 8..]
669+
.try_into()
670+
.map_err(|_| {
671+
TransitionError::DecodeError("Invalid block_number bytes".to_string())
672+
})?,
673+
);
672674

673-
if let Some(block_timestamp_bytes) = delta
675+
let block_timestamp_bytes = delta
674676
.updated_attributes
675677
.get("block_timestamp")
676-
{
677-
self.block.timestamp = u64::from_be_bytes(
678-
block_timestamp_bytes[block_timestamp_bytes.len() - 8..]
679-
.try_into()
680-
.map_err(|_| {
681-
TransitionError::DecodeError("Invalid block_timestamp bytes".to_string())
682-
})?,
683-
);
684-
}
678+
.ok_or_else(|| {
679+
SimulationError::FatalError(
680+
"block_timestamp not found in updated attributes".into(),
681+
)
682+
})?;
683+
684+
self.block.timestamp = u64::from_be_bytes(
685+
block_timestamp_bytes[block_timestamp_bytes.len() - 8..]
686+
.try_into()
687+
.map_err(|_| {
688+
TransitionError::DecodeError("Invalid block_timestamp bytes".to_string())
689+
})?,
690+
);
685691

686692
if self.manual_updates {
687693
// Directly check for "update_marker" in `updated_attributes`

0 commit comments

Comments
 (0)