forked from zeroreserve/ZeroReserve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTmRemoteCoordinator.cpp
More file actions
107 lines (91 loc) · 3.8 KB
/
TmRemoteCoordinator.cpp
File metadata and controls
107 lines (91 loc) · 3.8 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
/*
This file is part of the Zero Reserve Plugin for Retroshare.
Zero Reserve is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zero Reserve 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Zero Reserve. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TmRemoteCoordinator.h"
#include "RSZRRemoteItems.h"
#include "Router.h"
#include "ZeroReservePlugin.h"
#include "p3ZeroReserverRS.h"
#include "Payment.h"
TmRemoteCoordinator::TmRemoteCoordinator(const ZR::VirtualAddress & addr , Payment *payment, const std::string & myId ) :
TransactionManager( addr + ':' + myId ),
m_Destination( addr ),
m_Payment( payment ),
m_myId( myId )
{
}
TmRemoteCoordinator::~TmRemoteCoordinator()
{
delete m_Payment;
}
void TmRemoteCoordinator::rollback()
{
}
ZR::RetVal TmRemoteCoordinator::init()
{
std::cerr << "Zero Reserve: Setting TX manager up as coordinator" << std::endl;
p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() );
RSZRRemoteTxInitItem * item = new RSZRRemoteTxInitItem( m_Destination, QUERY, Router::SERVER, m_Payment, m_myId );
ZR::PeerAddress addr = Router::Instance()->nextHop( m_Destination );
if( addr.empty() )
return ZR::ZR_FAILURE;
item->PeerId( addr );
p3zr->sendItem( item );
return ZR::ZR_SUCCESS;
}
ZR::RetVal TmRemoteCoordinator::processItem( RSZRRemoteTxItem * item )
{
RSZRRemoteTxItem * reply;
p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() );
switch( item->getTxPhase() )
{
case VOTE_YES:
{
std::cerr << "Zero Reserve: TX Coordinator: Received Vote: YES" << std::endl;
RSZRRemoteTxInitItem * initItem = dynamic_cast< RSZRRemoteTxInitItem * >( item );
if( !initItem )return abortTx( item );
reply = new RSZRRemoteTxItem( m_Destination, COMMIT, Router::SERVER, item->getPayerId() );
reply->PeerId( m_Payment->getCounterparty() );
ZR::ZR_Number receivedAmount = initItem->getPayment()->getAmount();
if( m_Payment->getAmount() != receivedAmount ){
if( m_Payment->getAmount() < receivedAmount){
std::cerr << "Zero Reserve: ERROR: reveived payment request higher than original" << std::endl;
return abortTx( item ); // someone attempting fraud?
}
m_Payment->setAmount( receivedAmount );
}
p3zr->sendItem( reply );
return ZR::ZR_SUCCESS;
}
case VOTE_NO:
std::cerr << "Zero Reserve: TX Coordinator: Received Vote: NO" << std::endl;
return abortTx( item );
case ACK_COMMIT:
std::cerr << "Zero Reserve: TX Coordinator: Received Acknowledgement, Committing" << std::endl;
m_Payment->commit( m_TxId );
return ZR::ZR_FINISH;
case ABORT:
return abortTx( item );
default:
throw std::runtime_error( "Unknown Transaction Phase");
}
return ZR::ZR_SUCCESS;
}
ZR::RetVal TmRemoteCoordinator::abortTx( RSZRRemoteTxItem *item )
{
p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() );
RSZRRemoteTxItem * abortItem = new RSZRRemoteTxItem( m_Destination, ABORT, Router::SERVER, item->getPayerId() );
abortItem->PeerId( m_Payment->getCounterparty() );
p3zr->sendItem( abortItem );
return ZR::ZR_FINISH;
}