diff --git a/Cargo.lock b/Cargo.lock index b26f2af47..1a00d7dda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7257,6 +7257,7 @@ dependencies = [ "frame-system", "log", "pallet-balances", + "pallet-oracle", "parity-scale-codec", "scale-info", "sp-core 29.0.0", diff --git a/pallets/programs/Cargo.toml b/pallets/programs/Cargo.toml index 0091d4aee..a840d00ff 100644 --- a/pallets/programs/Cargo.toml +++ b/pallets/programs/Cargo.toml @@ -23,6 +23,7 @@ sp-io ={ version="31.0.0", default-features=false } sp-runtime ={ version="32.0.0", default-features=false } sp-staking ={ version="27.0.0", default-features=false } sp-std ={ version="14.0.0", default-features=false } +pallet-oracle ={ version='0.3.0', path='../oracle', default-features=false } [dev-dependencies] pallet-balances={ version="29.0.0" } @@ -40,5 +41,6 @@ std=[ 'frame-support/std', 'frame-system/std', 'log/std', + 'pallet-oracle/std', ] try-runtime=['frame-support/try-runtime'] diff --git a/pallets/programs/src/lib.rs b/pallets/programs/src/lib.rs index bdf61e4c5..5596bee3f 100644 --- a/pallets/programs/src/lib.rs +++ b/pallets/programs/src/lib.rs @@ -62,7 +62,7 @@ pub mod pallet { pub use crate::weights::WeightInfo; #[pallet::config] - pub trait Config: frame_system::Config { + pub trait Config: frame_system::Config + pallet_oracle::Config { /// The overarching event type. type RuntimeEvent: From> + IsType<::RuntimeEvent>; @@ -218,10 +218,14 @@ pub mod pallet { ProgramAlreadySet, /// User owns too many programs. TooManyProgramsOwned, - /// Program is being used by an account + /// Program is being used by an account. ProgramInUse, - /// Arithmetic overflow error + /// Arithmetic overflow error. ArithmeticError, + /// Oracle data non existent. + CannotFindOracleData, + /// The oracle data length is too long. + OracleDataLengthExceeded, } #[pallet::call] @@ -400,6 +404,13 @@ pub mod pallet { ) -> Result<(usize, Vec), Error> { let mut length: usize = 0; for oracle_data in oracle_datas { + ensure!( + pallet_oracle::OracleData::::contains_key( + BoundedVec::try_from(oracle_data.clone()) + .map_err(|_| Error::::CannotFindOracleData)? + ), + Error::::CannotFindOracleData + ); hash_input.extend(oracle_data); length = length.checked_add(oracle_data.len()).ok_or(Error::::ArithmeticError)?; diff --git a/pallets/programs/src/mock.rs b/pallets/programs/src/mock.rs index 20ece9a7b..ad463e463 100644 --- a/pallets/programs/src/mock.rs +++ b/pallets/programs/src/mock.rs @@ -32,6 +32,7 @@ frame_support::construct_runtime!( System: frame_system, ProgramsPallet: pallet_programs, Balances: pallet_balances, + Oracle: pallet_oracle, } ); @@ -95,6 +96,18 @@ impl pallet_balances::Config for Test { type WeightInfo = (); } +parameter_types! { + pub const MaxOracleKeyLength: u32 = 100; + pub const MaxOracleValueLength: u32 = 100; +} + +impl pallet_oracle::Config for Test { + type RuntimeEvent = RuntimeEvent; + type MaxOracleKeyLength = MaxOracleKeyLength; + type MaxOracleValueLength = MaxOracleValueLength; + type WeightInfo = (); +} + impl pallet_programs::Config for Test { type Currency = Balances; type MaxBytecodeLength = MaxBytecodeLength; diff --git a/pallets/programs/src/tests.rs b/pallets/programs/src/tests.rs index 365e12025..cae8c6344 100644 --- a/pallets/programs/src/tests.rs +++ b/pallets/programs/src/tests.rs @@ -37,11 +37,24 @@ fn set_program() { hash_input.extend(&configuration_schema); hash_input.extend(&auxiliary_data_schema); hash_input.extend(&vec![version_number]); - let (_oracle_length, hash_input_with_oracle) = - ProgramsPallet::get_length_and_hash_of_oracle(&oracle_data_pointers, hash_input) - .unwrap(); - let program_hash = ::Hashing::hash(&hash_input_with_oracle); + assert_noop!( + ProgramsPallet::set_program( + RuntimeOrigin::signed(PROGRAM_MODIFICATION_ACCOUNT), + program.clone(), + configuration_schema.clone(), + auxiliary_data_schema.clone(), + oracle_data_pointers.clone(), + version_number + ), + Error::::CannotFindOracleData + ); + + pallet_oracle::OracleData::::insert( + BoundedVec::try_from(oracle_data_pointers[0].clone()).unwrap(), + BoundedVec::default(), + ); + // can't pay deposit assert_noop!( ProgramsPallet::set_program( @@ -57,6 +70,12 @@ fn set_program() { Balances::make_free_balance_be(&PROGRAM_MODIFICATION_ACCOUNT, 100); + let (_oracle_length, hash_input_with_oracle) = + ProgramsPallet::get_length_and_hash_of_oracle(&oracle_data_pointers, hash_input) + .unwrap(); + + let program_hash = ::Hashing::hash(&hash_input_with_oracle); + assert_ok!(ProgramsPallet::set_program( RuntimeOrigin::signed(PROGRAM_MODIFICATION_ACCOUNT), program.clone(), @@ -141,6 +160,11 @@ fn remove_program() { hash_input.extend(&auxiliary_data_schema); hash_input.extend(&vec![version_number]); + pallet_oracle::OracleData::::insert( + BoundedVec::try_from(oracle_data_pointers[0].clone()).unwrap(), + BoundedVec::default(), + ); + let (_oracle_length, hash_input_with_oracle) = ProgramsPallet::get_length_and_hash_of_oracle(&oracle_data_pointers, hash_input) .unwrap();