This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
168 lines (155 loc) · 5.36 KB
/
main.js
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
//Set variables
var net = require('net');
var JsonSocket = require('json-socket');
request = require('request-json');
var BigNumber = require('bignumber.js');
var clients = [];
var blocks;
var clientsbal = [];
//Settings
var port = 7077;
var rai_node_ip = "127.0.0.1";
var rai_node_port = "7076";
//Create server
var server = net.createServer();
server.listen(port);
//Create connection to rai_node
var raid = request.createClient('http://'+rai_node_ip+':'+rai_node_port);
//Handle connection
var id = 0;
server.on('connection', function(socket) {
socket = new JsonSocket(socket);
var socketid = id;
id++;
clients.push(socket);
//Handle request
socket.on('message', function(r) {
//console.log(r);
// If request = getBlocksCount
if (r.requestType == "getBlocksCount") {
var data = {"action": "block_count"};
raid.post('/', data, function(err, res, body) {
socket.sendMessage({type: "BlocksCount", count: body.count});
});
}
// If request = getBalance
if (r.requestType == "getBalance") {
var data = {"action": "account_balance","account": r.address};
raid.post('/', data, function(err, res, body) {
balance = new BigNumber(body.balance).plus(body.pending);
socket.sendMessage({type: "Balance", balance: balance});
});
}
// If request = getInfo
if (r.requestType == "getInfo") {
var data = {"action": "account_info", "account": r.address, "representative": "true", "weight": "true", "pending": "true"}
raid.post('/', data, function(err, res, body) {
socket.sendMessage({type: "Info", balance: body.balance, pending: body.pending, block_count: body.block_count, representative: body.representative});
});
}
// If request = getHistory
if (r.requestType == "getHistory") {
var data = {"action": "account_history", "account": r.address, "count": r.count}
raid.post('/', data, function(err, res, body) {
socket.sendMessage({type: "History", history: body.history});
});
}
// If request = getChain
if (r.requestType == "getChain") {
//console.log(r);
var data = {"action": "account_history", "account": r.address, "count": r.count}
raid.post('/', data, function(err, res, body) {
var hashes = [];
if(body.history.length > 0){
body.history.forEach(function(val, key){
hashes.push(val.hash);
});
var data = {"action": "blocks_info", "hashes": hashes}
raid.post('/', data, function(err, res, body) {
socket.sendMessage({type: "Chain", blocks: body.blocks});
});
} else {
socket.sendMessage({type: "Chain", blocks: false});
}
});
}
// If request = getPendingBlocks
if (r.requestType == "getPendingBlocks") {
if (typeof r.threshold == 'undefined') { r.threshold = 1000000000000000000; }
if (typeof r.source == 'undefined') { r.source = true; }
var data = {"action": "accounts_pending", "accounts": r.addresses, "threshold": r.threshold, "source": r.source}
raid.post('/', data, function(err, res, body) {
socket.sendMessage({type: "PendingBlocks", blocks: body.blocks});
});
}
// If request = processBlock
if (r.requestType == "processBlock") {
console.log("processing a block");
var data = {"action": "process", "block": r.block}
console.log(data);
raid.post('/', data, function(err, res, body) {
if (typeof body.error == 'undefined') {
socket.sendMessage({type: "processResponse", status: true, hash: body.hash});
console.log("sucess");
} else {
socket.sendMessage({type: "processResponse", status: false});
console.log(body.error);
}
});
}
// If request = registerAddresses
if (r.requestType == "registerAddresses") {
if (r.addresses) {
console.log("registered");
clientsbal[socketid] = [];
updateAddresses(socket, socketid, r.addresses);
}
}
});
socket.on('error', function(){
clients.pop(socket);
if (typeof clientsbal[socketid] == 'undefined') { clientsbal[socketid] = []; }
clientsbal[socketid]['stop'] = true;
console.log("down");
});
socket.on('disconnect', function(){
clients.pop(socket);
if (typeof clientsbal[socketid] == 'undefined') { clientsbal[socketid] = []; }
clientsbal[socketid]['stop'] = true;
console.log("down");
});
});
function broadcast(message) {
clients.forEach(function (cli) {
cli.sendMessage(message);
});
}
function updateBlocks() {
var data = {"action": "block_count"};
raid.post('/', data, function(err, res, body) {
if (blocks != body.count) {
if (typeof body.count == 'undefined') { body.count = 0; }
broadcast({type: "BlocksCount", count: body.count});
blocks = body.count;
}
});
}
setInterval(updateBlocks, 250);
function updateAddresses(socket, socketid, addresses) {
if (clientsbal[socketid]['stop']) {
clientsbal[socketid] = [];
}
var data = {"action": "accounts_balances","accounts": addresses};
raid.post('/', data, function(err, res, body) {
for(let address in body['balances']){
balance = new BigNumber(body['balances'][address]['balance']).plus(body['balances'][address]['pending']);
if (clientsbal[socketid][address] != balance.toNumber()) {
clientsbal[socketid][address] = balance.toNumber();
socket.sendMessage({type: "balanceUpdate", address: address, balance:balance});
console.log("send update to address "+address);
}
}
setTimeout(function(){updateAddresses(socket, socketid, addresses);}, 250);
});
};
console.log("RaiLightServer is listening in port 7077.");