remove old WASI stuff, update app store transfer lib

This commit is contained in:
dr-frmr 2023-10-17 13:00:21 -04:00
parent 7e26dfd27d
commit a4c271e81e
No known key found for this signature in database
9 changed files with 130 additions and 165 deletions

View File

@ -15,6 +15,7 @@ dependencies = [
"anyhow",
"bincode",
"cargo-component-bindings",
"rand",
"serde",
"serde_json",
"wit-bindgen",
@ -65,6 +66,12 @@ dependencies = [
"wit-component",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "equivalent"
version = "1.0.1"
@ -80,6 +87,17 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "getrandom"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.14.0"
@ -134,6 +152,12 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67"
[[package]]
name = "libc"
version = "0.2.149"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b"
[[package]]
name = "log"
version = "0.4.20"
@ -152,6 +176,12 @@ version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro2"
version = "1.0.66"
@ -181,6 +211,36 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "ryu"
version = "1.0.15"
@ -324,6 +384,12 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-encoder"
version = "0.32.0"

View File

@ -14,6 +14,7 @@ lto = true
anyhow = "1.0"
bincode = "1.3.3"
cargo-component-bindings = { git = "https://github.com/bytecodealliance/cargo-component" }
rand = "0.8.5"
serde = {version = "1.0", features = ["derive"] }
serde_json = "1.0"
wit-bindgen = { version = "0.11.0", default_features = false }

View File

@ -1,6 +1,5 @@
use super::bindings::component::uq_process::types::*;
use crate::bindings::wasi::random::random;
use crate::bindings::{get_payload, get_unix_time, receive, send_request, send_response};
use crate::bindings::{get_payload, receive, send_request, send_response};
use serde::{Deserialize, Serialize};
#[derive(Debug)]
@ -29,7 +28,7 @@ pub fn transfer(
Result<(), TransferError>,
Vec<Result<(Address, Message), (SendError, Option<Context>)>>,
) {
let transfer_context_id = random::get_random_u64();
let transfer_context_id: u64 = rand::random();
let mut bytes_remaining: u64 = bytes.len() as u64;
let mut offset: u64 = 0;
let mut chunk_size: u64 = 1048576; // 1MB
@ -151,7 +150,7 @@ pub fn receive_transfer(
Result<Vec<u8>, TransferError>,
Vec<Result<(Address, Message), (SendError, Option<Context>)>>,
) {
let start_time: u64 = get_unix_time();
let start_time = std::time::SystemTime::now();
// get first payload then loop and receive rest
let mut file = match get_payload() {
Some(payload) => payload.bytes,
@ -175,7 +174,7 @@ pub fn receive_transfer(
let mut non_transfer_message_queue = Vec::new();
loop {
let next = receive();
if start_time + max_timeout < get_unix_time() {
if start_time.elapsed().expect("time error").as_secs() > max_timeout {
return (
Err(TransferError::TargetTimeout(file.len() as u64)),
non_transfer_message_queue,

View File

@ -12,7 +12,7 @@ use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use wasmtime::component::*;
use wasmtime::{Config, Engine, Store, WasmBacktraceDetails};
use wasmtime_wasi::preview2::{DirPerms, FilePerms, Table, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi::preview2::{Table, WasiCtx, WasiCtxBuilder, WasiView};
use crate::types as t;
use crate::FILESYSTEM_PROCESS_ID;
@ -98,47 +98,47 @@ impl WasiView for ProcessWasi {
/// intercept wasi random
///
#[async_trait::async_trait]
impl wasi::random::insecure::Host for ProcessWasi {
async fn get_insecure_random_bytes(&mut self, len: u64) -> Result<Vec<u8>> {
let mut bytes = Vec::with_capacity(len as usize);
for _ in 0..len {
bytes.push(rand::random());
}
Ok(bytes)
}
// #[async_trait::async_trait]
// impl wasi::random::insecure::Host for ProcessWasi {
// async fn get_insecure_random_bytes(&mut self, len: u64) -> Result<Vec<u8>> {
// let mut bytes = Vec::with_capacity(len as usize);
// for _ in 0..len {
// bytes.push(rand::random());
// }
// Ok(bytes)
// }
async fn get_insecure_random_u64(&mut self) -> Result<u64> {
Ok(rand::random())
}
}
// async fn get_insecure_random_u64(&mut self) -> Result<u64> {
// Ok(rand::random())
// }
// }
#[async_trait::async_trait]
impl wasi::random::insecure_seed::Host for ProcessWasi {
async fn insecure_seed(&mut self) -> Result<(u64, u64)> {
Ok((rand::random(), rand::random()))
}
}
// #[async_trait::async_trait]
// impl wasi::random::insecure_seed::Host for ProcessWasi {
// async fn insecure_seed(&mut self) -> Result<(u64, u64)> {
// Ok((rand::random(), rand::random()))
// }
// }
#[async_trait::async_trait]
impl wasi::random::random::Host for ProcessWasi {
async fn get_random_bytes(&mut self, len: u64) -> Result<Vec<u8>> {
let mut bytes = Vec::with_capacity(len as usize);
getrandom::getrandom(&mut bytes[..])?;
Ok(bytes)
}
// #[async_trait::async_trait]
// impl wasi::random::random::Host for ProcessWasi {
// async fn get_random_bytes(&mut self, len: u64) -> Result<Vec<u8>> {
// let mut bytes = Vec::with_capacity(len as usize);
// getrandom::getrandom(&mut bytes[..])?;
// Ok(bytes)
// }
async fn get_random_u64(&mut self) -> Result<u64> {
let mut bytes = Vec::with_capacity(8);
getrandom::getrandom(&mut bytes[..])?;
// async fn get_random_u64(&mut self) -> Result<u64> {
// let mut bytes = Vec::with_capacity(8);
// getrandom::getrandom(&mut bytes[..])?;
let mut number = 0u64;
for (i, &byte) in bytes.iter().enumerate() {
number |= (byte as u64) << (i * 8);
}
Ok(number)
}
}
// let mut number = 0u64;
// for (i, &byte) in bytes.iter().enumerate() {
// number |= (byte as u64) << (i * 8);
// }
// Ok(number)
// }
// }
///
/// create the process API. this is where the functions that a process can use live.
@ -160,13 +160,6 @@ impl UqProcessImports for ProcessWasi {
}
}
async fn get_unix_time(&mut self) -> Result<u64> {
match std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) {
Ok(t) => Ok(t.as_secs()),
Err(e) => Err(e.into()),
}
}
async fn get_eth_block(&mut self) -> Result<u64> {
// TODO connect to eth RPC
unimplemented!()
@ -1146,7 +1139,6 @@ async fn persist_state(
async fn make_process_loop(
booted: Arc<AtomicBool>,
keypair: Arc<signature::Ed25519KeyPair>,
home_directory_path: String,
metadata: t::ProcessMetadata,
send_to_loop: t::MessageSender,
send_to_terminal: t::PrintSender,
@ -1183,9 +1175,6 @@ async fn make_process_loop(
pre_boot_queue.push(Ok(message));
}
}
// let dir = std::env::current_dir().unwrap();
// let dir = cap_std::fs::Dir::open_ambient_dir(home_directory_path, cap_std::ambient_authority())
// .unwrap();
let component =
Component::new(&engine, wasm_bytes).expect("make_process_loop: couldn't read file");
@ -1194,30 +1183,28 @@ async fn make_process_loop(
UqProcess::add_to_linker(&mut linker, |state: &mut ProcessWasi| state).unwrap();
let mut table = Table::new();
let wasi = WasiCtxBuilder::new()
// .push_preopened_dir(dir, DirPerms::all(), FilePerms::all(), &"")
.build(&mut table)
.unwrap();
let wasi = WasiCtxBuilder::new().build(&mut table).unwrap();
// wasmtime_wasi::preview2::command::add_to_linker(&mut linker).unwrap();
wasmtime_wasi::preview2::bindings::clocks::wall_clock::add_to_linker(&mut linker, |t| t)
.unwrap();
wasmtime_wasi::preview2::bindings::clocks::monotonic_clock::add_to_linker(&mut linker, |t| t)
.unwrap();
wasmtime_wasi::preview2::bindings::clocks::timezone::add_to_linker(&mut linker, |t| t).unwrap();
wasmtime_wasi::preview2::bindings::filesystem::filesystem::add_to_linker(&mut linker, |t| t)
.unwrap();
wasmtime_wasi::preview2::bindings::poll::poll::add_to_linker(&mut linker, |t| t).unwrap();
wasmtime_wasi::preview2::bindings::io::streams::add_to_linker(&mut linker, |t| t).unwrap();
wasmtime_wasi::preview2::command::add_to_linker(&mut linker).unwrap();
// wasmtime_wasi::preview2::bindings::clocks::wall_clock::add_to_linker(&mut linker, |t| t)
// .unwrap();
// wasmtime_wasi::preview2::bindings::clocks::monotonic_clock::add_to_linker(&mut linker, |t| t)
// .unwrap();
// wasmtime_wasi::preview2::bindings::clocks::timezone::add_to_linker(&mut linker, |t| t).unwrap();
// wasmtime_wasi::preview2::bindings::filesystem::filesystem::add_to_linker(&mut linker, |t| t)
// .unwrap();
// wasmtime_wasi::preview2::bindings::poll::poll::add_to_linker(&mut linker, |t| t).unwrap();
// wasmtime_wasi::preview2::bindings::io::streams::add_to_linker(&mut linker, |t| t).unwrap();
// wasmtime_wasi::preview2::bindings::random::random::add_to_linker(&mut linker, |t| t).unwrap();
wasmtime_wasi::preview2::bindings::cli_base::exit::add_to_linker(&mut linker, |t| t).unwrap();
wasmtime_wasi::preview2::bindings::cli_base::environment::add_to_linker(&mut linker, |t| t)
.unwrap();
wasmtime_wasi::preview2::bindings::cli_base::preopens::add_to_linker(&mut linker, |t| t)
.unwrap();
wasmtime_wasi::preview2::bindings::cli_base::stdin::add_to_linker(&mut linker, |t| t).unwrap();
wasmtime_wasi::preview2::bindings::cli_base::stdout::add_to_linker(&mut linker, |t| t).unwrap();
wasmtime_wasi::preview2::bindings::cli_base::stderr::add_to_linker(&mut linker, |t| t).unwrap();
// wasmtime_wasi::preview2::bindings::cli_base::exit::add_to_linker(&mut linker, |t| t).unwrap();
// wasmtime_wasi::preview2::bindings::cli_base::environment::add_to_linker(&mut linker, |t| t)
// .unwrap();
// wasmtime_wasi::preview2::bindings::cli_base::preopens::add_to_linker(&mut linker, |t| t)
// .unwrap();
// wasmtime_wasi::preview2::bindings::cli_base::stdin::add_to_linker(&mut linker, |t| t).unwrap();
// wasmtime_wasi::preview2::bindings::cli_base::stdout::add_to_linker(&mut linker, |t| t).unwrap();
// wasmtime_wasi::preview2::bindings::cli_base::stderr::add_to_linker(&mut linker, |t| t).unwrap();
let mut store = Store::new(
engine,
ProcessWasi {
@ -1397,7 +1384,6 @@ async fn handle_kernel_request(
our_name: String,
booted: Arc<AtomicBool>,
keypair: Arc<signature::Ed25519KeyPair>,
home_directory_path: String,
km: t::KernelMessage,
send_to_loop: t::MessageSender,
send_to_terminal: t::PrintSender,
@ -1539,7 +1525,6 @@ async fn handle_kernel_request(
our_name,
booted,
keypair.clone(),
home_directory_path,
km.id,
&payload.bytes,
send_to_loop,
@ -1685,7 +1670,6 @@ async fn handle_kernel_response(
our_name: String,
booted: Arc<AtomicBool>,
keypair: Arc<signature::Ed25519KeyPair>,
home_directory_path: String,
km: t::KernelMessage,
send_to_loop: t::MessageSender,
send_to_terminal: t::PrintSender,
@ -1748,7 +1732,6 @@ async fn handle_kernel_response(
our_name,
booted,
keypair.clone(),
home_directory_path,
km.id,
&payload.bytes,
send_to_loop,
@ -1767,7 +1750,6 @@ async fn start_process(
our_name: String,
booted: Arc<AtomicBool>,
keypair: Arc<signature::Ed25519KeyPair>,
home_directory_path: String,
km_id: u64,
km_payload_bytes: &Vec<u8>,
send_to_loop: t::MessageSender,
@ -1823,7 +1805,6 @@ async fn start_process(
make_process_loop(
booted,
keypair.clone(),
home_directory_path,
metadata.clone(),
send_to_loop.clone(),
send_to_terminal.clone(),
@ -1873,7 +1854,6 @@ async fn start_process(
async fn make_event_loop(
our_name: String,
keypair: Arc<signature::Ed25519KeyPair>,
home_directory_path: String,
mut process_map: t::ProcessMap,
caps_oracle_sender: t::CapMessageSender,
mut caps_oracle_receiver: t::CapMessageReceiver,
@ -2155,7 +2135,6 @@ async fn make_event_loop(
our_name.clone(),
booted.clone(),
keypair.clone(),
home_directory_path.clone(),
kernel_message,
send_to_loop.clone(),
send_to_terminal.clone(),
@ -2171,7 +2150,6 @@ async fn make_event_loop(
our_name.clone(),
booted.clone(),
keypair.clone(),
home_directory_path.clone(),
kernel_message,
send_to_loop.clone(),
send_to_terminal.clone(),
@ -2282,7 +2260,6 @@ async fn make_event_loop(
pub async fn kernel(
our: t::Identity,
keypair: Arc<signature::Ed25519KeyPair>,
home_directory_path: String,
process_map: t::ProcessMap,
caps_oracle_sender: t::CapMessageSender,
caps_oracle_receiver: t::CapMessageReceiver,
@ -2310,7 +2287,6 @@ pub async fn kernel(
make_event_loop(
our.name,
keypair,
home_directory_path,
process_map,
caps_oracle_sender,
caps_oracle_receiver,

View File

@ -404,7 +404,6 @@ async fn main() {
tasks.spawn(kernel::kernel(
our.clone(),
networking_keypair_arc.clone(),
home_directory_path.into(),
kernel_process_map.clone(),
caps_oracle_sender.clone(),
caps_oracle_receiver,

View File

@ -1,26 +0,0 @@
// https://github.com/bytecodealliance/wasmtime/blob/432b5471ec4bf6d51173def284cd418be6849a49/crates/wasi/wit/deps/random/insecure-seed.wit
/// The insecure-seed interface for seeding hash-map DoS resistance.
///
/// It is intended to be portable at least between Unix-family platforms and
/// Windows.
interface insecure-seed {
/// Return a 128-bit value that may contain a pseudo-random value.
///
/// The returned value is not required to be computed from a CSPRNG, and may
/// even be entirely deterministic. Host implementations are encouraged to
/// provide pseudo-random values to any program exposed to
/// attacker-controlled content, to enable DoS protection built into many
/// languages' hash-map implementations.
///
/// This function is intended to only be called once, by a source language
/// to initialize Denial Of Service (DoS) protection in its hash-map
/// implementation.
///
/// # Expected future evolution
///
/// This will likely be changed to a value import, to prevent it from being
/// called multiple times and potentially used for purposes other than DoS
/// protection.
insecure-seed: func() -> tuple<u64, u64>
}

View File

@ -1,23 +0,0 @@
// https://github.com/bytecodealliance/wasmtime/blob/432b5471ec4bf6d51173def284cd418be6849a49/crates/wasi/wit/deps/random/insecure.wit
/// The insecure interface for insecure pseudo-random numbers.
///
/// It is intended to be portable at least between Unix-family platforms and
/// Windows.
interface insecure {
/// Return `len` insecure pseudo-random bytes.
///
/// This function is not cryptographically secure. Do not use it for
/// anything related to security.
///
/// There are no requirements on the values of the returned bytes, however
/// implementations are encouraged to return evenly distributed values with
/// a long period.
get-insecure-random-bytes: func(len: u64) -> list<u8>
/// Return an insecure pseudo-random `u64` value.
///
/// This function returns the same type of pseudo-random data as
/// `get-insecure-random-bytes`, represented as a `u64`.
get-insecure-random-u64: func() -> u64
}

View File

@ -1,23 +0,0 @@
// https://github.com/bytecodealliance/wasmtime/blob/432b5471ec4bf6d51173def284cd418be6849a49/crates/wasi/wit/deps/random/random.wit
package wasi:random
interface random {
/// Return `len` cryptographically-secure pseudo-random bytes.
///
/// This function must produce data from an adequately seeded
/// cryptographically-secure pseudo-random number generator (CSPRNG), so it
/// must not block, from the perspective of the calling program, and the
/// returned data is always unpredictable.
///
/// This function must always return fresh pseudo-random data. Deterministic
/// environments must omit this function, rather than implementing it with
/// deterministic data.
get-random-bytes: func(len: u64) -> list<u8>
/// Return a cryptographically-secure pseudo-random `u64` value.
///
/// This function returns the same type of pseudo-random data as
/// `get-random-bytes`, represented as a `u64`.
get-random-u64: func() -> u64
}

View File

@ -130,7 +130,8 @@ world uq-process {
// system utils:
import print-to-terminal: func(verbosity: u8, message: string)
import get-unix-time: func() -> u64
// **more will be added here with regard to blockchains**
import get-eth-block: func() -> u64
// process management:
@ -199,9 +200,4 @@ world uq-process {
import send-and-await-response:
func(target: address, request: request, payload: option<payload>) ->
result<tuple<address, message>, send-error>
// wasi
import wasi:random/insecure
import wasi:random/insecure-seed
import wasi:random/random
}