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

Contract deployment via transaction #1941

Merged
merged 54 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
dd9154c
bob-contract: added methods init, echo and value
miloszm Jul 5, 2024
94a7c66
execution-core: extended transaction with contract deploy
miloszm Jul 5, 2024
cca6843
rusk: added contract deployment test
miloszm Jul 5, 2024
ebcc5f5
rusk: hooked up deployment in execute
miloszm Jul 5, 2024
d61f80f
rusk: adjustments to changed transaction format
miloszm Jul 5, 2024
8d8f31e
test-wallet: adjustments to changed transaction format
miloszm Jul 5, 2024
e6b80cd
rusk-prover: test adjustment to changed transaction format
miloszm Jul 5, 2024
ceb0e7f
node: adjustment to changed transaction format
miloszm Jul 5, 2024
907a978
node-data: test adjustment to changed transaction format
miloszm Jul 5, 2024
6901266
transfer-contract: test adjustments to changed transaction format
miloszm Jul 5, 2024
252db3b
stake-contract: test adjustment to changed transaction format
miloszm Jul 5, 2024
0a21e7b
rusk-abi: upgraded piecrust dependency
miloszm Jul 5, 2024
44ee87d
rusk: test for failing deployment
miloszm Jul 5, 2024
fa7da9a
execution-core: bytecode as hash and bytes
miloszm Jul 8, 2024
687bc58
rusk: stripping off bytecode
miloszm Jul 8, 2024
19be190
rusk: deployment failure handling
miloszm Jul 9, 2024
92bdb8c
execution-core: removed contract deployment contract id
miloszm Jul 9, 2024
4ec1732
rusk: removed contract deployment contract id
miloszm Jul 9, 2024
e71b09b
rusk: check bytecode hash
miloszm Jul 9, 2024
e1ba153
rusk: tx serialization includes bytecode
miloszm Jul 10, 2024
d63c719
rusk: paying limit if deploy fails
miloszm Jul 11, 2024
09c560c
execution-core: improved deserialization
miloszm Jul 12, 2024
60e2f9b
rusk: insufficient gas tests for deployment
miloszm Jul 15, 2024
3a669e2
rusk: refactored deployment tests
miloszm Jul 15, 2024
cafd4a4
execution-core: updated changelog
miloszm Jul 15, 2024
e8ac21f
rusk: review remarks follow up
miloszm Jul 15, 2024
e5facdc
execution-core: transaction strip off bytecode method
miloszm Jul 16, 2024
43ec8c3
rusk: transaction strip off bytecode method
miloszm Jul 16, 2024
fcc186d
rusk: precheck for sufficient gas for deploy
miloszm Jul 16, 2024
45a6147
execution-core: renamed contract execution, cosmetics
miloszm Jul 17, 2024
86115ed
stake-contract: renamed contract execution
miloszm Jul 17, 2024
7d9268c
transfer-contract: renamed contract execution
miloszm Jul 17, 2024
896e3e3
node-data: renamed contract execution
miloszm Jul 17, 2024
cf4313a
rusk-prover: renamed contract execution
miloszm Jul 17, 2024
c3a2dc5
rusk: renamed contract execution
miloszm Jul 17, 2024
0ecc2be
test-wallet: renamed contract execution
miloszm Jul 17, 2024
7cb6524
stake-contract: review follow-up renames
miloszm Jul 18, 2024
c1e9691
transfer-contract: review follow-up renames
miloszm Jul 18, 2024
4303fcd
execution-core: review follow-up renames
miloszm Jul 18, 2024
d6cfa97
node-data: review follow-up renames
miloszm Jul 18, 2024
f065cd7
rusk-prover: review follow-up renames
miloszm Jul 18, 2024
7463d3f
rusk: review follow-up reformat
miloszm Jul 18, 2024
6af4398
test-wallet: review follow-up renames
miloszm Jul 18, 2024
e60bf70
execution-core: optimized stripping off bytecode
miloszm Jul 18, 2024
8cbaefa
rusk: optimized stripping off bytecode
miloszm Jul 18, 2024
40d8acd
rusk: addressed clippy error
miloszm Jul 18, 2024
67d41dd
stake-contract: renamed exec to contract_exec
miloszm Jul 18, 2024
ce71ac8
transfer-contract: renamed exec to contract_exec
miloszm Jul 18, 2024
2aa0caf
execution-core: renamed exec to contract_exec
miloszm Jul 18, 2024
68a6719
node-data: renamed exec to contract_exec
miloszm Jul 18, 2024
3dc62f1
rusk-prover: renamed exec to contract_exec
miloszm Jul 18, 2024
b566a14
test-wallet: renamed exec to contract_exec
miloszm Jul 18, 2024
7208f91
rusk: gas_per_deploy_byte configuration parameter
miloszm Jul 18, 2024
c1cd18e
rusk: refactored deploy execution, comments
miloszm Jul 18, 2024
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
15 changes: 15 additions & 0 deletions contracts/bob/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,26 @@ mod wasm {
#[no_mangle]
static mut STATE: Bob = Bob::new();

#[no_mangle]
unsafe fn init(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |n| STATE.init(n))
}

