update to 1.0 of nebkor-maelstrom
This commit is contained in:
parent
350d24d284
commit
75a34398b3
3 changed files with 33 additions and 38 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -83,9 +83,9 @@ checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nebkor-maelstrom"
|
name = "nebkor-maelstrom"
|
||||||
version = "0.0.2"
|
version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "aae3eeab09bcb4f923122da7b5962b2f02fcbd7201baf45c679a6c6a258fdb11"
|
checksum = "5eb58f994271971f7d2474b883e9d60c61c876551856f467611e9c2f970dd440"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|
|
@ -4,10 +4,10 @@ resolver = "2"
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
authors = ["joe ardent"]
|
authors = ["joe ardent <code@ardent.nebcorp.com>"]
|
||||||
license-file = "LICENSE.md"
|
license-file = "LICENSE.md"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
nebkor-maelstrom = "*"
|
nebkor-maelstrom = "1"
|
||||||
|
|
|
@ -1,14 +1,9 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use nebkor_maelstrom::{mk_payload, Body, ErrorCode, Message, Node, Runner};
|
use nebkor_maelstrom::{mk_payload, Body, ErrorCode, Message, Node, Runner, Value};
|
||||||
use serde_json::Value;
|
|
||||||
|
|
||||||
type Txn = Vec<TxnOp>;
|
type Txn = Vec<TxnOp>;
|
||||||
|
|
||||||
trait TxnSer {
|
|
||||||
fn serialize(&self) -> Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
||||||
struct TxnOp {
|
struct TxnOp {
|
||||||
op: String,
|
op: String,
|
||||||
|
@ -16,19 +11,6 @@ struct TxnOp {
|
||||||
value: Option<u64>,
|
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() {
|
fn main() {
|
||||||
let node = TANode::default();
|
let node = TANode::default();
|
||||||
let runner = Runner::new(node);
|
let runner = Runner::new(node);
|
||||||
|
@ -41,11 +23,13 @@ struct TANode {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 out = Txn::with_capacity(txn.len());
|
||||||
let mut yo = Txn::new();
|
let mut yo = Txn::new();
|
||||||
let mut new = HashMap::new();
|
let mut new = HashMap::new();
|
||||||
for txn_op in txn.iter() {
|
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();
|
let op = txn_op.op.as_str();
|
||||||
match op {
|
match op {
|
||||||
"r" => {
|
"r" => {
|
||||||
|
@ -70,11 +54,11 @@ impl TANode {
|
||||||
|
|
||||||
// if the transaction were aborted, we'd not want to commit it by inserting from
|
// if the transaction were aborted, we'd not want to commit it by inserting from
|
||||||
// `new` here
|
// `new` here
|
||||||
for (k, v) in new.into_iter() {
|
// if !abort {
|
||||||
self.store.insert(k, v);
|
self.store.extend(new);
|
||||||
}
|
// }
|
||||||
|
|
||||||
(out, yo)
|
Ok((out, yo))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,28 +69,26 @@ impl Node for TANode {
|
||||||
match typ {
|
match typ {
|
||||||
"txn" => {
|
"txn" => {
|
||||||
let txn = get_txn(&msg);
|
let txn = get_txn(&msg);
|
||||||
|
let res = self.transact(&txn);
|
||||||
|
|
||||||
let (txn, yo) = self.transact(&txn);
|
|
||||||
// simulated aborted transaction for G1a-resistance
|
// simulated aborted transaction for G1a-resistance
|
||||||
if txn.is_empty() {
|
if res.is_err() {
|
||||||
let mut error = Body::from_type("error");
|
|
||||||
let ec =
|
let ec =
|
||||||
ErrorCode::Definite(nebkor_maelstrom::protocol::DefiniteError::TxnConflict);
|
ErrorCode::Definite(nebkor_maelstrom::protocol::DefiniteError::TxnConflict);
|
||||||
error.code = Some(ec);
|
let text = Some("shit's fucked");
|
||||||
error.text = Some("shit's fucked".into());
|
let error = Body::error(ec, text);
|
||||||
runner.reply(&msg, error);
|
runner.reply(&msg, error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let (txn, yo) = res.unwrap();
|
||||||
gossip(runner, &yo);
|
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);
|
let body = Body::from_type("txn_ok").with_payload(payload);
|
||||||
runner.reply(&msg, body);
|
runner.reply(&msg, body);
|
||||||
}
|
}
|
||||||
"yo" => {
|
"yo" => {
|
||||||
let txn = get_txn(&msg);
|
let txn = get_txn(&msg);
|
||||||
for op in txn.iter() {
|
let _ = self.transact(&txn);
|
||||||
self.store.insert(op.key, op.value.unwrap());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
eprintln!("unknown message type {typ}")
|
eprintln!("unknown message type {typ}")
|
||||||
|
@ -116,7 +98,7 @@ impl Node for TANode {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gossip(runner: &Runner, goss: &Txn) {
|
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 body = Body::from_type("yo").with_payload(payload);
|
||||||
|
|
||||||
let slf = runner.node_id();
|
let slf = runner.node_id();
|
||||||
|
@ -126,6 +108,7 @@ fn gossip(runner: &Runner, goss: &Txn) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// deserialize
|
||||||
fn get_txn(msg: &Message) -> Txn {
|
fn get_txn(msg: &Message) -> Txn {
|
||||||
msg.body
|
msg.body
|
||||||
.payload
|
.payload
|
||||||
|
@ -144,3 +127,15 @@ fn get_txn(msg: &Message) -> Txn {
|
||||||
})
|
})
|
||||||
.collect()
|
.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()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue