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

Rich state completed #3

Open
wants to merge 58 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
c149c67
Exercise1 completed. Tests has passed
RustNinja Jan 26, 2024
4f80147
Exercise2
RustNinja Jan 26, 2024
9f0914b
Exercise2 completed
RustNinja Jan 27, 2024
1d8fa64
cargo fmt
RustNinja Jan 27, 2024
8f62c8e
Merge pull request #1 from Salina-dev/Exercise3
rustlang-dev Jan 27, 2024
c19f8c8
Exercise3 completed
RustNinja Jan 28, 2024
86ce0f8
formatted
RustNinja Jan 28, 2024
06c38a8
Merge pull request #2 from Salina-dev/Laundry-machine-ex
rustlang-dev Jan 28, 2024
abb8fe8
Atm - exercise 4
RustNinja Jan 28, 2024
aa1b002
Merge pull request #3 from Salina-dev/Atm
rustlang-dev Jan 28, 2024
271ceaf
Atm ex4
RustNinja Jan 29, 2024
00e1888
Merge pull request #4 from Salina-dev/Atm-test4
rustlang-dev Jan 29, 2024
653be6f
Atm ex4
RustNinja Jan 30, 2024
307fd76
Merge pull request #5 from Salina-dev/Atm-tests5-6
rustlang-dev Jan 30, 2024
73f50ba
refactoring
RustNinja Jan 30, 2024
b263e7b
Atm ex4
RustNinja Jan 30, 2024
449ba48
Merge pull request #6 from Salina-dev/Atm-test7
rustlang-dev Jan 30, 2024
34b8240
Atm ex4 completed
RustNinja Jan 31, 2024
11e3895
Merge pull request #7 from Salina-dev/Atm-ex4-completed
rustlang-dev Jan 31, 2024
bdc2f7e
Accounted currency completed
RustNinja Feb 1, 2024
a4c59bd
Merge pull request #8 from Salina-dev/Accounted-currency-ex5
rustlang-dev Feb 1, 2024
5dbb773
Digital cash ex6
RustNinja Feb 3, 2024
cfa868b
Digital cash ex6
RustNinja Feb 3, 2024
bc635c8
Merge pull request #9 from Salina-dev/digital-cash-ex6
rustlang-dev Feb 3, 2024
8375b9b
Digital cash updated
RustNinja Feb 4, 2024
64a4b72
Merge pull request #10 from Salina-dev/digital-cash-updated
rustlang-dev Feb 4, 2024
31cd44a
first draft of tic tac toe logic
RustNinja Feb 5, 2024
d916c99
tic tac toe
RustNinja Feb 6, 2024
3144c27
Merge pull request #12 from Salina-dev/tic-tac-toe
rustlang-dev Feb 6, 2024
9d182bd
header chain comleted
RustNinja Feb 7, 2024
8b23ff5
Merge pull request #13 from Salina-dev/header-chain
rustlang-dev Feb 7, 2024
3a02f9d
extrinsic stated completed
RustNinja Feb 8, 2024
95e8e78
Merge pull request #14 from Salina-dev/extrinsic-state
rustlang-dev Feb 8, 2024
31fda4c
Consensus
RustNinja Feb 9, 2024
e4c5748
Consensus
RustNinja Feb 9, 2024
4a8e2dd
Completed consensus
RustNinja Feb 9, 2024
b34ea51
Merge pull request #15 from Salina-dev/consensus
rustlang-dev Feb 9, 2024
a2e9dec
task in progress
RustNinja Feb 10, 2024
0b1f11f
Merge pull request #16 from Salina-dev/batched-extrinsic
rustlang-dev Feb 10, 2024
987ee18
updated
RustNinja Feb 11, 2024
b4ae9ee
Merge pull request #17 from Salina-dev/batch-extrinsic-update
rustlang-dev Feb 11, 2024
b092bfa
mod 2, task 4 completed
RustNinja Feb 12, 2024
512c8ac
Merge pull request #18 from Salina-dev/batched-extrinsic-completed
rustlang-dev Feb 12, 2024
db157fe
fork choice
RustNinja Feb 13, 2024
8d22f14
Merge pull request #19 from Salina-dev/fork-choice
rustlang-dev Feb 13, 2024
553ba68
fork choice updated
RustNinja Feb 15, 2024
fdda751
Merge pull request #20 from Salina-dev/fork-choice-updated
rustlang-dev Feb 15, 2024
cf030b8
fork choice updated
RustNinja Feb 16, 2024
753b201
Merge pull request #21 from Salina-dev/fork-choice-updated1
rustlang-dev Feb 16, 2024
0c30b77
fork choice
RustNinja Feb 19, 2024
e5281c0
Merge pull request #22 from Salina-dev/fork-choice-ex9
rustlang-dev Feb 19, 2024
58767a3
fork choices ex 9
RustNinja Feb 20, 2024
51d6771
Merge pull request #23 from Salina-dev/fork-choice-updated2
rustlang-dev Feb 20, 2024
3d5bb54
fork choice completed
RustNinja Feb 20, 2024
fae6100
Merge pull request #24 from Salina-dev/fork-choice-completed
rustlang-dev Feb 20, 2024
69282e2
rich state almost completed
RustNinja Feb 21, 2024
b642e1a
Merge pull request #25 from Salina-dev/rich-state
rustlang-dev Feb 21, 2024
d8bd08d
rich state completed
RustNinja Feb 21, 2024
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
1 change: 1 addition & 0 deletions src/c1_state_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod p1_switches;
mod p2_laundry_machine;
mod p3_atm;
mod p4_accounted_currency;
//todo fix 2 tests related to serial
mod p5_digital_cash;
mod p6_open_ended;

