Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

New C API features: svm_create_account, svm_increase_balance, etc. #410

Merged
merged 85 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
290f3c3
Attempt explicit targets in CI
neysofu Oct 22, 2021
06d9169
Change windows artifact files
neysofu Oct 25, 2021
9301f77
Use dll.a file on windows
neysofu Oct 25, 2021
9999d6e
ls target/profile
neysofu Oct 25, 2021
3b8a704
Fix toolchain/target spec
neysofu Oct 25, 2021
b668af9
Use dll.lib
neysofu Oct 25, 2021
18e7da1
Use acitions/toolchain@v1
neysofu Oct 25, 2021
528a98e
Use svm, not libsvm
neysofu Oct 25, 2021
5e1ce78
Better caching
neysofu Oct 25, 2021
3bc343c
Change cache keys
neysofu Oct 25, 2021
04aa507
Cache .cargo/bin
neysofu Oct 25, 2021
ffb86e3
Explicit target
neysofu Oct 25, 2021
3bbbaee
Cache keys and use-cruss
neysofu Oct 25, 2021
a8756f9
ls target/${{ matrix.target }}
neysofu Oct 25, 2021
1965106
Separate ls output
neysofu Oct 25, 2021
e0b813f
Fix use of use-cross
neysofu Oct 25, 2021
6520543
Fix target subdir
neysofu Oct 25, 2021
91f82a0
Remove custom target in toolchain
neysofu Oct 25, 2021
7a6676a
No use-cross
neysofu Oct 25, 2021
560732b
Install matrix.target
neysofu Oct 25, 2021
7d9fb52
Empty commit
neysofu Oct 25, 2021
9cd0a81
Empty commit
neysofu Oct 25, 2021
16e0b10
use-cross
neysofu Oct 26, 2021
0b6e28c
Disable use-cross
neysofu Oct 26, 2021
15c0ea7
MSVC target
neysofu Oct 26, 2021
9af4404
Remove rlib
neysofu Oct 26, 2021
6d3ee4b
Revert "Remove rlib"
neysofu Oct 26, 2021
47d0ac0
ls artifacts
neysofu Oct 26, 2021
6cae7b3
Put .dll in artifacts on windows
neysofu Oct 26, 2021
8a67f82
Try building Windows gnu on ubuntu
neysofu Oct 26, 2021
19a8cb9
Add windows to the matrix
neysofu Oct 27, 2021
5c61a5c
Try install some msys2 tools
neysofu Oct 27, 2021
81a2f9b
Fix matrix configs
neysofu Oct 27, 2021
9c9d4a2
Fix windows-latest lib names
neysofu Oct 27, 2021
bcec609
Panic if Template::from_sections misses sections
neysofu Oct 27, 2021
807cebd
Revert "Panic if Template::from_sections misses sections"
neysofu Oct 27, 2021
3a178ec
Simplify ci.yml + copy .pcb on windows
neysofu Nov 1, 2021
8cb705a
Add missing brackets in bash if's
neysofu Nov 1, 2021
17754d9
Fix .pdb extension
neysofu Nov 1, 2021
0ec0643
Invalidate faulty cache (macOS)
neysofu Nov 1, 2021
6683d34
Add checks for missing sections
neysofu Nov 1, 2021
4b31d50
actions/upload-artifact@2
neysofu Nov 1, 2021
f2aa696
Release drafter initial work
neysofu Nov 1, 2021
7c95593
actions/upload-artifact@master
neysofu Nov 1, 2021
9f92eda
Don't cache .cargo/bin
neysofu Nov 1, 2021
198d2fb
release doesn't depend on release step anymore
neysofu Nov 1, 2021
c5a5090
Depend on build again
neysofu Nov 1, 2021
8a06d97
Change cache path
neysofu Nov 1, 2021
e862591
Fix faulty encoding of noncore sections
neysofu Nov 1, 2021
2981e54
Add automatic release step
neysofu Nov 1, 2021
f7e8fdd
Dummy commit for automatic release testing
neysofu Nov 1, 2021
7c7678a
Dummy commit
neysofu Nov 1, 2021
948fe73
Run workflow when pushing new tags
neysofu Nov 1, 2021
18e7dd7
Add version tag to release files
neysofu Nov 1, 2021
ddcaa17
Better zipping of directories
neysofu Nov 1, 2021
055502a
Add test dependency to release step
neysofu Nov 1, 2021
82462fc
Fix zipping step
neysofu Nov 1, 2021
f7f00d5
Add .zip extension to release files
neysofu Nov 1, 2021
78c1b23
Fix zipping once again
neysofu Nov 1, 2021
22d16ed
Move .zip files
neysofu Nov 1, 2021
1d58956
Remove double space
neysofu Nov 1, 2021
ac26cd4
chmod +x svm-cli
neysofu Nov 1, 2021
aeef895
Remove chmod +x svm-cli
neysofu Nov 1, 2021
6a0badb
Move cp-artifacts.sh into .github directory
neysofu Nov 1, 2021
4073f9b
New call to free svm_result_t
neysofu Nov 1, 2021
702e197
Use *mut u8 in svm_result_t
neysofu Nov 1, 2021
b297e23
Move Windows linkage fix from go-svm to svm CI
neysofu Nov 1, 2021
3a8282c
Refactor DefaultRuntime and RuntimeTracker
neysofu Nov 5, 2021
63f6940
Add some missing FFI methods
neysofu Nov 5, 2021
f9645ca
Change god template address
neysofu Nov 5, 2021
99bc398
Create account with god template address
neysofu Nov 5, 2021
60be5f3
Add panic for invalid template_storage calls
neysofu Nov 5, 2021
dd80bd5
Root state hash C fn
neysofu Nov 5, 2021
b9ab6b4
Fix some build errors
neysofu Nov 5, 2021
0b9a2ff
Add some docs to C APIs
neysofu Nov 8, 2021
811baf8
svm_layer_info
neysofu Nov 8, 2021
fbdf380
Fix extern "C"
neysofu Nov 8, 2021
731592f
Merge branch 'master' into new-capi-fns
neysofu Nov 8, 2021
05117f0
Merge branch 'master' into new-capi-fns
neysofu Nov 8, 2021
c17b440
GenesisTemplate
neysofu Nov 15, 2021
75c9b93
Merge branch 'master' into new-capi-fns
neysofu Nov 24, 2021
44e4c08
Delete genesis template
neysofu Nov 24, 2021
bef255e
Update Cargo.lock
neysofu Nov 24, 2021
dd52b64
#[must_use]
neysofu Nov 24, 2021
dd58520
Use ? rather than safe_try
neysofu Nov 24, 2021
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
80 changes: 40 additions & 40 deletions crates/abi-decoder/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ macro_rules! assert_no_eof {

macro_rules! decode_fixed_primitive {
($self:expr, $ty:ident, $n:expr, $iter:expr) => {{
let ptr = safe_try!($self.read_bytes($iter, $n));
let ptr = $self.read_bytes($iter, $n)?;
let value: $ty = ptr.as_ptr().into();

let prim = Primitive::$ty(value);
Expand All @@ -75,7 +75,7 @@ impl Decoder {
pub fn decode_value(&self, cursor: &mut Cursor) -> Result<Value, DecodeError> {
assert_no_eof!(cursor);

let kind = safe_try!(self.peek_kind(cursor));
let kind = self.peek_kind(cursor)?;

match kind {
TypeKind::Array => self.decode_composite(cursor),
Expand All @@ -85,33 +85,33 @@ impl Decoder {
}

fn decode_primitive(&self, cursor: &mut Cursor) -> Result<Value, DecodeError> {
let kind = safe_try!(self.peek_kind(cursor));
let kind = self.peek_kind(cursor)?;

let value = match kind {
TypeKind::None => safe_try!(self.decode_none(cursor)).into(),
TypeKind::Unit => safe_try!(self.decode_unit(cursor)).into(),
TypeKind::Bool => safe_try!(self.decode_bool(cursor)).into(),
TypeKind::Address => safe_try!(self.decode_addr(cursor)).into(),
TypeKind::Amount => safe_try!(self.decode_amount(cursor)).into(),
TypeKind::I8 => safe_try!(self.decode_i8(cursor)).into(),
TypeKind::U8 => safe_try!(self.decode_u8(cursor)).into(),
TypeKind::I16 => safe_try!(self.decode_i16(cursor)).into(),
TypeKind::U16 => safe_try!(self.decode_u16(cursor)).into(),
TypeKind::I32 => safe_try!(self.decode_i32(cursor)).into(),
TypeKind::U32 => safe_try!(self.decode_u32(cursor)).into(),
TypeKind::I64 => safe_try!(self.decode_i64(cursor)).into(),
TypeKind::U64 => safe_try!(self.decode_u64(cursor)).into(),
TypeKind::None => self.decode_none(cursor)?.into(),
TypeKind::Unit => self.decode_unit(cursor)?.into(),
TypeKind::Bool => self.decode_bool(cursor)?.into(),
TypeKind::Address => self.decode_addr(cursor)?.into(),
TypeKind::Amount => self.decode_amount(cursor)?.into(),
TypeKind::I8 => self.decode_i8(cursor)?.into(),
TypeKind::U8 => self.decode_u8(cursor)?.into(),
TypeKind::I16 => self.decode_i16(cursor)?.into(),
TypeKind::U16 => self.decode_u16(cursor)?.into(),
TypeKind::I32 => self.decode_i32(cursor)?.into(),
TypeKind::U32 => self.decode_u32(cursor)?.into(),
TypeKind::I64 => self.decode_i64(cursor)?.into(),
TypeKind::U64 => self.decode_u64(cursor)?.into(),
_ => svm_sdk_std::panic(),
};

Result::Ok(value)
}

fn decode_composite(&self, cursor: &mut Cursor) -> Result<Value, DecodeError> {
let kind = safe_try!(self.peek_kind(cursor));
let kind = self.peek_kind(cursor)?;

let value = match kind {
TypeKind::Array => safe_try!(self.decode_array(cursor)).into(),
TypeKind::Array => self.decode_array(cursor)?.into(),

_ => svm_sdk_std::panic(),
};
Expand All @@ -120,23 +120,23 @@ impl Decoder {
}

fn decode_none(&self, cursor: &mut Cursor) -> Result<Value, DecodeError> {
let byte = safe_try!(self.read_byte(cursor));
let byte = self.read_byte(cursor)?;

debug_assert_eq!(byte, layout::NONE);

Result::Ok(Value::none())
}

fn decode_unit(&self, cursor: &mut Cursor) -> Result<Value, DecodeError> {
let byte = safe_try!(self.read_byte(cursor));
let byte = self.read_byte(cursor)?;

debug_assert_eq!(byte, layout::UNIT);

Result::Ok(Value::unit())
}

fn decode_bool(&self, cursor: &mut Cursor) -> Result<Value, DecodeError> {
let byte = safe_try!(self.read_byte(cursor));
let byte = self.read_byte(cursor)?;

let v = match byte {
layout::BOOL_FALSE => false,
Expand All @@ -148,15 +148,15 @@ impl Decoder {
}

fn decode_addr(&self, cursor: &mut Cursor) -> Result<Value, DecodeError> {
let byte = safe_try!(self.read_byte(cursor));
let byte = self.read_byte(cursor)?;

debug_assert_eq!(byte, layout::ADDRESS);

decode_fixed_primitive!(self, Address, Address::len(), cursor)
}

fn decode_amount(&self, cursor: &mut Cursor) -> Result<Value, DecodeError> {
let byte = safe_try!(self.read_byte(cursor));
let byte = self.read_byte(cursor)?;

let nbytes = match byte {
layout::AMOUNT_1B => 1,
Expand All @@ -170,50 +170,50 @@ impl Decoder {
_ => svm_sdk_std::panic(),
};

let num = safe_try!(self.read_num(cursor, nbytes));
let num = self.read_num(cursor, nbytes)?;
let amount = Amount(num);

Result::Ok(amount.into())
}

fn decode_i8(&self, cursor: &mut Cursor) -> Result<i8, DecodeError> {
let byte = safe_try!(self.read_byte(cursor));
let byte = self.read_byte(cursor)?;

debug_assert!(byte == layout::I8 || byte == layout::U8);

let num = safe_try!(self.read_num(cursor, 1)) as i8;
let num = self.read_num(cursor, 1)? as i8;

Result::Ok(num)
}

fn decode_u8(&self, cursor: &mut Cursor) -> Result<u8, DecodeError> {
let num = safe_try!(self.decode_i8(cursor)) as u8;
let num = self.decode_i8(cursor)? as u8;

Result::Ok(num)
}

fn decode_i16(&self, cursor: &mut Cursor) -> Result<i16, DecodeError> {
let byte = safe_try!(self.read_byte(cursor));
let byte = self.read_byte(cursor)?;

let nbytes = match byte {
layout::I16_1B | layout::U16_1B => 1,
layout::I16_2B | layout::U16_2B => 2,
_ => svm_sdk_std::panic(),
};

let num = safe_try!(self.read_num(cursor, nbytes)) as i16;
let num = self.read_num(cursor, nbytes)? as i16;

Result::Ok(num)
}

fn decode_u16(&self, cursor: &mut Cursor) -> Result<u16, DecodeError> {
let num = safe_try!(self.decode_i16(cursor)) as u16;
let num = self.decode_i16(cursor)? as u16;

Result::Ok(num)
}

fn decode_i32(&self, cursor: &mut Cursor) -> Result<i32, DecodeError> {
let byte = safe_try!(self.read_byte(cursor));
let byte = self.read_byte(cursor)?;

let nbytes = match byte {
layout::I32_1B | layout::U32_1B => 1,
Expand All @@ -223,19 +223,19 @@ impl Decoder {
_ => svm_sdk_std::panic(),
};

let num = safe_try!(self.read_num(cursor, nbytes)) as i32;
let num = self.read_num(cursor, nbytes)? as i32;

Result::Ok(num)
}

fn decode_u32(&self, cursor: &mut Cursor) -> Result<u32, DecodeError> {
let num = safe_try!(self.decode_i32(cursor)) as u32;
let num = self.decode_i32(cursor)? as u32;

Result::Ok(num)
}

fn decode_i64(&self, cursor: &mut Cursor) -> Result<i64, DecodeError> {
let byte = safe_try!(self.read_byte(cursor));
let byte = self.read_byte(cursor)?;

let nbytes = match byte {
layout::I64_1B | layout::U64_1B => 1,
Expand All @@ -249,21 +249,21 @@ impl Decoder {
_ => svm_sdk_std::panic(),
};

let num = safe_try!(self.read_num(cursor, nbytes)) as i64;
let num = self.read_num(cursor, nbytes)? as i64;

Result::Ok(num)
}

fn decode_u64(&self, cursor: &mut Cursor) -> Result<u64, DecodeError> {
let num = safe_try!(self.decode_i64(cursor)) as u64;
let num = self.decode_i64(cursor)? as u64;

Result::Ok(num)
}

fn decode_array(&self, cursor: &mut Cursor) -> Result<Value, DecodeError> {
assert_no_eof!(cursor);

let len = safe_try!(self.read_byte(cursor));
let len = self.read_byte(cursor)?;

let mut values: Vec<Value> = Vec::with_capacity(len as usize);

Expand All @@ -283,7 +283,7 @@ impl Decoder {
};
seq_macro::seq!(n in 0..11 {
if len > n {
let value = safe_try!(self.decode_primitive(cursor));
let value = self.decode_primitive(cursor)?;
values.push(value);
}
});
Expand All @@ -304,7 +304,7 @@ impl Decoder {
fn read_num(&self, cursor: &mut Cursor, nbytes: usize) -> Result<u64, DecodeError> {
debug_assert!(nbytes > 0 && nbytes <= 8);

let ptr = safe_try!(self.read_bytes(cursor, nbytes)).as_ptr();
let ptr = self.read_bytes(cursor, nbytes)?.as_ptr();
let slice = unsafe { core::slice::from_raw_parts(ptr, nbytes) };
let mut data = [0u8; 8];

Expand Down Expand Up @@ -337,7 +337,7 @@ impl Decoder {

#[inline]
fn peek_kind(&self, cursor: &mut Cursor) -> Result<TypeKind, DecodeError> {
let byte = safe_try!(self.peek(cursor));
let byte = self.peek(cursor)?;

let kind = match byte {
layout::NONE => TypeKind::None,
Expand Down
25 changes: 12 additions & 13 deletions crates/runtime-ffi/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub unsafe extern "C" fn svm_free_result(_result: svm_result_t) {}

/// Creates an account at genesis with a given balance and nonce counter.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn svm_create_account(
runtime_ptr: *mut c_void,
addr: *const u8,
Expand All @@ -79,6 +80,7 @@ pub unsafe extern "C" fn svm_create_account(

/// Magically increases an account's balance by the given amount. Used for genesis setup.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn svm_increase_balance(
runtime_ptr: *mut c_void,
addr: *const u8,
Expand Down Expand Up @@ -138,7 +140,7 @@ pub unsafe extern "C" fn svm_runtime_create(runtime_ptr: *mut *mut c_void) -> sv
///
/// # Examples
///
/// ```rust, no_run
/// ```rust
/// use svm_runtime_ffi::*;
///
/// let mut runtime = std::ptr::null_mut();
Expand Down Expand Up @@ -176,7 +178,7 @@ pub unsafe extern "C" fn svm_runtimes_count(count: *mut u64) {
///
/// # Examples
///
/// ```rust, no_run
/// ```rust
/// use svm_runtime_ffi::*;
///
/// let mut runtime = std::ptr::null_mut();
Expand Down Expand Up @@ -214,7 +216,7 @@ pub unsafe extern "C" fn svm_validate_deploy(
///
/// # Examples
///
/// ```rust, no_run
/// ```rust
/// use svm_runtime_ffi::*;
///
/// let mut runtime = std::ptr::null_mut();
Expand Down Expand Up @@ -248,7 +250,7 @@ pub unsafe extern "C" fn svm_validate_spawn(
///
/// # Examples
///
/// ```rust, no_run
/// ```rust
/// use svm_runtime_ffi::*;
///
/// let mut runtime = std::ptr::null_mut();
Expand Down Expand Up @@ -282,7 +284,7 @@ pub unsafe extern "C" fn svm_validate_call(
///
/// # Examples
///
/// ```rust, no_run
/// ```rust
/// use svm_runtime_ffi::*;
///
/// let mut runtime = std::ptr::null_mut();
Expand Down Expand Up @@ -330,7 +332,7 @@ pub unsafe extern "C" fn svm_deploy(
///
/// # Examples
///
/// ```rust, no_run
/// ```rust
/// use svm_runtime_ffi::*;
///
/// let mut runtime = std::ptr::null_mut();
Expand Down Expand Up @@ -382,7 +384,7 @@ pub unsafe extern "C" fn svm_spawn(
///
/// # Examples
///
/// ```rust, no_run
/// ```rust
/// use svm_runtime_ffi::*;
///
/// let mut runtime = std::ptr::null_mut();
Expand Down Expand Up @@ -431,7 +433,7 @@ pub unsafe extern "C" fn svm_verify(
///
/// # Examples
///
/// ```rust, no_run
/// ```rust
/// use svm_runtime_ffi::*;
///
/// let mut runtime = std::ptr::null_mut();
Expand Down Expand Up @@ -547,7 +549,7 @@ unsafe fn svm_runtime_action<F, C>(
message_size: u32,
context: *const u8,
f: F,
f_name: &str,
_f_name: &str,
) -> svm_result_t
where
F: FnOnce(&mut Runtime, &Envelope, &[u8], &Context) -> C + UnwindSafe,
Expand All @@ -559,13 +561,10 @@ where
let envelope = slice::from_raw_parts(envelope, Envelope::fixed_size().unwrap());
let context = slice::from_raw_parts(context, Context::fixed_size().unwrap());

debug!("`{}` start", f_name);

let envelope = Envelope::decode_bytes(envelope)?;
let context = Context::decode_bytes(context)?;
let receipt = f(runtime, &envelope, &message, &context);

debug!("`{}` returns `svm_result_t::OK`", f_name);
svm_result_t::new_receipt(&receipt.encode_to_vec())
})
}
Expand All @@ -590,7 +589,7 @@ where
svm_result_t::OK
}
Err(e) => {
error!("`{}` returns `SVM_FAILURE`", f_name);
error!("`{}` returns an errors", f_name);
svm_result_t::new_error(e.to_string().as_bytes())
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"api":[{"doc":"","is_ctor":false,"is_fundable":false,"name":"store_addr","signature":{"params":[{"name":"addr","type":"Address"}],"returns":{}},"wasm_name":"store_addr"},{"doc":"","is_ctor":false,"is_fundable":false,"name":"load_addr","signature":{"params":[],"returns":{"type":"Address"}},"wasm_name":"load_addr"},{"doc":"","is_ctor":true,"is_fundable":false,"name":"initialize","signature":{"params":[{"name":"init","type":"Address"},{"name":"should_store","type":"bool"}],"returns":{"type":"bool"}},"wasm_name":"initialize"}],"schema":[{"byte_count":20,"id":0,"name":"addr","offset":0,"type":"Address"}]}
{"api":[{"doc":"","is_ctor":true,"is_fundable":false,"name":"initialize","signature":{"params":[{"name":"init","type":"Address"},{"name":"should_store","type":"bool"}],"returns":{"type":"bool"}},"wasm_name":"initialize"},{"doc":"","is_ctor":false,"is_fundable":false,"name":"store_addr","signature":{"params":[{"name":"addr","type":"Address"}],"returns":{}},"wasm_name":"store_addr"},{"doc":"","is_ctor":false,"is_fundable":false,"name":"load_addr","signature":{"params":[],"returns":{"type":"Address"}},"wasm_name":"load_addr"}],"schema":[{"byte_count":20,"id":0,"name":"addr","offset":0,"type":"Address"}]}
Binary file modified crates/runtime/tests/wasm/runtime_calldata.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions crates/sdk-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#![deny(unreachable_code)]
#![deny(rustdoc::broken_intra_doc_links)]
#![feature(core_intrinsics)]
#![feature(try_trait_v2)]

mod log;
pub use log::log;
Expand Down
Loading