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

fix: avoid loading result on is_promise_success #816

Merged
merged 2 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Binary file modified examples/callback-results/res/callback_results.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified examples/factory-contract/res/factory_contract_high_level.wasm
Binary file not shown.
Binary file modified examples/factory-contract/res/factory_contract_low_level.wasm
Binary file not shown.
Binary file modified examples/fungible-token/res/defi.wasm
Binary file not shown.
Binary file modified examples/fungible-token/res/fungible_token.wasm
Binary file not shown.
Binary file not shown.
Binary file modified examples/mission-control/res/mission_control.wasm
Binary file not shown.
Binary file modified examples/non-fungible-token/res/approval_receiver.wasm
Binary file not shown.
Binary file modified examples/non-fungible-token/res/non_fungible_token.wasm
Binary file not shown.
Binary file modified examples/non-fungible-token/res/token_receiver.wasm
Binary file not shown.
Binary file not shown.
Binary file modified examples/status-message/res/status_message.wasm
Binary file not shown.
Binary file modified examples/test-contract/res/test_contract.wasm
Binary file not shown.
Binary file modified examples/versioned/res/versioned.wasm
Binary file not shown.
18 changes: 13 additions & 5 deletions near-sdk/src/environment/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::mock::MockedBlockchain;
use crate::types::{
AccountId, Balance, BlockHeight, Gas, PromiseIndex, PromiseResult, PublicKey, StorageUsage,
};
use crate::GasWeight;
use crate::{GasWeight, PromiseError};
use near_sys as sys;

const REGISTER_EXPECTED_ERR: &str =
Expand Down Expand Up @@ -594,13 +594,21 @@ pub fn promise_results_count() -> u64 {
/// If the current function is invoked by a callback we can access the execution results of the
/// promises that caused the callback.
pub fn promise_result(result_idx: u64) -> PromiseResult {
match unsafe { sys::promise_result(result_idx, ATOMIC_OP_REGISTER) } {
0 => PromiseResult::NotReady,
1 => {
match promise_result_internal(result_idx) {
Err(PromiseError::NotReady) => PromiseResult::NotReady,
Ok(()) => {
let data = expect_register(read_register(ATOMIC_OP_REGISTER));
PromiseResult::Successful(data)
}
2 => PromiseResult::Failed,
Err(PromiseError::Failed) => PromiseResult::Failed,
}
}

pub(crate) fn promise_result_internal(result_idx: u64) -> Result<(), PromiseError> {
match unsafe { sys::promise_result(result_idx, ATOMIC_OP_REGISTER) } {
0 => Err(PromiseError::NotReady),
1 => Ok(()),
2 => Err(PromiseError::Failed),
_ => abort(),
}
}
Expand Down
3 changes: 2 additions & 1 deletion near-sdk/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ pub fn assert_one_yocto() {
/// Returns true if promise was successful.
/// Fails if called outside a callback that received 1 promise result.
pub fn is_promise_success() -> bool {
promise_result_as_success().is_some()
require!(env::promise_results_count() == 1, "Contract expected a result on the callback");
env::promise_result_internal(0).is_ok()
}

/// Returns the result of the promise if successful. Otherwise returns None.
Expand Down