Skip to content

Commit

Permalink
Add oracle data pointer check
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseAbram committed Dec 3, 2024
1 parent aae53a5 commit 0a4776c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pallets/programs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -40,5 +41,6 @@ std=[
'frame-support/std',
'frame-system/std',
'log/std',
'pallet-oracle/std',
]
try-runtime=['frame-support/try-runtime']
17 changes: 14 additions & 3 deletions pallets/programs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -400,6 +404,13 @@ pub mod pallet {
) -> Result<(usize, Vec<u8>), Error<T>> {
let mut length: usize = 0;
for oracle_data in oracle_datas {
ensure!(
pallet_oracle::OracleData::<T>::contains_key(
BoundedVec::try_from(oracle_data.clone())
.map_err(|_| Error::<T>::CannotFindOracleData)?
),
Error::<T>::CannotFindOracleData
);
hash_input.extend(oracle_data);
length =
length.checked_add(oracle_data.len()).ok_or(Error::<T>::ArithmeticError)?;
Expand Down
13 changes: 13 additions & 0 deletions pallets/programs/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ frame_support::construct_runtime!(
System: frame_system,
ProgramsPallet: pallet_programs,
Balances: pallet_balances,
Oracle: pallet_oracle,
}
);

Expand Down Expand Up @@ -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;
Expand Down
32 changes: 28 additions & 4 deletions pallets/programs/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Test as frame_system::Config>::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::<Test>::CannotFindOracleData
);

pallet_oracle::OracleData::<Test>::insert(
BoundedVec::try_from(oracle_data_pointers[0].clone()).unwrap(),
BoundedVec::default(),
);

// can't pay deposit
assert_noop!(
ProgramsPallet::set_program(
Expand All @@ -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 = <Test as frame_system::Config>::Hashing::hash(&hash_input_with_oracle);

assert_ok!(ProgramsPallet::set_program(
RuntimeOrigin::signed(PROGRAM_MODIFICATION_ACCOUNT),
program.clone(),
Expand Down Expand Up @@ -141,6 +160,11 @@ fn remove_program() {
hash_input.extend(&auxiliary_data_schema);
hash_input.extend(&vec![version_number]);

pallet_oracle::OracleData::<Test>::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();
Expand Down

0 comments on commit 0a4776c

Please sign in to comment.