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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
crate::ix!();

/**
  | Default for -whitelistrelay.
  |
  */
pub const DEFAULT_WHITELISTRELAY: bool = true;

/**
  | Default for -whitelistforcerelay.
  |
  */
pub const DEFAULT_WHITELISTFORCERELAY: bool = false;

/**
  | Time after which to disconnect, after
  | waiting for a ping response (or inactivity).
  |
  */
pub const TIMEOUT_INTERVAL: Duration = Duration::seconds(20 * 60);

/**
  | Run the feeler connection loop once
  | every 2 minutes. *
  |
  */
pub const FEELER_INTERVAL: Duration = Duration::minutes(2);

/**
  | Run the extra block-relay-only connection
  | loop once every 5 minutes. *
  |
  */
pub const EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL: Duration = Duration::minutes(5);

/**
  | Maximum length of incoming protocol
  | messages (no message over 4 MB is currently
  | acceptable).
  |
  */
pub const MAX_PROTOCOL_MESSAGE_LENGTH: u32 = 4 * 1000 * 1000;

/**
  | Maximum length of the user agent string
  | in `version` message
  |
  */
pub const MAX_SUBVERSION_LENGTH: usize = 256;

/**
  | Maximum number of automatic outgoing
  | nodes over which we'll relay everything
  | (blocks, tx, addrs, etc)
  |
  */
pub const MAX_OUTBOUND_FULL_RELAY_CONNECTIONS: usize = 8;

/**
  | Maximum number of addnode outgoing
  | nodes
  |
  */
pub const MAX_ADDNODE_CONNECTIONS: usize = 8;

/**
  | Maximum number of block-relay-only
  | outgoing connections
  |
  */
pub const MAX_BLOCK_RELAY_ONLY_CONNECTIONS: usize = 2;

/**
  | Maximum number of feeler connections
  |
  */
pub const MAX_FEELER_CONNECTIONS: usize = 1;

/**
  | -listen default
  |
  */
pub const DEFAULT_LISTEN: bool = true;

/**
  | The maximum number of peer connections
  | to maintain.
  |
  */
pub const DEFAULT_MAX_PEER_CONNECTIONS: usize = 125;

/**
  | The default for -maxuploadtarget.
  | 0 = Unlimited
  |
  */
pub const DEFAULT_MAX_UPLOAD_TARGET: u64 = 0;

/**
  | Default for blocks only
  |
  */
pub const DEFAULT_BLOCKSONLY: bool = false;

/**
  | -peertimeout default
  |
  */
pub const DEFAULT_PEER_CONNECT_TIMEOUT: Duration = Duration::seconds(60);

/**
  | Number of file descriptors required
  | for message capture *
  |
  */
pub const NUM_FDS_MESSAGE_CAPTURE: i32 = 1;

pub const DEFAULT_FORCEDNSSEED:     bool = false;
pub const DEFAULT_DNSSEED:          bool = true;
pub const DEFAULT_FIXEDSEEDS:       bool = true;
pub const DEFAULT_MAXRECEIVEBUFFER: usize = 5 * 1000;
pub const DEFAULT_MAXSENDBUFFER:    usize = 1 * 1000;



//-------------------------------------------[.cpp/bitcoin/src/net.cpp]

/**
  | Maximum number of block-relay-only
  | anchor connections
  |
  */
pub const MAX_BLOCK_RELAY_ONLY_ANCHORS: usize = 2;

const_assert!{
    MAX_BLOCK_RELAY_ONLY_ANCHORS <= MAX_BLOCK_RELAY_ONLY_CONNECTIONS as usize
} //"MAX_BLOCK_RELAY_ONLY_ANCHORS must not exceed MAX_BLOCK_RELAY_ONLY_CONNECTIONS."

/**
  | Anchor IP address database file name
  |
  */
pub const ANCHORS_DATABASE_FILENAME: &'static str = "anchors.dat";

/**
  | How often to dump addresses to peers.dat
  |
  */
pub const DUMP_PEERS_INTERVAL: Duration = Duration::minutes(15);

/**
  | Number of DNS seeds to query when the
  | number of connections is low.
  |
  */
pub const DNSSEEDS_TO_QUERY_AT_ONCE: i32 = 3;

/**
  | How long to delay before querying DNS
  | seeds
  | 
  | If we have more than THRESHOLD entries
  | in addrman, then it's likely that we
  | got those addresses from having previously
  | connected to the P2P network, and that
  | we'll be able to successfully reconnect
  | to the P2P network via contacting one
  | of them. So if that's the case, spend
  | a little longer trying to connect to
  | known peers before querying the
  | 
  | DNS seeds.
  |
  */
pub const DNSSEEDS_DELAY_FEW_PEERS:      Duration = Duration::seconds(11);
pub const DNSSEEDS_DELAY_MANY_PEERS:     Duration = Duration::minutes(5);

pub const DNSSEEDS_DELAY_PEER_THRESHOLD: i32 = 1000; // "many" vs "few" peers

/**
  | The default timeframe for -maxuploadtarget.
  | 1 day.
  |
  */
pub const MAX_UPLOAD_TIMEFRAME: Duration = Duration::seconds(60 * 60 * 24);

/**
  | We add a random period time (0 to 1 seconds)
  | to feeler connections to prevent synchronization.
  |
  */
pub const FEELER_SLEEP_WINDOW: usize = 1;

/**
  | Used to pass flags to the Bind() function
  |
  */
bitflags!{
    pub struct BindFlags: u32 {
        const BF_NONE         = 0;
        const BF_EXPLICIT     = 1 << 0;
        const BF_REPORT_ERROR = 1 << 1;

        /**
         | Do not call AddLocal() for our special
         | addresses, e.g., for incoming Tor connections,
         | to prevent gossiping them over the network.
         |
         */
        const BF_DONT_ADVERTISE = 1 << 2;
    }
}

/**
  | The set of sockets cannot be modified while
  | waiting
  |
  | The sleep time needs to be small to avoid new
  | sockets stalling
  */
pub const SELECT_TIMEOUT_MILLISECONDS: i64 = 50;

pub const NET_MESSAGE_COMMAND_OTHER: &'static str = "*other*";

pub const RANDOMIZER_ID_NETGROUP:       u64 = 0x6c0edd8036ef4036; // SHA256("netgroup")[0:8]
pub const RANDOMIZER_ID_LOCALHOSTNONCE: u64 = 0xd93e69e2bbfa5735; // SHA256("localhostnonce")[0:8]
pub const RANDOMIZER_ID_ADDRCACHE:      u64 = 0x1cf2e4ddd306dda9; // SHA256("addrcache")[0:8]

/**
   Global state variables
  */

lazy_static!{
    pub static ref DISCOVER: bool = true;
    pub static ref LISTEN:   bool = true;
    pub static ref STR_SUBVERSION: String = String::default();
}

lazy_static!{
    /*
    static RecursiveMutex cs_mapLocalHost;
    static std::map<CNetAddr, LocalServiceInfo> mapLocalHost GUARDED_BY(cs_mapLocalHost);
    static bool vfLimited[NET_MAX] GUARDED_BY(cs_mapLocalHost) = {};
    */
}