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

Gate all NEAR host functions behind the contract feature #356

Merged
merged 1 commit into from
Nov 18, 2021
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions engine-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ autobenches = false
aurora-engine-types = { path = "../engine-types", default-features = false }
borsh = { version = "0.8.2", default-features = false }
sha3 = { version = "0.9.1", default-features = false }
sha2 = { version = "0.9.3", default-features = false }

[features]
contract = []
Expand Down
40 changes: 27 additions & 13 deletions engine-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,47 @@
#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
#![cfg_attr(feature = "log", feature(panic_info_message))]

use crate::prelude::{Address, H256, STORAGE_PRICE_PER_BYTE};
#[cfg(feature = "contract")]
use crate::prelude::Address;
use crate::prelude::{H256, STORAGE_PRICE_PER_BYTE};
pub use types::keccak;

pub mod env;
pub mod error;
pub mod io;
#[cfg(feature = "contract")]
pub mod near_runtime;
mod prelude;
pub mod promise;
pub mod types;

#[cfg(feature = "contract")]
use near_runtime::exports;

#[cfg(feature = "contract")]
const ECRECOVER_MESSAGE_SIZE: u64 = 32;
#[cfg(feature = "contract")]
const ECRECOVER_SIGNATURE_LENGTH: u64 = 64;
#[cfg(feature = "contract")]
const ECRECOVER_MALLEABILITY_FLAG: u64 = 1;

#[allow(dead_code)]
pub fn panic() {
unsafe { exports::panic() }
}

#[cfg(feature = "contract")]
pub fn panic_utf8(bytes: &[u8]) -> ! {
unsafe {
exports::panic_utf8(bytes.len() as u64, bytes.as_ptr() as u64);
}
unreachable!()
}

#[allow(dead_code)]
#[cfg(feature = "contract")]
pub fn log_utf8(bytes: &[u8]) {
unsafe {
exports::log_utf8(bytes.len() as u64, bytes.as_ptr() as u64);
}
}

