Skip to content
Draft
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
15 changes: 15 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"contracts/stake",
"contracts/transfer",

"data-driver",
"core",
"vm",
"wallet-core",
Expand Down
27 changes: 25 additions & 2 deletions contracts/transfer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ crate-type = ["cdylib", "rlib"]
dusk-core = { workspace = true }
ringbuffer = { workspace = true }

[target.'cfg(target_family = "wasm")'.dependencies]
dusk-core = { workspace = true, features = ["abi-dlmalloc"] }
# data-driver dependency
dusk-data-driver = { path = "../../data-driver", optional = true }
wasm-bindgen = { version = "0.2", default-features = false, optional = true }
dlmalloc = { version = "0.2", features = ["global"], optional = true }
serde = { workspace = true, features = ["derive"], optional = true }
rkyv = { workspace = true, default-features = false, optional = true }
bytecheck = { workspace = true, default-features = false, optional = true }

[dev-dependencies]
dusk-vm = { workspace = true }
Expand All @@ -24,3 +29,21 @@ dusk-bytes = { workspace = true }

[build-dependencies]
rusk-profile = { workspace = true }


[[bin]]
name = "transfer-contract-dd"
path = "data-driver/bindgen.rs"
required-features = ["data-driver"]

[features]
contract = ["dusk-core/abi-dlmalloc"]
data-driver = [
"dusk-core/serde",
"serde",
"dlmalloc",
"wasm-bindgen",
"dusk-data-driver",
"rkyv",
"bytecheck",
]
22 changes: 19 additions & 3 deletions contracts/transfer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,29 @@ wasm: ## Build the WASM files
--release \
--color=always \
-Z build-std=core,alloc \
--features="contract" \
--target wasm64-unknown-unknown

data-driver: ## Build the data-driver WASM files using wasm-bindgen and wasm-opt
@cargo build \
--release \
--target wasm32-unknown-unknown \
--bin transfer-contract-dd \
--features="data-driver"

@wasm-bindgen \
--target nodejs \
--out-dir ../../target/data-driver/transfer-contract \
../../target/wasm32-unknown-unknown/release/transfer-contract-dd.wasm

@echo "WASM data-driver generated in: target/data-driver/transfer-contract"

clippy: ## Run clippy
@cargo clippy --all-features --release -- -D warnings
@cargo clippy -Z build-std=core,alloc --release --target wasm32-unknown-unknown -- -D warnings
@cargo clippy --release -- -D warnings
@cargo clippy -Z build-std=core,alloc --release --features="contract" --target wasm32-unknown-unknown -- -D warnings
@cargo clippy --release --target wasm32-unknown-unknown --features="data-driver" --bin transfer-contract-dd -- -D warnings

doc: ## Run doc gen
@cargo doc --release

.PHONY: all check test wasm help
.PHONY: all check test wasm help data-driver
103 changes: 103 additions & 0 deletions contracts/transfer/data-driver/bindgen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// 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.

#![no_std]

extern crate alloc;

mod driver;

use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;

use driver::ContractDriver;
use dusk_data_driver::ConvertibleContract;
use wasm_bindgen::prelude::*;

/// Set dlmalloc as the global allocator for heap allocs
#[global_allocator]
static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc;

/// Sends logs to the JS console
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}

#[wasm_bindgen]
pub struct WasmContractDriver {
inner: ContractDriver,
}
impl Default for WasmContractDriver {
fn default() -> Self {
Self::new()
}
}

#[wasm_bindgen]
impl WasmContractDriver {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self {
inner: ContractDriver,
}
}

#[wasm_bindgen]
pub fn encode_input_fn(
&self,
fn_name: &str,
json: &str,
) -> Result<Vec<u8>, JsValue> {
self.inner
.encode_input_fn(fn_name, json)
.map_err(|e| JsValue::from_str(&format!("{e:?}")))
}

#[wasm_bindgen]
pub fn decode_input_fn(
&self,
fn_name: &str,
rkyv: &[u8],
) -> Result<String, JsValue> {
self.inner
.decode_input_fn(fn_name, rkyv)
.map_err(|e| JsValue::from_str(&format!("{e:?}")))
}

