Skip to content

Commit

Permalink
Merge pull request #302 from dusk-network/apple-archs
Browse files Browse the repository at this point in the history
Add support for most common developer environments
  • Loading branch information
Eduardo Leegwater Simões authored Dec 6, 2023
2 parents 6e14fa8 + db9ac64 commit 550b68e
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 114 deletions.
18 changes: 15 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,23 @@ jobs:

test:
name: Make test
runs-on: core
strategy:
matrix:
target: [
x86_64-unknown-linux-gnu,
x86_64-apple-darwin,
aarch64-apple-darwin,
]
include:
- target: x86_64-unknown-linux-gnu
os: ubuntu-latest
- target: x86_64-apple-darwin
os: macos-latest-large
- target: aarch64-apple-darwin
os: macos-latest-xlarge
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: dsherret/rust-toolchain-file@v1
- run: rustup target add wasm32-unknown-unknown
- run: rustup component add rust-src
- name: Run `make test`
run: make test
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
help: ## Display this help screen
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'

COMPILER_VERSION=v0.0.0
COMPILER_VERSION=v0.1.0

setup-compiler: ## Setup the Dusk Contract Compiler
@./scripts/setup-compiler.sh $(COMPILER_VERSION)
Expand All @@ -15,12 +15,11 @@ contracts: setup-compiler ## Build example contracts
-Z build-std=core,alloc \
--target wasm64-unknown-unknown
@mkdir -p target/stripped
@contracts/c-example/build.sh
@find target/wasm64-unknown-unknown/release -maxdepth 1 -name "*.wasm" \
| xargs -I % basename % \
| xargs -I % wasm-tools strip -a \
| xargs -I % ./scripts/strip.sh \
target/wasm64-unknown-unknown/release/% \
-o target/stripped/%
target/stripped/%

test: contracts cold-reboot assert-counter-contract-small ## Run all tests
@cargo test \
Expand Down
1 change: 1 addition & 0 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"box",
"c-example",
"callcenter",
"constructor",
"counter",
Expand Down
12 changes: 12 additions & 0 deletions contracts/c-example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "c_example"
version = "0.1.0"
authors = [
"Eduardo Leegwater Simões <[email protected]>",
]
edition = "2021"

license = "MPL-2.0"

[lib]
crate-type = ["cdylib", "rlib"]
13 changes: 0 additions & 13 deletions contracts/c-example/build.sh

This file was deleted.

92 changes: 0 additions & 92 deletions contracts/c-example/contract.c

This file was deleted.

89 changes: 89 additions & 0 deletions contracts/c-example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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.

use std::ptr;

// Define the argument buffer used to communicate between contract and host
#[no_mangle]
pub static mut A: [u8; 65536] = [0; 65536];

// ==== Host functions ====
//
// These functions are provided by the host. See `piecrust-uplink` for a full
// list. Here we only declare the ones we will need.
mod ext {
extern "C" {
pub fn c(
contract_id: *const u8,
fn_name: *const u8,
fn_name_len: u32,
fn_arg_len: u32,
points_limit: u64,
) -> i32;
pub fn hd(name: *const u8, name_len: u32) -> u32;
}
}

// ==== Helper functions ====
//
// These will help us write the exported functions underneath

// Reads a contract ID from the argument buffer
unsafe fn read_contract_id(id: *mut u8) {
ptr::copy(&A[0], id, 32);
}

// Calls the counter contract to increment the counter
unsafe fn increment_counter(contract_id: *const u8) {
let fn_name = b"increment";
ext::c(contract_id, fn_name.as_ptr(), fn_name.len() as u32, 0, 0);
}

// Reads a 64-bit from the argument buffer
unsafe fn read_integer(i: *mut i64) {
ptr::copy(&A[0], i.cast(), 8);
}

// Writes a 64-bit integer to the argument buffer
unsafe fn write_integer(i: i64) {
let i: *const i64 = &i;
let i: *const u8 = i as _;
ptr::copy(&*i, &mut A[0], 8);
}

