Skip to content

Commit 44d9d5f

Browse files
author
Richard Myers
committed
add confirmation of setup tx before relaying data
1 parent 398a4a1 commit 44d9d5f

File tree

3 files changed

+54
-44
lines changed

3 files changed

+54
-44
lines changed

MeshNode.cpp

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ std::ostream &operator<<(std::ostream &out, const MeshMessage &m)
113113
if (m.mPayloadData.empty()) {
114114
out << " Payload: []" << endl;
115115
}
116+
else if (m.mIncentive.mWitness) {
117+
MeshMessage witness_message;
118+
witness_message.FromBytes(m.mPayloadData);
119+
out << "\tPayload: Witness " << witness_message << endl;
120+
}
116121
else {
117122
std::string payload_text(reinterpret_cast<const char*>(m.mPayloadData.data()), m.mPayloadData.size());
118123
out << " Payload: [" << payload_text << "]" << endl;
@@ -334,12 +339,12 @@ void MeshNode::OriginateMessage(const HGID inDestination, const std::vector<uint
334339
theChannel.mUnspentTokens -= PREPAID_TOKENS;
335340
theChannel.mSpentTokens += PREPAID_TOKENS;
336341
theChannel.mLastNonce += 1;
337-
theChannel.mState = eNegotiate1;
342+
theChannel.mState = (theMessage.mDestination == theMessage.mReceiver) ? eNegotiate2 : eNegotiate1;
338343

339344
theMessage.mIncentive.mWitness = false;
340345
theMessage.mIncentive.mPrepaidTokens = PREPAID_TOKENS;
341346
theMessage.mIncentive.mSignature = theChannel.mRefundSignature;
342-
theMessage.mIncentive.mType = eNegotiate1;
347+
theMessage.mIncentive.mType = theChannel.mState;
343348

344349
// save a local copy of the payload hash for confirming receipt2 messages
345350
assert(theMessage.mIncentive.mType < eReceipt1 );
@@ -356,15 +361,15 @@ void MeshNode::OriginateMessage(const HGID inDestination, const std::vector<uint
356361
}
357362

358363
// relay a message
359-
bool MeshNode::RelayMessage(const MeshMessage& inMessage)
364+
void MeshNode::RelayMessage(const MeshMessage& inMessage)
360365
{
361366
_log << "Node " << GetHGID() << ", ";
362367
_log << "RelayMessage: " << inMessage << endl;
363368

364369
// confirm setup transaction on the blockchain
365370
PeerChannel &theSenderChannel = GetChannel(inMessage.mSender);
366371
if (theSenderChannel.mConfirmed == false) {
367-
/// VerifySetupTransaction(inMessage, GetNearestGateway(GetHGID()));
372+
ConfirmSetupTransaction(inMessage, GetNearestGateway(GetHGID()));
368373
}
369374

370375
// pay next hop
@@ -399,12 +404,10 @@ bool MeshNode::RelayMessage(const MeshMessage& inMessage)
399404

400405
// send message to next hop
401406
SendTransmission(outMessage);
402-
403-
return true;
404407
}
405408

406409
// fund a channel
407-
bool MeshNode::FundChannel(const MeshMessage& inMessage)
410+
void MeshNode::FundChannel(const MeshMessage& inMessage)
408411
{
409412
_log << "Node " << GetHGID() << ", ";
410413
_log << "FundChannel: " << inMessage << endl;
@@ -427,12 +430,10 @@ bool MeshNode::FundChannel(const MeshMessage& inMessage)
427430
bls::Util::Hash256(&theChannel.mPayloadHash[0], reinterpret_cast<const uint8_t*>(inMessage.mPayloadData.data()), inMessage.mPayloadData.size());
428431

429432
mPeerChannels.push_back(theChannel);
430-
431-
return true;
432433
}
433434

434435
// receive message
435-
bool MeshNode::ReceiveMessage(const MeshMessage& inMessage)
436+
void MeshNode::ReceiveMessage(const MeshMessage& inMessage)
436437
{
437438
_log << "Node " << GetHGID() << ", ";
438439
_log << "ReceiveMessage: " << inMessage << endl;
@@ -452,19 +453,28 @@ bool MeshNode::ReceiveMessage(const MeshMessage& inMessage)
452453
// message received and marked for signing witness node ?
453454
if (inMessage.mIncentive.mWitness) {
454455
// verify the setup transaction on the blockchain
455-
MeshMessage theWitnessedMessage;
456-
theWitnessedMessage.FromBytes(inMessage.mPayloadData);
457-
std::vector<ImpliedTransaction> theTransactions = GetTransactions(theWitnessedMessage);
458-
if (!VerifyMessage(theWitnessedMessage)) {
456+
MeshMessage witness_message;
457+
witness_message.FromBytes(inMessage.mPayloadData);
458+
std::vector<ImpliedTransaction> theTransactions = GetTransactions(witness_message);
459+
if (!VerifyMessage(witness_message)) {
459460
assert(0);
460-
return false;
461+
return;
461462
}
463+
464+
_log << "Node " << std::setw(4) << std::setfill('0') << inMessage.mReceiver << ": confirmed setup transaction:" << endl << "\t" << witness_message << endl;
465+
cout << "Node " << std::setw(4) << std::setfill('0') << inMessage.mReceiver << ": confirmed setup transaction:" << endl << "\t" << witness_message << endl;
466+
462467
// get public key of Setup transaction
463468
assert(theTransactions[2].GetType() == eSetup);
464469
bls::PublicKey pk = theTransactions[1].GetSigner();
465470
// TODO: check that the setup tx signer has the required balance
466471
// TODO: commit the transaction chain to the blockchain
467472
}
473+
else {
474+
std::string payload_text(reinterpret_cast<const char*>(inMessage.mPayloadData.data()), inMessage.mPayloadData.size());
475+
_log << "Node " << std::setw(4) << std::setfill('0') << inMessage.mReceiver << " received message: [" << payload_text << "] !" << endl;
476+
cout << "Node " << std::setw(4) << std::setfill('0') << inMessage.mReceiver << " received message: [" << payload_text << "] !" << endl;
477+
}
468478

469479
// send return receipt
470480
MeshMessage theMessage = inMessage;
@@ -475,20 +485,18 @@ bool MeshNode::ReceiveMessage(const MeshMessage& inMessage)
475485

476486
// send proof of receipt to previous hop
477487
SendTransmission(theMessage);
478-
479-
return true;
480488
}
481489

482490
// receive delivery receipt
483-
bool MeshNode::RelayDeliveryReceipt(const MeshMessage& inMessage)
491+
void MeshNode::RelayDeliveryReceipt(const MeshMessage& inMessage)
484492
{
485493
_log << "Node " << GetHGID() << ", ";
486494
_log << "RelayDeliveryReceipt: " << inMessage << endl;
487495

488496
// destination node confirms message hash matches
489497
if (!VerifyMessage(inMessage)) {
490498
_log << "\tVerifyMessage, failed!" << endl;
491-
return false;
499+
return;
492500
}
493501

494502
// TODO: process confirmed payment
@@ -512,19 +520,21 @@ bool MeshNode::RelayDeliveryReceipt(const MeshMessage& inMessage)
512520
// send proof of receipt to previous hop
513521
SendTransmission(theMessage);
514522
}
523+
else if (inMessage.mIncentive.mWitness) {
524+
_log << "Confirmation of channel setup received by " << std::setw(4) << std::setfill('0') << inMessage.mSource << " from Witness Node " << std::setw(4) << inMessage.mDestination << "!" << endl;
525+
cout << "Confirmation of channel setup received by " << std::setw(4) << std::setfill('0') << inMessage.mSource << " from Witness Node " << std::setw(4) << inMessage.mDestination << "!" << endl;
526+
}
515527
else {
516-
_log << "Delivery Receipt received by " << std::setw(4) << std::setfill('0') << inMessage.mSource << " from message destination " << std::setw(4) << inMessage.mDestination << "!" << endl << endl;
517-
cout << "Delivery Receipt received by " << std::setw(4) << std::setfill('0') << inMessage.mSource << " from message destination " << std::setw(4) << inMessage.mDestination << "!" << endl << endl;
528+
_log << "Delivery Receipt received by " << std::setw(4) << std::setfill('0') << inMessage.mSource << " from message destination " << std::setw(4) << inMessage.mDestination << "!" << endl;
529+
cout << "Delivery Receipt received by " << std::setw(4) << std::setfill('0') << inMessage.mSource << " from message destination " << std::setw(4) << inMessage.mDestination << "!" << endl;
518530
}
519-
520-
return true;
521531
}
522532

523-
// verify the setup transaction for a payment channel with a witness node (via inGateway)
524-
bool MeshNode::VerifySetupTransaction(const MeshMessage& inMessage, const HGID inGateway)
533+
// confirm the setup transaction for a payment channel with a witness node (via inGateway)
534+
void MeshNode::ConfirmSetupTransaction(const MeshMessage& inMessage, const HGID inGateway)
525535
{
526536
_log << "Node " << GetHGID() << ", ";
527-
_log << "VerifyChannelSetup, Gateway: " << inGateway << ", Message Hash: [" << inMessage << "]" << endl << endl;
537+
_log << "ConfirmSetupTransaction, Gateway: " << inGateway << ", Message Hash: [" << inMessage << "]" << endl << endl;
528538

529539
MeshMessage theMessage;
530540
theMessage.mSender = GetHGID();
@@ -542,12 +552,12 @@ bool MeshNode::VerifySetupTransaction(const MeshMessage& inMessage, const HGID i
542552
theChannel.mUnspentTokens -= PREPAID_TOKENS;
543553
theChannel.mSpentTokens += PREPAID_TOKENS;
544554
theChannel.mLastNonce += 1;
545-
theChannel.mState = eNegotiate1;
555+
theChannel.mState = (theMessage.mDestination == theMessage.mReceiver ? eNegotiate2 : eNegotiate1);
546556

547557
theMessage.mIncentive.mWitness = true;
548558
theMessage.mIncentive.mPrepaidTokens = PREPAID_TOKENS;
549559
theMessage.mIncentive.mSignature = theChannel.mRefundSignature;
550-
theMessage.mIncentive.mType = eNegotiate1;
560+
theMessage.mIncentive.mType = theChannel.mState;
551561

552562
// save a local copy of the payload hash for confirming receipt2 messages
553563
assert(theMessage.mIncentive.mType < eReceipt1 );
@@ -649,15 +659,15 @@ std::vector<ImpliedTransaction> MeshNode::GetTransactions(const MeshMessage& inM
649659
const MeshNode& source = MeshNode::FromHGID(inMessage.mSource);
650660
const MeshNode& sender = MeshNode::FromHGID(inMessage.mSender);
651661

652-
HGID first_relay_hgid;
653-
if (incentive.mType == eSetup1) {
654-
first_relay_hgid = inMessage.mSender;
662+
HGID first_relay_hgid = inMessage.mDestination;
663+
if (!incentive.mRelayPath.empty()) {
664+
first_relay_hgid = incentive.mRelayPath.front();
655665
}
656-
else if (incentive.mRelayPath.empty()) {
666+
if (inMessage.mSender == inMessage.mSource) {
657667
first_relay_hgid = inMessage.mReceiver;
658668
}
659-
else {
660-
first_relay_hgid = incentive.mRelayPath.front();
669+
else if (inMessage.mReceiver == inMessage.mSource) {
670+
first_relay_hgid = inMessage.mSender;
661671
}
662672
const MeshNode& first_relay = MeshNode::FromHGID(first_relay_hgid);
663673

@@ -897,7 +907,7 @@ std::vector<uint8_t> L49Header::Serialize() const
897907
std::vector<uint8_t> MeshMessage::Serialize() const
898908
{
899909
std::vector<uint8_t> incent_buf = mIncentive.Serialize();
900-
std::vector<uint8_t> buf(5 * sizeof(HGID) + incent_buf.size() + sizeof(uint32_t) + mPayloadData.size());
910+
std::vector<uint8_t> buf(5 * sizeof(HGID) + incent_buf.size() + 1 + mPayloadData.size());
901911

902912
uint32_t offset = 0;
903913
std::copy((char*) &mSender, (char*)(&mSender) + sizeof(mSender), &buf[offset]);

MeshNode.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,19 @@ class MeshNode
167167
bool VerifyMessage(const MeshMessage &inMessage);
168168

169169
// relay a message
170-
bool RelayMessage(const MeshMessage& inMessage);
170+
void RelayMessage(const MeshMessage& inMessage);
171171

172172
// fund a channel
173-
bool FundChannel(const MeshMessage& inMessage);
173+
void FundChannel(const MeshMessage& inMessage);
174174

175175
// receive message
176-
bool ReceiveMessage(const MeshMessage& inMessage);
176+
void ReceiveMessage(const MeshMessage& inMessage);
177177

178178
// relay delivery receipt
179-
bool RelayDeliveryReceipt(const MeshMessage& inMessage);
179+
void RelayDeliveryReceipt(const MeshMessage& inMessage);
180180

181181
// verify the setup transaction for a payment channel with a witness node (via inGateway)
182-
bool VerifySetupTransaction(const MeshMessage& inMessage, const HGID inGateway);
182+
void ConfirmSetupTransaction(const MeshMessage& inMessage, const HGID inGateway);
183183

184184
// compute serialization of the Mesh Message for Witness verification
185185
std::vector<uint8_t> Serialize() const;

lot49.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ bool testBLS();
1111

1212
void TestRoute(HGID inSender, HGID inReceiver, std::string& inMessage)
1313
{
14-
// send a message from reverse of route 1 [D2 to A], and receive delivery receipt
15-
cout << std::hex << endl << "Node " << std::setw(4) << std::setfill('0') << inSender << ": send a message to Node " << std::setw(4) << inReceiver <<", and receive delivery receipt" << endl;
14+
// send a message and receive delivery receipt
15+
cout << std::hex << endl << "Node " << std::setw(4) << std::setfill('0') << inSender << ": send a message to Node " << std::setw(4) << inReceiver << endl;
1616
MeshNode::FromHGID(inSender).OriginateMessage(inReceiver, std::vector<uint8_t>(inMessage.begin(), inMessage.end()));
1717
}
1818

@@ -45,9 +45,9 @@ int main(int argc, char* argv[])
4545

4646
cout << "----------------------------------------------" << endl << endl;
4747

48-
// send a message from route 0 [B to D2], and receive delivery receipt
48+
// send a message from route 0 [A to C], and receive delivery receipt
4949
std::string payload = "TEST TEST TEST";
50-
TestRoute(route1[1], route2.back(), payload);
50+
TestRoute(route1[0], route1[2], payload);
5151

5252
cout << "----------------------------------------------" << endl << endl;
5353

0 commit comments

Comments
 (0)