From ddf71841fb8676c2267b2cc573404b54275c7592 Mon Sep 17 00:00:00 2001 From: Jesse Abramowitz Date: Fri, 22 Nov 2024 13:40:58 -0500 Subject: [PATCH 1/5] Add type info to oracle data --- pallets/oracle/src/lib.rs | 18 +++++++++++++++--- pallets/oracle/src/tests.rs | 9 +++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pallets/oracle/src/lib.rs b/pallets/oracle/src/lib.rs index ec1898f03..ffbac4847 100644 --- a/pallets/oracle/src/lib.rs +++ b/pallets/oracle/src/lib.rs @@ -55,13 +55,22 @@ pub mod module { type WeightInfo: WeightInfo; } + /// Oracle Info for oracle data + #[derive(Clone, Encode, Decode, Eq, PartialEqNoBound, RuntimeDebug, TypeInfo)] + #[scale_info(skip_type_params(T))] + pub struct OracleInfo { + pub oracle_data: BoundedVec, + pub oracle_type: Vec, + } + + /// Storage for oracle info to be passed to programs. #[pallet::storage] #[pallet::getter(fn oracle_data)] pub type OracleData = StorageMap< _, Blake2_128Concat, BoundedVec, - BoundedVec, + OracleInfo, OptionQuery, >; @@ -92,8 +101,11 @@ pub mod module { OracleData::::insert( BoundedVec::try_from("block_number_entropy".encode()) .expect("Key fits in bounded vec; qed"), - BoundedVec::try_from(block_number.encode()) - .expect("Block number fits in bounded vec; qed"), + OracleInfo { + oracle_data: BoundedVec::try_from(block_number.encode()) + .expect("Block number fits in bounded vec; qed"), + oracle_type: "u32".as_bytes().to_vec(), + }, ); T::WeightInfo::on_initialize() } diff --git a/pallets/oracle/src/tests.rs b/pallets/oracle/src/tests.rs index b823ed013..71a2d42cf 100644 --- a/pallets/oracle/src/tests.rs +++ b/pallets/oracle/src/tests.rs @@ -31,10 +31,11 @@ fn test_set_block_number() { >::on_initialize(50); - assert_eq!( + let oracle_info = Oracle::oracle_data(BoundedVec::try_from("block_number_entropy".encode()).unwrap()) - .unwrap()[0], - 50 - ); + .unwrap(); + + assert_eq!(u32::decode(&mut oracle_info.oracle_data.as_ref()).unwrap(), 50u32); + assert_eq!(std::str::from_utf8(&oracle_info.oracle_type).unwrap().to_string(), "u32"); }); } From 349db12e70a9a370f99ccb9b3a5004c520b5ce95 Mon Sep 17 00:00:00 2001 From: Jesse Abramowitz Date: Fri, 22 Nov 2024 15:05:44 -0500 Subject: [PATCH 2/5] update tss for oracle info --- CHANGELOG.md | 3 +++ crates/client/entropy_metadata.scale | Bin 209874 -> 210048 bytes .../src/helpers/substrate.rs | 10 ++++++---- pallets/oracle/src/lib.rs | 1 + 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5d4bfdbe..ddb31ae98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,11 +24,14 @@ At the moment this project **does not** adhere to - 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 +- In [#1184](https://github.com/entropyxyz/entropy-core/pull/1184/) The ```OracleData``` mapping now holds a +struct ```OracleInfo``` which includes the ```oracle_data``` and the ```oracle_type``` ### Added - Protocol message versioning ([#1140](https://github.com/entropyxyz/entropy-core/pull/1140)) - CLI command to get oracle headings ([#1170](https://github.com/entropyxyz/entropy-core/pull/1170)) - Add TSS endpoint to get TDX quote ([#1173](https://github.com/entropyxyz/entropy-core/pull/1173)) +- Add type info to oracle data ([#1184](https://github.com/entropyxyz/entropy-core/pull/1184)) ### Changed - Use correct key rotation endpoint in OCW ([#1104](https://github.com/entropyxyz/entropy-core/pull/1104)) diff --git a/crates/client/entropy_metadata.scale b/crates/client/entropy_metadata.scale index 4ee7c5daedbcf28523aa3038eac99f16ffb8a0cd..9d55c8bcdc2088d8b7c386b45b53814d7a738320 100644 GIT binary patch delta 1356 zcmZWpZ%A8L6u;-(H=A2DCTcXct_u#M=~5S0U9D4BtvZDIuNt?02<>Zfo1`Xrn!Jel z;A+6Z#wbimcM&Q@!9qVs@q;{C=%}Ub=7%yYY#^iiwv7$8(LuX1yKeWH{d2l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KZIu8Vb7z+`)t{M3=pt zg^UM+4t4Diw(`e^u$r|ZtkrtR6`9*!@bY8@9^g|&*in0dT*nR5O2o%fmlETq6^$7+ zeU26C8(^j9<>ex*gAjkMOz9B8(Lp18JUC$`qGmE0evxO3(9iy2>@8J&XrfaQhg3rt zBB67t7@G$T()_(+Ry1yET0iInF3-8RX(*uQVmxMOLRWYYBC2qGMsj@Aaw*)D7KE6> zx_U=6F{Qvlb(zpa%mK$ZQ;Kz5<-rEH#$6t)s~#tKb-)}k<8Pb&3EfQU;atv~@-+wi zkq1AAamRnB1bZRHUzK1zyvy4q*rH5LDf<1>DuKxT@I4ah2+d_+rleo4gkgmWsSm!$x*wM0{mtPuz5fIfLJlP)9vM z=feW=z~1uX>_Mn!y%yWCndUCLE9_5e@y2h30#XNnP(Zz%E|Vu=vYDP2+7gcAKUy2r zoID*d!Xp>QjPRh5NUF7HC(qHHSpURs$0*<+dg2L7AB|e66BKCL zL8$c&8$n99?%ZB8qE{nUH>EG1j_8T2#?JDUINNt#PsP-x^icW&_j~0z7~or8c~8?x z`+#rzq>rb4@-P_oichY93{jMa{BldCXo#P!l*5GsoYa;{yT_G{@@f9NK~}*s zdmH74u)+%Tvi(b={267&;Z~#lN2@H7uxiVIJcF(fxknAtbC-KNWaa<2HG8Z>z7KGp Lv(L$C`GCZK^bKFg diff --git a/crates/threshold-signature-server/src/helpers/substrate.rs b/crates/threshold-signature-server/src/helpers/substrate.rs index ee8a27df5..0ec2099c8 100644 --- a/crates/threshold-signature-server/src/helpers/substrate.rs +++ b/crates/threshold-signature-server/src/helpers/substrate.rs @@ -19,7 +19,8 @@ use crate::{ entropy::{ self, runtime_types::{ - bounded_collections::bounded_vec::BoundedVec, pallet_programs::pallet::ProgramInfo, + bounded_collections::bounded_vec::BoundedVec, pallet_oracle::module::OracleInfo, + pallet_programs::pallet::ProgramInfo, }, }, EntropyConfig, @@ -70,9 +71,10 @@ pub async fn get_oracle_data( 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); + let oracle_info = query_chain(api, rpc, oracle_data_call, None) + .await? + .unwrap_or(OracleInfo { oracle_data: BoundedVec(vec![]), oracle_type: vec![] }); + oracle_infos.push(oracle_info.oracle_data.0); } Ok(oracle_infos) } diff --git a/pallets/oracle/src/lib.rs b/pallets/oracle/src/lib.rs index ffbac4847..c9eba75b1 100644 --- a/pallets/oracle/src/lib.rs +++ b/pallets/oracle/src/lib.rs @@ -26,6 +26,7 @@ use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; +use sp_std::vec::Vec; #[cfg(test)] mod mock; From 7d6edc70836257fd5dba19cf582f86af9aee78ec Mon Sep 17 00:00:00 2001 From: Jesse Abramowitz Date: Fri, 22 Nov 2024 15:23:37 -0500 Subject: [PATCH 3/5] fix benchmarks --- pallets/oracle/src/benchmarking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/oracle/src/benchmarking.rs b/pallets/oracle/src/benchmarking.rs index 3f3713c6b..74991ce59 100644 --- a/pallets/oracle/src/benchmarking.rs +++ b/pallets/oracle/src/benchmarking.rs @@ -26,7 +26,7 @@ benchmarks! { }: { Oracle::::on_initialize(50u32.into()); } verify { - assert_eq!(OracleData::::get(BoundedVec::try_from("block_number_entropy".encode()).unwrap()).unwrap()[0], 50); + assert_eq!(OracleData::::get(BoundedVec::try_from("block_number_entropy".encode()).unwrap()).unwrap().oracle_data[0], 50); } impl_benchmark_test_suite!(Oracle, crate::mock::new_test_ext(), crate::mock::Test); From c2420e540df5e9a81d79b1c2f7e598a62aeab9e3 Mon Sep 17 00:00:00 2001 From: Jesse Abramowitz Date: Mon, 2 Dec 2024 14:46:11 -0500 Subject: [PATCH 4/5] update test cli --- crates/client/src/client.rs | 24 +++++++++++++++++++----- crates/client/src/errors.rs | 2 ++ crates/client/src/tests.rs | 11 +++++++---- crates/test-cli/src/lib.rs | 4 ++-- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs index 062de590a..c3f7e4842 100644 --- a/crates/client/src/client.rs +++ b/crates/client/src/client.rs @@ -50,6 +50,7 @@ use base64::prelude::{Engine, BASE64_STANDARD}; use entropy_protocol::RecoverableSignature; use entropy_shared::HashingAlgorithm; use futures::stream::StreamExt; +use serde::{Deserialize, Serialize}; use sp_core::{ sr25519::{self, Signature}, Pair, @@ -446,20 +447,33 @@ pub async fn request_attestation( Ok(user::request_attestation(api, rpc, attestee).await?) } +/// Return type for getting oracle headings. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct OracleDataReturn { + pub oracle_heading: String, + pub oracle_type: String, +} /// Get oracle data headings /// This is useful for program developers to know what oracle data is available pub async fn get_oracle_headings( api: &OnlineClient, - _rpc: &LegacyRpcMethods, -) -> Result, ClientError> { + rpc: &LegacyRpcMethods, +) -> Result, ClientError> { let storage_address = entropy::storage().oracle().oracle_data_iter(); - let mut iter = api.storage().at_latest().await?.iter(storage_address).await?; + let block_hash = rpc + .chain_get_block_hash(None) + .await? + .ok_or(ClientError::ChainFetch("Failed to get block hash"))?; + let mut iter = api.storage().at(block_hash).iter(storage_address).await?; let mut headings = Vec::new(); while let Some(Ok(kv)) = iter.next().await { // Key is: storage_address || 128 bit hash || key let mut input = &kv.key_bytes[32 + 16 + 1..]; - let heading = String::decode(&mut input)?; - headings.push(heading); + let oracle_heading = String::decode(&mut input)?; + headings.push(OracleDataReturn { + oracle_heading, + oracle_type: std::str::from_utf8(&kv.value.oracle_type)?.to_string(), + }); } Ok(headings) } diff --git a/crates/client/src/errors.rs b/crates/client/src/errors.rs index c2f4ab241..42f6e5d52 100644 --- a/crates/client/src/errors.rs +++ b/crates/client/src/errors.rs @@ -123,4 +123,6 @@ pub enum ClientError { Codec(#[from] parity_scale_codec::Error), #[error("Attestation request: {0}")] AttestationRequest(#[from] AttestationRequestError), + #[error("Chain Fetch: {0}")] + ChainFetch(&'static str), } diff --git a/crates/client/src/tests.rs b/crates/client/src/tests.rs index 1663dbedb..e062475b7 100644 --- a/crates/client/src/tests.rs +++ b/crates/client/src/tests.rs @@ -14,7 +14,7 @@ use crate::{ change_endpoint, change_threshold_accounts, get_oracle_headings, register, remove_program, request_attestation, store_program, substrate::query_chain, - update_programs, + update_programs, OracleDataReturn, }; use entropy_shared::{QuoteContext, QuoteInputData}; @@ -284,7 +284,10 @@ async fn test_get_oracle_headings() { current_block = rpc.chain_get_header(Some(finalized_head)).await.unwrap().unwrap().number; } - let headings = get_oracle_headings(&api, &rpc).await.unwrap(); - - assert_eq!(headings, vec!["block_number_entropy".to_string()]); + let oracle_headings = get_oracle_headings(&api, &rpc).await.unwrap(); + let mock_data = OracleDataReturn { + oracle_heading: "block_number_entropy".to_string(), + oracle_type: "u32".to_string(), + }; + assert_eq!(oracle_headings, vec![mock_data]); } diff --git a/crates/test-cli/src/lib.rs b/crates/test-cli/src/lib.rs index 8b76fe5f8..d4e18eb4c 100644 --- a/crates/test-cli/src/lib.rs +++ b/crates/test-cli/src/lib.rs @@ -573,8 +573,8 @@ pub async fn run_command( } }, CliCommand::GetOracleHeadings => { - let headings = get_oracle_headings(&api, &rpc).await?; - Ok(serde_json::to_string_pretty(&headings)?) + let oracles_data = get_oracle_headings(&api, &rpc).await?; + Ok(format!("{:?}", oracles_data)) }, CliCommand::GetTdxQuote { tss_endpoint, output_filename } => { let quote_bytes = From fef7477de7e93fdb41b76d488dd026c2742f9fcf Mon Sep 17 00:00:00 2001 From: Jesse Abramowitz Date: Tue, 3 Dec 2024 10:56:39 -0500 Subject: [PATCH 5/5] cli json --- crates/test-cli/src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/test-cli/src/lib.rs b/crates/test-cli/src/lib.rs index d4e18eb4c..4d82ab622 100644 --- a/crates/test-cli/src/lib.rs +++ b/crates/test-cli/src/lib.rs @@ -574,7 +574,11 @@ pub async fn run_command( }, CliCommand::GetOracleHeadings => { let oracles_data = get_oracle_headings(&api, &rpc).await?; - Ok(format!("{:?}", oracles_data)) + if cli.json { + Ok(serde_json::to_string_pretty(&oracles_data)?) + } else { + Ok(format!("{:?}", oracles_data)) + } }, CliCommand::GetTdxQuote { tss_endpoint, output_filename } => { let quote_bytes =