Files
bft-crdt-experiment/side-node/src/node.rs

66 lines
1.7 KiB
Rust
Raw Normal View History

use bft_json_crdt::json_crdt::{BaseCrdt, SignedOp};
2024-06-07 17:35:38 +01:00
use fastcrypto::ed25519::Ed25519KeyPair;
use tokio::sync::mpsc;
use crate::crdt::TransactionList;
pub(crate) struct SideNode {
crdt: BaseCrdt<TransactionList>,
2024-06-07 17:35:38 +01:00
keys: fastcrypto::ed25519::Ed25519KeyPair,
incoming_receiver: mpsc::Receiver<SignedOp>,
}
impl SideNode {
2024-06-07 17:35:38 +01:00
pub(crate) fn new(
crdt: BaseCrdt<TransactionList>,
keys: Ed25519KeyPair,
incoming_receiver: mpsc::Receiver<SignedOp>,
2024-06-07 17:35:38 +01:00
) -> Self {
let node = Self {
crdt,
2024-06-07 17:35:38 +01:00
keys,
incoming_receiver,
};
node
}
pub(crate) async fn start(&mut self) {
println!("Starting node...");
loop {
let incoming = self.incoming_receiver.recv().await.unwrap();
println!("Received incoming message: {:?}", incoming);
self.handle_incoming(incoming);
}
}
fn handle_incoming(&mut self, incoming: SignedOp) {
println!("WINNNINGINGINGINGINGIGNIGN");
self.crdt.apply(incoming.clone());
}
2024-06-07 17:35:38 +01:00
pub(crate) fn add_transaction_local(
2024-06-07 17:35:38 +01:00
&mut self,
transaction: serde_json::Value,
) -> bft_json_crdt::json_crdt::SignedOp {
let last = self
.crdt
.doc
.list
.ops
.last()
.expect("couldn't find last op");
let signed_op = self
.crdt
.doc
.list
.insert(last.id, transaction)
.sign(&self.keys);
signed_op
}
/// Print the current state of the CRDT, can be used to debug
pub(crate) fn trace_crdt(&self) {
println!("{:?}", self.crdt.doc.list);
}
}