Skip to content

Commit

Permalink
Integrate oracle to programs (#788)
Browse files Browse the repository at this point in the history
* Integrate oracle data to core

* integrate oracle runtime for programs

* update entropy programs runtime

* fix wasm tests
  • Loading branch information
JesseAbram authored Apr 29, 2024
1 parent 227710c commit ea8e7e9
Show file tree
Hide file tree
Showing 25 changed files with 108 additions and 27 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

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

Binary file modified crates/shared/device_key_proxy.wasm
Binary file not shown.
14 changes: 11 additions & 3 deletions crates/test-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,16 @@ async fn run_command() -> anyhow::Result<String> {
None => vec![],
};

let hash =
store_program(&api, &rpc, &keypair, program, config_interface, aux_data_interface)
.await?;
let hash = store_program(
&api,
&rpc,
&keypair,
program,
config_interface,
aux_data_interface,
vec![],
)
.await?;
Ok(format!("Program stored {hash}"))
},
CliCommand::UpdatePrograms { signature_verifying_key, program_account_name, programs } => {
Expand Down Expand Up @@ -509,6 +516,7 @@ impl Program {
program_bytecode.clone(),
config_description,
auxiliary_data_schema,
vec![],
)
.await
{
Expand Down
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 removed crates/testing-utils/example_noop.wasm
Binary file not shown.
Binary file modified crates/testing-utils/infinite_loop.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions crates/testing-utils/src/test_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,13 @@ pub async fn store_program(
program: Vec<u8>,
configuration_interface: Vec<u8>,
auxiliary_data_interface: Vec<u8>,
oracle_data_pointer: Vec<u8>,
) -> anyhow::Result<<EntropyConfig as Config>::Hash> {
let update_program_tx = entropy::tx().programs().set_program(
program,
configuration_interface,
auxiliary_data_interface,
oracle_data_pointer,
);
let deployer = PairSigner::<EntropyConfig, sr25519::Pair>::new(deployer_pair.clone());

Expand Down
Binary file added 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 @@ -43,7 +43,7 @@ entropy-kvdb ={ path="../kvdb", default-features=false }
entropy-protocol={ path="../protocol", features=["server"] }

# Programs
entropy-programs-runtime="0.9.0"
entropy-programs-runtime="0.10.0"

# Logging
tracing ="0.1.37"
Expand Down
Binary file modified crates/threshold-signature-server/entropy_metadata.scale
Binary file not shown.
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 @@ -184,7 +184,7 @@ pub async fn sign_tx(
let program = get_program(&api, &rpc, &program_info.program_pointer).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(&program, &signature_request, Some(&program_info.program_config))?;
runtime.evaluate(&program, &signature_request, Some(&program_info.program_config), None)?;
}
// We decided to do Keccak for subgroup selection for frontend compatability
let message_hash_keccak =
Expand Down
24 changes: 20 additions & 4 deletions crates/threshold-signature-server/src/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ async fn test_sign_tx_no_chain() {
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down Expand Up @@ -479,6 +480,7 @@ async fn test_program_with_config() {
TEST_BASIC_TRANSACTION.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down Expand Up @@ -541,6 +543,7 @@ async fn test_fail_signing_group() {
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down Expand Up @@ -612,6 +615,7 @@ async fn test_store_share() {
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down Expand Up @@ -830,6 +834,7 @@ async fn test_send_and_receive_keys() {
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down Expand Up @@ -1064,6 +1069,7 @@ async fn test_sign_tx_user_participates() {
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down Expand Up @@ -1317,6 +1323,7 @@ async fn test_register_with_private_key_visibility() {
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down Expand Up @@ -1401,10 +1408,17 @@ async fn test_compute_hash() {
let rpc = get_rpc(&substrate_context.node_proc.ws_url).await.unwrap();

let mut runtime = Runtime::default();
let program_hash =
store_program(&api, &rpc, &one.pair(), TEST_PROGRAM_CUSTOM_HASH.to_owned(), vec![], vec![])
.await
.unwrap();
let program_hash = store_program(
&api,
&rpc,
&one.pair(),
TEST_PROGRAM_CUSTOM_HASH.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();

let message_hash = compute_hash(
&api,
Expand Down Expand Up @@ -1486,6 +1500,7 @@ async fn test_fail_infinite_program() {
TEST_INFINITE_LOOP_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down Expand Up @@ -1682,6 +1697,7 @@ async fn test_mutiple_confirm_done() {
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down
2 changes: 2 additions & 0 deletions crates/threshold-signature-server/tests/protocol_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ async fn test_wasm_sign_tx_user_participates() {
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down Expand Up @@ -224,6 +225,7 @@ async fn test_wasm_register_with_private_key_visibility() {
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down
2 changes: 2 additions & 0 deletions crates/threshold-signature-server/tests/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ async fn integration_test_sign_public() {
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down Expand Up @@ -118,6 +119,7 @@ async fn integration_test_sign_private() {
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions crates/threshold-signature-server/tests/sign_eth_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ async fn integration_test_sign_eth_tx() {
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();
Expand Down
11 changes: 8 additions & 3 deletions pallets/programs/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ benchmarks! {
let program = vec![10];
let configuration_schema = vec![11];
let auxiliary_data_schema = vec![12];
let oracle_data_pointer = vec![13];
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);

let program_hash = T::Hashing::hash(&hash_input);
let deployer: T::AccountId = whitelisted_caller();
Expand All @@ -56,14 +58,15 @@ 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())
}: _(RawOrigin::Signed(deployer.clone()), program.clone(), configuration_schema.clone(), auxiliary_data_schema.clone(), oracle_data_pointer.clone())
verify {
assert_last_event::<T>(
Event::<T>::ProgramCreated {
deployer,
program_hash,
configuration_schema,
auxiliary_data_schema
auxiliary_data_schema,
oracle_data_pointer
}.into()
);
}
Expand All @@ -73,10 +76,12 @@ benchmarks! {
let program = vec![10];
let configuration_schema = vec![11];
let auxiliary_data_schema = vec![12];
let oracle_data_pointer = vec![13];
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);

let program_hash = T::Hashing::hash(&hash_input);
let random_program = vec![11];
Expand All @@ -85,7 +90,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, 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});
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
15 changes: 14 additions & 1 deletion pallets/programs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub mod pallet {
configuration_schema: program_info.2.clone(),
auxiliary_data_schema: program_info.3.clone(),
deployer: program_info.4.clone(),
oracle_data_pointer: vec![],
ref_counter: program_info.5,
},
);
Expand Down Expand Up @@ -136,6 +137,8 @@ pub mod pallet {
/// [JSON Schema](https://json-schema.org/) in order to simplify the life of off-chain
/// actors.
pub auxiliary_data_schema: Vec<u8>,
/// The location of the oracle data needed for this program
pub oracle_data_pointer: Vec<u8>,
/// Deployer of the program
pub deployer: AccountId,
/// Accounts that use this program
Expand Down Expand Up @@ -176,6 +179,9 @@ pub mod pallet {

/// The new program auxiliary data schema.
auxiliary_data_schema: Vec<u8>,

/// The oracle data location needed for the program
oracle_data_pointer: Vec<u8>,
},
/// The bytecode of a program was removed.
ProgramRemoved {
Expand Down Expand Up @@ -217,18 +223,22 @@ pub mod pallet {
new_program: Vec<u8>,
configuration_schema: Vec<u8>,
auxiliary_data_schema: Vec<u8>,
oracle_data_pointer: Vec<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);
let program_hash = T::Hashing::hash(&hash_input);
let new_program_length = new_program
.len()
.checked_add(configuration_schema.len())
.ok_or(Error::<T>::ArithmeticError)?
.checked_add(auxiliary_data_schema.len())
.ok_or(Error::<T>::ArithmeticError)?
.checked_add(oracle_data_pointer.len())
.ok_or(Error::<T>::ArithmeticError)?;
ensure!(
new_program_length as u32 <= T::MaxBytecodeLength::get(),
Expand All @@ -244,6 +254,7 @@ pub mod pallet {
bytecode: new_program.clone(),
configuration_schema: configuration_schema.clone(),
auxiliary_data_schema: auxiliary_data_schema.clone(),
oracle_data_pointer: oracle_data_pointer.clone(),
deployer: deployer.clone(),
ref_counter: 0u128,
},
Expand All @@ -262,6 +273,7 @@ pub mod pallet {
program_hash,
configuration_schema,
auxiliary_data_schema,
oracle_data_pointer,
});
Ok(())
}
Expand All @@ -284,7 +296,8 @@ pub mod pallet {
&old_program_info.deployer,
old_program_info.bytecode.len()
+ old_program_info.configuration_schema.len()
+ old_program_info.auxiliary_data_schema.len(),
+ old_program_info.auxiliary_data_schema.len()
+ old_program_info.oracle_data_pointer.len(),
);
let mut owned_programs_length = 0;
OwnedPrograms::<T>::try_mutate(
Expand Down
2 changes: 1 addition & 1 deletion pallets/programs/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl system::Config for Test {
}

parameter_types! {
pub const MaxBytecodeLength: u32 = 4;
pub const MaxBytecodeLength: u32 = 5;
pub const ProgramDepositPerByte: u32 = 5;
pub const MaxOwnedPrograms: u32 = 1;
}
Expand Down
Loading

0 comments on commit ea8e7e9

Please sign in to comment.