| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- /* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2023 Dyne.org foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- use std::collections::{HashMap, HashSet};
- use darkfi_serial::{serialize, SerialDecodable, SerialEncodable};
- use rand::Rng;
- use crate::net;
- /// This struct represents a DHT key request
- #[derive(Debug, Clone, SerialDecodable, SerialEncodable)]
- pub struct KeyRequest {
- /// Request id
- pub id: blake3::Hash,
- /// Daemon id requesting the key
- pub from: blake3::Hash,
- /// Daemon id holding the key
- pub to: blake3::Hash,
- /// Key entry
- pub key: blake3::Hash,
- }
- impl KeyRequest {
- pub fn new(from: blake3::Hash, to: blake3::Hash, key: blake3::Hash) -> Self {
- // Generate a random id
- let mut rng = rand::thread_rng();
- let n: u16 = rng.gen();
- let id = blake3::hash(&serialize(&n));
- Self { id, from, to, key }
- }
- }
- impl net::Message for KeyRequest {
- fn name() -> &'static str {
- "keyrequest"
- }
- }
- /// This struct represents a DHT key request response
- #[derive(Debug, Clone, SerialDecodable, SerialEncodable)]
- pub struct KeyResponse {
- /// Response id
- pub id: blake3::Hash,
- /// Daemon id holding the key
- pub from: blake3::Hash,
- /// Daemon id holding the key
- pub to: blake3::Hash,
- /// Key entry
- pub key: blake3::Hash,
- /// Key value
- pub value: Vec<u8>,
- }
- impl KeyResponse {
- pub fn new(from: blake3::Hash, to: blake3::Hash, key: blake3::Hash, value: Vec<u8>) -> Self {
- // Generate a random id
- let mut rng = rand::thread_rng();
- let n: u16 = rng.gen();
- let id = blake3::hash(&serialize(&n));
- Self { id, from, to, key, value }
- }
- }
- impl net::Message for KeyResponse {
- fn name() -> &'static str {
- "keyresponse"
- }
- }
- /// This struct represents a lookup map request
- #[derive(Debug, Clone, SerialDecodable, SerialEncodable)]
- pub struct LookupRequest {
- /// Request id
- pub id: blake3::Hash,
- /// Daemon id executing the request
- pub daemon: blake3::Hash,
- /// Key entry
- pub key: blake3::Hash,
- /// Request type
- pub req_type: u8, // 0 for insert, 1 for remove
- }
- impl LookupRequest {
- pub fn new(daemon: blake3::Hash, key: blake3::Hash, req_type: u8) -> Self {
- // Generate a random id
- let mut rng = rand::thread_rng();
- let n: u16 = rng.gen();
- let id = blake3::hash(&serialize(&n));
- Self { id, daemon, key, req_type }
- }
- }
- impl net::Message for LookupRequest {
- fn name() -> &'static str {
- "lookuprequest"
- }
- }
- /// Auxiliary structure used for lookup map syncing.
- #[derive(Debug, SerialEncodable, SerialDecodable)]
- pub struct LookupMapRequest {
- /// Request id
- pub id: blake3::Hash,
- /// Daemon id executing the request
- pub daemon: blake3::Hash,
- }
- impl LookupMapRequest {
- pub fn new(daemon: blake3::Hash) -> Self {
- // Generate a random id
- let mut rng = rand::thread_rng();
- let n: u16 = rng.gen();
- let id = blake3::hash(&serialize(&n));
- Self { id, daemon }
- }
- }
- impl net::Message for LookupMapRequest {
- fn name() -> &'static str {
- "lookupmaprequest"
- }
- }
- /// Auxiliary structure used for consensus syncing.
- #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
- pub struct LookupMapResponse {
- /// Request id
- pub id: blake3::Hash,
- /// Daemon lookup map, containing nodes that holds each key
- pub lookup: HashMap<blake3::Hash, HashSet<blake3::Hash>>,
- }
- impl LookupMapResponse {
- pub fn new(lookup: HashMap<blake3::Hash, HashSet<blake3::Hash>>) -> Self {
- // Generate a random id
- let mut rng = rand::thread_rng();
- let n: u16 = rng.gen();
- let id = blake3::hash(&serialize(&n));
- Self { id, lookup }
- }
- }
- impl net::Message for LookupMapResponse {
- fn name() -> &'static str {
- "lookupmapresponse"
- }
- }
|