Skip to content

Commit

Permalink
fix WG's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
runtian-zhou committed Oct 21, 2024
1 parent 410bed9 commit 6ad13e6
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 130 deletions.
56 changes: 17 additions & 39 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ tonic = { version = "0.11.0", features = [
] }
tonic-reflection = "0.11.0"
triomphe = "0.1.9"
tsify-next = "0.5.4"
tui = "0.19.0"
typed-arena = "2.0.2"
typenum = "1.17.0"
Expand All @@ -807,6 +808,8 @@ variant_count = "1.1.0"
walkdir = "2.3.3"
warp = { version = "0.3.5", features = ["tls"] }
warp-reverse-proxy = "1.0.0"
wasm-bindgen = "0.2.95"
wasm-bindgen-futures = "0.4.42"
which = "4.2.5"
whoami = "1.5.0"
x25519-dalek = "1.2.0"
Expand Down
8 changes: 4 additions & 4 deletions aptos-move/dynamic-transaction-composer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ hex = { workspace = true }
move-binary-format = { workspace = true }
move-bytecode-verifier = { workspace = true }
move-core-types = { workspace = true }
reqwest = { version = "*", features = ["blocking"] }
reqwest = { workspace = true, features = ["blocking"] }
serde = { workspace = true }
serde_bytes = { workspace = true }
serde_json = { workspace = true }
tsify-next = "0.5.4"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4.42"
tsify-next = { workspace = true }
wasm-bindgen = { workspace = true }
wasm-bindgen-futures = { workspace = true }

[dev-dependencies]
aptos-types = { workspace = true }
Expand Down
51 changes: 31 additions & 20 deletions aptos-move/dynamic-transaction-composer/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
//! and get the corresponding `CompiledScript` via the `generate_batched_calls` api.
#[cfg(test)]
use crate::BatchedFunctionCall;
use crate::MoveFunctionCall;
use crate::{
helpers::{import_signature, import_type_tag, Script},
ArgumentOperation, BatchArgument, PreviousResult, APTOS_SCRIPT_BUILDER_KEY,
ArgumentOperation, CallArgument, ComposerVersion, PreviousResult, APTOS_SCRIPT_COMPOSER_KEY,
};
use anyhow::{anyhow, bail, Result};
use move_binary_format::{
Expand Down Expand Up @@ -97,7 +97,8 @@ impl TransactionComposer {
}
}

