Skip to content
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
16 changes: 12 additions & 4 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,3 @@ setup-rs:

.PHONY: setup
setup: setup-rs

41 changes: 36 additions & 5 deletions cargo-casper/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{bail, Context};
use casper_sdk::{abi_generator::Message, schema::SchemaMessage};

use std::{ffi::c_void, fs, path::PathBuf, ptr::NonNull};

Expand Down Expand Up @@ -29,6 +30,10 @@ type CasperLoadEntrypoints = unsafe extern "C" fn(
*mut c_void,
);
type CollectABI = unsafe extern "C" fn(*mut casper_sdk::abi::Definitions);
type CollectMessages = unsafe extern "C" fn(
callback: unsafe extern "C" fn(*const Message, usize, *mut c_void),
ctx: *mut c_void,
);

unsafe extern "C" fn load_entrypoints_cb(
entrypoint: *const casper_sdk::schema::SchemaEntryPoint,
Expand All @@ -41,6 +46,20 @@ unsafe extern "C" fn load_entrypoints_cb(
ctx.extend_from_slice(slice);
}

unsafe extern "C" fn collect_messages_cb(messages: *const Message, count: usize, ctx: *mut c_void) {
let slice = unsafe { std::slice::from_raw_parts(messages, count) };
// pass it to ctx
let ctx = unsafe { &mut *(ctx as *mut Vec<SchemaMessage>) };

for message in slice {
let schema_message = SchemaMessage {
name: message.name.to_string(),
decl: message.decl.to_string(),
};
ctx.push(schema_message);
}
}

fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
match cli.command {
Expand All @@ -60,10 +79,7 @@ fn main() -> anyhow::Result<()> {

let package_name = workspace.package.first().expect("no package");

let extra_features = [
"casper-sdk/__abi_generator".to_string(),
"casper-macros/__abi_generator".to_string(),
];
let extra_features = ["casper-sdk/__abi_generator".to_string()];
features.features.extend(extra_features);

let features_str = features.features.join(",");
Expand Down Expand Up @@ -122,10 +138,13 @@ fn main() -> anyhow::Result<()> {
unsafe { lib.get(b"__cargo_casper_load_entrypoints").unwrap() };
let collect_abi: libloading::Symbol<CollectABI> =
unsafe { lib.get(b"__cargo_casper_collect_abi").unwrap() };
let collect_messages: libloading::Symbol<CollectMessages> =
unsafe { lib.get(b"__cargo_casper_collect_messages").unwrap() };

let entry_points = {
let mut entrypoints: Vec<casper_sdk::schema::SchemaEntryPoint> = Vec::new();
let ctx = NonNull::from(&mut entrypoints);
let ctx: NonNull<Vec<casper_sdk::schema::SchemaEntryPoint>> =
NonNull::from(&mut entrypoints);
unsafe { load_entrypoints(load_entrypoints_cb, ctx.as_ptr() as _) };
entrypoints
};
Expand All @@ -139,6 +158,17 @@ fn main() -> anyhow::Result<()> {
defs
};

let messages = {
let mut messages: Vec<SchemaMessage> = Vec::new();
unsafe {
collect_messages(
collect_messages_cb,
NonNull::from(&mut messages).as_ptr() as _,
);
}
messages
};

// TODO: Move schema outside sdk to avoid importing unnecessary deps into wasm build

let schema = casper_sdk::schema::Schema {
Expand All @@ -150,6 +180,7 @@ fn main() -> anyhow::Result<()> {
},
definitions: defs,
entry_points,
messages,
};

if let Some(output) = output_path {
Expand Down
2 changes: 1 addition & 1 deletion execution_engine/src/runtime_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ where
let topic_value = StoredValue::MessageTopic(MessageTopicSummary::new(
topic_message_count,
block_time,
message.topic_name().clone(),
message.topic_name().to_owned(),
));
let message_key = message.message_key();
let message_value = StoredValue::Message(message.checksum().map_err(ExecError::BytesRepr)?);
Expand Down
29 changes: 29 additions & 0 deletions executor/wasm-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,54 @@ pub enum Error {
InvalidData,
/// The input to the host function was invalid.
InvalidInput,
/// The topic is too long.
TopicTooLong,
/// Too many topics.
TooManyTopics,
/// The payload is too long.
PayloadTooLong,
/// The message topic is full and cannot accept new messages.
MessageTopicFull,
/// The maximum number of messages emitted per block was exceeded when trying to emit a
/// message.
MaxMessagesPerBlockExceeded,
/// An error code not covered by the other variants.
Other(i32),
}

pub const HOST_ERROR_SUCCEED: i32 = 0;
pub const HOST_ERROR_NOT_FOUND: i32 = 1;
pub const HOST_ERROR_INVALID_DATA: i32 = 2;
pub const HOST_ERROR_INVALID_INPUT: i32 = 3;
pub const HOST_ERROR_TOPIC_TOO_LONG: i32 = 4;
pub const HOST_ERROR_TOO_MANY_TOPICS: i32 = 5;
pub const HOST_ERROR_PAYLOAD_TOO_LONG: i32 = 6;
pub const HOST_ERROR_MESSAGE_TOPIC_FULL: i32 = 7;
pub const HOST_ERROR_MAX_MESSAGES_PER_BLOCK_EXCEEDED: i32 = 8;

impl From<i32> for Error {
fn from(value: i32) -> Self {
match value {
HOST_ERROR_NOT_FOUND => Error::NotFound,
HOST_ERROR_INVALID_DATA => Error::InvalidData,
HOST_ERROR_INVALID_INPUT => Error::InvalidInput,
HOST_ERROR_TOPIC_TOO_LONG => Error::TopicTooLong,
HOST_ERROR_TOO_MANY_TOPICS => Error::TooManyTopics,
HOST_ERROR_PAYLOAD_TOO_LONG => Error::PayloadTooLong,
HOST_ERROR_MESSAGE_TOPIC_FULL => Error::MessageTopicFull,
HOST_ERROR_MAX_MESSAGES_PER_BLOCK_EXCEEDED => Error::MaxMessagesPerBlockExceeded,
other => Error::Other(other),
}
}
}

pub fn result_from_code(code: i32) -> Result<(), Error> {
match code {
HOST_ERROR_SUCCEED => Ok(()),
other => Err(Error::from(other)),
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 3 additions & 1 deletion executor/wasm-host/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use bytes::Bytes;
use casper_executor_wasm_interface::executor::Executor;
use casper_storage::{global_state::GlobalStateReader, AddressGenerator, TrackingCopy};
use casper_types::{
account::AccountHash, BlockTime, Key, StorageCosts, TransactionHash, WasmV2Config,
account::AccountHash, BlockTime, Key, MessageLimits, StorageCosts, TransactionHash,
WasmV2Config,
};
use parking_lot::RwLock;

Expand All @@ -24,6 +25,7 @@ pub struct Context<S: GlobalStateReader, E: Executor> {
pub transferred_value: u128,
pub config: WasmV2Config,
pub storage_costs: StorageCosts,
pub message_limits: MessageLimits,
pub tracking_copy: TrackingCopy<S>,
pub executor: E, // TODO: This could be part of the caller
pub transaction_hash: TransactionHash,
Expand Down
Loading