Skip to content
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

Make PromiseIndex a newtype #1066

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions near-sdk/src/environment/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ pub fn promise_create(
) -> PromiseIndex {
let account_id = account_id.as_bytes();
unsafe {
sys::promise_create(
PromiseIndex(sys::promise_create(
account_id.len() as _,
account_id.as_ptr() as _,
function_name.len() as _,
Expand All @@ -465,7 +465,7 @@ pub fn promise_create(
arguments.as_ptr() as _,
&amount as *const Balance as _,
gas.0,
)
))
}
}

Expand All @@ -480,8 +480,8 @@ pub fn promise_then(
) -> PromiseIndex {
let account_id = account_id.as_bytes();
unsafe {
sys::promise_then(
promise_idx,
PromiseIndex(sys::promise_then(
promise_idx.0,
account_id.len() as _,
account_id.as_ptr() as _,
function_name.len() as _,
Expand All @@ -490,7 +490,7 @@ pub fn promise_then(
arguments.as_ptr() as _,
&amount as *const Balance as _,
gas.0,
)
))
}
}

Expand All @@ -499,31 +499,37 @@ pub fn promise_and(promise_indices: &[PromiseIndex]) -> PromiseIndex {
let mut data = vec![0u8; size_of_val(promise_indices)];
for i in 0..promise_indices.len() {
data[i * size_of::<PromiseIndex>()..(i + 1) * size_of::<PromiseIndex>()]
.copy_from_slice(&promise_indices[i].to_le_bytes());
.copy_from_slice(&promise_indices[i].0.to_le_bytes());
}
unsafe { sys::promise_and(data.as_ptr() as _, promise_indices.len() as _) }
unsafe { PromiseIndex(sys::promise_and(data.as_ptr() as _, promise_indices.len() as _)) }
}

pub fn promise_batch_create(account_id: &AccountId) -> PromiseIndex {
let account_id = account_id.as_ref();
unsafe { sys::promise_batch_create(account_id.len() as _, account_id.as_ptr() as _) }
unsafe {
PromiseIndex(sys::promise_batch_create(account_id.len() as _, account_id.as_ptr() as _))
}
}

pub fn promise_batch_then(promise_index: PromiseIndex, account_id: &AccountId) -> PromiseIndex {
let account_id: &str = account_id.as_ref();
unsafe {
sys::promise_batch_then(promise_index, account_id.len() as _, account_id.as_ptr() as _)
PromiseIndex(sys::promise_batch_then(
promise_index.0,
account_id.len() as _,
account_id.as_ptr() as _,
))
}
}

pub fn promise_batch_action_create_account(promise_index: PromiseIndex) {
unsafe { sys::promise_batch_action_create_account(promise_index) }
unsafe { sys::promise_batch_action_create_account(promise_index.0) }
}

pub fn promise_batch_action_deploy_contract(promise_index: u64, code: &[u8]) {
pub fn promise_batch_action_deploy_contract(promise_index: PromiseIndex, code: &[u8]) {
unsafe {
sys::promise_batch_action_deploy_contract(
promise_index,
promise_index.0,
code.len() as _,
code.as_ptr() as _,
)
Expand All @@ -539,7 +545,7 @@ pub fn promise_batch_action_function_call(
) {
unsafe {
sys::promise_batch_action_function_call(
promise_index,
promise_index.0,
function_name.len() as _,
function_name.as_ptr() as _,
arguments.len() as _,
Expand All @@ -560,7 +566,7 @@ pub fn promise_batch_action_function_call_weight(
) {
unsafe {
sys::promise_batch_action_function_call_weight(
promise_index,
promise_index.0,
function_name.len() as _,
function_name.as_ptr() as _,
arguments.len() as _,
Expand All @@ -573,7 +579,7 @@ pub fn promise_batch_action_function_call_weight(
}

pub fn promise_batch_action_transfer(promise_index: PromiseIndex, amount: Balance) {
unsafe { sys::promise_batch_action_transfer(promise_index, &amount as *const Balance as _) }
unsafe { sys::promise_batch_action_transfer(promise_index.0, &amount as *const Balance as _) }
}

pub fn promise_batch_action_stake(
Expand All @@ -583,7 +589,7 @@ pub fn promise_batch_action_stake(
) {
unsafe {
sys::promise_batch_action_stake(
promise_index,
promise_index.0,
&amount as *const Balance as _,
public_key.as_bytes().len() as _,
public_key.as_bytes().as_ptr() as _,
Expand All @@ -597,7 +603,7 @@ pub fn promise_batch_action_add_key_with_full_access(
) {
unsafe {
sys::promise_batch_action_add_key_with_full_access(
promise_index,
promise_index.0,
public_key.as_bytes().len() as _,
public_key.as_bytes().as_ptr() as _,
nonce,
Expand Down Expand Up @@ -645,7 +651,7 @@ pub fn promise_batch_action_add_key_allowance_with_function_call(
};
unsafe {
sys::promise_batch_action_add_key_with_function_call(
promise_index,
promise_index.0,
public_key.as_bytes().len() as _,
public_key.as_bytes().as_ptr() as _,
nonce,
Expand All @@ -660,7 +666,7 @@ pub fn promise_batch_action_add_key_allowance_with_function_call(
pub fn promise_batch_action_delete_key(promise_index: PromiseIndex, public_key: &PublicKey) {
unsafe {
sys::promise_batch_action_delete_key(
promise_index,
promise_index.0,
public_key.as_bytes().len() as _,
public_key.as_bytes().as_ptr() as _,
)
Expand All @@ -674,7 +680,7 @@ pub fn promise_batch_action_delete_account(
let beneficiary_id: &str = beneficiary_id.as_ref();
unsafe {
sys::promise_batch_action_delete_account(
promise_index,
promise_index.0,
beneficiary_id.len() as _,
beneficiary_id.as_ptr() as _,
)
Expand Down Expand Up @@ -709,7 +715,7 @@ pub(crate) fn promise_result_internal(result_idx: u64) -> Result<(), PromiseErro
/// Consider the execution result of promise under `promise_idx` as execution result of this
/// function.
pub fn promise_return(promise_idx: PromiseIndex) {
unsafe { sys::promise_return(promise_idx) }
unsafe { sys::promise_return(promise_idx.0) }
}

// ###############
Expand Down
4 changes: 3 additions & 1 deletion near-sdk/src/types/vm_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ pub use near_vm_logic::types::{PromiseResult as VmPromiseResult, ReturnData};

//* Types from near_vm_logic
/// Promise index that is computed only once.
pub type PromiseIndex = u64;
#[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Copy, Clone)]
pub struct PromiseIndex(pub(crate) u64);

/// An index of Receipt to append an action
#[deprecated(since = "4.1.0", note = "type not used within SDK, use u64 directly or another alias")]
pub type ReceiptIndex = u64;
Expand Down