Upgrade deps, tokens

This commit is contained in:
Pascal Engélibert 2023-07-18 00:58:44 +02:00
parent 5437fe2771
commit 896ad2ac2f
15 changed files with 513 additions and 412 deletions

822
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -6,6 +6,7 @@ members = [
"runtime-interface-macro", "runtime-interface-macro",
"runtime", "runtime",
] ]
resolver = "2"
[profile.release] [profile.release]
lto = "fat" lto = "fat"

View file

@ -16,7 +16,7 @@ Idées pour un jeu comme [LeekWars](https://leekwars.com) :
### Runtime (AI) ### Runtime (AI)
cargo build --release -p runtime RUSTFLAGS="-C target-cpu=mvp -C target-feature=-sign-ext" cargo build --release -p runtime --target wasm32-unknown-unknown
### Executor ### Executor

View file

@ -5,7 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
num-traits = { version = "0.2.15", default-features = false } num-traits = { version = "0.2.15", default-features = false }
parity-scale-codec = "3.4.0" parity-scale-codec = "3.6.3"
sp-runtime-interface = { version = "16.0.0", default-features = false } sp-runtime-interface = { version = "16.0.0", default-features = false }
[features] [features]

View file

@ -3,11 +3,14 @@ use sp_runtime_interface::runtime_interface;
#[runtime_interface] #[runtime_interface]
pub trait Api { pub trait Api {
fn get_turn(sim_token: SimToken) -> Result<u32, SimError> {
unreachable!()
}
fn say(msg: &str) { fn say(msg: &str) {
unreachable!() unreachable!()
} }
fn walk( fn walk(
sim_id: SimId, sim_token: SimToken,
entity_id: EntityId, entity_id: EntityId,
direction: Direction, direction: Direction,
) -> Result<Result<Position, (Position, WalkError)>, SimError> { ) -> Result<Result<Position, (Position, WalkError)>, SimError> {

1
common/src/events.rs Normal file
View file

@ -0,0 +1 @@
pub enum Event {}

View file

@ -4,6 +4,7 @@
pub mod api; pub mod api;
pub mod board; pub mod board;
pub mod entities; pub mod entities;
pub mod events;
pub mod sim; pub mod sim;
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
@ -21,7 +22,7 @@ pub mod prelude {
pub use crate::{ pub use crate::{
board::{Board, Direction, Position}, board::{Board, Direction, Position},
entities::{ferris::Ferris, traits::Walker, Entity, WalkError}, entities::{ferris::Ferris, traits::Walker, Entity, WalkError},
sim::{EntityId, Sim, SimError, SimId}, sim::{EntityId, Sim, SimError, SimToken},
}; };
} }
@ -31,6 +32,6 @@ pub mod prelude {
api::api::*, api::api::*,
board::{Board, Direction, Position}, board::{Board, Direction, Position},
entities::WalkError, entities::WalkError,
sim::{EntityId, SimError, SimId}, sim::{EntityId, SimError, SimToken},
}; };
} }

View file

@ -6,9 +6,9 @@ use parity_scale_codec::{Decode, Encode};
use std::collections::BTreeMap; use std::collections::BTreeMap;
#[derive(Clone, Copy, Debug, Decode, Encode, Eq, Hash, Ord, PartialEq, PartialOrd)] #[derive(Clone, Copy, Debug, Decode, Encode, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct SimId(pub [u8; 16]); pub struct SimToken(pub [u8; 16]);
impl sp_runtime_interface::pass_by::PassBy for SimId { impl sp_runtime_interface::pass_by::PassBy for SimToken {
type PassBy = sp_runtime_interface::pass_by::Codec<Self>; type PassBy = sp_runtime_interface::pass_by::Codec<Self>;
} }
@ -41,6 +41,7 @@ mod sim {
pub board: board::Board, pub board: board::Board,
pub entities: BTreeMap<EntityId, entities::Entity>, pub entities: BTreeMap<EntityId, entities::Entity>,
pub entity_counter: u32, pub entity_counter: u32,
pub turn: u32,
} }
impl Sim { impl Sim {

View file

@ -4,13 +4,13 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
dashmap = "5.4.0" dashmap = "5.5.0"
cultivar-common = { path = "../common", features = ["std"] } cultivar-common = { path = "../common", features = ["std"] }
once_cell = "1.17.1" once_cell = "1.18.0"
parity-scale-codec = "3.4.0" parity-scale-codec = "3.6.3"
parking_lot = "0.12.1" parking_lot = "0.12.1"
sc-executor-common = "0.21.0" sc-executor-common = "0.22.0"
sc-executor-wasmtime = "0.21.0" sc-executor-wasmtime = "0.22.0"
sp-runtime-interface = { version = "16.0.0", default-features = false, features = ["std"] } sp-runtime-interface = { version = "16.0.0", default-features = false, features = ["std"] }
sp-wasm-interface = "13.0.0" sp-wasm-interface = "13.0.0"

View file

@ -6,16 +6,24 @@ use sp_runtime_interface::runtime_interface;
#[allow(dead_code)] #[allow(dead_code)]
#[runtime_interface] #[runtime_interface]
pub trait Api { pub trait Api {
fn get_turn(sim_token: SimToken) -> Result<u32, SimError> {
Ok(SIMS
.write()
.get_sim_from_token_mut(&sim_token)
.ok_or(SimError::SimNotFound)?
.turn)
}
fn say(msg: &str) { fn say(msg: &str) {
println!("{msg}"); println!("{msg}");
} }
fn walk( fn walk(
sim_id: SimId, sim_token: SimToken,
entity_id: EntityId, entity_id: EntityId,
direction: Direction, direction: Direction,
) -> Result<Result<Position, (Position, WalkError)>, SimError> { ) -> Result<Result<Position, (Position, WalkError)>, SimError> {
if let Some(mut sim) = SIMS.write().sims.get_mut(&sim_id) { if let Some(mut sim) = SIMS.write().get_sim_from_token_mut(&sim_token) {
let sim = sim.value_mut(); let sim = sim.value_mut();
if let Some(entity) = sim.entities.get_mut(&entity_id) { if let Some(entity) = sim.entities.get_mut(&entity_id) {
match entity { match entity {

View file

@ -14,13 +14,15 @@ fn main() {
let mut sim = Sim { let mut sim = Sim {
board: Board { board: Board {
origin: (0, 0), origin: (0, 0),
size: (4, 4), size: (16, 16),
}, },
entities: Default::default(), entities: Default::default(),
entity_counter: 0, entity_counter: 0,
turn: 0,
}; };
sim.add_entity(Entity::Ferris(Box::new(Ferris { position: (0, 0) }))); sim.add_entity(Entity::Ferris(Box::new(Ferris { position: (8, 8) })));
let sim_id = SIMS.read().new_sim(sim); let sim_id = SIMS.read().new_sim(sim);
let sim_token = SIMS.read().new_token(sim_id);
let ai_code = std::fs::read("target/wasm32-unknown-unknown/release/runtime.wasm") let ai_code = std::fs::read("target/wasm32-unknown-unknown/release/runtime.wasm")
.expect("Cannot read runtime file"); .expect("Cannot read runtime file");
@ -42,6 +44,10 @@ fn main() {
heap_alloc_strategy: sc_executor_common::wasm_runtime::HeapAllocStrategy::Dynamic { heap_alloc_strategy: sc_executor_common::wasm_runtime::HeapAllocStrategy::Dynamic {
maximum_pages: Some(32), maximum_pages: Some(32),
}, },
wasm_bulk_memory: false,
wasm_multi_value: false,
wasm_reference_types: false,
wasm_simd: false,
}, },
}, },
) )
@ -49,7 +55,11 @@ fn main() {
let mut instance = runtime.new_instance().unwrap(); let mut instance = runtime.new_instance().unwrap();
dbg!(instance.call_export("run", &sim_id.encode()).unwrap()); for turn in 0usize..64 {
instance.call_export("run", &sim_token.encode()).unwrap();
SIMS.write().sims.get_mut(&sim_id).unwrap().turn += 1;
}
dbg!(SIMS.read().sims.remove(&sim_id)); dbg!(SIMS.read().sims.remove(&sim_id));

View file

@ -4,12 +4,17 @@ use dashmap::DashMap;
pub struct Sims { pub struct Sims {
pub sims: DashMap<SimId, Sim>, pub sims: DashMap<SimId, Sim>,
pub tokens: DashMap<SimToken, SimId>,
} }
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct SimId([u8; 16]);
impl Sims { impl Sims {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
sims: DashMap::new(), sims: DashMap::new(),
tokens: DashMap::new(),
} }
} }
@ -18,4 +23,26 @@ impl Sims {
self.sims.insert(sim_id, sim); self.sims.insert(sim_id, sim);
sim_id sim_id
} }
pub fn new_token(&self, sim_id: SimId) -> SimToken {
let sim_token = SimToken([0; 16]);
self.tokens.insert(sim_token, sim_id);
sim_token
} }
pub fn get_sim_from_token<'a>(
&'a self,
sim_token: &SimToken,
) -> Option<dashmap::mapref::one::Ref<'a, SimId, Sim>> {
self.sims.get(&*self.tokens.get(sim_token)?)
}
pub fn get_sim_from_token_mut<'a>(
&'a self,
sim_token: &SimToken,
) -> Option<dashmap::mapref::one::RefMut<'a, SimId, Sim>> {
self.sims.get_mut(&*self.tokens.get(sim_token)?)
}
}
pub struct CallContext {}

