-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtsmbackend.js
204 lines (175 loc) · 4.86 KB
/
tsmbackend.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
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
198
199
200
201
202
203
204
// Transaction state monitoring
var Q = require('q')
var crypto = require('crypto')
var Promise = require('bluebird')
var TxStateSet = require('blockchainjs').TxStateSet
var _ = require('lodash')
var fs = require('fs')
var makeConcurrent = require('make-concurrent')(Promise)
var wallet = null
exports.setWallet = function (_wallet) {
wallet = _wallet
}
// helper
function makeRandomId() {
return crypto.randomBytes(8).toString('hex')
}
function validateGroupId (groupId) {
if (!groupId.match(/^[01-9a-f]{16}$/))
throw new Error("groupId doesn't look good")
}
function getMGFileName(groupId) {
validateGroupId(groupId)
return 'mgs/' + groupId
}
// in-memory backend prototype
var monitoringGroups = {}
function MonitoringGroup (groupId, storedState) {
this.groupId = groupId
if (storedState) {
if (storedState.groupId !== groupId)
throw new Error("internal error: groupId")
this.tss = new TxStateSet(storedState.tssState)
this.pendingTxIds = storedState.pendingTxIds
this.pendingAddresses = storedState.pendingAddresses
this.log = storedState.log
} else {
this.tss = new TxStateSet()
this.pendingTxIds = []
this.pendingAddresses = []
this.log = []
}
}
MonitoringGroup.prototype.addTxId = function (txId) {
this.pendingTxIds.push(txId)
return this.saveState()
}
MonitoringGroup.prototype.addAddress = function (address) {
this.pendingAddresses.push(address)
return this.saveState()
}
MonitoringGroup.prototype.sync = function () {
var self = this
return this.tss.autoSync(wallet.getBlockchain(),
this.pendingAddresses,
this.pendingTxIds)
.then(function (newTSS) {
self.pendingTxIds = []
self.pendingAddresses = []
self.tss = newTSS
var changes = newTSS.getChanges()
if (changes.length > 0) self.log.push(changes)
return self.saveState()
})
}
MonitoringGroup.prototype.getLastBlock = function () {
return this.tss.latest
}
MonitoringGroup.prototype.getState = function () {
return {
tssState: this.tss.getState(),
pendingTxIds: this.pendingTxIds,
pendingAddresses: this.pendingAddresses,
log: this.log,
groupId: this.groupId
}
}
MonitoringGroup.prototype.saveState = function () {
return Q.nfcall(fs.writeFile,
getMGFileName(this.groupId),
JSON.stringify(this.getState()))
}
MonitoringGroup.loadState = function (groupId) {
return Q.nfcall(fs.readFile,
getMGFileName(groupId))
.then(function (data) {
return new MonitoringGroup(groupId, JSON.parse(data))
})
}
MonitoringGroup.prototype.getLog = function (fromPoint) {
var entries = _.flatten(this.log.slice(fromPoint).reverse())
var log = []
var txIds = {}
entries.forEach(function (entry) {
// skip old entries
if (txIds[entry.txid]) return
txIds[entry.txid] = true
var lentry = _.omit(entry, 'txid')
lentry.txId = entry.txid
log.push(lentry)
})
return log.reverse()
}
function getMonitoringGroup (groupId) {
return Q.try(function () {
if (monitoringGroups[groupId])
return monitoringGroups[groupId]
else
return MonitoringGroup.loadState(groupId)
.then(function (mg) {
monitoringGroups[groupId] = mg
return mg
}).catch(function (error) {
console.log(error.state || error)
throw new Error('groupId not found')
})
})
}
var withLock = makeConcurrent(
function (fn) { return fn() }, { concurency: 1 }
)
exports.newMonitoringGroup = function () {
var groupId = null
return Q.try(function () {
groupId = makeRandomId()
// TODO: also try loading it
if (monitoringGroups[groupId])
throw new Error('internal error: duplicate groupId')
monitoringGroups[groupId] = new MonitoringGroup(groupId)
return monitoringGroups[groupId].saveState()
}).then(function () {
return groupId
})
}
exports.addTx = function (params) {
return withLock(function() {
var groupId = params.groupId,
txId = params.txId
return getMonitoringGroup(groupId)
.then(function (mg) {
return mg.addTxId(txId)
}).then(function () {
return true
})
})
}
exports.addAddress = function (params) {
return withLock(function() {
var groupId = params.groupId,
address = params.address
return getMonitoringGroup(groupId)
.then(function (mg) {
return mg.addAddress(address)
}).then(function () {
return true
})
})
}
exports.getLog = function (params) {
return withLock(function() {
var mg
var groupId = params.groupId,
fromPoint = params.fromPoint
return getMonitoringGroup(groupId)
.then(function (_mg) {
mg = _mg
return mg.sync()
}).then(function () {
return {
lastPoint: mg.log.length,
txStates: mg.getLog(fromPoint),
lastBlock: mg.getLastBlock()
}
})
})
}