From 170e6547f07f73561784c88e9ba6193835eceb1b Mon Sep 17 00:00:00 2001 From: Dmitry Murzin Date: Mon, 23 Sep 2024 16:17:13 +0300 Subject: [PATCH 1/2] refactor: Use `ConstVec` for instructions in `Executable` Signed-off-by: Dmitry Murzin --- crates/iroha_core/src/smartcontracts/isi/triggers/set.rs | 3 ++- crates/iroha_data_model/src/block.rs | 2 +- crates/iroha_data_model/src/transaction.rs | 5 +++-- crates/iroha_data_model/src/visit.rs | 2 +- crates/iroha_executor/src/default.rs | 2 +- crates/iroha_schema_gen/src/lib.rs | 1 + 6 files changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/iroha_core/src/smartcontracts/isi/triggers/set.rs b/crates/iroha_core/src/smartcontracts/isi/triggers/set.rs index f270a9fbd34..43eb9efe721 100644 --- a/crates/iroha_core/src/smartcontracts/isi/triggers/set.rs +++ b/crates/iroha_core/src/smartcontracts/isi/triggers/set.rs @@ -19,6 +19,7 @@ use iroha_data_model::{ query::error::FindError, transaction::WasmSmartContract, }; +use iroha_primitives::const_vec::ConstVec; use mv::{ cell::{Block as CellBlock, Cell, Transaction as CellTransaction, View as CellView}, storage::{ @@ -1012,7 +1013,7 @@ pub enum ExecutableRef { /// Loaded WASM Wasm(HashOf), /// Vector of ISI - Instructions(Vec), + Instructions(ConstVec), } impl core::fmt::Debug for ExecutableRef { diff --git a/crates/iroha_data_model/src/block.rs b/crates/iroha_data_model/src/block.rs index d7cc1003822..1ffa33ff268 100644 --- a/crates/iroha_data_model/src/block.rs +++ b/crates/iroha_data_model/src/block.rs @@ -454,7 +454,7 @@ mod candidate { else { return Err("Genesis transaction must contain instructions"); }; - let [InstructionBox::Upgrade(_)] = instructions_executor.as_slice() else { + let [InstructionBox::Upgrade(_)] = instructions_executor.as_ref() else { return Err( "First transaction must contain single `Upgrade` instruction to set executor", ); diff --git a/crates/iroha_data_model/src/transaction.rs b/crates/iroha_data_model/src/transaction.rs index 5b5a3cfc968..d3dc575d39b 100644 --- a/crates/iroha_data_model/src/transaction.rs +++ b/crates/iroha_data_model/src/transaction.rs @@ -30,6 +30,7 @@ use crate::{ #[model] mod model { use getset::Getters; + use iroha_primitives::const_vec::ConstVec; use super::*; use crate::account::AccountId; @@ -53,7 +54,7 @@ mod model { pub enum Executable { /// Ordered set of instructions. #[debug(fmt = "{_0:?}")] - Instructions(Vec), + Instructions(ConstVec), /// WebAssembly smartcontract Wasm(WasmSmartContract), } @@ -182,7 +183,7 @@ mod model { impl FromIterator for Executable { fn from_iter>(iter: T) -> Self { - Self::Instructions(iter.into_iter().map(Into::into).collect()) + Self::Instructions(iter.into_iter().map(Into::into).collect::>().into()) } } diff --git a/crates/iroha_data_model/src/visit.rs b/crates/iroha_data_model/src/visit.rs index 102ce612686..82e6c5da5c7 100644 --- a/crates/iroha_data_model/src/visit.rs +++ b/crates/iroha_data_model/src/visit.rs @@ -145,7 +145,7 @@ pub fn visit_transaction( match transaction.instructions() { Executable::Wasm(wasm) => visitor.visit_wasm(authority, wasm), Executable::Instructions(instructions) => { - for isi in instructions { + for isi in instructions.as_ref() { visitor.visit_instruction(authority, isi); } } diff --git a/crates/iroha_executor/src/default.rs b/crates/iroha_executor/src/default.rs index 56a8d5f3a41..950ccdadc4f 100644 --- a/crates/iroha_executor/src/default.rs +++ b/crates/iroha_executor/src/default.rs @@ -63,7 +63,7 @@ pub fn visit_transaction( match transaction.instructions() { Executable::Wasm(wasm) => executor.visit_wasm(authority, wasm), Executable::Instructions(instructions) => { - for isi in instructions { + for isi in instructions.as_ref() { if executor.verdict().is_ok() { executor.visit_instruction(authority, isi); } diff --git a/crates/iroha_schema_gen/src/lib.rs b/crates/iroha_schema_gen/src/lib.rs index 79a80583ed1..f4c0de08ea0 100644 --- a/crates/iroha_schema_gen/src/lib.rs +++ b/crates/iroha_schema_gen/src/lib.rs @@ -192,6 +192,7 @@ types!( ConfigurationEventSet, ConstString, ConstVec, + ConstVec, CustomInstruction, CustomParameter, CustomParameterId, From 7240b9918f5702fff56b05a6592d34860357f921 Mon Sep 17 00:00:00 2001 From: Dmitry Murzin Date: Tue, 24 Sep 2024 14:47:05 +0300 Subject: [PATCH 2/2] Review fixes Signed-off-by: Dmitry Murzin --- crates/iroha_data_model/src/transaction.rs | 2 +- crates/iroha_data_model/src/visit.rs | 2 +- crates/iroha_executor/src/default.rs | 2 +- crates/iroha_primitives/src/const_vec.rs | 17 +++++++++++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/crates/iroha_data_model/src/transaction.rs b/crates/iroha_data_model/src/transaction.rs index d3dc575d39b..6bd5efb4cc5 100644 --- a/crates/iroha_data_model/src/transaction.rs +++ b/crates/iroha_data_model/src/transaction.rs @@ -183,7 +183,7 @@ mod model { impl FromIterator for Executable { fn from_iter>(iter: T) -> Self { - Self::Instructions(iter.into_iter().map(Into::into).collect::>().into()) + Self::Instructions(iter.into_iter().map(Into::into).collect()) } } diff --git a/crates/iroha_data_model/src/visit.rs b/crates/iroha_data_model/src/visit.rs index 82e6c5da5c7..102ce612686 100644 --- a/crates/iroha_data_model/src/visit.rs +++ b/crates/iroha_data_model/src/visit.rs @@ -145,7 +145,7 @@ pub fn visit_transaction( match transaction.instructions() { Executable::Wasm(wasm) => visitor.visit_wasm(authority, wasm), Executable::Instructions(instructions) => { - for isi in instructions.as_ref() { + for isi in instructions { visitor.visit_instruction(authority, isi); } } diff --git a/crates/iroha_executor/src/default.rs b/crates/iroha_executor/src/default.rs index 950ccdadc4f..56a8d5f3a41 100644 --- a/crates/iroha_executor/src/default.rs +++ b/crates/iroha_executor/src/default.rs @@ -63,7 +63,7 @@ pub fn visit_transaction( match transaction.instructions() { Executable::Wasm(wasm) => executor.visit_wasm(authority, wasm), Executable::Instructions(instructions) => { - for isi in instructions.as_ref() { + for isi in instructions { if executor.verdict().is_ok() { executor.visit_instruction(authority, isi); } diff --git a/crates/iroha_primitives/src/const_vec.rs b/crates/iroha_primitives/src/const_vec.rs index acba77ac1a7..54f7c628b28 100644 --- a/crates/iroha_primitives/src/const_vec.rs +++ b/crates/iroha_primitives/src/const_vec.rs @@ -92,6 +92,16 @@ impl IntoSchema for ConstVec { } } +impl<'a, T> IntoIterator for &'a ConstVec { + type Item = &'a T; + + type IntoIter = <&'a [T] as IntoIterator>::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + impl IntoIterator for ConstVec { type Item = T; @@ -102,6 +112,13 @@ impl IntoIterator for ConstVec { } } +impl FromIterator for ConstVec { + fn from_iter>(iter: I) -> Self { + let vec: Vec = iter.into_iter().collect(); + Self::new(vec) + } +} + /// Trait to extend `[T]` with a method to convert it to `ConstVec` by analogy with `[T]::to_vec()`. pub trait ToConstVec { /// The type of the items in the slice.