2024-06-10 14:25:05 +01:00
|
|
|
use bft_json_crdt::json_crdt::{BaseCrdt, SignedOp};
|
2024-06-07 17:35:38 +01:00
|
|
|
use fastcrypto::ed25519::Ed25519KeyPair;
|
2024-06-10 14:25:05 +01:00
|
|
|
use tokio::sync::mpsc;
|
2024-06-07 17:03:05 +01:00
|
|
|
|
2024-06-10 14:25:05 +01:00
|
|
|
use crate::crdt::TransactionList;
|
2024-06-07 17:03:05 +01:00
|
|
|
|
|
|
|
|
pub(crate) struct SideNode {
|
|
|
|
|
crdt: BaseCrdt<TransactionList>,
|
2024-06-07 17:35:38 +01:00
|
|
|
keys: fastcrypto::ed25519::Ed25519KeyPair,
|
2024-06-10 14:25:05 +01:00
|
|
|
incoming_receiver: mpsc::Receiver<SignedOp>,
|
2024-06-07 17:03:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SideNode {
|
2024-06-07 17:35:38 +01:00
|
|
|
pub(crate) fn new(
|
|
|
|
|
crdt: BaseCrdt<TransactionList>,
|
|
|
|
|
keys: Ed25519KeyPair,
|
2024-06-10 14:25:05 +01:00
|
|
|
incoming_receiver: mpsc::Receiver<SignedOp>,
|
2024-06-07 17:35:38 +01:00
|
|
|
) -> Self {
|
2024-06-10 14:25:05 +01:00
|
|
|
let node = Self {
|
2024-06-07 17:03:05 +01:00
|
|
|
crdt,
|
2024-06-07 17:35:38 +01:00
|
|
|
keys,
|
2024-06-10 14:25:05 +01:00
|
|
|
incoming_receiver,
|
|
|
|
|
};
|
|
|
|
|
node
|
2024-06-07 17:03:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) async fn start(&mut self) {
|
2024-06-10 14:25:05 +01:00
|
|
|
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:03:05 +01:00
|
|
|
}
|
2024-06-07 17:35:38 +01:00
|
|
|
|
2024-06-10 14:25:05 +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
|
|
|
|
|
}
|
2024-06-10 14:25:05 +01:00
|
|
|
|
|
|
|
|
/// Print the current state of the CRDT, can be used to debug
|
|
|
|
|
pub(crate) fn trace_crdt(&self) {
|
|
|
|
|
println!("{:?}", self.crdt.doc.list);
|
|
|
|
|
}
|
2024-06-07 17:03:05 +01:00
|
|
|
}
|