Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanup #46

Merged
merged 1 commit into from
Jul 20, 2024
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
19 changes: 13 additions & 6 deletions core/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ use athena_interface::{AthenaContext, HostInterface, HostProvider, StatusCode};

/// An implementation of a runtime for the Athena RISC-V VM.
///
/// The runtime is responsible for executing a user program and tracing important events which occur
/// during execution (i.e., memory reads, alu operations, etc).
/// The runtime is responsible for executing a user program.
///
/// For more information on the RV32IM instruction set, see the following:
/// https://www.cs.sfu.ca/~ashriram/Courses/CS295/assets/notebooks/RISCV/RISCV_CARD.pdf
Expand Down Expand Up @@ -67,7 +66,7 @@ pub struct Runtime<T: HostInterface> {
/// the unconstrained block. The only thing preserved is writes to the input stream.
pub unconstrained: bool,

/// Max gas for the runtime. If gas is set to 0, the runtime will not meter gas.
/// Max gas for the runtime.
pub max_gas: Option<u32>,

pub(crate) unconstrained_state: ForkState,
Expand Down Expand Up @@ -831,6 +830,7 @@ pub mod tests {
},
};

use super::syscall::SyscallCode;
use super::{Instruction, Opcode, Program, Runtime};

pub fn simple_program() -> Program {
Expand Down Expand Up @@ -900,7 +900,7 @@ pub mod tests {
Arc::new(RefCell::new(HostProvider::new(MockHost::new())))
}

// program should cost 4332 gas units
// program should cost 11237 gas units

// failure
let mut runtime = Runtime::<MockHost>::new(
Expand Down Expand Up @@ -957,8 +957,15 @@ pub mod tests {
// X12 is arg3 (input len)
// no input
Instruction::new(Opcode::ADD, Register::X12 as u32, 0, 0, false, true),
// X5 is syscall ID: 0xA2 (HOST_CALL)
Instruction::new(Opcode::ADD, Register::X5 as u32, 0, 0xA2, false, true),
// X5 is syscall ID
Instruction::new(
Opcode::ADD,
Register::X5 as u32,
0,
SyscallCode::HOST_CALL as u32,
false,
true,
),
Instruction::new(Opcode::ECALL, 0, 0, 0, false, false),
];
let program = Program::new(instructions, 0, 0);
Expand Down
7 changes: 4 additions & 3 deletions ffi/vmlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use athcon_vm::{
SetOptionError,
};
use athena_interface::{
Address, AthenaMessage, AthenaRevision, Balance, Bytes32, ExecutionResult, HostInterface,
HostProvider, MessageKind, StatusCode, StorageStatus, TransactionContext, VmInterface,
Address, AthenaMessage, AthenaRevision, Balance, Bytes32, Bytes32AsU64, ExecutionResult,
HostInterface, HostProvider, MessageKind, StatusCode, StorageStatus, TransactionContext,
VmInterface,
};
use athena_runner::{AthenaVm, Bytes32AsU64};
use athena_runner::AthenaVm;

#[athcon_declare_vm("Athena", "athena1", "0.1.0")]
pub struct AthenaVMWrapper {
Expand Down
31 changes: 31 additions & 0 deletions interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,37 @@ pub type Balance = u64;
pub type Bytes32 = [u8; BYTES32_LENGTH];
pub type Bytes = [u8];

pub struct Bytes32AsU64(Bytes32);

impl Bytes32AsU64 {
pub fn new(bytes: Bytes32) -> Self {
Bytes32AsU64(bytes)
}
}

impl From<Bytes32AsU64> for u64 {
fn from(bytes: Bytes32AsU64) -> Self {
// take most significant 8 bytes, assume little-endian
let slice = &bytes.0[..8];
u64::from_le_bytes(slice.try_into().expect("slice with incorrect length"))
}
}

impl From<Bytes32AsU64> for Bytes32 {
fn from(bytes: Bytes32AsU64) -> Self {
bytes.0
}
}

impl From<u64> for Bytes32AsU64 {
fn from(value: u64) -> Self {
let mut bytes = [0u8; 32];
let value_bytes = value.to_le_bytes();
bytes[..8].copy_from_slice(&value_bytes);
Bytes32AsU64(bytes)
}
}

pub struct AddressWrapper(Address);

impl From<Vec<u32>> for AddressWrapper {
Expand Down
32 changes: 0 additions & 32 deletions runner/src/host.rs

This file was deleted.

2 changes: 0 additions & 2 deletions runner/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
pub mod host;
pub mod vm;

pub use host::Bytes32AsU64;
pub use vm::AthenaVm;