-
Notifications
You must be signed in to change notification settings - Fork 7
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
Deferred_calls #335
base: main
Are you sure you want to change the base?
Deferred_calls #335
Changes from 10 commits
5a858d7
c21d4fa
6e2250f
f01ad17
4b57c66
64638e0
e19017b
d841539
ca1b867
116dd8a
7c0a487
3d6ccdb
4c44288
5a9cdc7
ee11f55
25bb52c
033ac6e
48d42c5
b582bcd
7129201
8a07a50
77c87b6
3f2a370
f51d247
4220039
f5fd64c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1531,6 +1531,149 @@ pub(crate) fn assembly_script_chain_id(mut ctx: FunctionEnvMut<ASEnv>) -> ABIRes | |
Ok(chain_id as u64) | ||
} | ||
|
||
/// Return the price in nMAS to book an deferred call space in a specific slot. | ||
#[named] | ||
pub(crate) fn assembly_script_get_deferred_call_quote( | ||
mut ctx: FunctionEnvMut<ASEnv>, | ||
deferred_call_period: i64, | ||
deferred_call_thread: i32, | ||
max_gas: i64, | ||
) -> ABIResult<u64> { | ||
let env = get_env(&ctx)?; | ||
sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; | ||
let asc_slot: (u64, u8) = match ( | ||
deferred_call_period.try_into(), | ||
deferred_call_thread.try_into(), | ||
) { | ||
(Ok(p), Ok(t)) => (p, t), | ||
(Err(_), _) => abi_bail!("negative validity end period"), | ||
(_, Err(_)) => abi_bail!("invalid validity end thread"), | ||
}; | ||
if max_gas.is_negative() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. best practice: better do a try_into u64 directly since below we convert using "as u64" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
abi_bail!("negative max gas"); | ||
} | ||
let (available, mut price) = env | ||
.get_interface() | ||
.deferred_call_quote(asc_slot, max_gas as u64)?; | ||
if !available { | ||
price = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If !available, why don't we abi_bail!() instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because it's a quote, not a reservation attempt |
||
} | ||
#[cfg(feature = "execution-trace")] | ||
ctx.data_mut().trace.push(AbiTrace { | ||
name: function_name!().to_string(), | ||
params: vec![ | ||
into_trace_value!(deferred_call_period), | ||
into_trace_value!(deferred_call_thread), | ||
into_trace_value!(max_gas), | ||
], | ||
return_value: price.into(), | ||
sub_calls: None, | ||
}); | ||
Ok(price) | ||
} | ||
|
||
/// Register a new deferred call in the target slot with the given parameters. | ||
#[named] | ||
#[allow(clippy::too_many_arguments)] | ||
pub(crate) fn assembly_script_deferred_call_register( | ||
mut ctx: FunctionEnvMut<ASEnv>, | ||
target_address: i32, | ||
target_function: i32, | ||
target_period: i64, | ||
target_thread: i32, | ||
max_gas: i64, | ||
raw_coins: i64, | ||
params: i32, | ||
) -> ABIResult<i32> { | ||
let env = get_env(&ctx)?; | ||
sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; | ||
let asc_target_slot: (u64, u8) = match (target_period.try_into(), target_thread.try_into()) { | ||
(Ok(p), Ok(t)) => (p, t), | ||
(Err(_), _) => abi_bail!("negative validity end period"), | ||
(_, Err(_)) => abi_bail!("invalid validity end thread"), | ||
}; | ||
if max_gas.is_negative() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try_into u64 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
abi_bail!("negative max gas"); | ||
} | ||
if raw_coins.is_negative() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try into u64 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
abi_bail!("negative coins") | ||
} | ||
let memory = get_memory!(env); | ||
let target_address = read_string(memory, &ctx, target_address)?; | ||
let target_function = read_string(memory, &ctx, target_function)?; | ||
let params = read_buffer(memory, &ctx, params)?; | ||
let response = env.get_interface().deferred_call_register( | ||
&target_address, | ||
&target_function, | ||
asc_target_slot, | ||
max_gas as u64, | ||
raw_coins as u64, | ||
¶ms, | ||
)?; | ||
let res = match BufferPtr::alloc(&response, env.get_ffi_env(), &mut ctx) { | ||
Ok(ret) => Ok(ret.offset() as i32), | ||
_ => abi_bail!("Cannot allocate response in asc call register"), | ||
}; | ||
#[cfg(feature = "execution-trace")] | ||
ctx.data_mut().trace.push(AbiTrace { | ||
name: function_name!().to_string(), | ||
params: vec![ | ||
into_trace_value!(target_address), | ||
into_trace_value!(target_function), | ||
into_trace_value!(target_period), | ||
into_trace_value!(target_thread), | ||
into_trace_value!(max_gas as u64), | ||
into_trace_value!(raw_coins as u64), | ||
into_trace_value!(params), | ||
], | ||
return_value: response.to_owned().into(), | ||
sub_calls: None, | ||
}); | ||
res | ||
} | ||
|
||
/// Check if an deferred call exists with the given deferred_call_id (exists meaning to be executed in the future). | ||
#[named] | ||
pub(crate) fn assembly_script_deferred_call_exists( | ||
mut ctx: FunctionEnvMut<ASEnv>, | ||
deferred_id: i32, | ||
) -> ABIResult<i32> { | ||
let env = get_env(&ctx)?; | ||
sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; | ||
let memory = get_memory!(env); | ||
let asc_id = read_buffer(memory, &ctx, deferred_id)?; | ||
let exists = env.get_interface().deferred_call_exists(&asc_id)?; | ||
#[cfg(feature = "execution-trace")] | ||
ctx.data_mut().trace.push(AbiTrace { | ||
name: function_name!().to_string(), | ||
params: vec![into_trace_value!(asc_id)], | ||
return_value: exists.into(), | ||
sub_calls: None, | ||
}); | ||
Ok(exists as i32) | ||
} | ||
|
||
/// Cancel an deferred call with the given deferred_call_id. This will reimburse the user with the coins they provided | ||
#[named] | ||
pub(crate) fn assembly_script_deferred_call_cancel( | ||
mut ctx: FunctionEnvMut<ASEnv>, | ||
deferred_call_id: i32, | ||
) -> ABIResult<()> { | ||
let env = get_env(&ctx)?; | ||
sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; | ||
let memory = get_memory!(env); | ||
let deferred_id = read_buffer(memory, &ctx, deferred_call_id)?; | ||
env.get_interface().deferred_call_cancel(&deferred_id)?; | ||
#[cfg(feature = "execution-trace")] | ||
ctx.data_mut().trace.push(AbiTrace { | ||
name: function_name!().to_string(), | ||
params: vec![into_trace_value!(deferred_id)], | ||
return_value: (), | ||
sub_calls: None, | ||
}); | ||
Ok(()) | ||
} | ||
|
||
/// Assembly script builtin `abort` function. | ||
/// | ||
/// It prints the origin filename, an error messag, the line and column. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -271,6 +271,10 @@ impl Default for GasCosts { | |
abi_costs.insert(String::from("assembly_script_get_bytecode_for"), 11); | ||
abi_costs.insert(String::from("assembly_script_caller_has_write_access"), 11); | ||
abi_costs.insert(String::from("assembly_script_function_exists"), 11); | ||
abi_costs.insert(String::from("assembly_script_get_deferred_call_quote"), 11); | ||
abi_costs.insert(String::from("assembly_script_deferred_call_register"), 11); | ||
abi_costs.insert(String::from("assembly_script_deferred_call_exists"), 11); | ||
abi_costs.insert(String::from("assembly_script_deferred_call_cancel"), 11); | ||
Comment on lines
+274
to
+277
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO calibrate |
||
abi_costs.insert(String::from("assembly_script_seed"), 11); | ||
abi_costs.insert(String::from("assembly_script_abort"), 11); | ||
abi_costs.insert(String::from("assembly_script_date_now"), 11); | ||
|
@@ -573,6 +577,26 @@ pub trait Interface: Send + Sync + InterfaceClone { | |
// Return the current chain id | ||
fn chain_id(&self) -> Result<u64>; | ||
|
||
// Return a boolean that determine if there is place in this slot and an amount of fee needed to take the space | ||
fn deferred_call_quote(&self, target_slot: (u64, u8), gas_limit: u64) -> Result<(bool, u64)>; | ||
|
||
// Register a new deferred call and return his id | ||
fn deferred_call_register( | ||
&self, | ||
target_addr: &str, | ||
target_func: &str, | ||
target_slot: (u64, u8), | ||
max_gas: u64, | ||
coins: u64, | ||
params: &[u8], | ||
) -> Result<Vec<u8>>; | ||
|
||
// Return true if the current deferred call exists | ||
fn deferred_call_exists(&self, id: &[u8]) -> Result<bool>; | ||
|
||
// Cancel a deferred call (will return the coins) | ||
fn deferred_call_cancel(&self, id: &[u8]) -> Result<()>; | ||
|
||
fn native_amount_from_str_wasmv1(&self, amount: &str) -> Result<NativeAmount>; | ||
|
||
fn native_amount_to_string_wasmv1(&self, amount: &NativeAmount) -> Result<String>; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happened here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update of massa-proto-rs :
https://github.com/massalabs/massa-proto-rs/commits/main/