Skip to content

Commit

Permalink
[feature] hyperledger-iroha#3371: Split validator entrypoint
Browse files Browse the repository at this point in the history
Signed-off-by: Daniil Polyakov <arjentix@gmail.com>
  • Loading branch information
Arjentix committed Jul 24, 2023
1 parent 456498e commit 3d4979f
Show file tree
Hide file tree
Showing 15 changed files with 941 additions and 553 deletions.
13 changes: 5 additions & 8 deletions cli/src/torii/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl VerifiedQuery {
/// - Account has incorrect permissions
pub fn validate(
self,
wsv: &mut WorldStateView,
wsv: &WorldStateView,
) -> Result<(ValidQueryRequest, PredicateBox), ValidationFail> {
let account_has_public_key = wsv
.map_account(&self.payload.authority, |account| {
Expand All @@ -77,11 +77,8 @@ impl VerifiedQuery {
))
.into());
}
wsv.validator_view().clone().validate(
wsv,
&self.payload.authority,
self.payload.query.clone(),
)?;
wsv.validator()
.validate_query(wsv, &self.payload.authority, self.payload.query.clone())?;
Ok((
ValidQueryRequest::new(self.payload.query),
self.payload.filter,
Expand Down Expand Up @@ -139,8 +136,8 @@ pub(crate) async fn handle_queries(
let request: VerifiedQuery = request.try_into().map_err(ValidationFail::from)?;

let (result, filter) = {
let mut wsv = sumeragi.wsv_clone();
let (valid_request, filter) = request.validate(&mut wsv)?;
let wsv = sumeragi.wsv_clone();
let (valid_request, filter) = request.validate(&wsv)?;
let original_result = valid_request.execute(&wsv).map_err(ValidationFail::from)?;
(filter.filter(original_result), filter)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! If authority is not `admin@admin` then [`DefaultValidator`] is used as a backup.

#![no_std]
#![allow(missing_docs, clippy::missing_errors_doc)]

use iroha_validator::{
data_model::evaluate::{EvaluationError, ExpressionEvaluator},
Expand Down Expand Up @@ -113,29 +114,37 @@ impl ExpressionEvaluator for CustomValidator {
}
}

/// Migration entrypoint.
#[entrypoint]
pub fn migrate() -> MigrationResult {
CustomValidator::migrate()
}

/// Allow operation if authority is `admin@admin` and if not,
/// fallback to [`DefaultValidator::validate()`].
#[entrypoint(params = "[authority, operation]")]
pub fn validate(authority: AccountId, operation: NeedsValidationBox) -> Result {
#[entrypoint]
pub fn validate_transaction(
authority: AccountId,
transaction: VersionedSignedTransaction,
) -> Result {
let mut validator = CustomValidator(DefaultValidator::new());

match operation {
NeedsValidationBox::Transaction(transaction) => {
validator.visit_transaction(&authority, &transaction);
}
NeedsValidationBox::Instruction(instruction) => {
validator.visit_instruction(&authority, &instruction);
}
NeedsValidationBox::Query(query) => {
validator.visit_query(&authority, &query);
}
}
validator.visit_transaction(&authority, &transaction);

validator.0.verdict
}

#[entrypoint]
pub fn validate_instruction(authority: AccountId, instruction: InstructionBox) -> Result {
let mut validator = CustomValidator(DefaultValidator::new());

validator.visit_instruction(&authority, &instruction);

validator.0.verdict
}

#[entrypoint]
pub fn validate_query(authority: AccountId, query: QueryBox) -> Result {
let mut validator = CustomValidator(DefaultValidator::new());

validator.visit_query(&authority, &query);

validator.0.verdict
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//! get access to control all domains. Remember that this is just a test example.

#![no_std]
#![allow(missing_docs, clippy::missing_errors_doc)]

extern crate alloc;

Expand Down Expand Up @@ -103,17 +104,15 @@ impl CustomValidator {
})
}

fn grant_can_control_domain_lives_to<'acc>(
accounts: impl IntoIterator<Item = &'acc Account>,
) -> MigrationResult {
fn grant_can_control_domain_lives_to(accounts: &[Account]) -> MigrationResult {
let can_unregister_domain_definition =
iroha_validator::default::domain::tokens::CanUnregisterDomain::definition();
let can_unregister_domain_definition_id = can_unregister_domain_definition.id();

let can_control_domain_lives_definition = token::CanControlDomainLives::definition();
let can_control_domain_lives_definition_id = can_control_domain_lives_definition.id();

accounts.into_iter().try_for_each(|account| {
accounts.iter().try_for_each(|account| {
GrantBox::new(PermissionToken::new(can_control_domain_lives_definition_id.clone()), account.id().clone()).execute()
}).map_err(|error| {
iroha_validator::iroha_wasm::error!(&error);
Expand Down Expand Up @@ -242,28 +241,37 @@ impl ExpressionEvaluator for CustomValidator {
}
}

/// Migration entrypoint.
#[entrypoint]
pub fn migrate() -> MigrationResult {
CustomValidator::migrate()
}

/// Validate operation
#[entrypoint(params = "[authority, operation]")]
pub fn validate(authority: AccountId, operation: NeedsValidationBox) -> Result {
#[entrypoint]
pub fn validate_transaction(
authority: AccountId,
transaction: VersionedSignedTransaction,
) -> Result {
let mut validator = CustomValidator(DefaultValidator::new());

match operation {
NeedsValidationBox::Transaction(transaction) => {
validator.visit_transaction(&authority, &transaction);
}
NeedsValidationBox::Instruction(instruction) => {
validator.visit_instruction(&authority, &instruction);
}
NeedsValidationBox::Query(query) => {
validator.visit_query(&authority, &query);
}
}
validator.visit_transaction(&authority, &transaction);

validator.0.verdict
}

#[entrypoint]
pub fn validate_instruction(authority: AccountId, instruction: InstructionBox) -> Result {
let mut validator = CustomValidator(DefaultValidator::new());

validator.visit_instruction(&authority, &instruction);

validator.0.verdict
}

#[entrypoint]
pub fn validate_query(authority: AccountId, query: QueryBox) -> Result {
let mut validator = CustomValidator(DefaultValidator::new());

validator.visit_query(&authority, &query);

validator.0.verdict
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Runtime Validator which copies [`DefaultValidator`] logic but forbids any queries and fails to migrate.

#![no_std]
#![allow(missing_docs, clippy::missing_errors_doc)]

#[cfg(not(test))]
extern crate panic_halt;
Expand Down Expand Up @@ -150,22 +151,32 @@ pub fn migrate() -> MigrationResult {
CustomValidator::migrate()
}

/// Validation entrypoint.
#[entrypoint(params = "[authority, operation]")]
pub fn validate(authority: AccountId, operation: NeedsValidationBox) -> Result {
#[entrypoint]
pub fn validate_transaction(
authority: AccountId,
transaction: VersionedSignedTransaction,
) -> Result {
let mut validator = CustomValidator(DefaultValidator::new());

match operation {
NeedsValidationBox::Transaction(transaction) => {
validator.visit_transaction(&authority, &transaction);
}
NeedsValidationBox::Instruction(instruction) => {
validator.visit_instruction(&authority, &instruction);
}
NeedsValidationBox::Query(query) => {
validator.visit_query(&authority, &query);
}
}
validator.visit_transaction(&authority, &transaction);

validator.0.verdict
}

#[entrypoint]
pub fn validate_instruction(authority: AccountId, instruction: InstructionBox) -> Result {
let mut validator = CustomValidator(DefaultValidator::new());

validator.visit_instruction(&authority, &instruction);

validator.0.verdict
}

#[entrypoint]
pub fn validate_query(authority: AccountId, query: QueryBox) -> Result {
let mut validator = CustomValidator(DefaultValidator::new());

validator.visit_query(&authority, &query);

validator.0.verdict
}
Binary file modified configs/peer/validator.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion core/src/smartcontracts/isi/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ pub mod isi {
})?;

let world = wsv.world_mut();
let _ = world.upgraded_validator.insert(new_validator);
let _ = world.validator.insert(new_validator);

wsv.emit_events(std::iter::once(ValidatorEvent::Upgraded));

Expand Down
Loading

0 comments on commit 3d4979f

Please sign in to comment.