Skip to content

Commit

Permalink
Add command to get oracle headings (#1170)
Browse files Browse the repository at this point in the history
* Add command to get oracle headings

* Tidy, changelog

* Wait an extra block

* test gets finalized blocks

---------

Co-authored-by: Jesse Abramowitz <[email protected]>
  • Loading branch information
ameba23 and JesseAbram authored Nov 22, 2024
1 parent 5e357c2 commit ceb378a
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ runtime

### 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))

### Changed
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

22 changes: 12 additions & 10 deletions crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ futures ="0.3"
sp-core ={ version="31.0.0", default-features=false, features=["full_crypto", "serde"] }
tracing ="0.1.37"
rand ={ version="0.8", default-features=false }
anyhow ="1.0.93"

# Present when "full-client" feature is active
blake2 ={ version="0.10.4", optional=true }
rand_core ={ version="0.6.4", optional=true }
serde_json ={ version="1.0", optional=true }
x25519-dalek ={ version="2.0.1", features=["static_secrets"], optional=true }
entropy-protocol={ version="0.3.0", path="../protocol", optional=true, default-features=false }
reqwest ={ version="0.12.9", features=["json", "stream"], optional=true }
base64 ={ version="0.22.0", optional=true }
synedrion ={ version="0.2.0-beta.0", optional=true }
hex ={ version="0.4.3", optional=true }
anyhow ="1.0.93"
blake2 ={ version="0.10.4", optional=true }
rand_core ={ version="0.6.4", optional=true }
serde_json ={ version="1.0", optional=true }
x25519-dalek ={ version="2.0.1", features=["static_secrets"], optional=true }
entropy-protocol ={ version="0.3.0", path="../protocol", optional=true, default-features=false }
reqwest ={ version="0.12.9", features=["json", "stream"], optional=true }
base64 ={ version="0.22.0", optional=true }
synedrion ={ version="0.2.0-beta.0", optional=true }
hex ={ version="0.4.3", optional=true }
parity-scale-codec={ version="3.6.3", default-features=false, optional=true }

# Only for the browser
js-sys={ version="0.3.72", optional=true }
Expand Down Expand Up @@ -64,6 +65,7 @@ full-client=[
"dep:base64",
"dep:synedrion",
"dep:hex",
"dep:parity-scale-codec",
]
full-client-native=["full-client", "entropy-protocol/server"]
full-client-wasm=["full-client", "entropy-protocol/wasm", "dep:js-sys"]
19 changes: 19 additions & 0 deletions crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use crate::{
};
use anyhow::anyhow;
pub use entropy_protocol::{sign_and_encrypt::EncryptedSignedMessage, KeyParams};
use parity_scale_codec::Decode;
use rand::Rng;
use std::str::FromStr;
pub use synedrion::KeyShare;
Expand Down Expand Up @@ -444,3 +445,21 @@ pub async fn request_attestation(
) -> Result<[u8; 32], ClientError> {
Ok(user::request_attestation(api, rpc, attestee).await?)
}

/// 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<EntropyConfig>,
_rpc: &LegacyRpcMethods<EntropyConfig>,
) -> Result<Vec<String>, ClientError> {
let storage_address = entropy::storage().oracle().oracle_data_iter();
let mut iter = api.storage().at_latest().await?.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);
}
Ok(headings)
}
2 changes: 2 additions & 0 deletions crates/client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ pub enum ClientError {
BadVerifyingKeyLength,
#[error("There are no validators which can act as a relay node for signature requests")]
NoNonSigningValidators,
#[error("Scale decode: {0}")]
Codec(#[from] parity_scale_codec::Error),
#[error("Attestation request: {0}")]
AttestationRequest(#[from] AttestationRequestError),
}
23 changes: 21 additions & 2 deletions crates/client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{
},
get_api, get_rpc, EntropyConfig,
},
change_endpoint, change_threshold_accounts, register, remove_program, request_attestation,
store_program,
change_endpoint, change_threshold_accounts, get_oracle_headings, register, remove_program,
request_attestation, store_program,
substrate::query_chain,
update_programs,
};
Expand Down Expand Up @@ -274,3 +274,22 @@ async fn test_remove_program_reference_counter() {
// We can now remove the program because no-one is using it
remove_program(&api, &rpc, &program_owner, program_pointer).await.unwrap();
}

#[tokio::test]
#[serial]
async fn test_get_oracle_headings() {
let force_authoring = true;
let substrate_context = &test_node_process_testing_state(force_authoring).await[0];
let api = get_api(&substrate_context.ws_url).await.unwrap();
let rpc = get_rpc(&substrate_context.ws_url).await.unwrap();

let mut current_block = 0;
while current_block < 2 {
let finalized_head = rpc.chain_get_finalized_head().await.unwrap();
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()]);
}
14 changes: 11 additions & 3 deletions crates/test-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ use entropy_client::{
EntropyConfig,
},
client::{
change_endpoint, change_threshold_accounts, get_accounts, get_api, get_programs, get_rpc,
jumpstart_network, register, remove_program, sign, store_program, update_programs,
VERIFYING_KEY_LENGTH,
change_endpoint, change_threshold_accounts, get_accounts, get_api, get_oracle_headings,
get_programs, get_rpc, jumpstart_network, register, remove_program, sign, store_program,
update_programs, VERIFYING_KEY_LENGTH,
},
};
pub use entropy_shared::PROGRAM_VERSION_NUMBER;
Expand Down Expand Up @@ -189,6 +189,10 @@ enum CliCommand {
#[arg(short, long)]
mnemonic_option: Option<String>,
},
/// Get headings of oracle data
///
/// This is useful for program developers to know what oracle data is available.
GetOracleHeadings,
/// Request a TDX quote from a TSS server and write it to a file.
GetTdxQuote {
/// The socket address of the TS server, eg: `127.0.0.1:3002`
Expand Down Expand Up @@ -568,6 +572,10 @@ pub async fn run_command(
Ok("Succesfully jumpstarted network.".to_string())
}
},
CliCommand::GetOracleHeadings => {
let headings = get_oracle_headings(&api, &rpc).await?;
Ok(serde_json::to_string_pretty(&headings)?)
},
CliCommand::GetTdxQuote { tss_endpoint, output_filename } => {
let quote_bytes =
reqwest::get(format!("http://{}/attest", tss_endpoint)).await?.bytes().await?;
Expand Down

0 comments on commit ceb378a

Please sign in to comment.