Skip to content

Commit

Permalink
refactor(aot): start adding timeline steps for execution
Browse files Browse the repository at this point in the history
  • Loading branch information
gluax committed May 10, 2024
1 parent c07234b commit e71ac8d
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 22 deletions.
49 changes: 32 additions & 17 deletions crates/snops/src/cannon/authorized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ pub enum Authorize {
private_key: String,
recipient: String,
amount: u64,
priority_fee: u64,
},
Other {
private_key: String,
program_id: String,
function_name: String,
inputs: Vec<String>,
},
}

Expand All @@ -21,29 +26,45 @@ impl Authorize {
command
.stdout(std::io::stdout())
.stderr(std::io::stderr())
.arg("program")
.arg("authorize");

match self {
Self::TransferPublic {
private_key,
recipient,
amount,
priority_fee,
} => {
command
.arg("--private-key")
.arg(private_key)
.arg("--program-id")
.arg("credits.aleo")
.arg("--function-name")
.arg("transfer-public")
.arg("--inputs")
.args([recipient, format!("{amount}u64")]);
}
Self::Other {
private_key,
program_id,
function_name,
inputs,
} => {
command
.arg("other")
.arg("--private-key")
.arg(private_key)
.arg("--recipient")
.arg(recipient)
.arg("--amount")
.arg(amount.to_string())
.arg("--priority-fee")
.arg(priority_fee.to_string());
.arg("--program-id")
.arg(program_id)
.arg("--function-name")
.arg(function_name)
.arg("--inputs")
.args(inputs);
}
}

command.arg("--broadcast");
// command.arg("--broadcast");

let res = command.output().await.map_err(|e| {
AuthorizeError::Command(CommandError::action("output", "aot authorize", e))
Expand All @@ -60,18 +81,12 @@ impl Authorize {
let blob: serde_json::Value =
serde_json::from_slice(&res.stdout).map_err(AuthorizeError::Json)?;

// TODO consider making a type for this json object
dbg!(&blob);

if !blob.is_object() {
Err(AuthorizeError::JsonNotObject)?;
}

if blob.get("function").is_none()
|| blob.get("broadcast").is_none()
|| blob.get("fee").is_none()
{
Err(AuthorizeError::InvalidJson)?;
}

Ok(blob)
}
}
3 changes: 0 additions & 3 deletions crates/snops/src/cannon/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ pub enum AuthorizeError {
#[error(transparent)]
Command(#[from] CommandError),
/// For if invalid JSON is returned from the AOT command
#[error("expected function, fee, and broadcast fields in response")]
InvalidJson,
/// For if invalid JSON is returned from the AOT command
#[error("{0}")]
Json(#[source] serde_json::Error),
/// For if invalid JSON is returned from the AOT command
Expand Down
1 change: 1 addition & 0 deletions crates/snops/src/cannon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ impl ExecutionContext {
};
trace!("cannon {}.{} generating authorization...", env.id, self.id);

// TODO this is only the function auth rn.
let auth = self.source.get_auth(&env)?.run(&env.aot_bin).await?;
self.auth_sender
.send(auth)
Expand Down
3 changes: 2 additions & 1 deletion crates/snops/src/cannon/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,15 @@ impl TxSource {
private_key: sample_pk()?,
recipient: sample_addr()?,
amount: 1,
priority_fee: 0,
},
CreditsTxMode::TransferPublicToPrivate => todo!(),
CreditsTxMode::TransferPrivate => todo!(),
CreditsTxMode::TransferPrivateToPublic => todo!(),
},
};

// TODO this no longer does the fee as well

Ok(auth)
}
_ => Err(SourceError::CannotAuthorizePlaybackTx.into()),
Expand Down
7 changes: 7 additions & 0 deletions crates/snops/src/env/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ impl Environment {
}
}
}
Action::Execute(execute) => match execute {
// TODO
crate::schema::timeline::Execute::Program { .. } => todo!(),
crate::schema::timeline::Execute::Transaction { .. } => {
todo!()
}
},
};
}

Expand Down
35 changes: 34 additions & 1 deletion crates/snops/src/schema/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{
};
use snops_common::state::{CannonId, DocHeightRequest, InternedId, NodeKey};

use super::NodeTargets;
use super::{NodeTarget, NodeTargets};

/// A document describing a test's event timeline.
#[derive(Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -50,6 +50,38 @@ pub enum Action {
Cannon(Vec<SpawnCannon>),
/// Set the height of some nodes' ledgers
Config(IndexMap<NodeTargets, Reconfig>),
/// Execute
Execute(Execute),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum Execute {
/// Execute a program
#[serde(rename_all = "kebab-case")]
Program {
private_key: String,
/// The program to execute
program: String,
/// The function to call
function: String,
/// The target node defaults to the source node
target: Option<NodeTarget>,
/// The inputs to the function
inputs: Vec<String>,
/// The optional priority fee
priority_fee: Option<u64>,
/// The optional fee record for a private fee
fee_record: Option<String>,
},
Transaction {
/// The transaction to execute
tx: String,
/// The cannon id of who to execute the transaction
cannon: CannonId,
/// The target node defaults to the source node
target: Option<NodeTarget>,
},
}

impl<'de> Deserialize<'de> for Actions {
Expand Down Expand Up @@ -83,6 +115,7 @@ impl<'de> Deserialize<'de> for Actions {
"offline" => Action::Offline(map.next_value()?),
"cannon" => Action::Cannon(map.next_value()?),
"config" => Action::Config(map.next_value()?),
"execute" => Action::Execute(map.next_value()?),

_ => return Err(A::Error::custom(format!("unsupported action {key}"))),
},
Expand Down
25 changes: 25 additions & 0 deletions crates/snops/src/schema/timeline_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ fn test_timeline_config_serde() {
assert!(serde_yaml::from_str::<TimelineEvent>(yaml).is_ok());
}

#[test]
fn test_timeline_execute() {
let yaml = r#"
execute:
private-key: committee.0.private
program: credits.aleo
function: transfer_public
target: validator/1
inputs:
- 0u64
- committee.1.public
priority-fee: 13
fee-record: "fee_record_json"
duration: 1m
"#;
assert!(serde_yaml::from_str::<TimelineEvent>(yaml).is_ok());

let yaml = r#"
execute:
tx: txdetails
cannon: cannonfoo
"#;
assert!(serde_yaml::from_str::<TimelineEvent>(yaml).is_ok());
}

#[test]
fn test_reconfig_serde() {
let reconfig = Reconfig {
Expand Down

0 comments on commit e71ac8d

Please sign in to comment.