// Calls the counter contract to read the counter
unsafe fn read_counter(contract_id: *const u8) -> i64 {
let fn_name = b"read_value";
ext::c(contract_id, fn_name.as_ptr(), fn_name.len() as u32, 0, 0);

let mut i = 0;
read_integer(&mut i);
i
}

// ==== Exported functions ====

// Increments and reads the counter contract. The function expects the counter
// contract ID to be written to the argument buffer before being called.
#[no_mangle]
unsafe fn increment_and_read(_: i32) -> i32 {
let mut counter_id = [0u8; 32];

read_contract_id(&mut counter_id[0]);
increment_counter(&counter_id[0]);

write_integer(read_counter(&counter_id[0]));

8
}

// Calls the "hd" extern with an (almost) certainly out of bounds pointer, in an
// effort to trigger an error.
#[no_mangle]
unsafe fn out_of_bounds(_: i32) -> i32 {
ext::hd(4398046511103 as *const u8, 2);
0
}
2 changes: 1 addition & 1 deletion piecrust/tests/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn call_through_c() -> Result<(), Error> {
LIMIT,
)?;
let c_example_id = session.deploy(
contract_bytecode!("c-example"),
contract_bytecode!("c_example"),
ContractData::builder(OWNER),
LIMIT,
)?;
Expand Down
2 changes: 1 addition & 1 deletion piecrust/tests/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn out_of_bounds() -> Result<(), Error> {
let mut session = vm.session(SessionData::builder())?;

let c_example_id = session.deploy(
contract_bytecode!("c-example"),
contract_bytecode!("c_example"),
ContractData::builder(OWNER),
LIMIT,
)?;
Expand Down
54 changes: 54 additions & 0 deletions scripts/strip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/sh

# The script should check whether the inputs are set, or print usage
# information otherwise.
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: $0 <input-wasm> <output-wasm>"
exit 1
fi

TRIPLE=$(rustc -vV | sed -n 's|host: ||p')
ARCH=$(echo "$TRIPLE" | cut -f 1 -d'-')

# This is a bit of a mess because target triples are pretty inconsistent
OS=$(echo "$TRIPLE" | cut -f 2 -d'-')
# If OS is not currently linux, or apple, then we get it again
if [ "$OS" != "linux" ] && [ "$OS" != "apple" ]; then
OS=$(echo "$TRIPLE" | cut -f 3 -d'-')
fi
if [ "$OS" != "linux" ] && [ "$OS" != "apple" ]; then
echo "OS not supported: $OS"
exit 1
fi
# If OS is apple, change it to macos
if [ "$OS" = "apple" ]; then
OS="macos"
fi

RELEASES_URL=https://github.com/bytecodealliance/wasm-tools/releases/download

PROGRAM_VERSION=1.0.54

ARTIFACT_NAME=wasm-tools-$PROGRAM_VERSION-$ARCH-$OS.tar.gz
ARTIFACT_URL=$RELEASES_URL/wasm-tools-$PROGRAM_VERSION/$ARTIFACT_NAME

ARTIFACT_DIR=$PWD/target/wasm-tools/$PROGRAM_VERSION
ARTIFACT_PATH=$ARTIFACT_DIR/$ARTIFACT_NAME

# If the artifact doesn't already exist in the target directory, download it,
# otherwise skip.
if [ ! -f "$ARTIFACT_PATH" ]; then
echo "Downloading wasm-tools version $PROGRAM_VERSION"
mkdir -p "$ARTIFACT_DIR"
curl -L "$ARTIFACT_URL" -o "$ARTIFACT_PATH"
fi

# Extract the tarball, if they aren't already extracted
EXTRACTED_DIR=$ARTIFACT_DIR/extracted

if [ ! -d "$EXTRACTED_DIR" ]; then
mkdir -p "$EXTRACTED_DIR"
tar -xzf "$ARTIFACT_PATH" -C "$EXTRACTED_DIR" --strip-components=1
fi

"$EXTRACTED_DIR"/wasm-tools strip -a "$1" -o "$2"

0 comments on commit 550b68e

Please sign in to comment.