-
Notifications
You must be signed in to change notification settings - Fork 1
/
TangleModules.cc
351 lines (246 loc) · 10.1 KB
/
TangleModules.cc
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <string>
#include <stdio.h>
#include <omnetpp.h>
#include "Tangle.h"
#include <fstream>
#include <sstream>
using namespace omnetpp;
enum MessageType { NEXT_TX_TIMER, POW_TIMER, TIP_REQUEST, ATTACH_CONFIRM };
std::ofstream tipData;
std::ofstream blockWeightData;
std::ofstream tipAgeData;
std::stringstream tipDataStream;
std::stringstream tipAgeDataStream;
// store pointers here to limit reallocation / copying
std::vector<std::stringstream*> blockWeightDataStreams;
std::vector<t_ptrTx> tracker;
/*
* Classes for Transactors and the tangle network
* TxActor has a field for the tips it can currently see - which it will
* request from the Tangle module before attaching
*/
class TxActorModule : public cSimpleModule
{
private:
int issueCount;
TxActor self; // non omnetpp implmentation of transactor
simtime_t powTime;
protected:
virtual void initialize() override;
virtual void handleMessage( cMessage * msg ) override;
public:
std::map<int, t_ptrTx> actorTipView; // tips sent by tangle are stored by transactor till they can approve some
simtime_t tipTime; //time our tips view is from
};
Define_Module( TxActorModule );
class TangleModule : public cSimpleModule
{
private:
int txCount;
int txLimit;
Tangle tn;
protected:
virtual void initialize() override;
virtual void handleMessage( cMessage * msg ) override;
};
Define_Module( TangleModule );
void TxActorModule::initialize()
{
cMessage * timer = new cMessage( "nextTxTimer", NEXT_TX_TIMER );
scheduleAt( simTime() + par( "txGenRate" ), timer );
EV_DEBUG << "Starting next transaction procedure" << std::endl;
powTime = par( "powTime" );
}
void TxActorModule::handleMessage(cMessage * msg)
{
if( msg->isSelfMessage() ){
if( msg->getKind() == NEXT_TX_TIMER )
{ //SELF MESSAGE TO START TRANSACTION PROCEDURE AGAIN
delete msg;
//send request to tangle for tips
EV_DEBUG << "TxActor " << getId() << ": requesting tips from tangle" << std::endl;
cMessage * tipRequest = new cMessage( "tipRequest", TIP_REQUEST );
send( tipRequest, "tangleConnect$o" );
}
else
{ //POW_TIMER
EV_DEBUG << "TxActor " << getId() << ": POW completed, approving tips" << std::endl;
issueCount++;
delete msg;
EV_DEBUG << "Tips seen before starting POW: " << actorTipView.size() << std::endl;
if( strcmp( par( "tipSelectionMethod" ), "URTS" ) == 0 )
{
t_txApproved chosenTips = self.URTipSelection( actorTipView );
self.attach( actorTipView, tipTime, chosenTips);
}
else if ( strcmp( par( "tipSelectionMethod" ), "WALK" ) == 0 )
{ //WALK
t_txApproved chosenTips;
for(int i = 0; i < APPROVE_VAL; ++i)
{
//get start point for each walk
t_ptrTx walkStart = self.getWalkStart( actorTipView, par( "walkDepth" ) );
EV_DEBUG << "Backtrack TX ID: " << walkStart->TxNumber << " found Tx: " << walkStart << " with weight: " << self.ComputeWeight( walkStart, simTime() ) << std::endl;
//find a tip
chosenTips.push_back( self.EasyWalkTipSelection( walkStart, par( "walkAlphaValue" ), actorTipView, tipTime ) );
}
self.attach( actorTipView, tipTime, chosenTips );
}
else // KWALK
{
t_txApproved chosenTips = self.NKWalkTipSelection( par( "walkAlphaValue" ), actorTipView, tipTime, par( "k_Multiplier" ), par( "walkDepth" ) );
self.attach( actorTipView, tipTime, chosenTips );
}
EV_DEBUG << "Actual tips after: " << self.getTanglePtr()->giveTips().size() << std::endl;
for( auto& tipSelected : self.getMyTx().back()->m_TxApproved )
{
EV_DEBUG << "Approved Tx #" << tipSelected->TxNumber << std::endl;
}
//start a new issue timer
cMessage * timer = new cMessage( "requestTimer" );
scheduleAt( simTime() + par( "txGenRate" ), timer );
//Inform tangle of attached Tx
cMessage * attachConfirm = new cMessage( "attachConfirmed", ATTACH_CONFIRM );
//Tangle knows which tx was just attached from message context pointer
attachConfirm->setContextPointer( self.getMyTx().back() );
send( attachConfirm, "tangleConnect$o" );
//log data for new tx in .txt file
std::string data;
data.reserve(100);
//tx Number
data.append(std::to_string(self.getMyTx().back()->TxNumber));
data.push_back(',');
//tip count before
data.append( std::to_string(actorTipView.size()) );
data.push_back(',');
//tip count after
data.append( std::to_string(self.getTanglePtr()->getTipNumber()) );
data.push_back('\n');
data.shrink_to_fit();
tipDataStream << data;
if( par("recordWeights") )
{
// Track 10% of transactions
if( self.getMyTx().back()->TxNumber % 10 == 0 )
{
tracker.push_back( self.getMyTx().back() );
}
//append weights of transactions to data file to track how they change
if( self.getMyTx().back()->TxNumber % 100 == 0 )
{
if( tracker.size() != 0 )
{
std::stringstream* pCurrentBlockweightStream = new std::stringstream();
for( int i = 0; i < tracker.size(); i++ )
{
(*pCurrentBlockweightStream) << tracker[i]->TxNumber << "," << self.ComputeWeight( tracker[i], simTime() ) << std::endl;
}
blockWeightDataStreams.push_back( pCurrentBlockweightStream );
}
}
}
}
} else
{ //TIP MESSAGE FROM TANGLE
// give the transactors a pointer to the tangle they're interacting with
if( self.getTanglePtr() == nullptr )
{
self.setTanglePtr( ( Tangle *) msg->getContextPointer() );
}
//get copy of current tips
actorTipView = self.getTanglePtr()->giveTips();
tipTime = simTime();
//start timer for when POW is completed
cMessage * powTimer = new cMessage( "powTimer", POW_TIMER );
scheduleAt( simTime() + powTime, powTimer );
delete msg;
}
}
void TangleModule::initialize()
{
tracker.clear();
txCount = 0;
txLimit = par( "transactionLimit" );
Tx::tx_totalCount = 0;
std::string filename = par("tipDataFilename");
tipData.open(filename.c_str(), std::ios::app);
tipData << "TxNumber," << "Tips seen," << "Tips after" << std::endl;
std::string filename2 = par("tipAgeFilename");
tipAgeData.open(filename2.c_str(), std::ios::app);
tipAgeData << "TxNumber," << "Tip Age," << "First Approval Time," << "Attach Time," << "Direct Approvers" << std::endl;
std::string filename3 = par("blockWeightFilename");
blockWeightData.open(filename3.c_str(), std::ios::app);
blockWeightData << "TxNumber," << "Weight" << std::endl;
}
void TangleModule::handleMessage( cMessage * msg )
{
if( msg->getKind() == TIP_REQUEST )
{
try {
int arrivalGateIndex = msg->getArrivalGate()->getIndex();
EV_DEBUG << "Tip request from TxActor " << msg->getSenderModuleId() << std::endl;
EV_DEBUG << "Total tips at time " << simTime() << ": " << tn.giveTips().size() << std::endl;
txCount++;
cMessage* tipMessage = new cMessage( "tipMessage" );
tipMessage->setContextPointer( &tn ); //so transactor can access Tangle methods
send( tipMessage, "actorConnect$o", arrivalGateIndex );
delete msg;
}
catch(...)
{
}
}
else if( msg->getKind() == ATTACH_CONFIRM )
{
Tx* justAttached = ( Tx* ) msg->getContextPointer();
if( justAttached->TxNumber >= txLimit )
{
EV_DEBUG << "Transaction Limit reached, stopping simulation" << std::endl;
delete msg;
// write out data files before cleaning up
//record time from attach to first approval
double tipAge;
for(int i = 0; i < tn.allTx.size(); i++)
{
tipAge = tn.allTx[i]->firstApprovedTime.dbl() - tn.allTx[i]->timeStamp.dbl();
tipAgeDataStream << tn.allTx[i]->TxNumber << "," << tipAge << "," << tn.allTx[i]->firstApprovedTime.dbl() << "," << tn.allTx[i]->timeStamp.dbl() << "," << tn.allTx[i]->m_approvedBy.size() << std::endl;
}
// Write out the data in one go
tipAgeData << tipAgeDataStream.str();
tipData << tipDataStream.str();
if( blockWeightDataStreams.size() > 0 )
{
for(auto& pStream : blockWeightDataStreams )
{
blockWeightData << (*pStream).str();
}
}
// Clean up for next run
tipDataStream.str("");
tipDataStream.clear();
tipAgeDataStream.str("");
tipAgeDataStream.clear();
for( auto tx : tn.allTx)
{
delete tx;
}
if( blockWeightDataStreams.size() > 0 )
{
for(auto ptr : blockWeightDataStreams )
{
delete ptr;
}
}
blockWeightDataStreams.clear();
tipData.close();
blockWeightData.close();
tipAgeData.close();
endSimulation();
}
else
{
EV_DEBUG << "Total Transactions now: " << justAttached->TxNumber << std::endl;
delete msg;
}
}
}