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

feat: Introduce new random_seed_array method and update docs #692

Merged
merged 2 commits into from
Jan 10, 2022
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [unreleased]

### Features
- Added `env::random_seed_array` to return a fixed length array of the `random_seed` and optimizes the existing function. [PR 692](https://github.com/near/near-sdk-rs/pull/692)

## `4.0.0-pre.5` [12-23-2021]
- fix(standards): Fix NFT impl macros to not import `HashMap` and `near_sdk::json_types::U128`. [PR 571](https://github.com/near/near-sdk-rs/pull/571).
- Add drain iterator for `near_sdk::store::UnorderedMap`. [PR 613](https://github.com/near/near-sdk-rs/pull/613).
Expand Down
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/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.
30 changes: 28 additions & 2 deletions near-sdk/src/environment/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,25 @@ pub fn used_gas() -> Gas {
// ############
// # Math API #
// ############
/// Get random seed from the register.

/// Returns the random seed from the current block. This 32 byte hash is based on the VRF value from
/// the block. This value is not modified in any way each time this function is called within the
/// same method/block.
pub fn random_seed() -> Vec<u8> {
method_into_register!(random_seed)
random_seed_array().to_vec()
}

/// Returns the random seed from the current block. This 32 byte hash is based on the VRF value from
/// the block. This value is not modified in any way each time this function is called within the
/// same method/block.
pub fn random_seed_array() -> [u8; 32] {
//* SAFETY: random_seed syscall will always generate 32 bytes inside of the atomic op register
//* so the read will have a sufficient buffer of 32, and can transmute from uninit
//* because all bytes are filled. This assumes a valid random_seed implementation.
unsafe {
sys::random_seed(ATOMIC_OP_REGISTER);
read_register_fixed_32(ATOMIC_OP_REGISTER)
}
}

/// Hashes the random sequence of bytes using sha256.
Expand Down Expand Up @@ -845,6 +861,16 @@ mod tests {
);
}

#[cfg(not(target_arch = "wasm32"))]
#[test]
fn random_seed_smoke_test() {
crate::testing_env!(crate::test_utils::VMContextBuilder::new()
.random_seed([8; 32])
.build());

assert_eq!(super::random_seed(), [8; 32]);
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "unstable")]
#[test]
Expand Down
4 changes: 2 additions & 2 deletions near-sdk/src/test_utils/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ impl VMContextBuilder {
self
}

pub fn random_seed(&mut self, seed: Vec<u8>) -> &mut Self {
self.context.random_seed = seed;
pub fn random_seed(&mut self, seed: [u8; 32]) -> &mut Self {
self.context.random_seed = seed.to_vec();
self
}

Expand Down