Skip to content

Commit

Permalink
Add a version number to programs
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseAbram committed Sep 11, 2024
1 parent af97639 commit d369b6e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
11 changes: 8 additions & 3 deletions pallets/programs/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ benchmarks! {
let configuration_schema = vec![11];
let auxiliary_data_schema = vec![12];
let oracle_data_pointer = vec![13];
let version_number = 0u8;
let mut hash_input: Vec<u8> = vec![];
hash_input.extend(&program);
hash_input.extend(&configuration_schema);
hash_input.extend(&auxiliary_data_schema);
hash_input.extend(&oracle_data_pointer);
hash_input.extend(&vec![version_number]);

let program_hash = T::Hashing::hash(&hash_input);
let deployer: T::AccountId = whitelisted_caller();
Expand All @@ -58,15 +60,16 @@ benchmarks! {
let value = CurrencyOf::<T>::minimum_balance().saturating_mul(1_000_000_000u32.into());
let _ = CurrencyOf::<T>::make_free_balance_be(&deployer, value);

}: _(RawOrigin::Signed(deployer.clone()), program.clone(), configuration_schema.clone(), auxiliary_data_schema.clone(), oracle_data_pointer.clone())
}: _(RawOrigin::Signed(deployer.clone()), program.clone(), configuration_schema.clone(), auxiliary_data_schema.clone(), oracle_data_pointer.clone(), version_number)
verify {
assert_last_event::<T>(
Event::<T>::ProgramCreated {
deployer,
program_hash,
configuration_schema,
auxiliary_data_schema,
oracle_data_pointer
oracle_data_pointer,
version_number
}.into()
);
}
Expand All @@ -77,11 +80,13 @@ benchmarks! {
let configuration_schema = vec![11];
let auxiliary_data_schema = vec![12];
let oracle_data_pointer = vec![13];
let version_number = 0u8;
let mut hash_input: Vec<u8> = vec![];
hash_input.extend(&program);
hash_input.extend(&configuration_schema);
hash_input.extend(&auxiliary_data_schema);
hash_input.extend(&oracle_data_pointer);
hash_input.extend(&vec![version_number]);

let program_hash = T::Hashing::hash(&hash_input);
let random_program = vec![11];
Expand All @@ -90,7 +95,7 @@ benchmarks! {

let value = CurrencyOf::<T>::minimum_balance().saturating_mul(1_000_000_000u32.into());
let _ = CurrencyOf::<T>::make_free_balance_be(&deployer, value);
<Programs<T>>::insert(program_hash.clone(), ProgramInfo {bytecode: program, configuration_schema, auxiliary_data_schema, oracle_data_pointer, deployer: deployer.clone(), ref_counter: 0u128});
<Programs<T>>::insert(program_hash.clone(), ProgramInfo {bytecode: program, configuration_schema, auxiliary_data_schema, oracle_data_pointer, deployer: deployer.clone(), ref_counter: 0u128, version_number});
let mut program_hashes = vec![random_hash.clone(); p as usize];
// remove one to make room for the targetted removal program hash
program_hashes.pop();
Expand Down
11 changes: 11 additions & 0 deletions pallets/programs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub mod pallet {
deployer: program_info.4.clone(),
oracle_data_pointer: vec![],
ref_counter: program_info.5,
version_number: 0,
},
);
}
Expand Down Expand Up @@ -143,6 +144,9 @@ pub mod pallet {
pub deployer: AccountId,
/// Accounts that use this program
pub ref_counter: u128,
/// The user submitted version number of the program
/// It tells the TSS what runtime to use
pub version_number: u8,
}

