-
Notifications
You must be signed in to change notification settings - Fork 13
/
process.go
278 lines (234 loc) · 6.58 KB
/
process.go
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2014-2017 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package peer
import (
"encoding/binary"
"github.com/bitmark-inc/bitmarkd/announce"
"github.com/bitmark-inc/bitmarkd/asset"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/messagebus"
"github.com/bitmark-inc/bitmarkd/mode"
"github.com/bitmark-inc/bitmarkd/pay"
"github.com/bitmark-inc/bitmarkd/payment"
"github.com/bitmark-inc/bitmarkd/reservoir"
"github.com/bitmark-inc/bitmarkd/transactionrecord"
"github.com/bitmark-inc/bitmarkd/util"
"github.com/bitmark-inc/logger"
)
// process received data
func processSubscription(log *logger.L, command string, arguments [][]byte) {
dataLength := len(arguments)
switch string(command) {
case "block":
if dataLength < 1 {
log.Warnf("block with too few data: %d items", dataLength)
return
}
log.Infof("received block: %x", arguments[0])
if !mode.Is(mode.Normal) {
err := fault.ErrNotAvailableDuringSynchronise
log.Warnf("failed assets: error: %v", err)
} else {
messagebus.Bus.Blockstore.Send("remote", arguments[0])
}
case "assets":
if dataLength < 1 {
log.Warnf("assets with too few data: %d items", dataLength)
return
}
log.Infof("received assets: %x", arguments[0])
err := processAssets(arguments[0])
if nil != err {
log.Warnf("failed assets: error: %v", err)
} else {
messagebus.Bus.Broadcast.Send("assets", arguments[0])
}
case "issues":
if dataLength < 2 {
log.Warnf("issues with too few data: %d items", dataLength)
return
}
log.Infof("received issues: %x, verified: %x", arguments[0], arguments[1])
err := processIssues(arguments[0], arguments[1])
if nil != err {
log.Warnf("failed issues: error: %v", err)
} else {
messagebus.Bus.Broadcast.Send("issues", arguments[0:2]...)
}
case "transfer":
if dataLength < 1 {
log.Warnf("transfer with too few data: %d items", dataLength)
return
}
log.Infof("received transfer: %x", arguments[0])
err := processTransfer(arguments[0])
if nil != err {
log.Warnf("failed transfer: error: %v", err)
} else {
messagebus.Bus.Broadcast.Send("transfer", arguments[0])
}
case "proof":
if dataLength < 1 {
log.Warnf("proof with too few data: %d items", dataLength)
return
}
log.Infof("received proof: %x", arguments[0])
err := processProof(arguments[0])
if nil != err {
log.Warnf("failed proof: error: %v", err)
} else {
messagebus.Bus.Broadcast.Send("proof", arguments[0])
}
case "rpc":
if dataLength < 3 {
log.Warnf("rpc with too few data: %d items", dataLength)
return
}
timestamp := binary.BigEndian.Uint64(arguments[2])
log.Infof("received rpc: fingerprint: %x rpc: %x timestamp: %d", arguments[0], arguments[1], timestamp)
if announce.AddRPC(arguments[0], arguments[1], timestamp) {
messagebus.Bus.Broadcast.Send("rpc", arguments[0:3]...)
}
case "peer":
if dataLength < 4 {
log.Warnf("peer with too few data: %d items", dataLength)
return
}
timestamp := binary.BigEndian.Uint64(arguments[3])
log.Infof("received peer: %x broadcast: %x listener: %x timestamp: %d", arguments[0], arguments[1], arguments[2], timestamp)
if announce.AddPeer(arguments[0], arguments[1], arguments[2], timestamp) {
messagebus.Bus.Broadcast.Send("peer", arguments[0:4]...)
}
case "heart":
if dataLength < 1 {
log.Warnf("heart with too few data: %d items", dataLength)
return
}
log.Infof("received heart: %q", arguments[0])
// nothing to forward, this is just to keep communication alive
default:
log.Warnf("received unhandled command: %q arguments: %x", command, arguments)
}
}
// un pack each asset and cache them
func processAssets(packed []byte) error {
if 0 == len(packed) {
return fault.ErrMissingParameters
}
if !mode.Is(mode.Normal) {
return fault.ErrNotAvailableDuringSynchronise
}
ok := false
for 0 != len(packed) {
transaction, n, err := transactionrecord.Packed(packed).Unpack(mode.IsTesting())
if nil != err {
return err
}
switch tx := transaction.(type) {
case *transactionrecord.AssetData:
_, packedAsset, err := asset.Cache(tx)
if nil != err {
return err
}
if nil != packedAsset {
ok = true
}
default:
return fault.ErrTransactionIsNotAnAsset
}
packed = packed[n:]
}
if !ok {
return fault.ErrNoNewTransactions
}
return nil
}
// un pack each issue and cache them
func processIssues(packed, verified []byte) error {
if 0 == len(packed) {
return fault.ErrMissingParameters
}
if !mode.Is(mode.Normal) {
return fault.ErrNotAvailableDuringSynchronise
}
packedIssues := transactionrecord.Packed(packed)
issueCount := 0 // for payment difficulty
issues := make([]*transactionrecord.BitmarkIssue, 0, 100)
for 0 != len(packedIssues) {
transaction, n, err := packedIssues.Unpack(mode.IsTesting())
if nil != err {
return err
}
switch tx := transaction.(type) {
case *transactionrecord.BitmarkIssue:
issues = append(issues, tx)
issueCount += 1
default:
return fault.ErrTransactionIsNotAnIssue
}
packedIssues = packedIssues[n:]
}
if 0 == len(issues) {
return fault.ErrMissingParameters
}
isVerified := false
if v, _ := util.FromVarint64(verified); v == 1 {
isVerified = true
}
_, duplicate, err := reservoir.StoreIssues(issues, isVerified)
if nil != err {
return err
}
if duplicate {
return fault.ErrTransactionAlreadyExists
}
return nil
}
// unpack transfer and process it
func processTransfer(packed []byte) error {
if 0 == len(packed) {
return fault.ErrMissingParameters
}
if !mode.Is(mode.Normal) {
return fault.ErrNotAvailableDuringSynchronise
}
transaction, _, err := transactionrecord.Packed(packed).Unpack(mode.IsTesting())
if nil != err {
return err
}
switch tx := transaction.(type) {
case *transactionrecord.BitmarkTransfer:
_, duplicate, err := reservoir.StoreTransfer(tx)
if nil != err {
return err
}
if duplicate {
return fault.ErrTransactionAlreadyExists
}
default:
return fault.ErrTransactionIsNotATransfer
}
return nil
}
// process proof block
func processProof(packed []byte) error {
if 0 == len(packed) {
return fault.ErrMissingParameters
}
if !mode.Is(mode.Normal) {
return fault.ErrNotAvailableDuringSynchronise
}
var payId pay.PayId
if len(packed) > payment.NonceLength+len(payId) {
return fault.ErrInvalidNonce
}
copy(payId[:], packed[:len(payId)])
nonce := packed[len(payId):]
status := reservoir.TryProof(payId, nonce)
if reservoir.TrackingAccepted != status {
// pay id already processed or was invalid
return fault.ErrPayIdAlreadyUsed
}
return nil
}