fedimint_server/consensus/aleph_bft/
network.rsuse bitcoin::hashes::{sha256, Hash};
use fedimint_core::encoding::Encodable;
use fedimint_core::net::peers::{IPeerConnections, Recipient};
use parity_scale_codec::{Decode, Encode, IoReader};
use super::data_provider::UnitData;
use super::keychain::Keychain;
use super::Message;
use crate::net::peers::ReconnectPeerConnections;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Hasher;
impl aleph_bft::Hasher for Hasher {
type Hash = [u8; 32];
fn hash(input: &[u8]) -> Self::Hash {
input.consensus_hash::<sha256::Hash>().to_byte_array()
}
}
pub type NetworkData = aleph_bft::NetworkData<
Hasher,
UnitData,
<Keychain as aleph_bft::Keychain>::Signature,
<Keychain as aleph_bft::MultiKeychain>::PartialMultisignature,
>;
pub struct Network {
connections: ReconnectPeerConnections<Message>,
}
impl Network {
pub fn new(connections: ReconnectPeerConnections<Message>) -> Self {
Self { connections }
}
}
#[async_trait::async_trait]
impl aleph_bft::Network<NetworkData> for Network {
fn send(&self, network_data: NetworkData, recipient: aleph_bft::Recipient) {
let recipient = match recipient {
aleph_bft::Recipient::Node(node_index) => {
Recipient::Peer(super::to_peer_id(node_index))
}
aleph_bft::Recipient::Everyone => Recipient::Everyone,
};
self.connections
.try_send(recipient, Message(network_data.encode()));
}
async fn next_event(&mut self) -> Option<NetworkData> {
loop {
if let Ok(network_data) = NetworkData::decode(&mut IoReader(
self.connections.receive().await?.1 .0.as_slice(),
)) {
if network_data.included_data().iter().all(UnitData::is_valid) {
return Some(network_data);
}
}
}
}
}