-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] #3435: Introduce validator visitor
Signed-off-by: Marin Veršić <marin.versic101@gmail.com>
- Loading branch information
Showing
42 changed files
with
2,610 additions
and
2,520 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 37 additions & 13 deletions
50
client/tests/integration/smartcontracts/validator_with_admin/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,45 @@ | ||
//! Runtime Validator which allows any operation done by `admin@admin` account. | ||
//! If authority is not `admin@admin` then `iroha_default_validator` is used. | ||
|
||
//! Runtime Validator which allows any [`TransferBox`] instructions by `admin@admin` account. | ||
//! If authority is not `admin@admin` then [`DefaultValidator`] is used as a backup. | ||
#![no_std] | ||
#![no_main] | ||
|
||
use iroha_validator::{parse, prelude::*, DefaultValidator}; | ||
|
||
#[cfg(not(test))] | ||
extern crate panic_halt; | ||
|
||
use iroha_validator::{parse, prelude::*}; | ||
struct Validator(DefaultValidator); | ||
|
||
/// Allow operation if authority is `admin@admin` and fallback to | ||
/// [`iroha_default_validator::validate()`] if not. | ||
impl Validate for Validator { | ||
fn validate_transfer(&mut self, authority: &AccountId, isi: &TransferBox) -> Verdict { | ||
pass_if!(*authority == parse!("admin@admin" as <Account as Identifiable>::Id)); | ||
self.0.validate_transfer(authority, isi) | ||
} | ||
} | ||
|
||
/// Allow operation if authority is `admin@admin` and if not, | ||
/// fallback to [`DefaultValidator::validate()`]. | ||
#[entrypoint(params = "[authority, operation]")] | ||
pub fn validate( | ||
authority: <Account as Identifiable>::Id, | ||
operation: NeedsValidationBox, | ||
) -> Verdict { | ||
pass_if!(authority == parse!("admin@admin" as <Account as Identifiable>::Id)); | ||
iroha_default_validator::validate(authority, operation) | ||
pub fn validate(authority: AccountId, operation: NeedsValidationBox) -> Verdict { | ||
let mut validator = Validator(DefaultValidator); | ||
|
||
match operation { | ||
// NOTE: Invoked from Iroha | ||
NeedsValidationBox::Transaction(transaction) => { | ||
validator.validate_transaction(&authority, transaction) | ||
} | ||
|
||
// NOTE: Invoked only from another Wasm | ||
NeedsValidationBox::Instruction(instruction) => { | ||
let verdict = validator.validate_instruction(&authority, &instruction); | ||
|
||
if !verdict.is_deny() { | ||
instruction.execute(); | ||
} | ||
|
||
verdict | ||
} | ||
|
||
// NOTE: Invoked only from another Wasm | ||
NeedsValidationBox::Query(query) => validator.validate_query(&authority, &query), | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.