mirror of
https://github.com/fluencelabs/examples
synced 2025-04-25 10:42:16 +00:00
refactor returns
This commit is contained in:
parent
ae0c40094e
commit
d1e17df96b
@ -2,9 +2,17 @@
|
|||||||
"drand": {
|
"drand": {
|
||||||
"name": "drand",
|
"name": "drand",
|
||||||
"modules": [
|
"modules": [
|
||||||
|
{
|
||||||
|
"name": "curl_adapter",
|
||||||
|
"path": "./artifacts/curl_adapter.wasm",
|
||||||
|
"max_heap_size": "512 KiB",
|
||||||
|
"mounted_binaries": [["curl", "/usr/bin/curl"]],
|
||||||
|
"logger_enabled": true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "drand",
|
"name": "drand",
|
||||||
"path": "./artifacts/drand.wasm"
|
"path": "./artifacts/drand.wasm",
|
||||||
|
"max_heap_size": "1 MiB"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -28,32 +28,37 @@ module_manifest!();
|
|||||||
pub fn main() {}
|
pub fn main() {}
|
||||||
|
|
||||||
#[marine]
|
#[marine]
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
pub struct CResult {
|
||||||
pub struct Chain {
|
pub chains: Vec<String>,
|
||||||
pub hash: String,
|
stderr: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[marine]
|
#[marine]
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
pub struct Chains {
|
pub struct Info {
|
||||||
pub hashes: Vec<Chains>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[marine]
|
|
||||||
pub struct DResult {
|
|
||||||
pub stderr: String,
|
|
||||||
pub stdout: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[marine]
|
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
|
||||||
pub struct DInfo {
|
|
||||||
pub public_key: String,
|
pub public_key: String,
|
||||||
pub period: u64,
|
pub period: u64,
|
||||||
pub genesis_time: u64,
|
pub genesis_time: u64,
|
||||||
pub hash: String,
|
pub hash: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Info {
|
||||||
|
fn empty() -> Self {
|
||||||
|
Info {
|
||||||
|
public_key: "".to_owned(),
|
||||||
|
period: 0,
|
||||||
|
genesis_time: 0,
|
||||||
|
hash: "".to_owned(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[marine]
|
||||||
|
pub struct IResult {
|
||||||
|
info: Info,
|
||||||
|
stderr: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[marine]
|
#[marine]
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
pub struct Randomness {
|
pub struct Randomness {
|
||||||
@ -63,125 +68,146 @@ pub struct Randomness {
|
|||||||
pub previous_signature: String,
|
pub previous_signature: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Randomness {
|
||||||
|
fn empty() -> Self {
|
||||||
|
Randomness {
|
||||||
|
round: 0,
|
||||||
|
randomness: "".to_owned(),
|
||||||
|
signature: "".to_owned(),
|
||||||
|
previous_signature: "".to_owned(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[marine]
|
#[marine]
|
||||||
pub fn chains(url: String, hash_chain: bool) -> DResult {
|
pub struct RResult {
|
||||||
|
pub randomness: Randomness,
|
||||||
|
pub stderr: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[marine]
|
||||||
|
pub struct VResult {
|
||||||
|
verified: bool,
|
||||||
|
randomness: String,
|
||||||
|
stderr: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn curl_cmd(url: String) -> Vec<String> {
|
||||||
|
vec![
|
||||||
|
"-X".to_string(),
|
||||||
|
"GET".to_string(),
|
||||||
|
"-H".to_string(),
|
||||||
|
"Accept: application/json".to_string(),
|
||||||
|
url,
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
#[marine]
|
||||||
|
pub fn chains(url: String) -> CResult {
|
||||||
let url = format!("{}/chains", url);
|
let url = format!("{}/chains", url);
|
||||||
let curl_cmd = vec![
|
let curl_cmd = curl_cmd(url);
|
||||||
"-X".to_string(),
|
|
||||||
"GET".to_string(),
|
|
||||||
"-H".to_string(),
|
|
||||||
"Accept: application/json".to_string(),
|
|
||||||
url,
|
|
||||||
];
|
|
||||||
|
|
||||||
let response = curl_request(curl_cmd);
|
let response = curl_request(curl_cmd);
|
||||||
if response.error.len() > 0 {
|
if response.error.len() > 0 {
|
||||||
return DResult {
|
return CResult {
|
||||||
stderr: response.error.to_string(),
|
stderr: response.error.to_string(),
|
||||||
stdout: "".to_string(),
|
chains: vec![],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
match String::from_utf8(response.clone().stdout) {
|
|
||||||
|
match String::from_utf8(response.stdout) {
|
||||||
Ok(r) => {
|
Ok(r) => {
|
||||||
let obj: Vec<String> = serde_json::from_str(&r).unwrap();
|
let chains: Result<Vec<String>, serde_json::Error> = serde_json::from_str(&r);
|
||||||
if !hash_chain {
|
match chains {
|
||||||
DResult {
|
Ok(r) => CResult {
|
||||||
stdout: r,
|
chains: r,
|
||||||
stderr: "".to_owned(),
|
stderr: "".to_string(),
|
||||||
}
|
},
|
||||||
} else {
|
Err(e) => CResult {
|
||||||
DResult {
|
chains: vec![],
|
||||||
stdout: format!("{}", obj[0]),
|
stderr: e.to_string(),
|
||||||
stderr: "".to_owned(),
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
Err(e) => CResult {
|
||||||
Err(e) => DResult {
|
chains: vec![],
|
||||||
stdout: "".to_owned(),
|
|
||||||
stderr: e.to_string(),
|
stderr: e.to_string(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[marine]
|
#[marine]
|
||||||
pub fn info(url: String, chain_hash: String, pub_key: bool) -> DResult {
|
pub fn info(url: String, chain_hash: String) -> IResult {
|
||||||
let url = format!("{}/{}/info", url, chain_hash);
|
let url = format!("{}/{}/info", url, chain_hash);
|
||||||
let curl_cmd = vec![
|
let curl_cmd = curl_cmd(url);
|
||||||
"-X".to_string(),
|
|
||||||
"GET".to_string(),
|
|
||||||
"-H".to_string(),
|
|
||||||
"Accept: application/json".to_string(),
|
|
||||||
url,
|
|
||||||
];
|
|
||||||
|
|
||||||
let response = curl_request(curl_cmd);
|
let response = curl_request(curl_cmd);
|
||||||
if response.error.len() > 0 {
|
if response.error.len() > 0 {
|
||||||
return DResult {
|
return IResult {
|
||||||
stderr: response.error.to_string(),
|
stderr: response.error.to_string(),
|
||||||
stdout: "".to_string(),
|
info: Info::empty(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
match String::from_utf8(response.clone().stdout) {
|
match String::from_utf8(response.clone().stdout) {
|
||||||
Ok(r) => {
|
Ok(r) => match serde_json::from_str(&r) {
|
||||||
let obj: DInfo = serde_json::from_str(&r).unwrap();
|
Ok(o) => IResult {
|
||||||
if pub_key {
|
info: o,
|
||||||
DResult {
|
stderr: "".to_string(),
|
||||||
stdout: obj.public_key,
|
},
|
||||||
stderr: "".to_owned(),
|
Err(e) => IResult {
|
||||||
|
info: Info::empty(),
|
||||||
|
stderr: e.to_string(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Err(e) => IResult {
|
||||||
|
info: Info::empty(),
|
||||||
|
stderr: e.to_string(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
DResult {
|
|
||||||
stdout: r,
|
pub fn randomness(url: String) -> RResult {
|
||||||
stderr: "".to_owned(),
|
let curl_cmd = curl_cmd(url);
|
||||||
|
|
||||||
|
let response = curl_request(curl_cmd);
|
||||||
|
if response.error.len() > 0 {
|
||||||
|
return RResult {
|
||||||
|
stderr: response.error.to_string(),
|
||||||
|
randomness: Randomness::empty(),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
match String::from_utf8(response.clone().stdout) {
|
||||||
}
|
Ok(r) => match serde_json::from_str(&r) {
|
||||||
Err(e) => DResult {
|
Ok(r) => RResult {
|
||||||
stdout: "".to_owned(),
|
randomness: r,
|
||||||
|
stderr: "".to_string(),
|
||||||
|
},
|
||||||
|
Err(e) => RResult {
|
||||||
|
randomness: Randomness::empty(),
|
||||||
|
stderr: e.to_string(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Err(e) => RResult {
|
||||||
|
randomness: Randomness::empty(),
|
||||||
stderr: e.to_string(),
|
stderr: e.to_string(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[marine]
|
#[marine]
|
||||||
pub fn randomness(url: String, chain_hash: String, round: String) -> DResult {
|
pub fn latest(url: String, chain_hash: String) -> RResult {
|
||||||
let mut uri: String;
|
let url = format!("{}/{}/public/latest", url, chain_hash);
|
||||||
if &round.to_lowercase() == "latest" {
|
randomness(url)
|
||||||
uri = format!("{}/{}/public/latest", url, chain_hash);
|
|
||||||
} else {
|
|
||||||
let round = &round.parse::<u64>().unwrap();
|
|
||||||
uri = format!("{}/{}/public/{}", url, chain_hash, round);
|
|
||||||
}
|
|
||||||
|
|
||||||
let curl_cmd = vec![
|
|
||||||
"-X".to_string(),
|
|
||||||
"GET".to_string(),
|
|
||||||
"-H".to_string(),
|
|
||||||
"Accept: application/json".to_string(),
|
|
||||||
uri,
|
|
||||||
];
|
|
||||||
|
|
||||||
let response = curl_request(curl_cmd);
|
|
||||||
if response.error.len() > 0 {
|
|
||||||
return DResult {
|
|
||||||
stderr: response.error.to_string(),
|
|
||||||
stdout: "".to_string(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
match String::from_utf8(response.clone().stdout) {
|
|
||||||
Ok(r) => DResult {
|
|
||||||
stdout: r,
|
|
||||||
stderr: "".to_owned(),
|
|
||||||
},
|
|
||||||
Err(e) => DResult {
|
|
||||||
stdout: "".to_owned(),
|
|
||||||
stderr: e.to_string(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[marine]
|
#[marine]
|
||||||
pub fn verify_bls(pk: String, round: u64, prev_signature: String, signature: String) -> DResult {
|
pub fn round(url: String, chain_hash: String, round: u64) -> RResult {
|
||||||
|
let url = format!("{}/{}/public/{}", url, chain_hash, round);
|
||||||
|
randomness(url)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[marine]
|
||||||
|
pub fn verify_bls(pk: String, round: u64, prev_signature: String, signature: String) -> VResult {
|
||||||
let hex_pk: [u8; 48] = hex::decode(&pk).unwrap().as_slice().try_into().unwrap();
|
let hex_pk: [u8; 48] = hex::decode(&pk).unwrap().as_slice().try_into().unwrap();
|
||||||
let pk = g1_from_fixed(hex_pk).unwrap();
|
let pk = g1_from_fixed(hex_pk).unwrap();
|
||||||
|
|
||||||
@ -191,23 +217,26 @@ pub fn verify_bls(pk: String, round: u64, prev_signature: String, signature: Str
|
|||||||
let hex_psig = hex::decode(prev_signature).unwrap();
|
let hex_psig = hex::decode(prev_signature).unwrap();
|
||||||
|
|
||||||
match verify(&pk, round, &hex_psig, &hex_sig) {
|
match verify(&pk, round, &hex_psig, &hex_sig) {
|
||||||
Err(err) => DResult {
|
Err(err) => VResult {
|
||||||
stderr: format!("Error during verification: {}", err),
|
stderr: format!("Error during verification: {}", err),
|
||||||
stdout: "".to_string(),
|
verified: false,
|
||||||
|
randomness: "".to_string(),
|
||||||
},
|
},
|
||||||
Ok(valid) => {
|
Ok(valid) => {
|
||||||
println!("ok verify");
|
println!("ok verify");
|
||||||
if valid {
|
if valid {
|
||||||
println!("Verification succeeded");
|
// println!("Verification succeeded");
|
||||||
let randomness = derive_randomness(&hex_sig);
|
let randomness = derive_randomness(&hex_sig);
|
||||||
println!("Randomness: {}", hex::encode(&randomness));
|
// println!("Randomness: {}", hex::encode(&randomness));
|
||||||
DResult {
|
VResult {
|
||||||
stdout: hex::encode(&randomness),
|
verified: valid,
|
||||||
|
randomness: hex::encode(&randomness),
|
||||||
stderr: "".to_string(),
|
stderr: "".to_string(),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
DResult {
|
VResult {
|
||||||
stdout: "".to_string(),
|
verified: false,
|
||||||
|
randomness: "".to_string(),
|
||||||
stderr: format!("Verification failed"),
|
stderr: format!("Verification failed"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -223,22 +252,18 @@ extern "C" {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
// use super::*;
|
|
||||||
use super::Randomness;
|
|
||||||
use marine_rs_sdk_test::marine_test;
|
use marine_rs_sdk_test::marine_test;
|
||||||
|
|
||||||
const URL: &'static str = "https://api.drand.sh";
|
const URL: &'static str = "https://api.drand.sh";
|
||||||
|
|
||||||
#[marine_test(
|
#[marine_test(
|
||||||
config_path = "/Users/bebo/localdev/examples/aqua-examples/drand/services/configs/Config.toml",
|
config_path = "../../configs/Config.toml",
|
||||||
modules_dir = "/Users/bebo/localdev/examples/aqua-examples/drand/services/artifacts/"
|
modules_dir = "../../artifacts"
|
||||||
)]
|
)]
|
||||||
fn test_chain(drand: marine_test_env::drand::ModuleInterface) {
|
fn test_chain(drand: marine_test_env::drand::ModuleInterface) {
|
||||||
let res = drand.chains(URL.to_string(), false);
|
let c_obj = drand.chains(URL.to_string());
|
||||||
assert_eq!(res.stderr.len(), 0);
|
assert_eq!(c_obj.stderr.len(), 0);
|
||||||
|
assert!(c_obj.chains.len() > 0);
|
||||||
let res = drand.chains(URL.to_string(), true);
|
|
||||||
assert_eq!(res.stderr.len(), 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[marine_test(
|
#[marine_test(
|
||||||
@ -256,7 +281,7 @@ mod tests {
|
|||||||
assert_eq!(res.stderr.len(), 0);
|
assert_eq!(res.stderr.len(), 0);
|
||||||
assert!(res.stdout.len() > 0);
|
assert!(res.stdout.len() > 0);
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
#[marine_test(
|
#[marine_test(
|
||||||
config_path = "/Users/bebo/localdev/examples/aqua-examples/drand/services/configs/Config.toml",
|
config_path = "/Users/bebo/localdev/examples/aqua-examples/drand/services/configs/Config.toml",
|
||||||
modules_dir = "/Users/bebo/localdev/examples/aqua-examples/drand/services/artifacts/"
|
modules_dir = "/Users/bebo/localdev/examples/aqua-examples/drand/services/artifacts/"
|
||||||
@ -325,4 +350,5 @@ mod tests {
|
|||||||
// let h: [u8; 48] = hex!("868f005eb8e6e4ca0a47c8a77ceaa5309a47978a7c71bc5cce96366b5d7a569937c529eeda66c7293784a9402801af31");
|
// let h: [u8; 48] = hex!("868f005eb8e6e4ca0a47c8a77ceaa5309a47978a7c71bc5cce96366b5d7a569937c529eeda66c7293784a9402801af31");
|
||||||
println!("hex!: {:?}", PK_LEO_MAINNET);
|
println!("hex!: {:?}", PK_LEO_MAINNET);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user