-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmessage.rs
More file actions
181 lines (152 loc) · 4.87 KB
/
message.rs
File metadata and controls
181 lines (152 loc) · 4.87 KB
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
// Copyright (c) 2021-2022 RBB S.r.l
// opensource@mintlayer.org
// SPDX-License-Identifier: MIT
// Licensed under the MIT License;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use chainstate::Locator;
use common::{
chain::{
block::{signed_block_header::SignedBlockHeader, Block},
SignedTransaction, Transaction,
},
primitives::Id,
};
use serialization::{Decode, Encode};
use crate::types::peer_address::PeerAddress;
#[derive(Debug, Clone, PartialEq, Eq, strum::EnumDiscriminants)]
#[strum_discriminants(name(BlockSyncMessageTag), derive(strum::EnumIter))]
pub enum BlockSyncMessage {
HeaderListRequest(HeaderListRequest),
BlockListRequest(BlockListRequest),
HeaderList(HeaderList),
BlockResponse(BlockResponse),
// A "sentinel" message for testing purposes that allows to ensure that all block sync messages
// that were sent into a channel have been processed by the receiver.
// When the sync manager receives it, it should simply send it "back" via MessagingService.
// TODO: it would be nice to refactor tests that depend on this, so that it can be removed.
#[cfg(test)]
TestSentinel(Id<()>),
}
#[derive(Debug, Clone, PartialEq, Eq, strum::EnumDiscriminants)]
#[strum_discriminants(name(TransactionSyncMessageTag), derive(strum::EnumIter))]
pub enum TransactionSyncMessage {
NewTransaction(Id<Transaction>),
TransactionRequest(Id<Transaction>),
TransactionResponse(TransactionResponse),
}
#[derive(Debug, Clone, PartialEq, Eq, strum::EnumDiscriminants)]
#[strum_discriminants(name(PeerManagerMessageTag), derive(strum::EnumIter))]
pub enum PeerManagerMessage {
AddrListRequest(AddrListRequest),
AnnounceAddrRequest(AnnounceAddrRequest),
PingRequest(PingRequest),
AddrListResponse(AddrListResponse),
PingResponse(PingResponse),
WillDisconnect(WillDisconnectMessage),
}
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct HeaderListRequest {
locator: Locator,
}
impl HeaderListRequest {
pub fn new(locator: Locator) -> Self {
HeaderListRequest { locator }
}
pub fn locator(&self) -> &Locator {
&self.locator
}
pub fn into_locator(self) -> Locator {
self.locator
}
}
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct BlockListRequest {
block_ids: Vec<Id<Block>>,
}
impl BlockListRequest {
pub fn new(block_ids: Vec<Id<Block>>) -> Self {
Self { block_ids }
}
pub fn block_ids(&self) -> &[Id<Block>] {
&self.block_ids
}
pub fn into_block_ids(self) -> Vec<Id<Block>> {
self.block_ids
}
}
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct AddrListRequest {}
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct AnnounceAddrRequest {
pub address: PeerAddress,
}
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct PingRequest {
pub nonce: u64,
}
/// A list of block headers.
///
/// This messages is sent as a response to the the `HeaderListRequest` message or as a new block
/// announcement.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct HeaderList {
headers: Vec<SignedBlockHeader>,
}
impl HeaderList {
pub fn new(headers: Vec<SignedBlockHeader>) -> Self {
Self { headers }
}
pub fn headers(&self) -> &[SignedBlockHeader] {
&self.headers
}
pub fn into_headers(self) -> Vec<SignedBlockHeader> {
self.headers
}
}
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct BlockResponse {
block: Box<Block>,
}
impl BlockResponse {
pub fn new(block: Block) -> Self {
Self {
block: Box::new(block),
}
}
pub fn block(&self) -> &Block {
&self.block
}
pub fn into_block(self) -> Block {
*self.block
}
}
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub enum TransactionResponse {
#[codec(index = 0)]
NotFound(Id<Transaction>),
#[codec(index = 1)]
Found(SignedTransaction),
}
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct AddrListResponse {
pub addresses: Vec<PeerAddress>,
}
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct PingResponse {
pub nonce: u64,
}
// Note: 'reason' is a string here, because we want to be able to add more reasons without upping
// the protocol version.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct WillDisconnectMessage {
pub reason: String,
}