From deb807660418435472c0cbe55ee34c60a6a940d7 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 6 Aug 2018 15:35:49 +0800 Subject: [PATCH 1/2] Add create2 under kip4 feature flag --- Cargo.toml | 1 + src/ext.rs | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index f0f02326..fe83bd2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,4 +31,5 @@ default-features = false [features] default = [] +kip4 = [] std = ["pwasm-std/std", "parity-hash/std", "bigint/std", "byteorder/std"] diff --git a/src/ext.rs b/src/ext.rs index 4b431052..cf15b797 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -74,6 +74,9 @@ mod external { pub fn create(endowment: *const u8, code_ptr: *const u8, code_len: u32, result_ptr: *mut u8) -> i32; + #[cfg(feature = "kip4")] + pub fn create2(endowment: *const u8, salt: *const u8, code_ptr: *const u8, code_len: u32, result_ptr: *mut u8) -> i32; + pub fn suicide(refund: *const u8) -> !; pub fn ret(ptr: *const u8, len: u32) -> !; @@ -118,6 +121,27 @@ pub fn create(endowment: U256, code: &[u8]) -> Result { } } +#[cfg(feature = "kip4")] +/// Create a new account with the given code and salt, requires KIP-4. +/// +/// # Errors +/// +/// Returns [`Error`] in case contract constructor failed. +/// +/// [`Error`]: struct.Error.html +pub fn create2(endowment: U256, salt: H256, code: &[u8]) -> Result { + let mut endowment_arr = [0u8; 32]; + endowment.to_big_endian(&mut endowment_arr); + let mut result = Address::new(); + unsafe { + if external::create2(endowment_arr.as_ptr(), salt.as_ptr(), code.as_ptr(), code.len() as u32, (&mut result).as_mut_ptr()) == 0 { + Ok(result) + } else { + Err(Error) + } + } +} + /// Message-call into an account /// /// # Arguments: From 232ba7cd20fabb70050fea1186813c09a43a8be6 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 6 Aug 2018 19:02:24 +0800 Subject: [PATCH 2/2] Wrap elog, create, and create2 ext defs where it's too long --- src/ext.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/ext.rs b/src/ext.rs index cf15b797..319d71d6 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -70,12 +70,28 @@ mod external { pub fn origin(dest: *mut u8); - pub fn elog(topic_ptr: *const u8, topic_count: u32, data_ptr: *const u8, data_len: u32); - - pub fn create(endowment: *const u8, code_ptr: *const u8, code_len: u32, result_ptr: *mut u8) -> i32; + pub fn elog( + topic_ptr: *const u8, + topic_count: u32, + data_ptr: *const u8, + data_len: u32 + ); + + pub fn create( + endowment: *const u8, + code_ptr: *const u8, + code_len: u32, + result_ptr: *mut u8 + ) -> i32; #[cfg(feature = "kip4")] - pub fn create2(endowment: *const u8, salt: *const u8, code_ptr: *const u8, code_len: u32, result_ptr: *mut u8) -> i32; + pub fn create2( + endowment: *const u8, + salt: *const u8, + code_ptr: *const u8, + code_len: u32, + result_ptr: *mut u8 + ) -> i32; pub fn suicide(refund: *const u8) -> !;