Skip to content

Commit

Permalink
fix: avoid loading result on is_promise_success (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinabell authored May 24, 2022
1 parent 2751292 commit f4c6b61
Show file tree
Hide file tree
Showing 18 changed files with 15 additions and 6 deletions.
Binary file modified examples/callback-results/res/callback_results.wasm
Binary file not shown.
Binary file modified examples/cross-contract-calls/res/cross_contract_high_level.wasm
Binary file not shown.
Binary file modified examples/cross-contract-calls/res/cross_contract_low_level.wasm
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 modified examples/lockable-fungible-token/res/lockable_fungible_token.wasm
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 @@ -13,7 +13,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 @@ -609,13 +609,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

0 comments on commit f4c6b61

Please sign in to comment.