Skip to content

Commit

Permalink
Rust code compiles
Browse files Browse the repository at this point in the history
First draft of Wallet template
  • Loading branch information
lrettig committed Jul 31, 2024
1 parent d52bed7 commit 1eb2a08
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 38 deletions.
6 changes: 3 additions & 3 deletions vm/templates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ pub type Pubkey = Bytes32;

#[derive(Encode, Decode)]
pub struct SendArguments {
recipient: Address,
amount: u64,
pub recipient: Address,
pub amount: u64,
}

#[derive(Encode, Decode)]
pub struct SpawnArguments {
owner: Pubkey,
pub owner: Pubkey,
}

// The method selectors
Expand Down
47 changes: 37 additions & 10 deletions vm/templates/program/Cargo.lock

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

3 changes: 2 additions & 1 deletion vm/templates/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ name = "wallet-template"
edition = "2021"

[dependencies]
athena-vm = { git = "https://github.com/athenavm/athena.git", branch = "main", features = [
athena-vm = { git = "https://github.com/athenavm/athena.git", branch = "vmsdk", features = [
"rv32e",
] }
athena-vm-sdk = { git = "https://github.com/athenavm/athena.git", branch = "vmsdk" }
parity-scale-codec = { version = "3.6.12", features = ["derive"] }
wallet-common = { path = "../common" }
Binary file added vm/templates/program/elf/wallet-template
Binary file not shown.
40 changes: 22 additions & 18 deletions vm/templates/program/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#![no_main]
athena_vm::entrypoint!(main);

use athena_vm_sdk::call;
use parity_scale_codec::{Decode, Encode};
use wallet_common::{SendArguments, MethodId, SpawnArguments};
use wallet_common::{MethodId, Pubkey, SendArguments, SpawnArguments};

#[derive(Encode, Decode)]
pub struct Wallet {
Expand All @@ -21,22 +22,15 @@ impl Wallet {
}
}

fn send(&mut self, args: SendArguments) {
fn send(self, args: SendArguments) {
// Send coins
// Note: error checking happens inside the host
unsafe {
athena_vm::host::call(
args.recipient.as_ptr(),
std::ptr::null(),
0,
args.amount.to_le_bytes().as_ptr(),
)
}
call(args.recipient, None, args.amount);
}
fn proxy(&mut self, _args: Vec<u8>) {
fn proxy(self, _args: Vec<u8>) {
unimplemented!();
}
fn deploy(&mut self, _args: Vec<u8>) {
fn deploy(self, _args: Vec<u8>) {
unimplemented!();
}
}
Expand All @@ -46,6 +40,15 @@ pub fn main() {
let method_id = athena_vm::io::read::<u8>();
let encoded_method_args = athena_vm::io::read::<Vec<u8>>();

// convert method_id to MethodId enum
let method = match method_id {
0 => MethodId::Spawn,
1 => MethodId::Send,
2 => MethodId::Proxy,
3 => MethodId::Deploy,
_ => panic!("unsupported method"),
};

// Template only implements a single method, spawn.
// A spawned program implements the other methods.
// This is the entrypoint for both.
Expand All @@ -56,18 +59,18 @@ pub fn main() {
// Instantiate the wallet and dispatch
let spawn_args = athena_vm::io::read::<Vec<u8>>();
let wallet = Wallet::decode(&mut &spawn_args[..]).unwrap();
match method_id {
MethodId::Send as u8 => {
match method {
MethodId::Send => {
let send_arguments = SendArguments::decode(&mut &encoded_method_args[..]).unwrap();
wallet.send(send_arguments);
}
MethodId::Proxy as u8 => {
MethodId::Proxy => {
wallet.proxy(encoded_method_args);
}
MethodId::Deploy as u8 => {
MethodId::Deploy => {
wallet.deploy(encoded_method_args);
}
_ => panic!("unsupported method")
_ => panic!("unsupported method"),
}
};
}
Expand All @@ -76,10 +79,11 @@ fn spawn(args: Vec<u8>) {
// Decode the arguments
// This just makes sure they're valid
let args = SpawnArguments::decode(&mut &args[..]).unwrap();
Wallet::new(args.owner);

// Spawn the wallet
// Note: newly-created program address gets passed directly back from the VM
// unsafe {
// athena_vm::host::spawn(args.as_ptr(), args.len());
// }
}
}
11 changes: 5 additions & 6 deletions vm/templates/script/src/bin/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
use athena_interface::MockHost;
use athena_sdk::{AthenaStdin, ExecutionClient};
use clap::Parser;
use wallet_common::Pubkey;

/// The ELF (executable and linkable format) file for the Athena RISC-V VM.
///
/// This file is generated by running `cargo athena build` inside the `program` directory.
pub const ELF: &[u8] = include_bytes!("../../../program/elf/wallet-program");
pub const ELF: &[u8] = include_bytes!("../../../program/elf/wallet-template");

/// The arguments for the run command.
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct RunArgs {
#[clap(long, default_value = "20")]
owner: string,
owner: String,
}

fn main() {
Expand All @@ -36,11 +35,11 @@ fn main() {
let mut stdin = AthenaStdin::new();

// Convert the owner to a public key.
let owner = Pubkey::from_str(&args.owner).expect("failed to parse owner");
stdin.write(owner);
let owner = args.owner.as_bytes().to_vec();
stdin.write(&owner);

// Run the program.
let (mut output, _) = client
client
.execute::<MockHost>(ELF, stdin, None, None, None)
.expect("failed to run program");
println!("Successfully executed program!");
Expand Down

0 comments on commit 1eb2a08

Please sign in to comment.