-
Notifications
You must be signed in to change notification settings - Fork 4
/
PaymentChannel.cpp
executable file
·181 lines (125 loc) · 4.46 KB
/
PaymentChannel.cpp
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
/*
* PaymentChannel.cpp
*
* Created on: 06 lug 2016
* Author: giovanni
*/
#include "PaymentChannel.h"
#include "LightningNetwork.h"
#include <iostream>
#include <cassert>
#include <cmath>
#include <iomanip>
#include "defs.h"
PaymentChannel::PaymentChannel(PaymentChannelEndPoint * A,
PaymentChannelEndPoint * B, ln_units resFundsA, ln_units resFundsB) {
this->A=A;
this->B=B;
this->residualFundsA=resFundsA;
this->residualFundsB=resFundsB;
updateFees(0);
this->feeCalc=0;
this->tbfu=0;
}
double PaymentChannel::getTbfu(){
return tbfu;
}
void PaymentChannel::setTbfu(double tbfu){
this->tbfu=tbfu;
}
void PaymentChannel::updateFees(double time){
this->residualFundsAForFeeCalc = this->residualFundsA;
this->residualFundsBForFeeCalc = this->residualFundsB;
this->lastTimeFeeUpdate = time;
}
double PaymentChannel::getLastTimeFeeUpdate(){
return lastTimeFeeUpdate;
}
void PaymentChannel::PayA(ln_units P) {
#ifdef DEBUG
std::cout << "Payment channel between " << this->A->getId() << " and " << this->B->getId() << "\n";
std::cout << "Paying " << P << " to " << "A\n";
std::cout << "Old balance A: " << this->residualFundsA << " B " << this->residualFundsB << "\n";
#endif
this->residualFundsB-=P;
this->residualFundsA+=P;
#ifdef DEBUG
std::cout << "New balance A: " << this->residualFundsA << " B " << this->residualFundsB << "\n";
#endif
assert(this->residualFundsB>=0);
assert(this->residualFundsA>=0);
if (this->residualFundsA<0){
std::cerr << " A " << this->residualFundsA << " B " << this->residualFundsB << " pay " << P << " \n";
residualFundsA=0;
}
if (this->residualFundsB<0){
std::cerr << " A " << this->residualFundsA << " B " << this->residualFundsB << " pay " << P << " \n";
residualFundsB=0;
}
//std::cout << "New balance A: " << this->residualFundsA << " B " << this->residualFundsB << "\n";
}
void PaymentChannel::PayB(ln_units P) {
#ifdef DEBUG
std::cout << "Payment channel between " << this->A->getId() << " and " << this->B->getId() << "\n";
std::cout << "Paying " << P << " to " << "B\n";
std::cout << "Old balance A: " << this->residualFundsA << " B " << this->residualFundsB << "\n";
#endif
this->residualFundsA-=P;
this->residualFundsB+=P;
#ifdef DEBUG
std::cout << "New balance A: " << this->residualFundsA << " B " << this->residualFundsB << "\n";
#endif
assert(this->residualFundsB>=0);
assert(this->residualFundsA>=0);
if (this->residualFundsA<0){
std::cerr << " A " << this->residualFundsA << " B " << this->residualFundsB << " pay " << P << " \n";
residualFundsA=0;
}
if (this->residualFundsB<0){
std::cerr << " A " << this->residualFundsA << " B " << this->residualFundsB << " pay " << P << " \n";
residualFundsB=0;
}
}
void PaymentChannelGeneralFees::addSlopeA(long start, double coeff){
coefficientsA.push_back(coeff);
starting_pointsA.push_back(start);
}
void PaymentChannelGeneralFees::addSlopeB(long start, double coeff){
coefficientsB.push_back(coeff);
starting_pointsB.push_back(start);
}
PaymentChannel::~PaymentChannel(){
if (feeCalc!=0){
delete feeCalc;
}
}
void PaymentChannel::dump(){
std::cerr <<"Payment channel dump. ";
std::cerr << "End points: " << A->getId() << " " << B->getId() << "\n";
std::cerr << "ResidualFundsA " << residualFundsA << "; ResidualFundsB " << residualFundsB << "\n";
if (feeCalc!=0)
feeCalc->dump();
else
std::cerr << "FeeCalc not set.\n";
}
std::vector<long> PaymentChannel::getPoints(bool reverse){
if (!reverse)
return feeCalc->getPoints(residualFundsAForFeeCalc,residualFundsBForFeeCalc);
else
return feeCalc->getPoints(residualFundsBForFeeCalc,residualFundsAForFeeCalc);
}
std::vector<long> PaymentChannel::getSlopes(bool reverse){
if (!reverse ) {
return feeCalc->getSlopes(residualFundsAForFeeCalc,residualFundsBForFeeCalc);
} else {
return feeCalc->getSlopes(residualFundsBForFeeCalc,residualFundsAForFeeCalc);
}
}
long PaymentChannel::getBaseFee(bool reverse){
return feeCalc->getBaseFee(residualFundsA, residualFundsB);
}
ln_units PaymentChannel::resFunds(int id) {
assert(id==this->A->getId() || id==this->B->getId());
if (A->getId()==id) return this->residualFundsA;
else return residualFundsB;
}