forked from libbitcoin/libbitcoin-node
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprotocol_observer.cpp
More file actions
153 lines (127 loc) · 4.16 KB
/
protocol_observer.cpp
File metadata and controls
153 lines (127 loc) · 4.16 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
/**
* Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/node/protocols/protocol_observer.hpp>
#include <bitcoin/node/define.hpp>
namespace libbitcoin {
namespace node {
#define CLASS protocol_observer
using namespace network::messages::peer;
using namespace std::placeholders;
// Shared pointers required for lifetime in handler parameters.
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
// start/stop
// ----------------------------------------------------------------------------
void protocol_observer::start() NOEXCEPT
{
BC_ASSERT(stranded());
if (started())
return;
// Events subscription is asynchronous, events may be missed.
subscribe_events(BIND(handle_event, _1, _2, _3));
if (relay_disallowed_)
{
SUBSCRIBE_CHANNEL(inventory, handle_receive_inventory, _1, _2);
}
////SUBSCRIBE_CHANNEL(get_data, handle_receive_get_data, _1, _2);
protocol_peer::start();
}
void protocol_observer::stopping(const code& ec) NOEXCEPT
{
// Unsubscriber race is ok.
BC_ASSERT(stranded());
unsubscribe_events();
protocol_peer::stopping(ec);
}
// handle events (suspend)
// ----------------------------------------------------------------------------
bool protocol_observer::handle_event(const code&, chase event_,
event_value) NOEXCEPT
{
// Do not pass ec to stopped as it is not a call status.
if (stopped())
return false;
switch (event_)
{
case chase::suspend:
{
stop(error::suspended_channel);
break;
}
case chase::stop:
{
return false;
}
default:
{
break;
}
}
return true;
}
// Inbound (inv, get_data).
// ----------------------------------------------------------------------------
// These implement protocol hygiene for messages that may not be captured.
// This also allows various protocols to not have to handle all conditions.
// Not currently comprehensive but at least catches a lot of disallowed relay.
bool protocol_observer::handle_receive_inventory(const code& ec,
const inventory::cptr& message) NOEXCEPT
{
BC_ASSERT(stranded());
if (stopped(ec))
return false;
// Common with Satoshi 25.0 and 25.1.
if (relay_disallowed_ && message->any_transaction())
{
LOGR("Unrequested tx relay from [" << opposite() << "] "
<< peer_version()->user_agent);
stop(network::error::protocol_violation);
return false;
}
////// Witness types never allowed in inventory (wxtid excluded).
////if (message->any_witness())
////{
//// LOGR("Unsupported witness inventory from [" << opposite() << "].");
//// stop(network::error::protocol_violation);
//// return false;
////}
return true;
}
////bool protocol_observer::handle_receive_get_data(const code& ec,
//// const get_data::cptr& message) NOEXCEPT
////{
//// BC_ASSERT(stranded());
////
//// if (stopped(ec))
//// return false;
////
//// // Witness types only allowed in get_data if witness service advertised.
//// if (!node_witness_ && message->any_witness())
//// {
//// LOGR("Unsupported witness get_data from [" << authority42() << "].");
//// stop(network::error::protocol_violation);
//// return false;
//// }
////
//// return true;
////}
BC_POP_WARNING()
BC_POP_WARNING()
} // namespace node
} // namespace libbitcoin