Skip to content

Commit

Permalink
Add set_checkpoint to Effect API
Browse files Browse the repository at this point in the history
  • Loading branch information
DogLooksGood committed Aug 21, 2023
1 parent bce77eb commit a22a45a
Show file tree
Hide file tree
Showing 53 changed files with 5,088 additions and 5,016 deletions.
15 changes: 15 additions & 0 deletions core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ pub struct GameContext {
pub(crate) decision_states: Vec<DecisionState>,
/// Settles, if is not None, will be handled by event loop.
pub(crate) settles: Option<Vec<Settle>>,
/// Checkpoint state
pub(crate) checkpoint: bool,
}

impl GameContext {
Expand Down Expand Up @@ -234,6 +236,7 @@ impl GameContext {
decision_states: vec![],
settles: None,
handler_state: "".into(),
checkpoint: false,
})
}

Expand Down Expand Up @@ -365,6 +368,10 @@ impl GameContext {
self.timestamp
}

pub fn is_checkpoint(&self) -> bool {
self.checkpoint
}

pub fn get_status(&self) -> GameStatus {
self.status
}
Expand Down Expand Up @@ -810,6 +817,7 @@ impl GameContext {
settles,
handler_state,
allow_exit,
checkpoint,
..
} = effect;

Expand Down Expand Up @@ -855,6 +863,7 @@ impl GameContext {

if !settles.is_empty() {
self.settle(settles);
self.checkpoint = checkpoint;
}

if let Some(state) = handler_state {
Expand Down Expand Up @@ -902,6 +911,11 @@ impl GameContext {

Ok(())
}

pub fn prepare_for_next_event(&mut self, timestamp: u64) {
self.set_timestamp(timestamp);
self.checkpoint = false;
}
}

impl Default for GameContext {
Expand All @@ -921,6 +935,7 @@ impl Default for GameContext {
random_states: Vec::new(),
decision_states: Vec::new(),
settles: None,
checkpoint: false,
}
}
}
30 changes: 18 additions & 12 deletions core/src/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use borsh::{BorshDeserialize, BorshSerialize};
use crate::{
context::GameContext,
engine::GameHandler,
error::{Error, HandleError, Result},
error::{Error, HandleError, Result, HandleResult},
random::RandomSpec,
types::{DecisionId, RandomId, Settle},
types::{DecisionId, RandomId, Settle}
};

#[derive(BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -153,6 +153,7 @@ pub struct Effect {
pub handler_state: Option<Vec<u8>>,
pub error: Option<HandleError>,
pub allow_exit: bool,
pub checkpoint: bool,
}

impl Effect {
Expand Down Expand Up @@ -198,6 +199,7 @@ impl Effect {
handler_state: Some(context.handler_state.clone()),
error: None,
allow_exit: context.allow_exit,
checkpoint: false,
}
}

Expand Down Expand Up @@ -297,6 +299,19 @@ impl Effect {
self.start_game = true;
}

pub fn set_checkpoint(&mut self) -> HandleResult<()> {
if self.settles.is_empty() {
Err(HandleError::CheckpointWithoutSettle)
} else {
self.checkpoint = true;
Ok(())
}
}

pub fn is_checkpoint(&self) -> bool {
self.checkpoint
}

