Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oracle data type generation #100

Merged
merged 8 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions examples/oracle-example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

extern crate alloc;

use alloc::{string::ToString, vec::Vec};
use codec::Decode;
use alloc::{
string::{String, ToString},
vec,
vec::Vec,
};
use codec::{Decode, Encode};
use entropy_programs_core::{bindgen::Error, bindgen::*, export_program, prelude::*};
use serde::{Deserialize, Serialize};
/// JSON-deserializable struct that will be used to derive the program-JSON interface.
Expand All @@ -18,6 +22,9 @@ pub struct UserConfig {}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct AuxData {}

// Oracle data is heading block_number_entropy as I expect that to be passed below to the evaluate function.
pub const ORACLE_DATA: [&str; 1] = ["block_number_entropy"];
JesseAbram marked this conversation as resolved.
Show resolved Hide resolved

// TODO confirm this isn't an issue for audit
register_custom_getrandom!(always_fail);

Expand Down
2 changes: 1 addition & 1 deletion templates/basic-template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ strip = "debuginfo"
crate-type = ["cdylib", "rlib"]

[dependencies]
entropy-programs-core = { git = "https://github.com/entropyxyz/programs.git", tag = "v0.10.0" }
entropy-programs-core = { git = "https://github.com/entropyxyz/programs.git", branch = "master" }
schemars = { version = "0.8.16", optional = true }
serde = { version = "1.0", default-features = false, features = [
"alloc",
Expand Down
15 changes: 15 additions & 0 deletions templates/basic-template/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use dotenv::dotenv;
use entropy_test_cli::{run_command, Cli, PROGRAM_VERSION_NUMBER};
use generate_types::generate_types;
use project_root::get_project_root;
use std::fs;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
Expand All @@ -22,13 +23,27 @@ async fn main() -> anyhow::Result<()> {
get_project_root()?.to_string_lossy()
);

let oracle_data = format!(
"{}/{{project-name}}_serialized_oracle_data_type.txt",
get_project_root()?.to_string_lossy()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come a lossy conversion is being used here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm idk just copying the ones above

);

// length is 1 if empty and can ignore, scale codec length
let decoded_oracle_data = fs::read(oracle_data.clone()).unwrap();
let oracle_option = if decoded_oracle_data.len() == 1 {
None
} else {
Some(oracle_data.into())
};

let cli = Cli::parse();
let json_ouput = cli.json;
match run_command(
cli,
Some(program.into()),
Some(config_interface.into()),
Some(aux_data_interface.into()),
oracle_option,
Some(PROGRAM_VERSION_NUMBER),
)
.await
Expand Down
1 change: 1 addition & 0 deletions templates/basic-template/generate-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ program = { package = "{{project-name}}", path = "..", default-features = false,
] }
schemars = "0.8.16"
serde_json = { version = "1.0" }
codec = { package = "parity-scale-codec", version = "3.6.8", default-features = false }
24 changes: 16 additions & 8 deletions templates/basic-template/generate-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
use schemars::schema_for;
use std::fs;
use program::{UserConfig, AuxData};
use program::{UserConfig, AuxData, ORACLE_DATA};
use codec::Encode;

pub fn generate_types() {
let schema_config = schema_for!(UserConfig);
fs::write(
"./{{project-name}}_serialized_config_type.txt",
"./tests_serialized_config_type.txt",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im a bit confused about this bit - does it need to be changed back to "./{{project-name}}_serialized_config_type.txt" when this generate template is actually used?

format!(
"{:?}",
serde_json::to_vec(&schema_config)
.expect("error converting user config for device key proxy")
.expect("error converting user config")
),
)
.expect("Failed to write to device key proxy config");
.expect("Failed to write to config");

let schema_aux_data = schema_for!(AuxData);
fs::write(
"./{{project-name}}_serialized_aux_data_type.txt",
"./tests_serialized_aux_data_type.txt",
format!(
"{:?}",
serde_json::to_vec(&schema_aux_data)
.expect("error converting user aux_data for device key proxy")
.expect("error converting user aux_data")
),
)
.expect("Failed to write to device key proxy aux_data");
}
.expect("Failed to write to proxy aux_data");

let oracle_data = ORACLE_DATA.iter().map(|x| x.encode()).collect::<Vec<_>>();
fs::write(
"./tests_serialized_oracle_data_type.txt",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i file has a .txt extension i would expect it to be human readable and valid utf-8 - im not sure if scale encoding a vector adds bytes which are invalid utf-8.

oracle_data.encode()
)
.expect("Failed to write oracle_data");
}
3 changes: 3 additions & 0 deletions templates/basic-template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub struct UserConfig {
pub struct AuxData {
}

/// Oracle data used, (change 0 to amount of items in vector)
pub const ORACLE_DATA: [&str; 0] = [];

pub struct {{project-name | upper_camel_case}};

impl Program for {{project-name | upper_camel_case}} {
Expand Down
Loading