View file

@ -7,5 +7,5 @@ edition = "2021"
proc-macro = true proc-macro = true
[dependencies] [dependencies]
quote = "1.0.26" quote = "1.0.31"
syn = { version = "1.0.109", features = [ "full" ] } syn = { version = "2.0.26", features = [ "full" ] }

View file

@ -14,5 +14,5 @@ crate-type = ["cdylib", "staticlib"]
#dlmalloc = { version = "0.2.4", features = ["global"] } #dlmalloc = { version = "0.2.4", features = ["global"] }
cultivar-common = { path = "../common" } cultivar-common = { path = "../common" }
#cultivar-runtime-interface = { path = "../runtime-interface" } #cultivar-runtime-interface = { path = "../runtime-interface" }
parity-scale-codec = "3.4.0" parity-scale-codec = "3.6.3"
wee_alloc = "0.4.5" wee_alloc = "0.4.5"

View file

@ -15,14 +15,17 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
pub unsafe fn run(ptr: *mut u8, len: usize) -> u64 { pub unsafe fn run(ptr: *mut u8, len: usize) -> u64 {
let data = unsafe { Vec::from_raw_parts(ptr, len, len) }; let data = unsafe { Vec::from_raw_parts(ptr, len, len) };
let sim_id = SimId::decode(&mut &data[..]).unwrap(); let sim_token = SimToken::decode(&mut &data[..]).unwrap();
run_inner(sim_id); run_inner(sim_token);
let (ptr, len, _) = data.into_raw_parts(); let (ptr, len, _) = data.into_raw_parts();
(len as u64) << 32 | ptr as u64 (len as u64) << 32 | ptr as u64
} }
fn run_inner(sim_id: SimId) { fn run_inner(sim_token: SimToken) {
say(&format!("{:?}", walk(sim_id, EntityId(0), Direction::East))); say(&format!(
"{:?}",
walk(sim_token, EntityId(0), Direction::East)
));
} }