/// Stop the game.
pub fn stop_game(&mut self) {
self.stop_game = true;
Expand Down Expand Up @@ -353,16 +368,6 @@ mod tests {

use super::*;

#[test]
fn test_deserialization() -> anyhow::Result<()> {
let buf = vec![0,0,0,0,0,110,167,204,141,137,1,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0];

let effect = Effect::try_from_slice(&buf)?;

println!("Effect: {:?}", effect);
Ok(())
}

#[test]
fn test_serialization() -> anyhow::Result<()> {
let mut answered = BTreeMap::new();
Expand Down Expand Up @@ -409,6 +414,7 @@ mod tests {
handler_state: Some(vec![1, 2, 3, 4]),
error: Some(HandleError::NoEnoughPlayers),
allow_exit: true,
checkpoint: false,
};
let bs = effect.try_to_vec()?;

Expand Down
6 changes: 5 additions & 1 deletion core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ pub fn general_handle_event(
) -> Result<(), Error> {
// General event handling
match event {
Event::Ready => Ok(()),
Event::Ready => {
// This is the first event, we make it a checkpoint
context.checkpoint = true;
Ok(())
}

Event::ShareSecrets { sender, shares } => {
context.add_shared_secrets(sender, shares.clone())?;
Expand Down
3 changes: 3 additions & 0 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ pub enum HandleError {

#[error("Internal error: {message:?}")]
InternalError { message: String },

#[error("Can't set checkpoint without settle")]
CheckpointWithoutSettle,
}

impl From<crate::error::Error> for HandleError {
Expand Down
2 changes: 1 addition & 1 deletion core/src/types/transactor_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub enum BroadcastFrame {
game_addr: String,
access_version: u64,
settle_version: u64,
state: Option<Vec<u8>>,
checkpoint_state: Vec<u8>,
},
Message {
game_addr: String,
Expand Down
21 changes: 15 additions & 6 deletions dev/demo-app.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,21 @@ layout {
}
}

pane {
name "Transactor"
command "just"
cwd "./"
args "facade-transactor" "1"
start_suspended true
pane split_direction="vertical" {
pane {
name "Transactor"
command "just"
cwd "./"
args "facade-transactor" "1"
start_suspended true
}
pane {
name "Transactor"
command "just"
cwd "./"
args "facade-transactor" "2"
start_suspended true
}
}
}
}
24 changes: 24 additions & 0 deletions dev/mtt-simple.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// The layout to launch facade and one transactors in Zellij

layout {
pane split_direction="vertical" {
pane {
name "Facade"
focus true
cwd "./"
command "just"
args "dev-facade" "../race-holdem/mtt/facade.json"
start_suspended true
}
pane split_direction="horizontal" {
pane {
name "Transactor"
command "just"
cwd "./"
args "facade-transactor" "1"
start_suspended true
}
pane
}
}
}
1 change: 1 addition & 0 deletions examples/draw-card/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl DrawCard {
}

effect.wait_timeout(NEXT_GAME_TIMEOUT);
effect.set_checkpoint()?;
Ok(())
}

Expand Down
Binary file modified examples/minimal/minimal.wasm
Binary file not shown.
12 changes: 6 additions & 6 deletions js/borsh/.prettierrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"semi": true,
"printWidth": 120,
"trailingComma": "es5",
"singleQuote": true,
"tabWidth": 4,
"arrowParens": "avoid"
"semi": true,
"printWidth": 120,
"trailingComma": "es5",
"singleQuote": true,
"tabWidth": 2,
"arrowParens": "avoid"
}
14 changes: 6 additions & 8 deletions js/borsh/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
export function invalidByteArrayLength(path: string[], expected: number, actual: number) {
throw new Error(`Borsh: Invalid byte array length at: ${path.join(',')}, expected: ${expected}, actual: ${actual}`);
throw new Error(`Borsh: Invalid byte array length at: ${path.join(',')}, expected: ${expected}, actual: ${actual}`);
}

export function unexpectedFieldSchema(path: string[], expected: string, actual: string) {
throw new Error(
`Borsh: Found an unexpected schema at: ${path.join(',')}, expected: ${expected}, actual: ${actual}`
);
throw new Error(`Borsh: Found an unexpected schema at: ${path.join(',')}, expected: ${expected}, actual: ${actual}`);
}

export function extendedWriterNotFound(path: string[]) {
throw new Error(`Borsh: Extended writer not found at: ${path.join(',')}`);
throw new Error(`Borsh: Extended writer not found at: ${path.join(',')}`);
}

export function extendedReaderNotFound(path: string[]) {
throw new Error(`Borsh: Extended reader not found at: ${path.join(',')}`);
throw new Error(`Borsh: Extended reader not found at: ${path.join(',')}`);
}

export function noSuperClassForVariant(cls: Function) {
throw new Error(`Borsh: No super class available for class ${cls} which is decorated as variant`);
throw new Error(`Borsh: No super class available for class ${cls} which is decorated as variant`);
}

export function invalidEnumField(path: string[]) {
throw new Error(`Borsh: Invalid enum field type at: ${path.join(',')}`);
throw new Error(`Borsh: Invalid enum field type at: ${path.join(',')}`);
}
Loading

0 comments on commit a22a45a

Please sign in to comment.