/// Stores the program info for a given program hash.
Expand Down Expand Up @@ -182,6 +186,9 @@ pub mod pallet {

/// The oracle data location needed for the program
oracle_data_pointer: Vec<u8>,

/// The version number of the program created
version_number: u8,
},
/// The bytecode of a program was removed.
ProgramRemoved {
Expand Down Expand Up @@ -224,13 +231,15 @@ pub mod pallet {
configuration_schema: Vec<u8>,
auxiliary_data_schema: Vec<u8>,
oracle_data_pointer: Vec<u8>,
version_number: u8,
) -> DispatchResult {
let deployer = ensure_signed(origin)?;
let mut hash_input = vec![];
hash_input.extend(&new_program);
hash_input.extend(&configuration_schema);
hash_input.extend(&auxiliary_data_schema);
hash_input.extend(&oracle_data_pointer);
hash_input.extend(&vec![version_number]);
let program_hash = T::Hashing::hash(&hash_input);
let new_program_length = new_program
.len()
Expand All @@ -257,6 +266,7 @@ pub mod pallet {
oracle_data_pointer: oracle_data_pointer.clone(),
deployer: deployer.clone(),
ref_counter: 0u128,
version_number,
},
);
OwnedPrograms::<T>::try_mutate(
Expand All @@ -274,6 +284,7 @@ pub mod pallet {
configuration_schema,
auxiliary_data_schema,
oracle_data_pointer,
version_number,
});
Ok(())
}
Expand Down
19 changes: 16 additions & 3 deletions pallets/programs/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ fn set_program() {
let configuration_schema = vec![14u8];
let auxiliary_data_schema = vec![15u8];
let oracle_data_pointer = vec![16u8];
let version_number = 0u8;
let too_long = vec![1u8, 2u8, 3u8, 4u8, 5u8];
let mut hash_input: Vec<u8> = vec![];
hash_input.extend(&program);
hash_input.extend(&configuration_schema);
hash_input.extend(&auxiliary_data_schema);
hash_input.extend(&oracle_data_pointer);
hash_input.extend(&vec![version_number]);

let program_hash = <Test as frame_system::Config>::Hashing::hash(&hash_input);
// can't pay deposit
Expand All @@ -45,7 +47,8 @@ fn set_program() {
program.clone(),
configuration_schema.clone(),
auxiliary_data_schema.clone(),
oracle_data_pointer.clone()
oracle_data_pointer.clone(),
version_number
),
BalancesError::<Test>::InsufficientBalance
);
Expand All @@ -57,7 +60,8 @@ fn set_program() {
program.clone(),
configuration_schema.clone(),
auxiliary_data_schema.clone(),
oracle_data_pointer.clone()
oracle_data_pointer.clone(),
version_number
));
let program_result = ProgramInfo {
bytecode: program.clone(),
Expand All @@ -66,6 +70,7 @@ fn set_program() {
oracle_data_pointer: oracle_data_pointer.clone(),
deployer: PROGRAM_MODIFICATION_ACCOUNT,
ref_counter: 0u128,
version_number,
};
assert_eq!(
ProgramsPallet::programs(program_hash).unwrap(),
Expand All @@ -88,6 +93,7 @@ fn set_program() {
configuration_schema.clone(),
auxiliary_data_schema.clone(),
oracle_data_pointer.clone(),
version_number
),
Error::<Test>::ProgramAlreadySet
);
Expand All @@ -100,6 +106,7 @@ fn set_program() {
configuration_schema.clone(),
auxiliary_data_schema.clone(),
oracle_data_pointer.clone(),
version_number
),
Error::<Test>::TooManyProgramsOwned
);
Expand All @@ -111,6 +118,7 @@ fn set_program() {
configuration_schema,
auxiliary_data_schema.clone(),
oracle_data_pointer.clone(),
version_number
),
Error::<Test>::ProgramLengthExceeded
);
Expand All @@ -124,11 +132,13 @@ fn remove_program() {
let configuration_schema = vec![14u8];
let auxiliary_data_schema = vec![15u8];
let oracle_data_pointer = vec![16u8];
let version_number = 0u8;
let mut hash_input: Vec<u8> = vec![];
hash_input.extend(&program);
hash_input.extend(&configuration_schema);
hash_input.extend(&auxiliary_data_schema);
hash_input.extend(&oracle_data_pointer);
hash_input.extend(&vec![version_number]);
let program_hash = <Test as frame_system::Config>::Hashing::hash(&hash_input);

// no program
Expand All @@ -147,7 +157,8 @@ fn remove_program() {
program.clone(),
configuration_schema.clone(),
auxiliary_data_schema.clone(),
oracle_data_pointer.clone()
oracle_data_pointer.clone(),
version_number
));
assert_eq!(
ProgramsPallet::owned_programs(PROGRAM_MODIFICATION_ACCOUNT),
Expand Down Expand Up @@ -196,6 +207,7 @@ fn remove_program_fails_ref_count() {
let configuration_schema = vec![14u8];
let auxiliary_data_schema = vec![15u8];
let oracle_data_pointer = vec![16u8];
let version_number = 0u8;

Programs::<Test>::insert(
program_hash,
Expand All @@ -206,6 +218,7 @@ fn remove_program_fails_ref_count() {
oracle_data_pointer,
deployer: PROGRAM_MODIFICATION_ACCOUNT,
ref_counter: 1u128,
version_number,
},
);

Expand Down

0 comments on commit d369b6e

Please sign in to comment.