Skip to content

Commit

Permalink
Update programs to accept multiple oracle data (#1153)
Browse files Browse the repository at this point in the history
* Update programs to accespt multiple oracle data

* update tests

* fix tests

* fix benchmarks

* handle oracle hash and length

* bounde oracle data length

* bound oracle pointer length

* fix benchmarks and bounded vec

* fix tests

* update changelog

* put programs repo back to master branch

* Update crates/client/src/client.rs

Co-authored-by: peg <[email protected]>

* fix

---------

Co-authored-by: peg <[email protected]>
  • Loading branch information
JesseAbram and ameba23 authored Nov 14, 2024
1 parent 065b866 commit 0629ad2
Show file tree
Hide file tree
Showing 28 changed files with 211 additions and 137 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ At the moment this project **does not** adhere to
`change_threshold_accounts()` extrinsics got new TDX `quote` related parameters added.
- In [#1134](https://github.com/entropyxyz/entropy-core/pull/1134/) the `--no-sync` option was
removed.
- In [#1153](https://github.com/entropyxyz/entropy-core/pull/1153/) the program runtime was updated to accept
multiple oracle inputs, this means any programs that were compiled and used need to be recompiled to the new
runtime

### Added
- Protocol message versioning ([#1140](https://github.com/entropyxyz/entropy-core/pull/1140))
Expand All @@ -31,6 +34,7 @@ At the moment this project **does not** adhere to
- Handle PCK certificates ([#1068](https://github.com/entropyxyz/entropy-core/pull/1068))
- Add quote guards to `ServerInfo` related extrinsics ([#1123](https://github.com/entropyxyz/entropy-core/pull/1123/))
- Remove declare synced ([#1134](https://github.com/entropyxyz/entropy-core/pull/1134/))
- Update programs to accept multiple oracle data ([#1153](https://github.com/entropyxyz/entropy-core/pull/1153/))

## [0.3.0](https://github.com/entropyxyz/entropy-core/compare/release/v0.2.0...release/v0.3.0) - 2024-10-22

Expand Down
6 changes: 2 additions & 4 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.
6 changes: 3 additions & 3 deletions crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ pub async fn store_program(
program: Vec<u8>,
configuration_interface: Vec<u8>,
auxiliary_data_interface: Vec<u8>,
oracle_data_pointer: Vec<u8>,
oracle_data_pointers: Vec<Vec<u8>>,
version_number: u8,
) -> Result<<EntropyConfig as Config>::Hash, ClientError> {
let set_program_tx = entropy::tx().programs().set_program(
program,
configuration_interface,
auxiliary_data_interface,
oracle_data_pointer,
BoundedVec(oracle_data_pointers),
version_number,
);
let in_block =
Expand Down Expand Up @@ -290,7 +290,7 @@ pub async fn get_accounts(
pub async fn get_programs(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
) -> Result<Vec<(H256, ProgramInfo<<EntropyConfig as Config>::AccountId>)>, ClientError> {
) -> Result<Vec<(H256, ProgramInfo)>, ClientError> {
let block_hash = rpc.chain_get_block_hash(None).await?.ok_or(ClientError::BlockHash)?;

let storage_address = entropy::storage().programs().programs_iter();
Expand Down
Binary file modified crates/shared/device_key_proxy.wasm
Binary file not shown.
Binary file modified crates/testing-utils/example_barebones_with_auxilary.wasm
Binary file not shown.
Binary file modified crates/testing-utils/example_custom_hash.wasm
Binary file not shown.
Binary file modified crates/testing-utils/faucet_program.wasm
Binary file not shown.
Binary file modified crates/testing-utils/infinite_loop.wasm
Binary file not shown.
Binary file modified crates/testing-utils/template_barebones.wasm
Binary file not shown.
Binary file modified crates/testing-utils/template_basic_transaction.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/threshold-signature-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ entropy-protocol={ version="0.3.0", path="../protocol", features=["server"] }
entropy-client ={ version="0.3.0", path="../client", default-features=false, features=["native"] }

# Programs
entropy-programs-runtime="0.10.0"
entropy-programs-runtime={ git="https://github.com/entropyxyz/programs.git", branch="master" }

# Logging
tracing ="0.1.37"
Expand Down
19 changes: 12 additions & 7 deletions crates/threshold-signature-server/src/helpers/substrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub async fn get_program(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
program_pointer: &<EntropyConfig as Config>::Hash,
) -> Result<ProgramInfo<AccountId32>, UserErr> {
) -> Result<ProgramInfo, UserErr> {
let bytecode_address = entropy::storage().programs().programs(program_pointer);
let program_info = query_chain(api, rpc, bytecode_address, None)
.await?
Expand All @@ -64,12 +64,17 @@ pub async fn get_program(
pub async fn get_oracle_data(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
program_oracle_data: Vec<u8>,
) -> Result<Vec<u8>, UserErr> {
let oracle_data_call = entropy::storage().oracle().oracle_data(BoundedVec(program_oracle_data));
let oracle_info =
query_chain(api, rpc, oracle_data_call, None).await?.unwrap_or(BoundedVec(vec![]));
Ok(oracle_info.0)
program_oracle_datas: Vec<Vec<u8>>,
) -> Result<Vec<Vec<u8>>, UserErr> {
let mut oracle_infos = vec![];
for program_oracle_data in program_oracle_datas {
let oracle_data_call =
entropy::storage().oracle().oracle_data(BoundedVec(program_oracle_data));
let oracle_info =
query_chain(api, rpc, oracle_data_call, None).await?.unwrap_or(BoundedVec(vec![]));
oracle_infos.push(oracle_info.0);
}
Ok(oracle_infos)
}

/// Takes Stash keys and returns validator info from chain
Expand Down
2 changes: 1 addition & 1 deletion crates/threshold-signature-server/src/user/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ pub async fn pre_sign_checks(

for (i, program_data) in user_details.programs_data.0.iter().enumerate() {
let program_info = get_program(api, rpc, &program_data.program_pointer).await?;
let oracle_data = get_oracle_data(api, rpc, program_info.oracle_data_pointer).await?;
let oracle_data = get_oracle_data(api, rpc, program_info.oracle_data_pointers.0).await?;
let auxilary_data = auxilary_data_vec[i].as_ref().map(hex::decode).transpose()?;
let signature_request = SignatureRequest { message: message.clone(), auxilary_data };
runtime.evaluate(
Expand Down
12 changes: 8 additions & 4 deletions crates/threshold-signature-server/src/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1601,13 +1601,17 @@ async fn test_get_oracle_data() {
let rpc = get_rpc(&cxt.node_proc.ws_url).await.unwrap();
run_to_block(&rpc, 1).await;

let oracle_data = get_oracle_data(&api, &rpc, "block_number_entropy".encode()).await.unwrap();
let oracle_data =
get_oracle_data(&api, &rpc, vec!["block_number_entropy".encode()]).await.unwrap();
let current_block = rpc.chain_get_header(None).await.unwrap().unwrap().number;
assert_eq!(current_block.encode(), oracle_data);
assert_eq!(oracle_data.len(), 1);
assert_eq!(current_block.encode(), oracle_data[0]);

// fails gracefully
let oracle_data_fail = get_oracle_data(&api, &rpc, "random_heading".encode()).await.unwrap();
assert_eq!(oracle_data_fail.len(), 0);
let oracle_data_fail =
get_oracle_data(&api, &rpc, vec!["random_heading".encode()]).await.unwrap();
assert_eq!(oracle_data_fail.len(), 1);
assert_eq!(oracle_data_fail[0].len(), 0);
}

pub async fn submit_transaction_request(
Expand Down
23 changes: 14 additions & 9 deletions pallets/programs/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,21 @@ fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
benchmarks! {

set_program {
let o in 0 .. T::MaxOracleLookups::get();
let program = vec![10];
let configuration_schema = vec![11];
let auxiliary_data_schema = vec![12];
let oracle_data_pointer = vec![13];
let oracle_data_pointers = BoundedVec::try_from([vec![13u8; o as usize]].to_vec()).unwrap();
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 (_oracle_length, hash_input_with_oracle) =
ProgramsPallet::<T>::get_length_and_hash_of_oracle(&oracle_data_pointers, hash_input).unwrap();

let program_hash = T::Hashing::hash(&hash_input);
let program_hash = T::Hashing::hash(&hash_input_with_oracle);
let deployer: T::AccountId = whitelisted_caller();
let sig_req_account: T::AccountId = whitelisted_caller();

Expand All @@ -65,7 +67,7 @@ benchmarks! {
program.clone(),
configuration_schema.clone(),
auxiliary_data_schema.clone(),
oracle_data_pointer.clone(),
oracle_data_pointers.clone(),
version_number
)
verify {
Expand All @@ -75,34 +77,37 @@ benchmarks! {
program_hash,
configuration_schema,
auxiliary_data_schema,
oracle_data_pointer,
oracle_data_pointers,
version_number
}.into()
);
}

remove_program {
let p in 0..T::MaxOwnedPrograms::get();
let o in 0 .. T::MaxOracleLookups::get();

let program = vec![10];
let configuration_schema = vec![11];
let auxiliary_data_schema = vec![12];
let oracle_data_pointer = vec![13];
let oracle_data_pointers = BoundedVec::try_from([vec![13u8; o as usize]].to_vec()).unwrap();
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 (_oracle_length, hash_input_with_oracle) =
ProgramsPallet::<T>::get_length_and_hash_of_oracle(&oracle_data_pointers, hash_input).unwrap();

let program_hash = T::Hashing::hash(&hash_input);
let program_hash = T::Hashing::hash(&hash_input_with_oracle);
let random_program = vec![11];
let random_hash = T::Hashing::hash(&random_program);
let deployer: T::AccountId = whitelisted_caller();

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, version_number});
<Programs<T>>::insert(program_hash.clone(), ProgramInfo {bytecode: program, configuration_schema, auxiliary_data_schema, oracle_data_pointers, 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
Loading

0 comments on commit 0629ad2

Please sign in to comment.