update to 1.0 of nebkor-maelstrom

This commit is contained in:
Joe Ardent 2024-06-10 12:55:25 -07:00
parent 350d24d284
commit 75a34398b3
3 changed files with 33 additions and 38 deletions

4
Cargo.lock generated
View file

@ -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",

View file

@ -4,10 +4,10 @@ resolver = "2"
[workspace.package]
version = "0.0.1"
authors = ["joe ardent"]
authors = ["joe ardent <code@ardent.nebcorp.com>"]
license-file = "LICENSE.md"
[workspace.dependencies]
serde_json = "1"
rand = "0.8"
nebkor-maelstrom = "*"
nebkor-maelstrom = "1"

View file

@ -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<TxnOp>;
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<u64>,
}
impl TxnSer for Txn {
fn serialize(&self) -> Value {
let out: Vec<Value> = self
.iter()
.map(|t| {
let v: Vec<Value> = 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<Value> = txn
.iter()
.map(|t| {
let v: Vec<Value> = vec![t.op.clone().into(), t.key.into(), t.value.into()];
v.into()
})
.collect();
out.into()
}