/// Create a builder with one signer needed for script. This would be needed for multi-agent transaction where multiple signers are present.
/// Create a builder with one signer needed for script. This would be needed for multi-agent
/// transaction where multiple signers are present.
pub fn multi_signer(signer_count: u16) -> Self {
let mut script = empty_script();
script.code.code = vec![];
Expand Down Expand Up @@ -127,7 +128,8 @@ impl TransactionComposer {
.map_err(|e| e.to_string())
}

/// Load up a module from a remote endpoint. Will need to invoke this function prior to the call.
/// Load up a module from a remote endpoint. Will need to invoke this function prior to the
/// call.
pub async fn load_module(
&mut self,
api_url: String,
Expand All @@ -140,6 +142,7 @@ impl TransactionComposer {
.map_err(|err| JsValue::from(format!("{:?}", err)))
}

/// Load up the dependency modules of a TypeTag from a remote endpoint.
pub async fn load_type_tag(
&mut self,
api_url: String,
Expand All @@ -152,14 +155,22 @@ impl TransactionComposer {
.map_err(|err| JsValue::from(format!("{:?}", err)))
}

/// This would be the core api for the `TransactionComposer`. The function would:
/// - add the function call to the builder
/// - allocate the locals and parameters needed for this function call
/// - return the arguments back to the caller which could be passed into subsequent calls
/// into `add_batched_call`.
///
/// This function would also check for the ability and type safety when passing values
/// into the function call, and will abort if there's a violation.
#[wasm_bindgen(js_name = add_batched_call)]
pub fn add_batched_call_wasm(
&mut self,
module: String,
function: String,
ty_args: Vec<String>,
args: Vec<BatchArgument>,
) -> Result<Vec<BatchArgument>, JsValue> {
args: Vec<CallArgument>,
) -> Result<Vec<CallArgument>, JsValue> {
self.add_batched_call(module, function, ty_args, args)
.map_err(|err| JsValue::from(format!("{:?}", err)))
}
Expand Down Expand Up @@ -216,8 +227,8 @@ impl TransactionComposer {
module: String,
function: String,
ty_args: Vec<String>,
args: Vec<BatchArgument>,
) -> anyhow::Result<Vec<BatchArgument>> {
args: Vec<CallArgument>,
) -> anyhow::Result<Vec<CallArgument>> {
let ty_args = ty_args
.iter()
.map(|s| TypeTag::from_str(s))
Expand Down Expand Up @@ -261,7 +272,7 @@ impl TransactionComposer {

for (arg, ty) in args.into_iter().zip(expected_args_ty) {
match arg {
BatchArgument::PreviousResult(r) => {
CallArgument::PreviousResult(r) => {
let argument = AllocatedLocal {
op_type: r.operation_type,
is_parameter: false,
Expand All @@ -270,12 +281,12 @@ impl TransactionComposer {
self.check_argument_compatibility(&argument, &ty)?;
arguments.push(argument)
},
BatchArgument::Signer(i) => arguments.push(AllocatedLocal {
CallArgument::Signer(i) => arguments.push(AllocatedLocal {
op_type: ArgumentOperation::Copy,
is_parameter: true,
local_idx: i,
}),
BatchArgument::Raw(bytes) => {
CallArgument::Raw(bytes) => {
let new_local_idx = self.parameters_ty.len() as u16;
self.parameters_ty.push(ty);
self.parameters.push(bytes);
Expand Down Expand Up @@ -321,7 +332,7 @@ impl TransactionComposer {

Ok((0..returns.len())
.map(|idx| {
BatchArgument::PreviousResult(PreviousResult {
CallArgument::PreviousResult(PreviousResult {
operation_type: ArgumentOperation::Move,
call_idx: num_of_calls,
return_idx: idx as u16,
Expand Down Expand Up @@ -388,8 +399,8 @@ impl TransactionComposer {
move_bytecode_verifier::verify_script(&script).map_err(|err| anyhow!("{:?}", err))?;
if with_metadata {
script.metadata.push(Metadata {
key: APTOS_SCRIPT_BUILDER_KEY.to_owned(),
value: vec![],
key: APTOS_SCRIPT_COMPOSER_KEY.to_owned(),
value: bcs::to_bytes(&ComposerVersion::V1).unwrap(),
});
}
let mut bytes = vec![];
Expand Down Expand Up @@ -459,13 +470,13 @@ impl TransactionComposer {
}
Ok(())
},
TypeTag::Vector(v) => Box::pin(self.load_type_tag_impl(api_url, &v)).await,
TypeTag::Vector(v) => Box::pin(self.load_type_tag_impl(api_url, v)).await,
_ => Ok(()),
}
}

#[cfg(test)]
pub(crate) fn assert_decompilation_eq(&self, calls: &[BatchedFunctionCall]) {
pub(crate) fn assert_decompilation_eq(&self, calls: &[MoveFunctionCall]) {
let script = self.builder.as_script();

assert_eq!(self.calls.len(), calls.len());
Expand All @@ -488,11 +499,11 @@ impl TransactionComposer {
}

#[cfg(test)]
fn check_argument_eq(&self, lhs: &AllocatedLocal, rhs: &BatchArgument) {
fn check_argument_eq(&self, lhs: &AllocatedLocal, rhs: &CallArgument) {
use crate::PreviousResult;

match rhs {
BatchArgument::PreviousResult(PreviousResult {
CallArgument::PreviousResult(PreviousResult {
call_idx,
return_idx,
operation_type,
Expand All @@ -504,15 +515,15 @@ impl TransactionComposer {
is_parameter: false,
});
},
BatchArgument::Raw(input) => {
CallArgument::Raw(input) => {
assert!(lhs.is_parameter);
assert_eq!(lhs.op_type, ArgumentOperation::Move);
assert_eq!(
&self.parameters[(lhs.local_idx - self.signer_count) as usize],
input
);
},
BatchArgument::Signer(idx) => {
CallArgument::Signer(idx) => {
assert_eq!(lhs, &AllocatedLocal {
op_type: ArgumentOperation::Copy,
is_parameter: true,
Expand Down
Loading

0 comments on commit 6ad13e6

Please sign in to comment.