Skip to content

Commit

Permalink
Add oracle data pointer check (#1194)
Browse files Browse the repository at this point in the history
* Add oracle data pointer check

* update benchmarks

* fix tests
  • Loading branch information
JesseAbram authored Dec 5, 2024
1 parent aca49ad commit 765a86e
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

Binary file modified crates/client/entropy_metadata.scale
Binary file not shown.
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']
13 changes: 13 additions & 0 deletions pallets/programs/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use frame_support::{
BoundedVec,
};
use frame_system::{EventRecord, RawOrigin};
use pallet_oracle::OracleData;
use sp_runtime::{traits::Hash, Saturating};
use sp_std::{vec, vec::Vec};

Expand Down Expand Up @@ -52,6 +53,12 @@ benchmarks! {
hash_input.extend(&configuration_schema);
hash_input.extend(&auxiliary_data_schema);
hash_input.extend(&vec![version_number]);

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

let (_oracle_length, hash_input_with_oracle) =
ProgramsPallet::<T>::get_length_and_hash_of_oracle(&oracle_data_pointers, hash_input).unwrap();

Expand Down Expand Up @@ -97,6 +104,12 @@ benchmarks! {
hash_input.extend(&configuration_schema);
hash_input.extend(&auxiliary_data_schema);
hash_input.extend(&vec![version_number]);

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

let (_oracle_length, hash_input_with_oracle) =
ProgramsPallet::<T>::get_length_and_hash_of_oracle(&oracle_data_pointers, hash_input).unwrap();

Expand Down
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
1 change: 1 addition & 0 deletions pallets/propagation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pallet-timestamp ={ version="28.0.0", default-features=false }
sp-keystore ={ version="0.35.0" }
sp-npos-elections ={ version="27.0.0", default-features=false }
pallet-parameters ={ version="0.3.0", path="../parameters", default-features=false }
pallet-oracle ={ version='0.3.0', path='../oracle', default-features=false }

[features]
default=['std']
Expand Down
13 changes: 13 additions & 0 deletions pallets/propagation/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ frame_support::construct_runtime!(
BagsList: pallet_bags_list,
Parameters: pallet_parameters,
Attestation: pallet_attestation,
Oracle: pallet_oracle,
}
);

Expand Down Expand Up @@ -347,6 +348,18 @@ impl pallet_registry::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 = ();
}

parameter_types! {
pub const MaxBytecodeLength: u32 = 3;
pub const ProgramDepositPerByte: u32 = 5;
Expand Down
1 change: 1 addition & 0 deletions pallets/registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pallet-timestamp ={ version="28.0.0", default-features=false }
sp-io ={ version="31.0.0", default-features=false }
sp-npos-elections ={ version="27.0.0", default-features=false }
sp-staking ={ version="27.0.0", default-features=false }
pallet-oracle ={ version='0.3.0', path='../oracle', default-features=false }

[features]

Expand Down
13 changes: 13 additions & 0 deletions pallets/registry/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ frame_support::construct_runtime!(
BagsList: pallet_bags_list,
Programs: pallet_programs,
Parameters: pallet_parameters,
Oracle: pallet_oracle,
}
);

Expand Down Expand Up @@ -344,6 +345,18 @@ impl pallet_registry::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 = ();
}

parameter_types! {
pub const MaxBytecodeLength: u32 = 3;
pub const ProgramDepositPerByte: u32 = 5;
Expand Down
1 change: 1 addition & 0 deletions pallets/transaction-pause/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sp-std ={ version="14.0.0", default-features=false }
smallvec="1.13.2"

pallet-balances={ version="29.0.0" }
pallet-oracle ={ version='0.3.0', path='../oracle', default-features=false }
sp-core ={ version="29.0.0" }
sp-io ={ version="31.0.0" }

Expand Down
13 changes: 13 additions & 0 deletions pallets/transaction-pause/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = ();
}

parameter_types! {
pub const MaxOracleKeyLength: u32 = 100;
pub const MaxOracleValueLength: u32 = 100;
}

impl pallet_oracle::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type MaxOracleKeyLength = MaxOracleKeyLength;
type MaxOracleValueLength = MaxOracleValueLength;
type WeightInfo = ();
}

parameter_types! {
pub const MaxBytecodeLength: u32 = 3;
pub const ProgramDepositPerByte: u32 = 5;
Expand Down Expand Up @@ -117,6 +129,7 @@ construct_runtime!(
TransactionPause: transaction_pause,
Balances: pallet_balances,
ProgramsPallet: pallet_programs,
Oracle: pallet_oracle,
}
);

Expand Down

0 comments on commit 765a86e

Please sign in to comment.