Skip to content

Commit

Permalink
Oracle data type generation (#100)
Browse files Browse the repository at this point in the history
* Oracle data type generation

* update template

* fix oracle decoding issue

* Update templates/basic-template/generate-types/Cargo.toml

Co-authored-by: Hernando Castano <[email protected]>

* Update examples/oracle-example/src/lib.rs

Co-authored-by: Hernando Castano <[email protected]>

* comments

* put back to master

* fix scale codec version

---------

Co-authored-by: Hernando Castano <[email protected]>
  • Loading branch information
JesseAbram and HCastano authored Dec 20, 2024
1 parent de27c61 commit c351230
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
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"];

// 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()
);

// 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",
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",
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

0 comments on commit c351230

Please sign in to comment.