forked from arquimed/CNmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_contract_v1.sol
201 lines (133 loc) · 6.52 KB
/
backup_contract_v1.sol
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
pragma solidity ^0.4.17;
import "./SafeMath.sol";
contract SuperNodeFactory {
using SafeMath for uint256;
using SafeMath for uint;
using SafeMath for int256;
using SafeMath for int;
address[] public deployedSuperNodes;
address public Foundation = this;
mapping(address => mapping(address => uint)) public outFlowsGB; // mapping of SupperNodes --> Nodes --> GB served
mapping(address => mapping(address => uint)) public inFlowsGB; //mapping of SuperNodes --> Links --> GB received
mapping(address => mapping(address => uint)) public inFlowsIncome; //mapping of Supernodes --> Nodes --> Inflow of Income received from that node
mapping(address => uint) public superNodeTotalExpense;
// mapping(uint => mapping(uint => uint)) mymap;
//This function will allow to create instances of SuperNodeManager smart contract.
function createSuperNode(string id, string area) public {
address newSuperNode = new SuperNodeManager(id, area);
deployedSuperNodes.push(newSuperNode);
}
function getDeployedSuperNodes() public view returns (address[]) {
return deployedSuperNodes;
}
function setOutFlowGB(address _from, address _to, uint _value) public {
outFlowsGB[_from][_to]= outFlowsGB[_from][_to].add(_value);
}
function setInFlowGB(address _superNodeOrigin, address _superNodeReceiver, uint _value) public {
inFlowsGB[_superNodeReceiver][_superNodeOrigin]=inFlowsGB[_superNodeReceiver][_superNodeOrigin].add(_value);
}
function addInFlowIncome(address _superNode, address _node, uint _value) public {
inFlowsIncome[_superNode][_node]=inFlowsIncome[_superNode][_node].add(_value);
}
function addSuperNodeExpense(address _superNode, uint _expense) public {
superNodeTotalExpense[_superNode]=superNodeTotalExpense[_superNode].add(_expense);
}
}
contract SuperNodeManager {
SuperNodeFactory factory;
constructor(string id, string area) public{
ownerSuperNode = msg.sender;
idSuperNode = id;
areaSuperNode = area;
}
using SafeMath for uint256;
using SafeMath for uint;
using SafeMath for int256;
using SafeMath for int;
modifier restricted() {
require(msg.sender == ownerSuperNode);
_;
}
struct Expenses {
string description;
// address recipient;
string IPFSHash;
uint cost;
bool isValidated;
}
Expenses[] public expenses;
address ownerSuperNode;
string idSuperNode;
string areaSuperNode;
address[] public connectedNodes;
address[] linksToOtherSuperNodes;
uint minimumContribution=100; //this should probably be specified on Constructor --> TO BE REVIEWED
mapping(address => bool) public contributors;
mapping(address => uint) public ammountContributed; //ammount contributed per each contributor
//mapping(address => uint) public outFlowGB; // mapping of addresses and their outFlowGB (GB served by their SuperNode in current period)
mapping(address => uint) public inFlowIncome;
//mapping(address => uint) public inFlowGB;
uint public contributorsCount;
uint public totalContributed;
uint public totalExpensesCost;
uint public totalInflowIncome;
uint public totalInflowGB;
event NewExpense(/*address indexed to,*/ uint256 value);
event NewOutFlowGB(address emiter, address receiver, uint gb);
event NewInflowIncome(address SuperNode, address sender, uint value);
event NewInflowGB(address supernode, address origin, uint gb);
event NewExpense(address supernode, uint expense);
function contribute() public payable {
require(msg.value > minimumContribution);
totalContributed += msg.value;
contributors[msg.sender] = true;
ammountContributed[msg.sender]=msg.value;
contributorsCount++;
}
function GetContributorProrata (address contributor) public view returns (uint, uint){
return (ammountContributed[contributor] , totalContributed);
}
function NodeSignUp (address newNode) external {
connectedNodes.push(newNode);
}
// We manually add GB served to node from SuperNode. In the future, this value should be retrieved dinamically.
function AddOutFlowGB (address _to, uint _outFlowGB) public {
factory.setOutFlowGB(this, _to, _outFlowGB);
emit NewOutFlowGB(this, _to, _outFlowGB);
}
// We manually add Income received from nodes in exchange of internet service. In the future, this value should be managed dinamically.
function AddInflowIncome() public payable {
require(msg.sender!=ownerSuperNode);
// totalInflowIncome = totalInflowIncome.add(msg.value);
factory.addInFlowIncome(this,msg.sender,msg.value);
emit NewInflowIncome(this, msg.sender, msg.value);
}
function AddInflowGB (address _superNodeOrigin, uint _inflowGB) public {
totalInflowGB = totalInflowGB.add(_inflowGB);
factory.setInFlowGB(this,_superNodeOrigin,_inflowGB);
// inFlowGB[_superNodeOrigin]=_inflowGB; //actualitzem mapping amb llistat de adreces origen que ens proporcionen GB
emit NewInflowGB(this, _superNodeOrigin, _inflowGB);
}
function CreateNewExpense(string description, /*address recipient, */string IPFSHash, uint cost, bool isValidated) public {
Expenses memory newExpense = Expenses({
description: description,
//recipient: recipient,
IPFSHash: IPFSHash,
cost: cost,
isValidated: isValidated
});
expenses.push(newExpense);
totalExpensesCost = totalExpensesCost.add(newExpense.cost);
emit NewExpense(/*newExpense.recipient,*/ newExpense.cost );
}
function approveExpense(uint index) public {
require(ownerSuperNode==msg.sender);
Expenses storage expense = expenses[index];
// require(approvers[msg.sender]);
//require(!request.approvals[msg.sender]);
expense.isValidated = true;
// after expense is validated, the total ammount of the expenses related to this SuperNode is updated in the Factory contract.
factory.addSuperNodeExpense(this, expense.cost);
emit NewExpense(this,expense.cost);
}
}