/// Calls environment sha256 on given input.
#[cfg(feature = "contract")]
pub fn sha256(input: &[u8]) -> H256 {
unsafe {
exports::sha256(input.len() as u64, input.as_ptr() as u64, 1);
Expand All @@ -49,7 +53,16 @@ pub fn sha256(input: &[u8]) -> H256 {
}
}

#[cfg(not(feature = "contract"))]
pub fn sha256(input: &[u8]) -> H256 {
use sha2::Digest;

let output = sha2::Sha256::digest(input);
H256(output.into())
}

/// Calls environment ripemd160 on given input.
#[cfg(feature = "contract")]
pub fn ripemd160(input: &[u8]) -> [u8; 20] {
unsafe {
const REGISTER_ID: u64 = 1;
Expand All @@ -61,6 +74,7 @@ pub fn ripemd160(input: &[u8]) -> [u8; 20] {
}

/// Recover address from message hash and signature.
#[cfg(feature = "contract")]
pub fn ecrecover(hash: H256, signature: &[u8]) -> Result<Address, ECRecoverErr> {
unsafe {
let hash_ptr = hash.as_ptr() as u64;
Expand Down Expand Up @@ -90,11 +104,16 @@ pub fn ecrecover(hash: H256, signature: &[u8]) -> Result<Address, ECRecoverErr>
}
}

#[allow(dead_code)]
#[cfg(feature = "contract")]
pub fn log(data: &str) {
log_utf8(data.as_bytes())
}

#[cfg(not(feature = "contract"))]
pub fn log(_data: &str) {
// TODO: standalone logging
}
joshuajbouw marked this conversation as resolved.
Show resolved Hide resolved

#[macro_export]
macro_rules! log {
($e: expr) => {
Expand All @@ -103,11 +122,6 @@ macro_rules! log {
};
}

#[allow(unused)]
pub fn prepaid_gas() -> u64 {
unsafe { exports::prepaid_gas() }
}

pub fn storage_byte_cost() -> u128 {
STORAGE_PRICE_PER_BYTE
}
Expand Down
3 changes: 3 additions & 0 deletions engine-sdk/src/promise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ use aurora_engine_types::types::PromiseResult;
pub struct PromiseId(u64);

impl PromiseId {
// TODO: can remove this annotation when there is a standalone implementation that uses PromiseId.
#[allow(dead_code)]
joshuajbouw marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) fn new(id: u64) -> Self {
Self(id)
}

#[allow(dead_code)]
birchmd marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) fn raw(self) -> u64 {
self.0
}
Expand Down
28 changes: 19 additions & 9 deletions engine-sdk/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "contract")]
use crate::io::IO;
use crate::panic_utf8;
use crate::prelude::{Address, H256};

#[cfg(not(feature = "contract"))]
Expand All @@ -22,86 +22,96 @@ pub fn keccak(data: &[u8]) -> H256 {
H256::from_slice(Keccak256::digest(data).as_slice())
}

#[allow(dead_code)]
pub fn near_account_to_evm_address(addr: &[u8]) -> Address {
Address::from_slice(&keccak(addr)[12..])
}

#[cfg(feature = "contract")]
pub trait ExpectUtf8<T> {
fn expect_utf8(self, message: &[u8]) -> T;
}

#[cfg(feature = "contract")]
impl<T> ExpectUtf8<T> for Option<T> {
fn expect_utf8(self, message: &[u8]) -> T {
match self {
Some(t) => t,
None => panic_utf8(message),
None => crate::panic_utf8(message),
}
}
}

#[cfg(feature = "contract")]
impl<T, E> ExpectUtf8<T> for core::result::Result<T, E> {
fn expect_utf8(self, message: &[u8]) -> T {
match self {
Ok(t) => t,
Err(_) => panic_utf8(message),
Err(_) => crate::panic_utf8(message),
}
}
}

#[cfg(feature = "contract")]
pub trait SdkExpect<T> {
fn sdk_expect(self, msg: &str) -> T;
}

#[cfg(feature = "contract")]
impl<T> SdkExpect<T> for Option<T> {
fn sdk_expect(self, msg: &str) -> T {
match self {
Some(t) => t,
None => panic_utf8(msg.as_ref()),
None => crate::panic_utf8(msg.as_ref()),
}
}
}

#[cfg(feature = "contract")]
impl<T, E> SdkExpect<T> for core::result::Result<T, E> {
fn sdk_expect(self, msg: &str) -> T {
match self {
Ok(t) => t,
Err(_) => panic_utf8(msg.as_ref()),
Err(_) => crate::panic_utf8(msg.as_ref()),
}
}
}

#[cfg(feature = "contract")]
pub trait SdkUnwrap<T> {
fn sdk_unwrap(self) -> T;
}

#[cfg(feature = "contract")]
impl<T> SdkUnwrap<T> for Option<T> {
fn sdk_unwrap(self) -> T {
match self {
Some(t) => t,
None => panic_utf8("ERR_UNWRAP".as_bytes()),
None => crate::panic_utf8("ERR_UNWRAP".as_bytes()),
}
}
}

#[cfg(feature = "contract")]
impl<T, E: AsRef<[u8]>> SdkUnwrap<T> for core::result::Result<T, E> {
fn sdk_unwrap(self) -> T {
match self {
Ok(t) => t,
Err(e) => panic_utf8(e.as_ref()),
Err(e) => crate::panic_utf8(e.as_ref()),
}
}
}

#[cfg(feature = "contract")]
pub trait SdkProcess<T> {
fn sdk_process(self);
}

#[cfg(feature = "contract")]
impl<T: AsRef<[u8]>, E: AsRef<[u8]>> SdkProcess<T> for Result<T, E> {
fn sdk_process(self) {
match self {
Ok(r) => crate::near_runtime::Runtime.return_output(r.as_ref()),
Err(e) => panic_utf8(e.as_ref()),
Err(e) => crate::panic_utf8(e.as_ref()),
}
}
}
2 changes: 1 addition & 1 deletion engine-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ publish = false
autobenches = false

[dependencies]
aurora-engine = { path = "../engine", default-features = false, features=["sha2"] }
aurora-engine = { path = "../engine", default-features = false }
aurora-engine-types = { path = "../engine-types", default-features = false }
aurora-engine-sdk = { path = "../engine-sdk", default-features = false }
aurora-engine-precompiles = { path = "../engine-precompiles", default-features = false }
Expand Down
3 changes: 1 addition & 2 deletions engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ num = { version = "0.4.0", default-features = false, features = ["alloc"] }
primitive-types = { version = "0.10.0", default-features = false, features = ["rlp"] }
ripemd160 = { version = "0.9.1", default-features = false }
rlp = { version = "0.5.0", default-features = false }
sha2 = { version = "0.9.3", default-features = false, optional = true }
sha3 = { version = "0.9.1", default-features = false }
wee_alloc = { version = "0.4.5", default-features = false }
logos = { version = "0.12", default-features = false, features = ["export_derive"] }
Expand All @@ -45,7 +44,7 @@ serde_json = "1"
rand = "0.7.3"

[features]
default = ["sha2", "std"]
default = ["std"]
std = ["borsh/std", "evm/std", "primitive-types/std", "rlp/std", "sha3/std", "ethabi/std", "logos/std", "bn/std", "aurora-engine-types/std"]
contract = ["aurora-engine-sdk/contract", "aurora-engine-precompiles/contract"]
evm_bully = []
Expand Down
9 changes: 0 additions & 9 deletions engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,15 +794,6 @@ pub fn compute_block_hash(chain_id: [u8; 32], block_height: u64, account_id: &[u
data.extend_from_slice(account_id);
data.extend_from_slice(&block_height.to_be_bytes());

#[cfg(not(feature = "contract"))]
{
use sha2::Digest;

let output = sha2::Sha256::digest(&data);
H256(output.into())
}

#[cfg(feature = "contract")]
sdk::sha256(&data)
}

Expand Down
2 changes: 1 addition & 1 deletion etc/state-migration-test/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions etc/state-migration-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ rpath = false

[dependencies]
borsh = { version = "0.8.2", default-features = false }
aurora-engine = { path = "../../engine", default-features = false, features = ["sha2"] }
aurora-engine-sdk = { path = "../../engine-sdk", default-features = false }
aurora-engine = { path = "../../engine", default-features = false }
aurora-engine-sdk = { path = "../../engine-sdk", default-features = false, features = ["contract"] }
aurora-engine-types = { path = "../../engine-types", default-features = false }