-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
159 lines (135 loc) · 4.17 KB
/
server.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
var express = require('express'),
cp = require('child_process'),
async = require('async'),
_ = require('underscore'),
fs = require('fs'),
db = require('./db')
var eh = function(fail, success) {
return function(err, res) {
if (err) {
console.log('e',err,'f',fail,'s',success);
if (fail) { fail(err); }
}
else {
success.apply(this,Array.prototype.slice.call(arguments,1));
}
};
};
var mkrespcb = function(res,code,success) {
return eh(function(e) { res.json(e,code); },success);
}
var app = express();
app.use('/bower_components', express.static('bower_components'));
app.use('/', express.static('build'));
app.use('/lib', express.static('build/lib'));
app.use('/views', express.static('build/views'));
var block = null;
// Prevent multiple pyethtool processes from locking each other up, by queuing them
var processQueue = []
var running = false;
var callProc = function(cmd, med, cb) {
processQueue.push({ cmd: cmd, med: med, cb: cb })
if (!running) runProcessQueue()
}
var runProcessQueue = function() {
running = true;
if (processQueue.length == 0) {
running = false;
return;
}
var first = processQueue.pop()
var cb = function(med, origcb) {
return function(err,res) {
if (err || !med) {
origcb(err, res)
runProcessQueue()
}
else {
med(res, function() {
origcb(err, res)
runProcessQueue()
})
}
}
}
cp.exec(first.cmd, cb(first.med, first.cb))
}
// Save latest block header to database
var saveBlock = function(b,cb) {
db.block.findOne({},eh(cb,function(dbblock) {
console.log('old',dbblock,'new',b.trim())
block = b.trim()
if (!dbblock) { db.block.insert({ data: b.trim() },cb) }
else { db.block.update({},{ data: b.trim() },cb) }
}))
}
var alert_error = function(e) {
console.log(e)
}
var initBlock = function() {
db.block.findOne({},eh(alert_error,function(dbblock) {
if (dbblock) {
console.log('existing block: '+dbblock.data)
block = dbblock.data
}
else {
console.log('making new block')
cmd = 'pyethtool mkgenesis'
callProc(cmd,saveBlock,eh(alert_error,function(r) {
console.log(r.trim())
}))
}
}))
}
setTimeout(initBlock, 1000)
app.use('/alloc',function(req,res) {
cmd = 'pyethtool alloc "'+block+'" "'+req.param('addr')+'" "'+req.param('amount')+'"'
callProc(cmd, saveBlock, mkrespcb(res,400,function(r) {
res.json(r.trim())
}))
})
app.post('/applytx',function(req,res) {
cmd = 'pyethtool applytx "'+block+'" "'+req.param('data')+'"'
var b = {}
callProc(cmd, function(r,cb) {
r = JSON.parse(r)
b.block = r.block\
b.response = r.result
saveBlock(r.block,cb)
}, mkrespcb(res,400,function() {
res.json(b)
}))
})
app.get('/account_to_dict',function(req,res) {
cmd = 'pyethtool account_to_dict "'+block+'" "'+req.param('address')+'"'
callProc(cmd, null, mkrespcb(res,400,function(r) {
res.json(JSON.parse(r))
}))
})
app.get('/pyethtool/:command',function(req,res) {
data = []
for (var q in req.query) {
data.push(req.query[q])
}
cmd = 'pyethtool "' + req.param('command') + '" "' + data.join('" "') + '"'
// anti-spam rule
if (req.param('command') == 'applytx') {
data[2] = '0'
data[3] = '10000'
}
callProc(cmd, null, mkrespcb(res,400,function(r) {
res.json(r.trim())
}))
})
app.post('/serpent/:command',function(req,res) {
console.log(req.param('data'))
var filename = '/tmp/'+Math.random()
fs.writeFile(filename,req.param('data'),mkrespcb(res,400,function() {
cmd = 'serpent "' + req.param('command') + '" "' + filename + '"'
console.log(cmd)
cp.exec(cmd,mkrespcb(res,400,function(r) {
res.json(r.trim())
}))
}))
});
app.listen(3000);