1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use serde::{Deserialize, Serialize};

use crate::{
    message::ProtocolVersion,
    packet::PayloadLength,
    pow::{NonceTrialsPerByte, PayloadLengthExtraBytes},
};

/// A set of configurations for the Koibumi Bitmessage core.
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct Config {
    #[serde(default = "default_protocol_version")]
    protocol_version: ProtocolVersion,

    #[serde(default = "default_max_payload_length")]
    max_payload_length: PayloadLength,

    #[serde(default = "default_nonce_trials_per_byte")]
    nonce_trials_per_byte: NonceTrialsPerByte,
    #[serde(default = "default_payload_length_extra_bytes")]
    payload_length_extra_bytes: PayloadLengthExtraBytes,
}

fn default_protocol_version() -> ProtocolVersion {
    ProtocolVersion::new(3)
}

fn default_max_payload_length() -> PayloadLength {
    PayloadLength::new(1_600_100)
}

pub(crate) fn default_nonce_trials_per_byte() -> NonceTrialsPerByte {
    NonceTrialsPerByte::new(1000)
}

pub(crate) fn default_payload_length_extra_bytes() -> PayloadLengthExtraBytes {
    PayloadLengthExtraBytes::new(1000)
}

impl Default for Config {
    fn default() -> Self {
        Self {
            protocol_version: default_protocol_version(),

            max_payload_length: default_max_payload_length(),

            nonce_trials_per_byte: default_nonce_trials_per_byte(),
            payload_length_extra_bytes: default_payload_length_extra_bytes(),
        }
    }
}

impl Config {
    /// Constructs a builder for building a configuration set.
    pub fn builder() -> Builder {
        Builder::new()
    }

    /// Constructs a default configuration set.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns the protocol version.
    /// The default is `3`.
    pub fn protocol_version(&self) -> ProtocolVersion {
        self.protocol_version
    }

    /// Returns the maximum payload length which can be stored
    /// in a Bitmessage packet.
    /// The default is `1_600_100`.
    pub fn max_payload_length(&self) -> PayloadLength {
        self.max_payload_length
    }

    /// Returns the nonce trials per byte,
    /// which is one of the parameters of the proof of work protocol
    /// Bitmessage uses.
    /// The default is `1000`.
    pub fn nonce_trials_per_byte(&self) -> NonceTrialsPerByte {
        self.nonce_trials_per_byte
    }

    /// Returns the payload length extra bytes,
    /// which is one of the parameters of the proof of work protocol
    /// Bitmessage uses.
    /// The default is `1000`.
    pub fn payload_length_extra_bytes(&self) -> PayloadLengthExtraBytes {
        self.payload_length_extra_bytes
    }
}

/// A builder for building a configuration set
/// for the Koibumi Bitmessage core.
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct Builder {
    config: Config,
}

impl Builder {
    fn new() -> Self {
        Self::default()
    }

    /// Sets the protocol version.
    /// The default is `3`.
    pub fn protocol_version(&mut self, v: ProtocolVersion) -> &mut Self {
        self.config.protocol_version = v;
        self
    }

    /// Sets the maximum payload length which can be stored
    /// in a Bitmessage packet.
    /// The default is `1_600_100`.
    pub fn max_payload_length(&mut self, v: PayloadLength) -> &mut Self {
        self.config.max_payload_length = v;
        self
    }

    /// Sets the nonce trials per byte,
    /// which is one of the parameters of the proof of work protocol
    /// Bitmessage uses.
    /// The default is `1000`.
    pub fn nonce_trials_per_byte(&mut self, v: NonceTrialsPerByte) -> &mut Self {
        self.config.nonce_trials_per_byte = v;
        self
    }

    /// Sets the payload length extra bytes,
    /// which is one of the parameters of the proof of work protocol
    /// Bitmessage uses.
    /// The default is `1000`.
    pub fn payload_length_extra_bytes(&mut self, v: PayloadLengthExtraBytes) -> &mut Self {
        self.config.payload_length_extra_bytes = v;
        self
    }

    /// Returns the configuration set this builder represents.
    pub fn build(&self) -> Config {
        self.config.clone()
    }
}