forked from ethereum/aleth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWarpPeerCapability.h
131 lines (94 loc) · 4.09 KB
/
WarpPeerCapability.h
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
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "CommonNet.h"
#include <libdevcore/Common.h>
#include <libp2p/Capability.h>
namespace dev
{
namespace eth
{
class SnapshotStorageFace;
class WarpPeerCapability;
const unsigned c_WarpProtocolVersion = 1;
enum WarpSubprotocolPacketType : byte
{
WarpStatusPacket = 0x00,
GetSnapshotManifest = 0x11,
SnapshotManifest = 0x12,
GetSnapshotData = 0x13,
SnapshotData = 0x14,
WarpSubprotocolPacketCount
};
class WarpPeerObserverFace
{
public:
virtual ~WarpPeerObserverFace() {}
virtual void onPeerStatus(std::shared_ptr<WarpPeerCapability> _peer) = 0;
virtual void onPeerManifest(std::shared_ptr<WarpPeerCapability> _peer, RLP const& _r) = 0;
virtual void onPeerBlockHeaders(std::shared_ptr<WarpPeerCapability> _peer, RLP const& _r) = 0;
virtual void onPeerData(std::shared_ptr<WarpPeerCapability> _peer, RLP const& _r) = 0;
virtual void onPeerDisconnect(std::shared_ptr<WarpPeerCapability> _peer, Asking _asking) = 0;
};
class WarpPeerCapability : public p2p::Capability
{
public:
WarpPeerCapability(std::shared_ptr<p2p::SessionFace> _s, p2p::HostCapabilityFace* _h,
unsigned _i, p2p::CapDesc const& _cap);
static std::string name() { return "par"; }
static u256 version() { return c_WarpProtocolVersion; }
static unsigned messageCount() { return WarpSubprotocolPacketCount; }
void init(unsigned _hostProtocolVersion, u256 _hostNetworkId, u256 _chainTotalDifficulty,
h256 _chainCurrentHash, h256 _chainGenesisHash,
std::shared_ptr<SnapshotStorageFace const> _snapshot,
std::weak_ptr<WarpPeerObserverFace> _observer);
/// Validates whether peer is able to communicate with the host, disables peer if not
bool validateStatus(h256 const& _genesisHash, std::vector<unsigned> const& _protocolVersions,
u256 const& _networkId);
void requestStatus(unsigned _hostProtocolVersion, u256 const& _hostNetworkId,
u256 const& _chainTotalDifficulty, h256 const& _chainCurrentHash,
h256 const& _chainGenesisHash, h256 const& _snapshotBlockHash,
u256 const& _snapshotBlockNumber);
void requestBlockHeaders(unsigned _startNumber, unsigned _count, unsigned _skip, bool _reverse);
void requestManifest();
void requestData(h256 const& _chunkHash);
/// Host runs this periodically to check up on the peer.
void tick();
u256 snapshotNumber() const { return m_snapshotNumber; }
using p2p::Capability::disable;
private:
using p2p::Capability::sealAndSend;
bool interpret(unsigned _id, RLP const& _r) override;
void onDisconnect() override;
void setAsking(Asking _a);
void setIdle() { setAsking(Asking::Nothing); }
/// Peer's protocol version.
unsigned m_protocolVersion = 0;
/// Peer's network id.
u256 m_networkId;
/// What, if anything, we last asked the other peer for.
Asking m_asking = Asking::Nothing;
/// When we asked for it. Allows a time out.
std::atomic<time_t> m_lastAsk;
/// These are determined through either a Status message.
h256 m_latestHash; ///< Peer's latest block's hash.
u256 m_totalDifficulty; ///< Peer's latest block's total difficulty.
h256 m_genesisHash; ///< Peer's genesis hash
h256 m_snapshotHash;
u256 m_snapshotNumber;
std::shared_ptr<SnapshotStorageFace const> m_snapshot;
std::weak_ptr<WarpPeerObserverFace> m_observer;
};
} // namespace eth
} // namespace dev