From 75a34398b3ec271d5601e3b491962eaa46735c94 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 10 Jun 2024 12:55:25 -0700 Subject: [PATCH] update to 1.0 of nebkor-maelstrom --- Cargo.lock | 4 +-- Cargo.toml | 4 +-- gg-txn/src/main.rs | 63 +++++++++++++++++++++------------------------- 3 files changed, 33 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 51df713..264f846 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -83,9 +83,9 @@ checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "nebkor-maelstrom" -version = "0.0.2" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aae3eeab09bcb4f923122da7b5962b2f02fcbd7201baf45c679a6c6a258fdb11" +checksum = "5eb58f994271971f7d2474b883e9d60c61c876551856f467611e9c2f970dd440" dependencies = [ "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 648d9cd..8645bea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,10 +4,10 @@ resolver = "2" [workspace.package] version = "0.0.1" -authors = ["joe ardent"] +authors = ["joe ardent "] license-file = "LICENSE.md" [workspace.dependencies] serde_json = "1" rand = "0.8" -nebkor-maelstrom = "*" +nebkor-maelstrom = "1" diff --git a/gg-txn/src/main.rs b/gg-txn/src/main.rs index 402a4e3..526be9a 100644 --- a/gg-txn/src/main.rs +++ b/gg-txn/src/main.rs @@ -1,14 +1,9 @@ use std::collections::HashMap; -use nebkor_maelstrom::{mk_payload, Body, ErrorCode, Message, Node, Runner}; -use serde_json::Value; +use nebkor_maelstrom::{mk_payload, Body, ErrorCode, Message, Node, Runner, Value}; type Txn = Vec; -trait TxnSer { - fn serialize(&self) -> Value; -} - #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] struct TxnOp { op: String, @@ -16,19 +11,6 @@ struct TxnOp { value: Option, } -impl TxnSer for Txn { - fn serialize(&self) -> Value { - let out: Vec = self - .iter() - .map(|t| { - let v: Vec = vec![t.op.clone().into(), t.key.into(), t.value.into()]; - v.into() - }) - .collect(); - out.into() - } -} - fn main() { let node = TANode::default(); let runner = Runner::new(node); @@ -41,11 +23,13 @@ struct TANode { } impl TANode { - fn transact(&mut self, txn: &Txn) -> (Txn, Txn) { + fn transact(&mut self, txn: &Txn) -> Result<(Txn, Txn), ()> { let mut out = Txn::with_capacity(txn.len()); let mut yo = Txn::new(); let mut new = HashMap::new(); for txn_op in txn.iter() { + // // simulate aborted transaction to test g1a + // if rand::thread_rng().gen_bool(0.1) { return Err(()); } let op = txn_op.op.as_str(); match op { "r" => { @@ -70,11 +54,11 @@ impl TANode { // if the transaction were aborted, we'd not want to commit it by inserting from // `new` here - for (k, v) in new.into_iter() { - self.store.insert(k, v); - } + // if !abort { + self.store.extend(new); + // } - (out, yo) + Ok((out, yo)) } } @@ -85,28 +69,26 @@ impl Node for TANode { match typ { "txn" => { let txn = get_txn(&msg); + let res = self.transact(&txn); - let (txn, yo) = self.transact(&txn); // simulated aborted transaction for G1a-resistance - if txn.is_empty() { - let mut error = Body::from_type("error"); + if res.is_err() { let ec = ErrorCode::Definite(nebkor_maelstrom::protocol::DefiniteError::TxnConflict); - error.code = Some(ec); - error.text = Some("shit's fucked".into()); + let text = Some("shit's fucked"); + let error = Body::error(ec, text); runner.reply(&msg, error); return; } + let (txn, yo) = res.unwrap(); gossip(runner, &yo); - let payload = mk_payload(&[("txn", txn.serialize())]); + let payload = mk_payload(&[("txn", txn2value(&txn))]); let body = Body::from_type("txn_ok").with_payload(payload); runner.reply(&msg, body); } "yo" => { let txn = get_txn(&msg); - for op in txn.iter() { - self.store.insert(op.key, op.value.unwrap()); - } + let _ = self.transact(&txn); } _ => { eprintln!("unknown message type {typ}") @@ -116,7 +98,7 @@ impl Node for TANode { } fn gossip(runner: &Runner, goss: &Txn) { - let payload = mk_payload(&[("txn", goss.serialize())]); + let payload = mk_payload(&[("txn", txn2value(goss))]); let body = Body::from_type("yo").with_payload(payload); let slf = runner.node_id(); @@ -126,6 +108,7 @@ fn gossip(runner: &Runner, goss: &Txn) { } } +// deserialize fn get_txn(msg: &Message) -> Txn { msg.body .payload @@ -144,3 +127,15 @@ fn get_txn(msg: &Message) -> Txn { }) .collect() } + +// serialize +fn txn2value(txn: &Txn) -> Value { + let out: Vec = txn + .iter() + .map(|t| { + let v: Vec = vec![t.op.clone().into(), t.key.into(), t.value.into()]; + v.into() + }) + .collect(); + out.into() +}