Skip to content

Commit

Permalink
Merge pull request #205 from athenavm/refactor/template-macro
Browse files Browse the repository at this point in the history
allow using `template` proc macro multiple times
  • Loading branch information
poszu authored Oct 31, 2024
2 parents 815cd53 + a93072a commit 2b05894
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 49 deletions.
22 changes: 11 additions & 11 deletions examples/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 examples/wallet/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2021"
[dependencies]
athena-interface = { path = "../../../interface" }
athena-vm = { path = "../../../vm/entrypoint", features = [
"noentrypoint",
"rv32e",
] }
athena-vm-declare = { path = "../../../vm/declare" }
Expand Down
16 changes: 6 additions & 10 deletions examples/wallet/program/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
extern crate alloc;

use athena_interface::Address;
use athena_vm::entrypoint;
use athena_vm_declare::{callable, template};
use athena_vm_sdk::{call, spawn, Pubkey, SpendArguments, VerifiableTemplate, WalletProgram};
use parity_scale_codec::{Decode, Encode};

#[derive(Debug, Encode, Decode)]
pub struct Wallet {
nonce: u64,
Expand All @@ -24,6 +26,8 @@ impl Wallet {
}
}

athena_vm::entrypoint!();

#[cfg(all(
any(target_arch = "riscv32", target_arch = "riscv64"),
target_feature = "e"
Expand Down Expand Up @@ -59,18 +63,10 @@ impl WalletProgram for Wallet {
}
}

#[template]
impl VerifiableTemplate for Wallet {
#[callable]
fn verify(&self, tx: alloc::vec::Vec<u8>, signature: [u8; 64]) -> bool {
athena_vm_sdk::precompiles::ed25519::verify(&tx, &self.owner.0, &signature)
}
}

#[link_section = ".text.athexp.verify"]
#[no_mangle]
pub unsafe extern "C" fn athexp_verify() {
athena_vm::program::Method::call_method(Wallet::verify, &mut athena_vm::io::Io::default());
syscall_halt(0);
}
#[used]
#[link_section = ".init_array"]
static DUMMY_VERIFY: unsafe extern "C" fn() = athexp_verify;
1 change: 0 additions & 1 deletion tests/entrypoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2021"

[dependencies]
athena-vm = { path = "../../vm/entrypoint", features = [
"noentrypoint",
"rv32e",
] }
athena-vm-declare = { path = "../../vm/declare" }
Expand Down
3 changes: 3 additions & 0 deletions tests/entrypoint/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#![no_main]
use athena_vm::entrypoint;
use athena_vm_declare::{callable, template};
use athena_vm_sdk::call;

pub struct EntrypointTest {}

athena_vm::entrypoint!();

#[cfg(all(
any(target_arch = "riscv32", target_arch = "riscv64"),
target_feature = "e"
Expand Down
6 changes: 1 addition & 5 deletions vm/declare/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn template(_attr: TokenStream, item: TokenStream) -> TokenStream {
#[no_mangle]
pub unsafe extern "C" fn #c_func_name() {
#call;
syscall_halt(0);
athena_vm::syscalls::syscall_halt(0);
}

// This black magic ensures the function symbol makes it into the final binary.
Expand All @@ -55,10 +55,6 @@ pub fn template(_attr: TokenStream, item: TokenStream) -> TokenStream {
}

let output = quote! {
// Basic Athena preamble, for use without a main entrypoint.
use athena_vm::syscalls::syscall_halt;
athena_vm::entrypoint!();

#input

#(#c_functions)*
Expand Down
1 change: 0 additions & 1 deletion vm/entrypoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ athena-lib = { path = "../lib", optional = true }
[features]
default = ["lib"]
lib = ["dep:athena-lib"]
noentrypoint = []
rv32e = []
30 changes: 10 additions & 20 deletions vm/entrypoint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,21 @@ pub mod types {
pub use athena_interface::*;
}

// This macro should be used for libraries with multiple exported functions
#[cfg(feature = "noentrypoint")]
/// Define the program entrypoint.
///
/// Configures the global allocator and an optional entrypoint function.
/// The entrypoint function is called by the runtime if no other method is
/// explicitly selected.
#[macro_export]
macro_rules! entrypoint {
() => {
use $crate::heap::SimpleAlloc;

#[global_allocator]
static HEAP: SimpleAlloc = SimpleAlloc;

mod vm_generated_main {
#[no_mangle]
fn main() {
panic!("noentrypoint feature is enabled");
}
fn main() {
panic!("No entrypoint found");
}
entrypoint!(main);
};
}

// This macro should be used for programs with a single entrypoint
#[cfg(not(feature = "noentrypoint"))]
#[macro_export]
macro_rules! entrypoint {
($path:path) => {
const VM_ENTRY: fn() = $path;
($entry:path) => {
const VM_ENTRY: fn() = $entry;

use $crate::heap::SimpleAlloc;

Expand Down

0 comments on commit 2b05894

Please sign in to comment.