Skip to content

Commit

Permalink
refactor: Update LookupMap to allow and default to identity keys (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinabell authored Jan 29, 2022
1 parent 5dee86c commit 9fe5ade
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 289 deletions.
44 changes: 0 additions & 44 deletions near-sdk/src/environment/hash.rs

This file was deleted.

3 changes: 0 additions & 3 deletions near-sdk/src/environment/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
pub mod env;

#[cfg(feature = "unstable")]
pub mod hash;

#[cfg(not(target_arch = "wasm32"))]
/// Mock blockchain utilities. These can only be used inside tests and are not available for
/// a wasm32 target.
Expand Down
3 changes: 0 additions & 3 deletions near-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ pub use near_sdk_macros::{
#[cfg(feature = "unstable")]
pub mod store;

#[cfg(feature = "unstable")]
pub use environment::hash as crypto_hash;

pub mod collections;
mod environment;
pub use environment::env;
Expand Down
78 changes: 78 additions & 0 deletions near-sdk/src/store/key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use borsh::BorshSerialize;

use crate::env;

mod private {
/// Seal `ToKey` implementations to limit usage to the builtin implementations
pub trait Sealed {}

impl Sealed for super::Sha256 {}
impl Sealed for super::Keccak256 {}
impl Sealed for super::Identity {}
}

/// Trait used to generate keys to store data based on a serializable structure.
pub trait ToKey: self::private::Sealed {
/// Output type for the generated lookup key.
type KeyType: AsRef<[u8]>;

fn to_key<Q: ?Sized>(prefix: &[u8], key: &Q, buffer: &mut Vec<u8>) -> Self::KeyType
where
Q: BorshSerialize;
}

/// Sha256 hash helper which hashes through a syscall. This type satisfies the [`ToKey`] trait.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Sha256 {}

impl ToKey for Sha256 {
type KeyType = [u8; 32];

fn to_key<Q: ?Sized>(prefix: &[u8], key: &Q, buffer: &mut Vec<u8>) -> Self::KeyType
where
Q: BorshSerialize,
{
// Prefix the serialized bytes, then hash the combined value.
buffer.extend(prefix);
key.serialize(buffer).unwrap_or_else(|_| env::abort());

env::sha256_array(buffer)
}
}

/// Keccak256 hash helper which hashes through a syscall. This type satisfies the [`ToKey`] trait.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Keccak256 {}

impl ToKey for Keccak256 {
type KeyType = [u8; 32];

fn to_key<Q: ?Sized>(prefix: &[u8], key: &Q, buffer: &mut Vec<u8>) -> Self::KeyType
where
Q: BorshSerialize,
{
// Prefix the serialized bytes, then hash the combined value.
buffer.extend(prefix);
key.serialize(buffer).unwrap_or_else(|_| env::abort());

env::keccak256_array(buffer)
}
}

/// Identity hash which just prefixes all of the serializes bytes and uses it as the key.
pub enum Identity {}

impl ToKey for Identity {
type KeyType = Vec<u8>;

fn to_key<Q: ?Sized>(prefix: &[u8], key: &Q, buffer: &mut Vec<u8>) -> Self::KeyType
where
Q: BorshSerialize,
{
// Prefix the serialized bytes and return a copy of this buffer.
buffer.extend(prefix);
key.serialize(buffer).unwrap_or_else(|_| env::abort());

buffer.clone()
}
}
11 changes: 6 additions & 5 deletions near-sdk/src/store/lookup_map/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::borrow::Borrow;

use borsh::{BorshDeserialize, BorshSerialize};

use super::{LookupMap, ERR_NOT_EXIST};
use crate::{crypto_hash::CryptoHasher, env};
use super::{LookupMap, ToKey, ERR_NOT_EXIST};
use crate::env;

impl<K, V, H> Extend<(K, V)> for LookupMap<K, V, H>
where
K: BorshSerialize + Ord,
V: BorshSerialize,
H: CryptoHasher<Digest = [u8; 32]>,
H: ToKey,
{
fn extend<I>(&mut self, iter: I)
where
Expand All @@ -23,9 +23,10 @@ where

impl<K, V, H, Q: ?Sized> core::ops::Index<&Q> for LookupMap<K, V, H>
where
K: BorshSerialize + Ord + Clone + Borrow<Q>,
K: BorshSerialize + Ord + Borrow<Q>,
V: BorshSerialize + BorshDeserialize,
H: CryptoHasher<Digest = [u8; 32]>,
H: ToKey,

Q: BorshSerialize + ToOwned<Owned = K>,
{
type Output = V;
Expand Down
Loading

0 comments on commit 9fe5ade

Please sign in to comment.