Skip to content

Commit

Permalink
refactor: Use ConstVec for instructions in Executable (#5096)
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Murzin <diralik@yandex.ru>
  • Loading branch information
dima74 authored Sep 26, 2024
1 parent c3ad903 commit 659f969
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion crates/iroha_core/src/smartcontracts/isi/triggers/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -1012,7 +1013,7 @@ pub enum ExecutableRef {
/// Loaded WASM
Wasm(HashOf<WasmSmartContract>),
/// Vector of ISI
Instructions(Vec<InstructionBox>),
Instructions(ConstVec<InstructionBox>),
}

impl core::fmt::Debug for ExecutableRef {
Expand Down
2 changes: 1 addition & 1 deletion crates/iroha_data_model/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
Expand Down
3 changes: 2 additions & 1 deletion crates/iroha_data_model/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::{
#[model]
mod model {
use getset::Getters;
use iroha_primitives::const_vec::ConstVec;

use super::*;
use crate::account::AccountId;
Expand All @@ -53,7 +54,7 @@ mod model {
pub enum Executable {
/// Ordered set of instructions.
#[debug(fmt = "{_0:?}")]
Instructions(Vec<InstructionBox>),
Instructions(ConstVec<InstructionBox>),
/// WebAssembly smartcontract
Wasm(WasmSmartContract),
}
Expand Down
17 changes: 17 additions & 0 deletions crates/iroha_primitives/src/const_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ impl<T: IntoSchema> IntoSchema for ConstVec<T> {
}
}

impl<'a, T> IntoIterator for &'a ConstVec<T> {
type Item = &'a T;

type IntoIter = <&'a [T] as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl<T> IntoIterator for ConstVec<T> {
type Item = T;

Expand All @@ -102,6 +112,13 @@ impl<T> IntoIterator for ConstVec<T> {
}
}

impl<T> FromIterator<T> for ConstVec<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let vec: Vec<T> = iter.into_iter().collect();
Self::new(vec)
}
}

/// Trait to extend `[T]` with a method to convert it to `ConstVec<T>` by analogy with `[T]::to_vec()`.
pub trait ToConstVec {
/// The type of the items in the slice.
Expand Down
1 change: 1 addition & 0 deletions crates/iroha_schema_gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ types!(
ConfigurationEventSet,
ConstString,
ConstVec<u8>,
ConstVec<InstructionBox>,
CustomInstruction,
CustomParameter,
CustomParameterId,
Expand Down

0 comments on commit 659f969

Please sign in to comment.