From 1e387abdea2f487158723b51a86a54c9d68c5e63 Mon Sep 17 00:00:00 2001 From: Michal Nazarewicz Date: Tue, 23 Apr 2024 23:06:36 +0200 Subject: [PATCH] Avoid unnecessary String allocations (#477) --- contracts/pallet-ibc/primitives/src/lib.rs | 8 +- contracts/pallet-ibc/rpc/src/events.rs | 12 +-- .../pallet-ibc/src/benchmarks/benchmarking.rs | 8 +- contracts/pallet-ibc/src/channel.rs | 10 +-- contracts/pallet-ibc/src/events.rs | 54 ++++++------- contracts/pallet-ibc/src/ics20/mod.rs | 40 ++++------ contracts/pallet-ibc/src/ics23/channels.rs | 2 +- contracts/pallet-ibc/src/impls.rs | 2 +- contracts/pallet-ibc/src/lib.rs | 12 +-- contracts/pallet-ibc/src/tests.rs | 14 ++-- .../ics07-tendermint-cw/src/client.rs | 6 +- .../ics07-tendermint-cw/src/context.rs | 4 +- .../ics07-tendermint-cw/src/contract.rs | 4 +- .../ics07-tendermint-cw/src/helpers.rs | 51 +++++------- .../src/ics23/consensus_states.rs | 2 +- .../src/ics23/processed_states.rs | 78 ++++++++----------- .../ics07-tendermint-cw/src/state.rs | 4 +- light-clients/ics08-wasm/src/lib.rs | 4 +- .../ics10-grandpa-cw/src/contract.rs | 18 +++-- .../src/ics23/consensus_states.rs | 2 +- light-clients/ics10-grandpa-cw/src/state.rs | 4 +- 21 files changed, 155 insertions(+), 184 deletions(-) diff --git a/contracts/pallet-ibc/primitives/src/lib.rs b/contracts/pallet-ibc/primitives/src/lib.rs index fd28311f9..9fb353496 100644 --- a/contracts/pallet-ibc/primitives/src/lib.rs +++ b/contracts/pallet-ibc/primitives/src/lib.rs @@ -140,10 +140,10 @@ impl From for PacketInfo { fn from(packet: Packet) -> Self { Self { sequence: packet.sequence.into(), - source_port: packet.source_port.to_string().as_bytes().to_vec(), - source_channel: packet.source_channel.to_string().as_bytes().to_vec(), - destination_port: packet.destination_port.to_string().as_bytes().to_vec(), - destination_channel: packet.destination_channel.to_string().as_bytes().to_vec(), + source_port: packet.source_port.to_string().into_bytes(), + source_channel: packet.source_channel.to_string().into_bytes(), + destination_port: packet.destination_port.to_string().into_bytes(), + destination_channel: packet.destination_channel.to_string().into_bytes(), data: packet.data, timeout_height: ( packet.timeout_height.revision_number, diff --git a/contracts/pallet-ibc/rpc/src/events.rs b/contracts/pallet-ibc/rpc/src/events.rs index 53644a5b2..4878056d9 100644 --- a/contracts/pallet-ibc/rpc/src/events.rs +++ b/contracts/pallet-ibc/rpc/src/events.rs @@ -27,7 +27,7 @@ where let packets: Vec = api .query_send_packet_info( at, - channel_id.to_string().as_bytes().to_vec(), + channel_id.to_string().into_bytes(), port_id.as_bytes().to_vec(), vec![sequence], ) @@ -45,7 +45,7 @@ where let packets: Vec = api .query_recv_packet_info( at, - channel_id.to_string().as_bytes().to_vec(), + channel_id.to_string().into_bytes(), port_id.as_bytes().to_vec(), vec![sequence], ) @@ -63,7 +63,7 @@ where let packets: Vec = api .query_recv_packet_info( at, - channel_id.to_string().as_bytes().to_vec(), + channel_id.to_string().into_bytes(), port_id.as_bytes().to_vec(), vec![sequence], ) @@ -81,7 +81,7 @@ where let packets: Vec = api .query_send_packet_info( at, - channel_id.to_string().as_bytes().to_vec(), + channel_id.to_string().into_bytes(), port_id.as_bytes().to_vec(), vec![sequence], ) @@ -99,7 +99,7 @@ where let packets: Vec = api .query_send_packet_info( at, - channel_id.to_string().as_bytes().to_vec(), + channel_id.to_string().into_bytes(), port_id.as_bytes().to_vec(), vec![sequence], ) @@ -117,7 +117,7 @@ where let packets: Vec = api .query_send_packet_info( at, - channel_id.to_string().as_bytes().to_vec(), + channel_id.to_string().into_bytes(), port_id.as_bytes().to_vec(), vec![sequence], ) diff --git a/contracts/pallet-ibc/src/benchmarks/benchmarking.rs b/contracts/pallet-ibc/src/benchmarks/benchmarking.rs index 1df24f1cd..e276ced45 100644 --- a/contracts/pallet-ibc/src/benchmarks/benchmarking.rs +++ b/contracts/pallet-ibc/src/benchmarks/benchmarking.rs @@ -915,7 +915,7 @@ benchmarks! { let timeout = Timeout::Offset { timestamp: Some(1690894363), height: Some(2000) }; let transfer_params = TransferParams { - to: MultiAddress::Raw("bob".to_string().as_bytes().to_vec()), + to: MultiAddress::Raw("bob".to_string().into_bytes()), source_channel: channel_id.sequence(), timeout, }; @@ -988,7 +988,7 @@ benchmarks! { let mut output = HandlerOutputBuilder::new(); let port_id = PortId::transfer(); let channel_id = ChannelId::new(0); - let channel_ids = vec![channel_id.to_string().as_bytes().to_vec()]; + let channel_ids = vec![channel_id.to_string().into_bytes()]; ChannelIds::::put(channel_ids); let mut handler = IbcModule::::default(); }:{ @@ -1003,7 +1003,7 @@ benchmarks! { let mut output = HandlerOutputBuilder::new(); let port_id = PortId::transfer(); let channel_id = ChannelId::new(0); - let channel_ids = vec![channel_id.to_string().as_bytes().to_vec()]; + let channel_ids = vec![channel_id.to_string().into_bytes()]; ChannelIds::::put(channel_ids); let mut handler = IbcModule::::default(); }:{ @@ -1351,7 +1351,7 @@ benchmarks! { } } - let channel_id_bytes = channel_id.to_string().as_bytes().to_vec(); + let channel_id_bytes = channel_id.to_string().into_bytes(); let port_id_bytes = port_id.as_bytes().to_vec(); let (send_seq_set, _) = diff --git a/contracts/pallet-ibc/src/channel.rs b/contracts/pallet-ibc/src/channel.rs index 3a80d9325..136893082 100644 --- a/contracts/pallet-ibc/src/channel.rs +++ b/contracts/pallet-ibc/src/channel.rs @@ -285,7 +285,7 @@ where packet: ibc::core::ics04_channel::packet::Packet, ) -> Result<(), ICS04Error> { // store packet offchain - let channel_id = key.1.to_string().as_bytes().to_vec(); + let channel_id = key.1.to_string().into_bytes(); let port_id = key.0.as_bytes().to_vec(); let seq = u64::from(key.2); let channel_end = ChannelReader::channel_end(self, &(key.0, key.1))?; @@ -306,7 +306,7 @@ where packet: ibc::core::ics04_channel::packet::Packet, ) -> Result<(), ICS04Error> { // Store packet offchain - let channel_id = key.1.to_string().as_bytes().to_vec(); + let channel_id = key.1.to_string().into_bytes(); let port_id = key.0.as_bytes().to_vec(); let seq = u64::from(key.2); let channel_end = ChannelReader::channel_end(self, &(key.0, key.1))?; @@ -396,10 +396,8 @@ where ) -> Result<(), ICS04Error> { let conn_id = conn_id.as_bytes().to_vec(); - let port_channel_id = ( - port_channel_id.0.as_bytes().to_vec(), - port_channel_id.1.to_string().as_bytes().to_vec(), - ); + let port_channel_id = + (port_channel_id.0.as_bytes().to_vec(), port_channel_id.1.to_string().into_bytes()); if >::contains_key(conn_id.clone()) { log::trace!(target: "pallet_ibc", "in channel: [store_connection_channels] >> insert port_channel_id"); diff --git a/contracts/pallet-ibc/src/events.rs b/contracts/pallet-ibc/src/events.rs index 8071b78dd..55948aca6 100644 --- a/contracts/pallet-ibc/src/events.rs +++ b/contracts/pallet-ibc/src/events.rs @@ -311,124 +311,114 @@ impl From for IbcEvent { RawIbcEvent::OpenInitChannel(ev) => IbcEvent::OpenInitChannel { revision_height: ev.height().revision_height, revision_number: ev.height().revision_number, - channel_id: ev - .channel_id() - .map(|channel_id| channel_id.to_string().as_bytes().to_vec()), + channel_id: ev.channel_id().map(|channel_id| channel_id.to_string().into_bytes()), port_id: ev.port_id().as_bytes().to_vec(), connection_id: ev.connection_id.as_bytes().to_vec(), counterparty_port_id: ev.counterparty_port_id.as_bytes().to_vec(), counterparty_channel_id: ev .counterparty_channel_id - .map(|val| val.to_string().as_bytes().to_vec()), + .map(|val| val.to_string().into_bytes()), }, RawIbcEvent::OpenTryChannel(ev) => IbcEvent::OpenTryChannel { revision_height: ev.height().revision_height, revision_number: ev.height().revision_number, - channel_id: ev - .channel_id() - .map(|channel_id| channel_id.to_string().as_bytes().to_vec()), + channel_id: ev.channel_id().map(|channel_id| channel_id.to_string().into_bytes()), port_id: ev.port_id().as_bytes().to_vec(), connection_id: ev.connection_id.as_bytes().to_vec(), counterparty_port_id: ev.counterparty_port_id.as_bytes().to_vec(), counterparty_channel_id: ev .counterparty_channel_id - .map(|val| val.to_string().as_bytes().to_vec()), + .map(|val| val.to_string().into_bytes()), }, RawIbcEvent::OpenAckChannel(ev) => IbcEvent::OpenAckChannel { revision_height: ev.height().revision_height, revision_number: ev.height().revision_number, - channel_id: ev - .channel_id() - .map(|channel_id| channel_id.to_string().as_bytes().to_vec()), + channel_id: ev.channel_id().map(|channel_id| channel_id.to_string().into_bytes()), port_id: ev.port_id().as_bytes().to_vec(), connection_id: ev.connection_id.as_bytes().to_vec(), counterparty_port_id: ev.counterparty_port_id.as_bytes().to_vec(), counterparty_channel_id: ev .counterparty_channel_id - .map(|val| val.to_string().as_bytes().to_vec()), + .map(|val| val.to_string().into_bytes()), }, RawIbcEvent::OpenConfirmChannel(ev) => IbcEvent::OpenConfirmChannel { revision_height: ev.height().revision_height, revision_number: ev.height().revision_number, - channel_id: ev - .channel_id() - .map(|channel_id| channel_id.to_string().as_bytes().to_vec()), + channel_id: ev.channel_id().map(|channel_id| channel_id.to_string().into_bytes()), port_id: ev.port_id().as_bytes().to_vec(), connection_id: ev.connection_id.as_bytes().to_vec(), counterparty_port_id: ev.counterparty_port_id.as_bytes().to_vec(), counterparty_channel_id: ev .counterparty_channel_id - .map(|val| val.to_string().as_bytes().to_vec()), + .map(|val| val.to_string().into_bytes()), }, RawIbcEvent::CloseInitChannel(ev) => IbcEvent::CloseInitChannel { revision_height: ev.height().revision_height, revision_number: ev.height().revision_number, - channel_id: ev.channel_id().to_string().as_bytes().to_vec(), + channel_id: ev.channel_id().to_string().into_bytes(), port_id: ev.port_id().as_bytes().to_vec(), connection_id: ev.connection_id.as_bytes().to_vec(), counterparty_port_id: ev.counterparty_port_id.as_bytes().to_vec(), counterparty_channel_id: ev .counterparty_channel_id - .map(|val| val.to_string().as_bytes().to_vec()), + .map(|val| val.to_string().into_bytes()), }, RawIbcEvent::CloseConfirmChannel(ev) => IbcEvent::CloseConfirmChannel { revision_height: ev.height().revision_height, revision_number: ev.height().revision_number, port_id: ev.port_id.as_bytes().to_vec(), - channel_id: ev - .channel_id() - .map(|channel_id| channel_id.to_string().as_bytes().to_vec()), + channel_id: ev.channel_id().map(|channel_id| channel_id.to_string().into_bytes()), connection_id: ev.connection_id.as_bytes().to_vec(), counterparty_port_id: ev.counterparty_port_id.as_bytes().to_vec(), counterparty_channel_id: ev .counterparty_channel_id - .map(|val| val.to_string().as_bytes().to_vec()), + .map(|val| val.to_string().into_bytes()), }, RawIbcEvent::SendPacket(ev) => IbcEvent::SendPacket { revision_height: ev.height().revision_height, revision_number: ev.height().revision_number, - channel_id: ev.src_channel_id().to_string().as_bytes().to_vec(), + channel_id: ev.src_channel_id().to_string().into_bytes(), port_id: ev.src_port_id().as_bytes().to_vec(), dest_port: ev.dst_port_id().as_bytes().to_vec(), - dest_channel: ev.dst_channel_id().to_string().as_bytes().to_vec(), + dest_channel: ev.dst_channel_id().to_string().into_bytes(), sequence: ev.packet.sequence.into(), }, RawIbcEvent::ReceivePacket(ev) => IbcEvent::ReceivePacket { revision_height: ev.height().revision_height, revision_number: ev.height().revision_number, - channel_id: ev.src_channel_id().to_string().as_bytes().to_vec(), + channel_id: ev.src_channel_id().to_string().into_bytes(), port_id: ev.src_port_id().as_bytes().to_vec(), dest_port: ev.dst_port_id().as_bytes().to_vec(), - dest_channel: ev.dst_channel_id().to_string().as_bytes().to_vec(), + dest_channel: ev.dst_channel_id().to_string().into_bytes(), sequence: ev.packet.sequence.into(), }, RawIbcEvent::WriteAcknowledgement(ev) => IbcEvent::WriteAcknowledgement { revision_height: ev.height().revision_height, revision_number: ev.height().revision_number, - channel_id: ev.src_channel_id().to_string().as_bytes().to_vec(), + channel_id: ev.src_channel_id().to_string().into_bytes(), port_id: ev.src_port_id().as_bytes().to_vec(), dest_port: ev.dst_port_id().as_bytes().to_vec(), - dest_channel: ev.dst_channel_id().to_string().as_bytes().to_vec(), + dest_channel: ev.dst_channel_id().to_string().into_bytes(), sequence: ev.packet.sequence.into(), }, RawIbcEvent::AcknowledgePacket(ev) => IbcEvent::AcknowledgePacket { revision_height: ev.height().revision_height, revision_number: ev.height().revision_number, - channel_id: ev.src_channel_id().to_string().as_bytes().to_vec(), + channel_id: ev.src_channel_id().to_string().into_bytes(), port_id: ev.src_port_id().as_bytes().to_vec(), sequence: ev.packet.sequence.into(), }, RawIbcEvent::TimeoutPacket(ev) => IbcEvent::TimeoutPacket { revision_height: ev.height().revision_height, revision_number: ev.height().revision_number, - channel_id: ev.src_channel_id().to_string().as_bytes().to_vec(), + channel_id: ev.src_channel_id().to_string().into_bytes(), port_id: ev.src_port_id().as_bytes().to_vec(), sequence: ev.packet.sequence.into(), }, RawIbcEvent::TimeoutOnClosePacket(ev) => IbcEvent::TimeoutOnClosePacket { revision_height: ev.height().revision_height, revision_number: ev.height().revision_number, - channel_id: ev.src_channel_id().to_string().as_bytes().to_vec(), + channel_id: ev.src_channel_id().to_string().into_bytes(), port_id: ev.src_port_id().as_bytes().to_vec(), sequence: ev.packet.sequence.into(), }, @@ -436,7 +426,7 @@ impl From for IbcEvent { RawIbcEvent::ChainError(_) => IbcEvent::ChainError, RawIbcEvent::AppModule(ev) => IbcEvent::AppModule { kind: ev.kind.as_bytes().to_vec(), - module_id: ev.module_name.to_string().as_bytes().to_vec(), + module_id: ev.module_name.to_string().into_bytes(), }, RawIbcEvent::PushWasmCode(ev) => { let wasm_checksum = ev.0; diff --git a/contracts/pallet-ibc/src/ics20/mod.rs b/contracts/pallet-ibc/src/ics20/mod.rs index c46a0d4f2..89a407e51 100644 --- a/contracts/pallet-ibc/src/ics20/mod.rs +++ b/contracts/pallet-ibc/src/ics20/mod.rs @@ -127,7 +127,7 @@ where _relayer: &Signer, ) -> Result<(), Ics04Error> { let _ = ChannelIds::::try_mutate::<_, (), _>(|channels| { - channels.push(channel_id.to_string().as_bytes().to_vec()); + channels.push(channel_id.to_string().into_bytes()); Ok(()) }); let mut ctx = Context::::default(); @@ -144,7 +144,7 @@ where _relayer: &Signer, ) -> Result<(), Ics04Error> { let _ = ChannelIds::::try_mutate::<_, (), _>(|channels| { - channels.push(channel_id.to_string().as_bytes().to_vec()); + channels.push(channel_id.to_string().into_bytes()); Ok(()) }); let mut ctx = Context::::default(); @@ -161,11 +161,8 @@ where _relayer: &Signer, ) -> Result<(), Ics04Error> { let _ = ChannelIds::::try_mutate::<_, (), _>(|channels| { - let rem = channels - .iter() - .filter(|chan| chan.as_slice() != channel_id.to_string().as_bytes()) - .cloned() - .collect(); + let channel_id = channel_id.to_string().into_bytes(); + let rem = channels.iter().filter(|chan| **chan != channel_id).cloned().collect(); *channels = rem; Ok(()) }); @@ -185,11 +182,8 @@ where _relayer: &Signer, ) -> Result<(), Ics04Error> { let _ = ChannelIds::::try_mutate::<_, (), _>(|channels| { - let rem = channels - .iter() - .filter(|chan| chan.as_slice() != channel_id.to_string().as_bytes()) - .cloned() - .collect(); + let channel_id = channel_id.to_string().into_bytes(); + let rem = channels.iter().filter(|chan| **chan != channel_id).cloned().collect(); *channels = rem; Ok(()) }); @@ -270,8 +264,8 @@ where packet.source_channel, &prefixed_denom, ), - source_channel: packet.source_channel.to_string().as_bytes().to_vec(), - destination_channel: packet.destination_channel.to_string().as_bytes().to_vec(), + source_channel: packet.source_channel.to_string().into_bytes(), + destination_channel: packet.destination_channel.to_string().into_bytes(), }); let packet = packet.clone(); Pallet::::handle_message(HandlerMessage::WriteAck { @@ -318,7 +312,7 @@ where Pallet::::deposit_event(Event::::TokenTransferCompleted { from: packet_data.sender, to: packet_data.receiver, - ibc_denom: packet_data.token.denom.to_string().as_bytes().to_vec(), + ibc_denom: packet_data.token.denom.to_string().into_bytes(), local_asset_id: T::IbcDenomToAssetIdConversion::from_denom_to_asset_id( &packet_data.token.denom.to_string(), ) @@ -329,8 +323,8 @@ where packet.source_channel, &packet_data.token.denom, ), - source_channel: packet.source_channel.to_string().as_bytes().to_vec(), - destination_channel: packet.destination_channel.to_string().as_bytes().to_vec(), + source_channel: packet.source_channel.to_string().into_bytes(), + destination_channel: packet.destination_channel.to_string().into_bytes(), }) }, Err(e) => { @@ -346,7 +340,7 @@ where Pallet::::deposit_event(Event::::TokenTransferFailed { from: packet_data.sender, to: packet_data.receiver, - ibc_denom: packet_data.token.denom.to_string().as_bytes().to_vec(), + ibc_denom: packet_data.token.denom.to_string().into_bytes(), local_asset_id: T::IbcDenomToAssetIdConversion::from_denom_to_asset_id( &packet_data.token.denom.to_string(), ) @@ -357,8 +351,8 @@ where packet.source_channel, &packet_data.token.denom, ), - source_channel: packet.source_channel.to_string().as_bytes().to_vec(), - destination_channel: packet.destination_channel.to_string().as_bytes().to_vec(), + source_channel: packet.source_channel.to_string().into_bytes(), + destination_channel: packet.destination_channel.to_string().into_bytes(), }) }, } @@ -385,7 +379,7 @@ where Pallet::::deposit_event(Event::::TokenTransferTimeout { from: packet_data.sender, to: packet_data.receiver, - ibc_denom: packet_data.token.denom.to_string().as_bytes().to_vec(), + ibc_denom: packet_data.token.denom.to_string().into_bytes(), local_asset_id: T::IbcDenomToAssetIdConversion::from_denom_to_asset_id( &packet_data.token.denom.to_string(), ) @@ -396,8 +390,8 @@ where packet.source_channel, &packet_data.token.denom, ), - source_channel: packet.source_channel.to_string().as_bytes().to_vec(), - destination_channel: packet.destination_channel.to_string().as_bytes().to_vec(), + source_channel: packet.source_channel.to_string().into_bytes(), + destination_channel: packet.destination_channel.to_string().into_bytes(), }); Ok(()) } diff --git a/contracts/pallet-ibc/src/ics23/channels.rs b/contracts/pallet-ibc/src/ics23/channels.rs index fc0e56c6a..b457aa17a 100644 --- a/contracts/pallet-ibc/src/ics23/channels.rs +++ b/contracts/pallet-ibc/src/ics23/channels.rs @@ -48,7 +48,7 @@ impl Channels { { return Some(( port_id.as_bytes().to_vec(), - channel_id.to_string().as_bytes().to_vec(), + channel_id.to_string().into_bytes(), value, )) } diff --git a/contracts/pallet-ibc/src/impls.rs b/contracts/pallet-ibc/src/impls.rs index 5106b40a6..2e5eb4f25 100644 --- a/contracts/pallet-ibc/src/impls.rs +++ b/contracts/pallet-ibc/src/impls.rs @@ -507,7 +507,7 @@ where key: (PortId, ChannelId, Sequence), ack: Vec, ) -> Result<(), Error> { - let channel_id = key.1.to_string().as_bytes().to_vec(); + let channel_id = key.1.to_string().into_bytes(); let port_id = key.0.as_bytes().to_vec(); let seq = u64::from(key.2); diff --git a/contracts/pallet-ibc/src/lib.rs b/contracts/pallet-ibc/src/lib.rs index 25562ae9f..028b6b95d 100644 --- a/contracts/pallet-ibc/src/lib.rs +++ b/contracts/pallet-ibc/src/lib.rs @@ -961,9 +961,9 @@ pub mod pallet { &coin.denom.to_string(), ) .ok(), - ibc_denom: coin.denom.to_string().as_bytes().to_vec(), - source_channel: source_channel.to_string().as_bytes().to_vec(), - destination_channel: destination_channel.to_string().as_bytes().to_vec(), + ibc_denom: coin.denom.to_string().into_bytes(), + source_channel: source_channel.to_string().into_bytes(), + destination_channel: destination_channel.to_string().into_bytes(), }); }; @@ -1047,10 +1047,10 @@ pub mod pallet { &coin.denom.to_string(), ) .ok(), - ibc_denom: coin.denom.to_string().as_bytes().to_vec(), + ibc_denom: coin.denom.to_string().into_bytes(), is_sender_source, - source_channel: source_channel.to_string().as_bytes().to_vec(), - destination_channel: destination_channel.to_string().as_bytes().to_vec(), + source_channel: source_channel.to_string().into_bytes(), + destination_channel: destination_channel.to_string().into_bytes(), }); Ok(()) } diff --git a/contracts/pallet-ibc/src/tests.rs b/contracts/pallet-ibc/src/tests.rs index 06e5db256..fcc25439f 100644 --- a/contracts/pallet-ibc/src/tests.rs +++ b/contracts/pallet-ibc/src/tests.rs @@ -254,7 +254,7 @@ fn send_transfer() { let channel_id = ChannelId::new(0); let port_id = PortId::transfer(); let packet_info = Pallet::::get_send_packet_info( - channel_id.to_string().as_bytes().to_vec(), + channel_id.to_string().into_bytes(), port_id.as_bytes().to_vec(), vec![1], ) @@ -384,7 +384,7 @@ fn send_transfer_no_fee_feeless_channels() { let channel_id = ChannelId::new(0); let port_id = PortId::transfer(); let packet_info = Pallet::::get_send_packet_info( - channel_id.to_string().as_bytes().to_vec(), + channel_id.to_string().into_bytes(), port_id.as_bytes().to_vec(), vec![1], ) @@ -782,7 +782,7 @@ fn on_ack_transfer_with_custom_success_result() { )); let packet_info = Ibc::get_send_packet_info( - channel_id.to_string().as_bytes().to_vec(), + channel_id.to_string().into_bytes(), PortId::transfer().as_bytes().to_vec(), vec![1], ) @@ -1087,7 +1087,7 @@ fn should_fetch_recv_packet_with_acknowledgement() { let channel_id = ChannelId::new(0); let port_id = PortId::transfer(); let packet_info = Pallet::::get_recv_packet_info( - channel_id.to_string().as_bytes().to_vec(), + channel_id.to_string().into_bytes(), port_id.as_bytes().to_vec(), vec![1], ) @@ -1174,7 +1174,7 @@ fn should_cleanup_offchain_packets_correctly() { }); ext.execute_with(|| { - let channel_id_bytes = channel_id.to_string().as_bytes().to_vec(); + let channel_id_bytes = channel_id.to_string().into_bytes(); let port_id_bytes = port_id.as_bytes().to_vec(); let (send_seq_set, _) = @@ -1193,7 +1193,7 @@ fn should_cleanup_offchain_packets_correctly() { }); ext.execute_with(|| { - let channel_id_bytes = channel_id.to_string().as_bytes().to_vec(); + let channel_id_bytes = channel_id.to_string().into_bytes(); let port_id_bytes = port_id.as_bytes().to_vec(); let (send_seq_set, last_removed_send) = @@ -1225,7 +1225,7 @@ fn should_cleanup_offchain_packets_correctly() { }); ext.execute_with(|| { - let channel_id_bytes = channel_id.to_string().as_bytes().to_vec(); + let channel_id_bytes = channel_id.to_string().into_bytes(); let port_id_bytes = port_id.as_bytes().to_vec(); let (send_seq_set, last_removed_send) = diff --git a/light-clients/ics07-tendermint-cw/src/client.rs b/light-clients/ics07-tendermint-cw/src/client.rs index c5d192107..4dde0d591 100644 --- a/light-clients/ics07-tendermint-cw/src/client.rs +++ b/light-clients/ics07-tendermint-cw/src/client.rs @@ -239,7 +239,7 @@ impl<'a, H: HostFunctionsProvider + 'static> ClientKeeper for Context<'a, H> { timestamp: Timestamp, ) -> Result<(), Error> { let mut processed_state = ProcessedStates::new(self.storage_mut()); - processed_state.set_processed_time(height, timestamp.nanoseconds(), &mut Vec::new()); + processed_state.set_processed_time(height, timestamp.nanoseconds(), ""); Ok(()) } @@ -251,8 +251,8 @@ impl<'a, H: HostFunctionsProvider + 'static> ClientKeeper for Context<'a, H> { host_height: Height, ) -> Result<(), Error> { let mut processed_state = ProcessedStates::new(self.storage_mut()); - processed_state.set_processed_height(height, host_height.revision_height, &mut Vec::new()); - processed_state.set_iteration_key(height, &mut Vec::new()); + processed_state.set_processed_height(height, host_height.revision_height, ""); + processed_state.set_iteration_key(height, ""); Ok(()) } diff --git a/light-clients/ics07-tendermint-cw/src/context.rs b/light-clients/ics07-tendermint-cw/src/context.rs index 7fecd9ba1..4e53bfb56 100644 --- a/light-clients/ics07-tendermint-cw/src/context.rs +++ b/light-clients/ics07-tendermint-cw/src/context.rs @@ -103,7 +103,7 @@ where { pub fn processed_timestamp(&self, height: Height) -> Result { let processed_state = ReadonlyProcessedStates::new(self.storage()); - match processed_state.get_processed_time(height, &mut Vec::new()) { + match processed_state.get_processed_time(height, "") { Some(time) => Ok(time), None => Err(Error::implementation_specific( "problem getting processed timestamp".to_string(), @@ -113,7 +113,7 @@ where pub fn processed_height(&self, height: Height) -> Result { let processed_state = ReadonlyProcessedStates::new(self.storage()); - match processed_state.get_processed_height(height, &mut Vec::new()) { + match processed_state.get_processed_height(height, "") { Some(p_height) => Ok(p_height), None => Err(Error::implementation_specific("problem getting processed height".to_string())), diff --git a/light-clients/ics07-tendermint-cw/src/contract.rs b/light-clients/ics07-tendermint-cw/src/contract.rs index c57cef78e..0a3beda82 100644 --- a/light-clients/ics07-tendermint-cw/src/contract.rs +++ b/light-clients/ics07-tendermint-cw/src/contract.rs @@ -238,8 +238,8 @@ fn process_message( .map_err(|e| ContractError::Tendermint(e.to_string())) .and_then(|(cs, cu)| { let height = cs.latest_height(); - ctx.store_consensus_state_prefixed(height, cu, SUBJECT_PREFIX); - ctx.store_client_state_prefixed(cs, SUBJECT_PREFIX, client_id) + ctx.store_consensus_state_prefixed(height, cu, SUBJECT_PREFIX.as_bytes()); + ctx.store_client_state_prefixed(cs, SUBJECT_PREFIX.as_bytes(), client_id) .map_err(|e| ContractError::Tendermint(e.to_string()))?; Ok(to_binary(&ContractResult::success())) }), diff --git a/light-clients/ics07-tendermint-cw/src/helpers.rs b/light-clients/ics07-tendermint-cw/src/helpers.rs index ae353146a..377d9c413 100644 --- a/light-clients/ics07-tendermint-cw/src/helpers.rs +++ b/light-clients/ics07-tendermint-cw/src/helpers.rs @@ -171,12 +171,14 @@ pub fn verify_upgrade_and_update_state( pub fn check_substitute_and_update_state( ctx: &mut Context, ) -> Result<(ClientState, ConsensusState), Ics02Error> { - let mut subject_client_state = ctx.client_state_prefixed(SUBJECT_PREFIX).map_err(|_| { - Ics02Error::implementation_specific("subject client state not found".to_string()) - })?; - let substitute_client_state = ctx.client_state_prefixed(SUBSTITUTE_PREFIX).map_err(|_| { - Ics02Error::implementation_specific("substitute client state not found".to_string()) - })?; + let mut subject_client_state = + ctx.client_state_prefixed(SUBJECT_PREFIX.as_bytes()).map_err(|_| { + Ics02Error::implementation_specific("subject client state not found".to_string()) + })?; + let substitute_client_state = + ctx.client_state_prefixed(SUBSTITUTE_PREFIX.as_bytes()).map_err(|_| { + Ics02Error::implementation_specific("substitute client state not found".to_string()) + })?; if subject_client_state.trust_level != substitute_client_state.trust_level || subject_client_state.unbonding_period != substitute_client_state.unbonding_period || @@ -188,32 +190,22 @@ pub fn check_substitute_and_update_state( } let height = substitute_client_state.latest_height(); - let substitute_consensus_state = - ctx.consensus_state_prefixed(height, SUBSTITUTE_PREFIX).map_err(|_| { + let substitute_consensus_state = ctx + .consensus_state_prefixed(height, SUBSTITUTE_PREFIX.as_bytes()) + .map_err(|_| { Ics02Error::implementation_specific("substitute consensus state not found".to_string()) })?; let mut process_states = ProcessedStates::new(ctx.storage_mut()); - let substitute_processed_time = process_states - .get_processed_time(height, &mut SUBSTITUTE_PREFIX.to_vec()) - .unwrap(); - let substitute_processed_height = process_states - .get_processed_height(height, &mut SUBSTITUTE_PREFIX.to_vec()) - .unwrap(); - let substitute_iteration_key = process_states - .get_iteration_key(height, &mut SUBSTITUTE_PREFIX.to_vec()) - .unwrap(); - process_states.set_processed_time( - height, - substitute_processed_time, - &mut SUBJECT_PREFIX.to_vec(), - ); - process_states.set_processed_height( - height, - substitute_processed_height, - &mut SUBJECT_PREFIX.to_vec(), - ); - process_states.set_iteration_key(substitute_iteration_key, &mut SUBJECT_PREFIX.to_vec()); + let substitute_processed_time = + process_states.get_processed_time(height, SUBSTITUTE_PREFIX).unwrap(); + let substitute_processed_height = + process_states.get_processed_height(height, SUBSTITUTE_PREFIX).unwrap(); + let substitute_iteration_key = + process_states.get_iteration_key(height, SUBSTITUTE_PREFIX).unwrap(); + process_states.set_processed_time(height, substitute_processed_time, SUBJECT_PREFIX); + process_states.set_processed_height(height, substitute_processed_height, SUBJECT_PREFIX); + process_states.set_iteration_key(substitute_iteration_key, SUBJECT_PREFIX); subject_client_state.latest_height = substitute_client_state.latest_height; subject_client_state.chain_id = substitute_client_state.chain_id; @@ -231,8 +223,7 @@ pub fn prune_oldest_consensus_state( let mut processed_states = ProcessedStates::new(ctx.storage_mut()); if let Some(earliest_height) = processed_states.get_earliest_height(client_state.latest_height) { - let processed_time = - processed_states.get_processed_time(earliest_height, &mut Vec::new()).unwrap(); + let processed_time = processed_states.get_processed_time(earliest_height, "").unwrap(); let elapsed = Duration::from_nanos(current_time - processed_time); if client_state.expired(elapsed) { processed_states.remove_states_at_height(earliest_height); diff --git a/light-clients/ics07-tendermint-cw/src/ics23/consensus_states.rs b/light-clients/ics07-tendermint-cw/src/ics23/consensus_states.rs index fa2f0a78c..8137f0db3 100644 --- a/light-clients/ics07-tendermint-cw/src/ics23/consensus_states.rs +++ b/light-clients/ics07-tendermint-cw/src/ics23/consensus_states.rs @@ -50,7 +50,7 @@ impl<'a> ConsensusStates<'a> { } pub fn consensus_state_client_key() -> Vec { - "consensusStates/".to_string().into_bytes() + b"consensusStates/".to_vec() } pub fn consensus_state_height_key(height: Height) -> Vec { diff --git a/light-clients/ics07-tendermint-cw/src/ics23/processed_states.rs b/light-clients/ics07-tendermint-cw/src/ics23/processed_states.rs index 0474c4925..9a5b47409 100644 --- a/light-clients/ics07-tendermint-cw/src/ics23/processed_states.rs +++ b/light-clients/ics07-tendermint-cw/src/ics23/processed_states.rs @@ -25,59 +25,51 @@ impl<'a> ProcessedStates<'a> { ProcessedStates(storage) } - pub fn processed_time_key(height: Height, prefix: &mut Vec) -> Vec { - prefix.append(&mut "consensusStates/".to_string().into_bytes()); - prefix.append(&mut format!("{height}").into_bytes()); - prefix.append(&mut "/processedTime".to_string().into_bytes()); - prefix.clone() + pub fn processed_time_key(height: Height, prefix: &str) -> Vec { + format!("{prefix}consensusStates/{height}/processedTime").into_bytes() } - pub fn processed_height_key(height: Height, prefix: &mut Vec) -> Vec { - prefix.append(&mut "consensusStates/".to_string().into_bytes()); - prefix.append(&mut format!("{height}").into_bytes()); - prefix.append(&mut "/processedHeight".to_string().into_bytes()); - prefix.clone() + pub fn processed_height_key(height: Height, prefix: &str) -> Vec { + format!("{prefix}consensusStates/{height}/processedHeight").into_bytes() } - pub fn iteration_key(height: Height, prefix: &mut Vec) -> Vec { - prefix.append(&mut "iterateConsensusStates".to_string().into_bytes()); - prefix.append(&mut height.revision_number.to_be_bytes().to_vec()); - prefix.append(&mut height.revision_height.to_be_bytes().to_vec()); - prefix.clone() + pub fn iteration_key(height: Height, prefix: &str) -> Vec { + [ + prefix.as_bytes(), + &b"iterateConsensusStates"[..], + &height.revision_number.to_be_bytes()[..], + &height.revision_height.to_be_bytes()[..], + ] + .concat() } - pub fn get_processed_time(&self, height: Height, prefix: &mut Vec) -> Option { + pub fn get_processed_time(&self, height: Height, prefix: &str) -> Option { let full_key = Self::processed_time_key(height, prefix); self.0 .get(&full_key) .map(|timestamp| u64::from_be_bytes(timestamp.try_into().unwrap())) } - pub fn set_processed_time(&mut self, height: Height, timestamp: u64, prefix: &mut Vec) { + pub fn set_processed_time(&mut self, height: Height, timestamp: u64, prefix: &str) { let full_key = Self::processed_time_key(height, prefix); let time_vec: [u8; 8] = timestamp.to_be_bytes(); self.0.set(&full_key, &time_vec); } - pub fn get_processed_height(&self, height: Height, prefix: &mut Vec) -> Option { + pub fn get_processed_height(&self, height: Height, prefix: &str) -> Option { let full_key = Self::processed_height_key(height, prefix); self.0 .get(&full_key) .map(|height| u64::from_be_bytes(height.try_into().unwrap())) } - pub fn set_processed_height( - &mut self, - height: Height, - processed_height: u64, - prefix: &mut Vec, - ) { + pub fn set_processed_height(&mut self, height: Height, processed_height: u64, prefix: &str) { let full_key = Self::processed_height_key(height, prefix); let height_vec: [u8; 8] = processed_height.to_be_bytes(); self.0.set(&full_key, &height_vec); } - pub fn get_iteration_key(&self, height: Height, prefix: &mut Vec) -> Option { + pub fn get_iteration_key(&self, height: Height, prefix: &str) -> Option { let full_key = Self::iteration_key(height, prefix); match self.0.get(&full_key) { Some(height) => match std::str::from_utf8(height.as_slice()) { @@ -88,16 +80,16 @@ impl<'a> ProcessedStates<'a> { } } - pub fn set_iteration_key(&mut self, height: Height, prefix: &mut Vec) { + pub fn set_iteration_key(&mut self, height: Height, prefix: &str) { let full_key = Self::iteration_key(height, prefix); let height_vec = format!("{height}").into_bytes(); self.0.set(&full_key, &height_vec); } pub fn get_earliest_height(&mut self, current_height: Height) -> Option { - let full_key = Self::iteration_key(current_height, &mut Vec::new()); - let start_key = "iterateConsensusStates".to_string().into_bytes(); - let mut iterator = self.0.range(Some(&start_key), Some(&full_key), Order::Ascending); + let full_key = Self::iteration_key(current_height, ""); + let start_key = &b"iterateConsensusStates"[..]; + let mut iterator = self.0.range(Some(start_key), Some(&full_key), Order::Ascending); match iterator.next() { Some((_, height)) => match std::str::from_utf8(height.as_slice()) { Ok(height_str) => Some(Height::try_from(height_str).unwrap()), @@ -108,9 +100,9 @@ impl<'a> ProcessedStates<'a> { } pub fn remove_states_at_height(&mut self, height: Height) { - let processed_time_key = Self::processed_time_key(height, &mut Vec::new()); - let processed_height_key = Self::processed_height_key(height, &mut Vec::new()); - let iteration_key = Self::iteration_key(height, &mut Vec::new()); + let processed_time_key = Self::processed_time_key(height, ""); + let processed_height_key = Self::processed_height_key(height, ""); + let iteration_key = Self::iteration_key(height, ""); self.0.remove(&processed_time_key); self.0.remove(&processed_height_key); @@ -125,21 +117,21 @@ impl<'a> ReadonlyProcessedStates<'a> { ReadonlyProcessedStates(storage) } - pub fn get_processed_time(&self, height: Height, prefix: &mut Vec) -> Option { + pub fn get_processed_time(&self, height: Height, prefix: &str) -> Option { let full_key = ProcessedStates::processed_time_key(height, prefix); self.0 .get(&full_key) .map(|timestamp| u64::from_be_bytes(timestamp.try_into().unwrap())) } - pub fn get_processed_height(&self, height: Height, prefix: &mut Vec) -> Option { + pub fn get_processed_height(&self, height: Height, prefix: &str) -> Option { let full_key = ProcessedStates::processed_height_key(height, prefix); self.0 .get(&full_key) .map(|height| u64::from_be_bytes(height.try_into().unwrap())) } - pub fn get_iteration_key(&self, height: Height, prefix: &mut Vec) -> Option { + pub fn get_iteration_key(&self, height: Height, prefix: &str) -> Option { let full_key = ProcessedStates::iteration_key(height, prefix); match self.0.get(&full_key) { Some(height) => match std::str::from_utf8(height.as_slice()) { @@ -151,7 +143,7 @@ impl<'a> ReadonlyProcessedStates<'a> { } pub fn get_next_height(&self, height: Height) -> Option { - let full_key = ProcessedStates::iteration_key(height, &mut Vec::new()); + let full_key = ProcessedStates::iteration_key(height, ""); let mut iterator = self.0.range(Some(&full_key), None, Order::Ascending); match iterator.next() { Some((_, height)) => match std::str::from_utf8(height.as_slice()) { @@ -163,7 +155,7 @@ impl<'a> ReadonlyProcessedStates<'a> { } pub fn get_prev_height(&self, height: Height) -> Option { - let full_key = ProcessedStates::iteration_key(height, &mut Vec::new()); + let full_key = ProcessedStates::iteration_key(height, ""); let mut iterator = self.0.range(None, Some(&full_key), Order::Descending); match iterator.next() { Some((_, height)) => match std::str::from_utf8(height.as_slice()) { @@ -177,20 +169,18 @@ impl<'a> ReadonlyProcessedStates<'a> { pub fn get_metadata(&self) -> Option> { let mut gm: Vec = Vec::::new(); - let start_key = "iterateConsensusStates".to_string().into_bytes(); - let iterator = self.0.range(Some(&start_key), None, Order::Ascending); + let start_key = &b"iterateConsensusStates"[..]; + let iterator = self.0.range(Some(start_key), None, Order::Ascending); for (_, height) in iterator { match std::str::from_utf8(height.as_slice()) { Ok(height_str) => { let height = Height::try_from(height_str).unwrap(); - let processed_height_key = - ProcessedStates::processed_height_key(height, &mut Vec::new()); + let processed_height_key = ProcessedStates::processed_height_key(height, ""); gm.push(GenesisMetadata { key: processed_height_key.clone(), value: self.0.get(&processed_height_key).unwrap(), }); - let processed_time_key = - ProcessedStates::processed_time_key(height, &mut Vec::new()); + let processed_time_key = ProcessedStates::processed_time_key(height, ""); gm.push(GenesisMetadata { key: processed_time_key.clone(), value: self.0.get(&processed_time_key).unwrap(), @@ -200,7 +190,7 @@ impl<'a> ReadonlyProcessedStates<'a> { } } - let iterator = self.0.range(Some(&start_key), None, Order::Ascending); + let iterator = self.0.range(Some(start_key), None, Order::Ascending); for (key, height) in iterator { gm.push(GenesisMetadata { key, value: height }); } diff --git a/light-clients/ics07-tendermint-cw/src/state.rs b/light-clients/ics07-tendermint-cw/src/state.rs index 057892a82..a9ebea116 100644 --- a/light-clients/ics07-tendermint-cw/src/state.rs +++ b/light-clients/ics07-tendermint-cw/src/state.rs @@ -30,7 +30,7 @@ pub fn get_client_state( client_id: ClientId, ) -> Result, Error> { deps.storage - .get(&"clientState".to_string().into_bytes()) + .get(b"clientState") .ok_or_else(|| Error::unknown_client_state_type(client_id.to_string())) .and_then(|client_state| deserialize_client_state(client_state, deps)) } @@ -84,5 +84,5 @@ fn deserialize_consensus_state(consensus_state: Vec) -> Result Vec { - ["consensusStates/".to_string().into_bytes(), format!("{height}").into_bytes()].concat() + format!("consensusStates/{height}").into_bytes() } diff --git a/light-clients/ics08-wasm/src/lib.rs b/light-clients/ics08-wasm/src/lib.rs index 9cc2b5a1d..35b6a127a 100644 --- a/light-clients/ics08-wasm/src/lib.rs +++ b/light-clients/ics08-wasm/src/lib.rs @@ -28,5 +28,5 @@ pub mod instantiate; pub mod msg; pub type Bytes = Vec; -pub static SUBJECT_PREFIX: &[u8] = "subject/".as_bytes(); -pub static SUBSTITUTE_PREFIX: &[u8] = "substitute/".as_bytes(); +pub static SUBJECT_PREFIX: &str = "subject/"; +pub static SUBSTITUTE_PREFIX: &str = "substitute/"; diff --git a/light-clients/ics10-grandpa-cw/src/contract.rs b/light-clients/ics10-grandpa-cw/src/contract.rs index 8503bc5aa..4589e6400 100644 --- a/light-clients/ics10-grandpa-cw/src/contract.rs +++ b/light-clients/ics10-grandpa-cw/src/contract.rs @@ -220,7 +220,7 @@ fn process_message( // load the substitute client state from the combined storage using the appropriate // prefix let substitute_client_state = ctx - .client_state_prefixed(SUBSTITUTE_PREFIX) + .client_state_prefixed(SUBSTITUTE_PREFIX.as_bytes()) .map_err(|e| ContractError::Grandpa(e.to_string()))?; // No items for the grandpa client state are required to be the same @@ -228,10 +228,18 @@ fn process_message( let height = substitute_client_state.latest_height(); // consensus state should be replaced as well let substitute_consensus_state = - ctx.consensus_state_prefixed(height, SUBSTITUTE_PREFIX)?; - ctx.store_consensus_state_prefixed(height, substitute_consensus_state, SUBJECT_PREFIX); - ctx.store_client_state_prefixed(substitute_client_state, SUBJECT_PREFIX, client_id) - .map_err(|e| ContractError::Grandpa(e.to_string()))?; + ctx.consensus_state_prefixed(height, SUBSTITUTE_PREFIX.as_bytes())?; + ctx.store_consensus_state_prefixed( + height, + substitute_consensus_state, + SUBJECT_PREFIX.as_bytes(), + ); + ctx.store_client_state_prefixed( + substitute_client_state, + SUBJECT_PREFIX.as_bytes(), + client_id, + ) + .map_err(|e| ContractError::Grandpa(e.to_string()))?; Ok(()).map(|_| to_binary(&ContractResult::success())) }, diff --git a/light-clients/ics10-grandpa-cw/src/ics23/consensus_states.rs b/light-clients/ics10-grandpa-cw/src/ics23/consensus_states.rs index f443129af..98fd0b471 100644 --- a/light-clients/ics10-grandpa-cw/src/ics23/consensus_states.rs +++ b/light-clients/ics10-grandpa-cw/src/ics23/consensus_states.rs @@ -51,7 +51,7 @@ impl<'a> ConsensusStates<'a> { } pub fn consensus_state_client_key() -> Vec { - "consensusStates/".to_string().into_bytes() + b"consensusStates/".to_vec() } pub fn consensus_state_height_key(height: Height) -> Vec { diff --git a/light-clients/ics10-grandpa-cw/src/state.rs b/light-clients/ics10-grandpa-cw/src/state.rs index 23b9813f4..6647e98f6 100644 --- a/light-clients/ics10-grandpa-cw/src/state.rs +++ b/light-clients/ics10-grandpa-cw/src/state.rs @@ -30,7 +30,7 @@ pub fn get_client_state( client_id: ClientId, ) -> Result, Error> { deps.storage - .get(&"clientState".to_string().into_bytes()) + .get(b"clientState") .ok_or_else(|| Error::unknown_client_state_type(client_id.to_string())) .and_then(deserialize_client_state) } @@ -63,5 +63,5 @@ pub fn get_consensus_state( } pub fn get_consensus_state_key(height: Height) -> Vec { - ["consensusStates/".to_string().into_bytes(), format!("{height}").into_bytes()].concat() + format!("consensusStates/{height}").into_bytes() }