forked from global-mesh-labs/lot49
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeshNode.hpp
executable file
·257 lines (179 loc) · 7.13 KB
/
MeshNode.hpp
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
246
247
248
249
250
251
252
253
254
255
256
257
#include "bls.hpp"
#include "ImpliedTransaction.hpp"
#include <list>
#include <memory>
#pragma once
namespace lot49
{
// Hashed GID
typedef uint16_t HGID;
// Mesh route
typedef std::list<HGID> MeshRoute;
static uint16_t COMMITTED_TOKENS = 2000;
//
// incentive headers
//
enum EChannelState
{
eSetup1,
eSetup2,
eNegotiate1,
eNegotiate2,
eNegotiate3,
eReceipt1,
eReceipt2,
eClose1,
eClose2
};
struct PeerChannel
{
HGID mFundingPeer;
HGID mProposingPeer;
uint16_t mUnspentTokens;
uint16_t mSpentTokens;
int16_t mPromisedTokens;
uint16_t mLastNonce; // used to create unique shared 2-of-2 address
EChannelState mState; // Setup1 -> Setup2 -> Negotiate1 -> Negotiate2 -> Receipt1 -> Receipt2 -> (Close1 -> Close2) or (back to Negotiate1)
std::vector<uint8_t> mRefundSignature; // bls::Signature::SIGNATURE_SIZE
std::vector<uint8_t> mPayloadHash; // bls::BLS::MESSAGE_HASH_LEN, hash of last payload - used for return receipt of payload
std::vector<uint8_t> mWitnessHash; // bls::BLS::MESSAGE_HASH_LEN, hash of last witess - used for return receipt of witness
bool mConfirmed; // setup tx confirmed
};
static const uint8_t MAXRELAYS = 5;
struct L49Header
{
friend std::ostream& operator<<(std::ostream& out, const L49Header& i);
bool mWitness; // witness verification?
EChannelState mType;
uint8_t mPrepaidTokens;
std::vector<HGID> mRelayPath; // lot49::MAXRELAYS
std::vector<uint8_t> mSignature; // bls::Signature::SIGNATURE_SIZE
// used by Witness to verify a transaction chain
std::vector<uint8_t> Serialize() const;
// reconstruct from serialized data
void FromBytes(const std::vector<uint8_t>& inData);
};
//
// Mesh Message
//
struct MeshMessage
{
friend std::ostream& operator<<(std::ostream& out, const MeshMessage& m);
HGID mSender;
HGID mReceiver;
HGID mSource;
HGID mDestination;
L49Header mIncentive;
// payload, max 236 bytes
std::vector<uint8_t> mPayloadData;
// used by Witness to verify a transaction chain
std::vector<uint8_t> Serialize() const;
// reconstruct from serialized data
void FromBytes(const std::vector<uint8_t>& inData);
};
class MeshNode
{
public:
// create mesh nodes
static void CreateNodes(const int inCount);
static MeshNode &FromIndex(const int inIndex);
// configure topology
static void AddRoute(MeshRoute inRoute);
static bool HasNeighbor(HGID inNode, HGID inNeighbor);
// Lookup a node from a Hashed GID
static MeshNode &FromHGID(const HGID &inHGID);
// Lookup a node from a public key
static MeshNode &FromPublicKey(const bls::PublicKey& inPk);
static void ClearRoutes();
static HGID GetNextHop(HGID inNode, HGID inDestination, int& outHops);
static bool GetNextHop(HGID inNode, HGID inDestination, HGID& outNextHop, int& outHops);
static void AddGateway(HGID inNode);
static void PrintTopology();
static void UpdateSimulation();
static void WriteStats(const std::string& inLabel, const MeshMessage& inMessage);
static void CloseLogs();
MeshNode();
HGID GetHGID() const;
// access private key
const bls::PrivateKey GetPrivateKey() const;
// access public key
const bls::PublicKey GetPublicKey() const;
// open a channel with neighbor node
void ProposeChannel(HGID inNeighbor);
// originate new message
bool OriginateMessage(const HGID inDestination, const std::vector<uint8_t> &inPayload);
// set/get correspondent node
void SetCorrespondentNode(const HGID inHGID);
HGID GetCorrespondentNode() const;
// is gateway node?
bool GetIsGateway() const;
bool GetNearestGateway(HGID& outGatewayNode);
bool IsWithinRange(HGID inNode2);
friend std::ostream &operator<<(std::ostream &out, const MeshNode &n);
static std::string sParametersString;
static double sGatewayPercent; // percent of nodes that are also internet gateways
static double sOriginatingPercent; // percent of nodes that originate messages
static int sMaxSize; // meters width
static double sMoveRate; // meters per minute
static int sPauseTime; // minutes of simulation
static int sPayloadSize; // bytes
static int sRadioRange; // radio communication range in meters
static int sCurrentTime; // minutes of simulation
private:
// recursively find shortest route to a node
bool FindRoute(const HGID inDestination, const int inDepth, MeshRoute& ioRoute, std::list<HGID>& ioVisited, double& ioDistance);
// return true if channel exists with this neighbor
bool HasChannel(HGID inProposer, HGID inFunder) const;
// get existing peer channel open with neighbor
PeerChannel& GetChannel(HGID inProposer, HGID inFunder);
const PeerChannel& GetChannel(HGID inProposer, HGID inFunder) const;
//
bls::Signature GetAggregateSignature(const MeshMessage& inMessage, const bool isSigning) const;
//
static std::vector<ImpliedTransaction> GetTransactions(const MeshMessage& inMessage);
//
void UpdateIncentiveHeader(MeshMessage& ioMessage);
//
bls::Signature SignTransaction(const ImpliedTransaction& inTransaction) const;
// destination node signs payload
bls::Signature SignMessage(const std::vector<uint8_t>& inPayload) const;
// transmit message
void SendTransmission(const MeshMessage& inMessage);
// receive message
void ReceiveTransmission(const MeshMessage& inMessage);
// check that aggregate signature is valid
bool VerifyMessage(const MeshMessage &inMessage) const;
// relay a message
void RelayMessage(const MeshMessage& inMessage);
// fund a channel
void FundChannel(const MeshMessage& inMessage);
// receive message
void ReceiveMessage(const MeshMessage& inMessage);
// relay delivery receipt
void RelayDeliveryReceipt(const MeshMessage& inMessage);
// verify the setup transaction for a payment channel with a witness node (via inGateway)
void ConfirmSetupTransaction(const MeshMessage& inMessage, const HGID inGateway);
void ChannelsBalances(int& outInChannels, int& outReceivedTokens, int& outOutChannels, int& outSpentTokens) const;
bool VerifySetupTransaction(const MeshMessage& inMessage);
// compute serialization of the Mesh Message for Witness verification
std::vector<uint8_t> Serialize() const;
static int sSeed;
static std::vector<MeshNode> sNodes;
// routes computed by routing protocol
static std::list<MeshRoute> sRoutes;
// state of the channel with a peer (receiving, next hop) node
std::map<std::pair<HGID,HGID>, PeerChannel> mPeerChannels;
// nearby node with setup transaction pending witness node confirmation
HGID mPendingChannelNode;
// used to create private key
std::vector<uint8_t> mSeed;
// gateway
bool mIsGateway;
// coordinates
std::pair<double, double> mCurrentPos; // meters from origin
std::pair<double, double> mWaypoint; // meters from origin
HGID mCorrespondent;
int mPausedUntil; // simulation minutes
friend std::ostream& operator<<(std::ostream& out, const MeshNode& n);
};
}; // namespace lot49