#[no_mangle]
unsafe fn ping(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |()| STATE.ping())
}

#[no_mangle]
unsafe fn echo(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |n| STATE.echo(n))
}

#[no_mangle]
unsafe fn value(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |()| STATE.value())
}

const PAYMENT_INFO: PaymentInfo = PaymentInfo::Transparent(None);

#[no_mangle]
Expand Down
20 changes: 16 additions & 4 deletions contracts/bob/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

/// Bob contract.
#[derive(Debug, Clone)]
pub struct Bob;
pub struct Bob {
value: u8,
}

impl Bob {
pub const fn new() -> Self {
Self
Self { value: 0 }
}

#[allow(dead_code)]
Expand All @@ -20,7 +22,17 @@ impl Bob {
}

impl Bob {
pub fn ping(&mut self) {
rusk_abi::debug!("Bob ping");
pub fn init(&mut self, n: u8) {
self.value = n;
}

pub fn ping(&mut self) {}
moCello marked this conversation as resolved.
Show resolved Hide resolved

pub fn echo(&mut self, n: u64) -> u64 {
n
}

pub fn value(&mut self) -> u8 {
self.value
}
}
5 changes: 3 additions & 2 deletions contracts/stake/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use rand::SeedableRng;

use execution_core::{
transfer::{
ContractCall, Fee, Payload, Transaction, TreeLeaf, TRANSFER_TREE_DEPTH,
ContractCall, ContractExec, Fee, Payload, Transaction, TreeLeaf,
TRANSFER_TREE_DEPTH,
},
value_commitment, JubJubScalar, Note, PublicKey, SchnorrSecretKey,
SecretKey, Sender, TxSkeleton, ViewKey,
Expand Down Expand Up @@ -272,7 +273,7 @@ pub fn create_transaction<const I: usize>(
let tx_payload = Payload {
tx_skeleton,
fee,
contract_call,
contract_exec: (contract_call.map(|c| ContractExec::Call(c))),
};

let payload_hash = tx_payload.hash();
Expand Down
4 changes: 2 additions & 2 deletions contracts/transfer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl TransferState {

// if present, place the contract deposit on the state
if tx.payload().tx_skeleton.deposit > 0 {
let contract = match &tx.payload().contract_call {
let contract = match &tx.payload().contract_call() {
Some(call) => ContractId::from_bytes(call.contract),
None => {
panic!("There needs to be a contract call when depositing funds");
Expand All @@ -202,7 +202,7 @@ impl TransferState {

// perform contract call if present
let mut result = Ok(Vec::new());
if let Some(call) = &tx.payload().contract_call {
if let Some(call) = &tx.payload().contract_call() {
result = rusk_abi::call_raw(
ContractId::from_bytes(call.contract),
&call.fn_name,
Expand Down
4 changes: 2 additions & 2 deletions contracts/transfer/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ use rusk_abi::{ContractError, ContractId, Error, Session, TRANSFER_CONTRACT};

use dusk_bytes::Serializable;
use dusk_plonk::prelude::*;
use execution_core::transfer::ContractExec;
use ff::Field;
use phoenix_circuits::transaction::{TxCircuit, TxInputNote, TxOutputNote};
use poseidon_merkle::Opening as PoseidonOpening;

use rand::rngs::StdRng;
use rand::SeedableRng;

Expand Down Expand Up @@ -287,7 +287,7 @@ pub fn create_transaction<const I: usize>(
let tx_payload = Payload {
tx_skeleton,
fee,
contract_call,
contract_exec: (contract_call.map(|c| ContractExec::Call(c))),
};

let payload_hash = tx_payload.hash();
Expand Down
8 changes: 7 additions & 1 deletion execution-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
### Changed

- Improved deserialization [#1963]
- Change payload to support contract deployment [#1882]


- Re-export
- `dusk-bls12_381::BlsScalar`
Expand Down Expand Up @@ -75,6 +79,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
StakeData,
}`

[#1963]: https://github.com/dusk-network/rusk/issues/1963
[#1882]: https://github.com/dusk-network/rusk/issues/1882

[Unreleased]: https://github.com/dusk-network/rusk/compare/execution-core-0.1.0...HEAD
[0.1.0]: https://github.com/dusk-network/dusk-abi/releases/tag/execution-core-0.1.0
2 changes: 1 addition & 1 deletion execution-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rand = { version = "0.8", default-features = false }
ff = { version = "0.13", default-features = false }

[dev-dependencies]
rand = { version = "0.8", default-features = false, features = ["std_rng"] }
rand = "0.8"

[features]
# It enables parallel thread aggregation of BlsPublicKey
Expand Down
53 changes: 53 additions & 0 deletions execution-core/src/bytecode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

//! Wrapper for a strip-able bytecode that we want to keep the integrity of.

extern crate alloc;
use crate::reader::{read_arr, read_vec};
use alloc::vec::Vec;
use bytecheck::CheckBytes;
use dusk_bytes::{Error as BytesError, Serializable};
use rkyv::{Archive, Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Archive, Serialize, Deserialize)]
#[archive_attr(derive(CheckBytes))]
/// Holds bytes of bytecode and its hash.
pub struct Bytecode {
/// Hash of the bytecode bytes.
pub hash: [u8; 32],
/// Bytecode bytes.
pub bytes: Vec<u8>,
}
miloszm marked this conversation as resolved.
Show resolved Hide resolved

impl Bytecode {
/// Provides contribution bytes for an external hash.
#[must_use]
pub fn to_hash_input_bytes(&self) -> Vec<u8> {
self.hash.to_vec()
}

/// Serializes this object into a variable length buffer
#[must_use]
pub fn to_var_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::new();
bytes.extend(self.hash);
miloszm marked this conversation as resolved.
Show resolved Hide resolved
bytes.extend((self.bytes.len() as u64).to_bytes());
bytes.extend(&self.bytes);
bytes
}

/// Deserialize from a bytes buffer.
/// Resets buffer to a position after the bytes read.
///
/// # Errors
/// Errors when the bytes are not available.
pub fn from_buf(buf: &mut &[u8]) -> Result<Self, BytesError> {
let hash = read_arr::<32>(buf)?;
let bytes = read_vec(buf)?;
Ok(Self { hash, bytes })
}
}
2 changes: 2 additions & 0 deletions execution-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
/// Block height type alias
pub type BlockHeight = u64;

pub mod bytecode;
pub mod reader;
pub mod stake;
pub mod transfer;

Expand Down
64 changes: 64 additions & 0 deletions execution-core/src/reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

//! Functions for reading variable length elements from bytes.

extern crate alloc;
use alloc::string::String;
use alloc::vec::Vec;

use dusk_bytes::Error::InvalidData;
use dusk_bytes::{DeserializableSlice, Error as BytesError};

/// Reads vector from a buffer.
/// Resets buffer to a position after the bytes read.
///
/// # Errors
/// When length or data could not be read.
pub fn read_vec(buf: &mut &[u8]) -> Result<Vec<u8>, BytesError> {
let len = usize::try_from(u64::from_reader(buf)?)
.map_err(|_| BytesError::InvalidData)?;
if buf.len() < len {
return Err(InvalidData);
}
let bytes = buf[..len].into();
*buf = &buf[len..];
Ok(bytes)
}

/// Reads string from a buffer.
/// Resets buffer to a position after the bytes read.
///
/// # Errors
/// When length or data could not be read.
pub fn read_str(buf: &mut &[u8]) -> Result<String, BytesError> {
let len = usize::try_from(u64::from_reader(buf)?)
.map_err(|_| BytesError::InvalidData)?;
if buf.len() < len {
return Err(InvalidData);
}
let str = String::from_utf8(buf[..len].into())
.map_err(|_| BytesError::InvalidData)?;
*buf = &buf[len..];
Ok(str)
}

/// Reads array from a buffer.
/// Resets buffer to a position after the bytes read.
///
/// # Errors
/// When length or data could not be read.
pub fn read_arr<const N: usize>(
buf: &mut &[u8],
) -> Result<[u8; N], BytesError> {
if buf.len() < N {
return Err(InvalidData);
}
let mut a = [0u8; N];
a.copy_from_slice(&buf[..N]);
*buf = &buf[N..];
Ok(a)
}
Loading
Loading