Expand Down
26 changes: 24 additions & 2 deletions src/c1_state_machine/p1_switches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl StateMachine for LightSwitch {
type Transition = ();

fn next_state(starting_state: &bool, t: &()) -> bool {
todo!("Exercise 1")
!starting_state
}
}

Expand All @@ -31,6 +31,7 @@ pub struct TwoSwitches {
}

/// Now there are two switches so we need a proper type for the transition.
#[derive(PartialEq, Eq)]
pub enum Toggle {
FirstSwitch,
SecondSwitch,
Expand All @@ -42,7 +43,28 @@ impl StateMachine for WeirdSwitchMachine {
type Transition = Toggle;

fn next_state(starting_state: &TwoSwitches, t: &Toggle) -> TwoSwitches {
todo!("Exercise 2")
let mut state = TwoSwitches {
first_switch: starting_state.first_switch,
second_switch: starting_state.second_switch,
};

if starting_state.first_switch == false && t == &Toggle::FirstSwitch {
state.first_switch = true;
}

if starting_state.first_switch == true && t == &Toggle::FirstSwitch {
state.first_switch = false;
state.second_switch = false;
}

if starting_state.second_switch == false && t == &Toggle::SecondSwitch {
state.second_switch = true;
}

if starting_state.second_switch == true && t == &Toggle::SecondSwitch {
state.second_switch = false;
}
return state;
}
}

Expand Down
83 changes: 82 additions & 1 deletion src/c1_state_machine/p2_laundry_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum ClothesState {
}

/// Something you can do with clothes
#[derive(PartialEq, Eq)]
pub enum ClothesAction {
/// Wearing clothes decreases their life by 1 and makes them dirty.
Wear,
Expand All @@ -40,7 +41,87 @@ impl StateMachine for ClothesMachine {
type Transition = ClothesAction;

fn next_state(starting_state: &ClothesState, t: &ClothesAction) -> ClothesState {
todo!("Exercise 3")
// let mut starting_state_is_clean_4 = starting_state == &ClothesState::Clean(4);
// let mut is_action_wear = t == &ClothesAction::Wear;

// if starting_state_is_clean_4 && is_action_wear{
// return ClothesState::Dirty(3);
// }

// let mut starting_state_is_dirty_4 = starting_state == &ClothesState::Dirty(4);

// if starting_state_is_dirty_4 && is_action_wear{
// return ClothesState::Dirty(3);
// }

// let mut starting_state_is_wet_4 =

match starting_state {
&ClothesState::Clean(d) => {
if &ClothesAction::Wear == t {
if d <= 1 {
return ClothesState::Tattered;
}
return ClothesState::Dirty(d - 1);
}
if &ClothesAction::Wash == t {
if d <= 1 {
return ClothesState::Tattered;
}
return ClothesState::Wet(d - 1);
}
if &ClothesAction::Dry == t {
if d <= 1 {
return ClothesState::Tattered;
}
return ClothesState::Clean(d - 1);
}
},
&ClothesState::Dirty(d) => {
if &ClothesAction::Wear == t {
if d <= 1 {
return ClothesState::Tattered;
}
return ClothesState::Dirty(d - 1);
}
if &ClothesAction::Wash == t {
if d <= 1 {
return ClothesState::Tattered;
}
return ClothesState::Wet(d - 1);
}
if &ClothesAction::Dry == t {
if d <= 1 {
return ClothesState::Tattered;
}
return ClothesState::Dirty(d - 1);
}
},
&ClothesState::Wet(d) => {
if &ClothesAction::Wear == t {
if d <= 1 {
return ClothesState::Tattered;
}
return ClothesState::Dirty(d - 1);
}
if &ClothesAction::Wash == t {
if d <= 1 {
return ClothesState::Tattered;
}
return ClothesState::Wet(d - 1);
}
if &ClothesAction::Dry == t {
if d <= 1 {
return ClothesState::Tattered;
}
return ClothesState::Clean(d - 1);
}
},
&ClothesState::Tattered => {
return ClothesState::Tattered;
},
};
todo!()
}
}

Expand Down
96 changes: 95 additions & 1 deletion src/c1_state_machine/p3_atm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,101 @@ impl StateMachine for Atm {
type Transition = Action;

fn next_state(starting_state: &Self::State, t: &Self::Transition) -> Self::State {
todo!("Exercise 4")
match &t{
Action::SwipeCard(d)=>{
if starting_state.expected_pin_hash == Auth::Waiting{
let mut ret_state = Atm {
cash_inside: starting_state.cash_inside,
expected_pin_hash: Auth::Authenticating(1234),
keystroke_register: Vec::new(),
};
return ret_state;
}
if starting_state.expected_pin_hash == Auth::Authenticating(1234){
let mut ret_state = Atm {
cash_inside: starting_state.cash_inside,
expected_pin_hash: starting_state.expected_pin_hash.clone(),
keystroke_register: starting_state.keystroke_register.clone(),
};
return ret_state;
}
todo!()
}
Action::PressKey(d)=>{
if starting_state.expected_pin_hash == Auth::Waiting{
let mut ret_state = Atm {
cash_inside: starting_state.cash_inside,
expected_pin_hash: starting_state.expected_pin_hash.clone(),
keystroke_register: starting_state.keystroke_register.clone(),
};
// ret_state.keystroke_register.push(d.clone());
return ret_state;
}
println!("Expected pin hash: {:?}", starting_state.expected_pin_hash);
if let Auth::Authenticating(_hash) = starting_state.expected_pin_hash {

let mut ret_state = Atm {
cash_inside: starting_state.cash_inside,
expected_pin_hash: starting_state.expected_pin_hash.clone(),
keystroke_register: starting_state.keystroke_register.clone(),
};
if Key::Enter == d.clone(){
let actual_pinhash = crate::hash(&ret_state.keystroke_register);
let actual_pinhash_auth = Auth::Authenticating(actual_pinhash);
if actual_pinhash_auth != ret_state.expected_pin_hash{
ret_state.expected_pin_hash = Auth::Waiting;
}
else{
ret_state.expected_pin_hash = Auth::Authenticated;
}
ret_state.keystroke_register = vec![];
return ret_state;
}
ret_state.keystroke_register.push(d.clone());
return ret_state;
}

if starting_state.expected_pin_hash == Auth::Authenticated{

let mut ret_state = Atm {
cash_inside: starting_state.cash_inside,
expected_pin_hash: starting_state.expected_pin_hash.clone(),
keystroke_register: starting_state.keystroke_register.clone(),
};
if Key::Enter == d.clone(){
let mut users_sum:String = "".to_string();
for i in ret_state.keystroke_register.iter(){
match i {
Key::One => {users_sum.push('1')},
Key::Two => {users_sum.push('2')},
Key::Three => {users_sum.push('3')},
Key::Four => {users_sum.push('4')},
_=>{}
};
}

let provided_sum = users_sum.parse::<u64>().unwrap();
println!("users sum {}", users_sum);
println!("provided sum {}", provided_sum);
let available_cash = ret_state.cash_inside;
if provided_sum > available_cash{
ret_state.expected_pin_hash = Auth::Waiting;
ret_state.keystroke_register = vec![];
}
else{
ret_state.cash_inside = ret_state.cash_inside - provided_sum;
ret_state.expected_pin_hash = Auth::Waiting;
ret_state.keystroke_register = vec![];
}
return ret_state;
}
ret_state.keystroke_register.push(d.clone());
return ret_state;

}
todo!()
}
}
}
}

Expand Down
48 changes: 46 additions & 2 deletions src/c1_state_machine/p4_accounted_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! In this module we design a state machine that tracks the currency balances of several users.
//! Each user is associated with an account balance and users are able to send money to other users.

use super::{StateMachine, User};
use super::{StateMachine, User, p5_digital_cash::Bank};
use std::collections::HashMap;

/// This state machine models a multi-user currency system. It tracks the balance of each
Expand Down Expand Up @@ -41,7 +41,51 @@ impl StateMachine for AccountedCurrency {
type Transition = AccountingTransaction;

fn next_state(starting_state: &Balances, t: &AccountingTransaction) -> Balances {
todo!("Exercise 1")
let mut ret_state = starting_state.clone();
match t{
&AccountingTransaction::Mint{minter, amount}=>{
let mut user_balance = ret_state.get(&minter);
if let Some(b) = user_balance{
ret_state.insert(minter, b+amount);
}
else{
if amount > 0{
ret_state.insert(minter, amount);
}
}
},
&AccountingTransaction::Burn{burner, amount}=>{
let mut user_balance = ret_state.get(&burner);
if let Some(b) = user_balance{
if amount >= *b{
ret_state.remove(&burner);
}
else{
ret_state.insert(burner, b-amount);
}
}
}
&AccountingTransaction::Transfer{sender, receiver, amount}=>{
let mut sender_balance = ret_state.get(&sender);
let mut receiver_balance = ret_state.get(&receiver).unwrap_or(&0);
if let Some(b) = sender_balance{
if &amount <= b{
if sender == receiver{
return ret_state;
}
let mut new_sender_balance = b - &amount;
let mut new_receiver_balance = receiver_balance + &amount;
ret_state.insert(sender, new_sender_balance);
ret_state.insert(receiver, new_receiver_balance);
if new_sender_balance == 0{
ret_state.remove(&sender);
}
}
}
}
_=>{}
}
return ret_state;
}
}

Expand Down
Loading