#[wasm_bindgen]
pub fn decode_output_fn(
&self,
fn_name: &str,
rkyv: &[u8],
) -> Result<String, JsValue> {
self.inner
.decode_output_fn(fn_name, rkyv)
.map_err(|e| JsValue::from_str(&format!("{e:?}")))
}

#[wasm_bindgen]
pub fn decode_event(
&self,
event_name: &str,
rkyv: &[u8],
) -> Result<String, JsValue> {
self.inner
.decode_event(event_name, rkyv)
.map_err(|e| JsValue::from_str(&format!("{e:?}")))
}

#[wasm_bindgen]
pub fn get_schema(&self) -> String {
self.inner.get_schema()
}
}

fn main() {
// Required for binaries, even if unused in WASM
}
69 changes: 69 additions & 0 deletions contracts/transfer/data-driver/driver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 alloc::string::{String, ToString};
use alloc::vec::Vec;
use dusk_core::transfer::{MoonlightTransactionEvent, MOONLIGHT_TOPIC};
use dusk_data_driver::{rkyv_to_json, ConvertibleContract};

/// JSON schema definition for contract data
const SCHEMA: &str = r#"
{
"type": "object",
"properties": {
"recipient": { "type": "string" },
"amount": { "type": "integer" }
},
"required": ["recipient", "amount"]
}
"#;
use dusk_data_driver::Error;

pub struct ContractDriver;

impl ConvertibleContract for ContractDriver {
#[allow(unused_variables)]
fn encode_input_fn(
&self,
fn_name: &str,
json: &str,
) -> Result<Vec<u8>, Error> {
todo!()
}

#[allow(unused_variables)]
fn decode_input_fn(
&self,
fn_name: &str,
rkyv: &[u8],
) -> Result<String, Error> {
todo!()
}

#[allow(unused_variables)]
fn decode_output_fn(
&self,
fn_name: &str,
rkyv: &[u8],
) -> Result<String, Error> {
todo!()
}

fn decode_event(
&self,
event_name: &str,
rkyv: &[u8],
) -> Result<String, Error> {
match event_name {
MOONLIGHT_TOPIC => rkyv_to_json::<MoonlightTransactionEvent>(rkyv),
_ => todo!(),
}
}

fn get_schema(&self) -> String {
SCHEMA.to_string()
}
}
1 change: 1 addition & 0 deletions contracts/transfer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

#![cfg(not(feature = "data-driver"))]
#![cfg_attr(target_family = "wasm", no_std)]
#![cfg(target_family = "wasm")]
#![feature(arbitrary_self_types)]
Expand Down
14 changes: 14 additions & 0 deletions data-driver/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Add `ConvertibleContract` trait for seamless conversion between JSON and RKYV formats.
- Add `rkyv_to_json` and `json_to_rkyv` functions for bidirectional serialization.
- Add support for encoding and decoding function inputs, outputs, and events in RKYV.
22 changes: 22 additions & 0 deletions data-driver/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "dusk-data-driver"
version = "0.0.1-alpha.1"
edition = "2021"

description = "Types used for interacting with Dusk's transfer and stake contracts."
license = "MPL-2.0"
repository = "https://github.com/dusk-network/rusk"

[dependencies]
rkyv = { workspace = true, features = ["size_32", "alloc", "validation"] }
bytecheck = { workspace = true }

# serde support dependencies
serde = { workspace = true }
serde_json = { workspace = true }


# [features]
# default = []
# stake-contract = []
# transfer-contract = []
31 changes: 31 additions & 0 deletions data-driver/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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.

//! Error-type for dusk-core.

use alloc::string::{String, ToString};
use core::fmt;

/// The dusk-core error type.
#[derive(Debug, Clone, PartialEq)]
pub enum Error {
/// Rkyv serialization.
Rkyv(String),
/// Json serialization
Json(String),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Data-Driver Error: {:?}", &self)
}
}

impl From<serde_json::Error> for Error {
fn from(value: serde_json::Error) -> Self {
Self::Json(value.to_string())
}
}
Loading