forked from iotaledger/goshimmer
/
visualizer.go
446 lines (396 loc) · 15.3 KB
/
visualizer.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package dagsvisualizer
import (
"context"
"net/http"
"strconv"
"sync"
"time"
"github.com/izuc/zipp.foundation/core/daemon"
"github.com/izuc/zipp.foundation/core/generics/event"
"github.com/izuc/zipp.foundation/core/generics/lo"
"github.com/izuc/zipp.foundation/core/generics/set"
"github.com/izuc/zipp.foundation/core/generics/walker"
"github.com/izuc/zipp.foundation/core/types/confirmation"
"github.com/izuc/zipp.foundation/core/workerpool"
"github.com/labstack/echo"
"github.com/izuc/zipp/packages/app/jsonmodels"
"github.com/izuc/zipp/packages/node/shutdown"
"github.com/izuc/zipp/packages/core/conflictdag"
"github.com/izuc/zipp/packages/core/ledger"
"github.com/izuc/zipp/packages/core/ledger/utxo"
"github.com/izuc/zipp/packages/core/ledger/vm/devnetvm"
"github.com/izuc/zipp/packages/core/mesh_old"
)
var (
visualizerWorkerCount = 1
visualizerWorkerQueueSize = 500
visualizerWorkerPool *workerpool.NonBlockingQueuedWorkerPool
maxWsBlockBufferSize = 200
buffer []*wsBlock
bufferMutex sync.RWMutex
)
func setupVisualizer() {
// create and start workerpool
visualizerWorkerPool = workerpool.NewNonBlockingQueuedWorkerPool(func(task workerpool.Task) {
broadcastWsBlock(task.Param(0))
}, workerpool.WorkerCount(visualizerWorkerCount), workerpool.QueueSize(visualizerWorkerQueueSize))
}
func runVisualizer() {
if err := daemon.BackgroundWorker("Dags Visualizer[Visualizer]", func(ctx context.Context) {
// register to events
registerMeshEvents()
registerUTXOEvents()
registerConflictEvents()
<-ctx.Done()
log.Info("Stopping DAGs Visualizer ...")
visualizerWorkerPool.Stop()
log.Info("Stopping DAGs Visualizer ... done")
}, shutdown.PriorityDashboard); err != nil {
log.Panicf("Failed to start as daemon: %s", err)
}
}
func registerMeshEvents() {
storeClosure := event.NewClosure(func(event *mesh_old.BlockStoredEvent) {
wsBlk := &wsBlock{
Type: BlkTypeMeshVertex,
Data: newMeshVertex(event.Block),
}
visualizerWorkerPool.TrySubmit(wsBlk)
storeWsBlock(wsBlk)
})
bookedClosure := event.NewClosure(func(event *mesh_old.BlockBookedEvent) {
blockID := event.BlockID
deps.Mesh.Storage.BlockMetadata(blockID).Consume(func(blkMetadata *mesh_old.BlockMetadata) {
conflictIDs, err := deps.Mesh.Booker.BlockConflictIDs(blockID)
if err != nil {
conflictIDs = set.NewAdvancedSet[utxo.TransactionID]()
}
wsBlk := &wsBlock{
Type: BlkTypeMeshBooked,
Data: &meshBooked{
ID: blockID.Base58(),
IsMarker: blkMetadata.StructureDetails().IsPastMarker(),
ConflictIDs: lo.Map(conflictIDs.Slice(), utxo.TransactionID.Base58),
},
}
visualizerWorkerPool.TrySubmit(wsBlk)
storeWsBlock(wsBlk)
})
})
blkConfirmedClosure := event.NewClosure(func(event *mesh_old.BlockAcceptedEvent) {
blockID := event.Block.ID()
deps.Mesh.Storage.BlockMetadata(blockID).Consume(func(blkMetadata *mesh_old.BlockMetadata) {
wsBlk := &wsBlock{
Type: BlkTypeMeshConfirmed,
Data: &meshConfirmed{
ID: blockID.Base58(),
ConfirmationState: blkMetadata.ConfirmationState().String(),
ConfirmationStateTime: blkMetadata.ConfirmationStateTime().UnixNano(),
},
}
visualizerWorkerPool.TrySubmit(wsBlk)
storeWsBlock(wsBlk)
})
})
txAcceptedClosure := event.NewClosure(func(event *ledger.TransactionAcceptedEvent) {
var blkID mesh_old.BlockID
deps.Mesh.Storage.Attachments(event.TransactionID).Consume(func(a *mesh_old.Attachment) {
blkID = a.BlockID()
})
wsBlk := &wsBlock{
Type: BlkTypeMeshTxConfirmationState,
Data: &meshTxConfirmationStateChanged{
ID: blkID.Base58(),
IsConfirmed: deps.AcceptanceGadget.IsTransactionConfirmed(event.TransactionID),
},
}
visualizerWorkerPool.TrySubmit(wsBlk)
storeWsBlock(wsBlk)
})
deps.Mesh.Storage.Events.BlockStored.Attach(storeClosure)
deps.Mesh.Booker.Events.BlockBooked.Attach(bookedClosure)
deps.AcceptanceGadget.Events().BlockAccepted.Attach(blkConfirmedClosure)
deps.Mesh.Ledger.Events.TransactionAccepted.Attach(txAcceptedClosure)
}
func registerUTXOEvents() {
storeClosure := event.NewClosure(func(event *mesh_old.BlockStoredEvent) {
if event.Block.Payload().Type() == devnetvm.TransactionType {
tx := event.Block.Payload().(*devnetvm.Transaction)
wsBlk := &wsBlock{
Type: BlkTypeUTXOVertex,
Data: newUTXOVertex(event.Block.ID(), tx),
}
visualizerWorkerPool.TrySubmit(wsBlk)
storeWsBlock(wsBlk)
}
})
bookedClosure := event.NewClosure(func(event *mesh_old.BlockBookedEvent) {
blockID := event.BlockID
deps.Mesh.Storage.Block(blockID).Consume(func(block *mesh_old.Block) {
if block.Payload().Type() == devnetvm.TransactionType {
tx := block.Payload().(*devnetvm.Transaction)
deps.Mesh.Ledger.Storage.CachedTransactionMetadata(tx.ID()).Consume(func(txMetadata *ledger.TransactionMetadata) {
wsBlk := &wsBlock{
Type: BlkTypeUTXOBooked,
Data: &utxoBooked{
ID: tx.ID().Base58(),
ConflictIDs: lo.Map(txMetadata.ConflictIDs().Slice(), utxo.TransactionID.Base58),
},
}
visualizerWorkerPool.TrySubmit(wsBlk)
storeWsBlock(wsBlk)
})
}
})
})
txAcceptedClosure := event.NewClosure(func(event *ledger.TransactionAcceptedEvent) {
txID := event.TransactionID
deps.Mesh.Ledger.Storage.CachedTransactionMetadata(txID).Consume(func(txMetadata *ledger.TransactionMetadata) {
wsBlk := &wsBlock{
Type: BlkTypeUTXOConfirmationStateChanged,
Data: &utxoConfirmationStateChanged{
ID: txID.Base58(),
ConfirmationState: txMetadata.ConfirmationState().String(),
ConfirmationStateTime: txMetadata.ConfirmationStateTime().UnixNano(),
IsConfirmed: deps.AcceptanceGadget.IsTransactionConfirmed(txID),
},
}
visualizerWorkerPool.TrySubmit(wsBlk)
storeWsBlock(wsBlk)
})
})
deps.Mesh.Storage.Events.BlockStored.Attach(storeClosure)
deps.Mesh.Booker.Events.BlockBooked.Attach(bookedClosure)
deps.Mesh.Ledger.Events.TransactionAccepted.Attach(txAcceptedClosure)
}
func registerConflictEvents() {
createdClosure := event.NewClosure(func(event *conflictdag.ConflictCreatedEvent[utxo.TransactionID, utxo.OutputID]) {
wsBlk := &wsBlock{
Type: BlkTypeConflictVertex,
Data: newConflictVertex(event.ID),
}
visualizerWorkerPool.TrySubmit(wsBlk)
storeWsBlock(wsBlk)
})
parentUpdateClosure := event.NewClosure(func(event *conflictdag.ConflictParentsUpdatedEvent[utxo.TransactionID, utxo.OutputID]) {
lo.Map(event.ParentsConflictIDs.Slice(), utxo.TransactionID.Base58)
wsBlk := &wsBlock{
Type: BlkTypeConflictParentsUpdate,
Data: &conflictParentUpdate{
ID: event.ConflictID.Base58(),
Parents: lo.Map(event.ParentsConflictIDs.Slice(), utxo.TransactionID.Base58),
},
}
visualizerWorkerPool.TrySubmit(wsBlk)
storeWsBlock(wsBlk)
})
conflictConfirmedClosure := event.NewClosure(func(event *conflictdag.ConflictAcceptedEvent[utxo.TransactionID]) {
wsBlk := &wsBlock{
Type: BlkTypeConflictConfirmationStateChanged,
Data: &conflictConfirmationStateChanged{
ID: event.ID.Base58(),
ConfirmationState: confirmation.Accepted.String(),
IsConfirmed: true,
},
}
visualizerWorkerPool.TrySubmit(wsBlk)
storeWsBlock(wsBlk)
})
conflictWeightChangedClosure := event.NewClosure(func(e *mesh_old.ConflictWeightChangedEvent) {
conflictConfirmationState := deps.Mesh.Ledger.ConflictDAG.ConfirmationState(utxo.NewTransactionIDs(e.ConflictID))
wsBlk := &wsBlock{
Type: BlkTypeConflictWeightChanged,
Data: &conflictWeightChanged{
ID: e.ConflictID.Base58(),
Weight: e.Weight,
ConfirmationState: conflictConfirmationState.String(),
},
}
visualizerWorkerPool.TrySubmit(wsBlk)
storeWsBlock(wsBlk)
})
deps.Mesh.Ledger.ConflictDAG.Events.ConflictCreated.Attach(createdClosure)
deps.Mesh.Ledger.ConflictDAG.Events.ConflictAccepted.Attach(conflictConfirmedClosure)
deps.Mesh.Ledger.ConflictDAG.Events.ConflictParentsUpdated.Attach(parentUpdateClosure)
deps.Mesh.ApprovalWeightManager.Events.ConflictWeightChanged.Attach(conflictWeightChangedClosure)
}
func setupDagsVisualizerRoutes(routeGroup *echo.Group) {
routeGroup.GET("/dagsvisualizer/conflict/:conflictID", func(c echo.Context) (err error) {
parents := make(map[string]*conflictVertex)
var conflictID utxo.TransactionID
if err = conflictID.FromBase58(c.Param("conflictID")); err != nil {
err = c.JSON(http.StatusBadRequest, jsonmodels.NewErrorResponse(err))
return
}
vertex := newConflictVertex(conflictID)
parents[vertex.ID] = vertex
getConflictsToMaster(vertex, parents)
var conflicts []*conflictVertex
for _, conflict := range parents {
conflicts = append(conflicts, conflict)
}
return c.JSON(http.StatusOK, conflicts)
})
routeGroup.GET("/dagsvisualizer/search/:start/:end", func(c echo.Context) (err error) {
startTimestamp := parseStringToTimestamp(c.Param("start"))
endTimestamp := parseStringToTimestamp(c.Param("end"))
reqValid := isTimeIntervalValid(startTimestamp, endTimestamp)
if !reqValid {
return c.JSON(http.StatusBadRequest, searchResult{Error: "invalid timestamp range"})
}
blocks := []*meshVertex{}
txs := []*utxoVertex{}
conflicts := []*conflictVertex{}
conflictMap := set.NewAdvancedSet[utxo.TransactionID]()
entryBlks := mesh_old.NewBlockIDs()
deps.Mesh.Storage.Children(mesh_old.EmptyBlockID).Consume(func(child *mesh_old.Child) {
entryBlks.Add(child.ChildBlockID())
})
deps.Mesh.Utils.WalkBlockID(func(blockID mesh_old.BlockID, walker *walker.Walker[mesh_old.BlockID]) {
deps.Mesh.Storage.Block(blockID).Consume(func(blk *mesh_old.Block) {
// only keep blocks that is issued in the given time interval
if blk.IssuingTime().After(startTimestamp) && blk.IssuingTime().Before(endTimestamp) {
// add block
meshNode := newMeshVertex(blk)
blocks = append(blocks, meshNode)
// add tx
if meshNode.IsTx {
utxoNode := newUTXOVertex(blk.ID(), blk.Payload().(*devnetvm.Transaction))
txs = append(txs, utxoNode)
}
// add conflict
conflictIDs, err := deps.Mesh.Booker.BlockConflictIDs(blk.ID())
if err != nil {
conflictIDs = set.NewAdvancedSet[utxo.TransactionID]()
}
for it := conflictIDs.Iterator(); it.HasNext(); {
conflictID := it.Next()
if conflictMap.Has(conflictID) {
continue
}
conflictMap.Add(conflictID)
conflicts = append(conflicts, newConflictVertex(conflictID))
}
}
// continue walking if the block is issued before endTimestamp
if blk.IssuingTime().Before(endTimestamp) {
deps.Mesh.Storage.Children(blockID).Consume(func(child *mesh_old.Child) {
walker.Push(child.ChildBlockID())
})
}
})
}, entryBlks)
return c.JSON(http.StatusOK, searchResult{Blocks: blocks, Txs: txs, Conflicts: conflicts})
})
}
func parseStringToTimestamp(str string) (t time.Time) {
ts, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return time.Time{}
}
return time.Unix(ts, 0)
}
func isTimeIntervalValid(start, end time.Time) (valid bool) {
if start.IsZero() || end.IsZero() {
return false
}
if start.After(end) {
return false
}
return true
}
func newMeshVertex(block *mesh_old.Block) (ret *meshVertex) {
deps.Mesh.Storage.BlockMetadata(block.ID()).Consume(func(blkMetadata *mesh_old.BlockMetadata) {
conflictIDs, err := deps.Mesh.Booker.BlockConflictIDs(block.ID())
if err != nil {
conflictIDs = set.NewAdvancedSet[utxo.TransactionID]()
}
ret = &meshVertex{
ID: block.ID().Base58(),
StrongParentIDs: block.ParentsByType(mesh_old.StrongParentType).Base58(),
WeakParentIDs: block.ParentsByType(mesh_old.WeakParentType).Base58(),
ShallowLikeParentIDs: block.ParentsByType(mesh_old.ShallowLikeParentType).Base58(),
ConflictIDs: lo.Map(conflictIDs.Slice(), utxo.TransactionID.Base58),
IsMarker: blkMetadata.StructureDetails() != nil && blkMetadata.StructureDetails().IsPastMarker(),
IsTx: block.Payload().Type() == devnetvm.TransactionType,
IsConfirmed: deps.AcceptanceGadget.IsBlockConfirmed(block.ID()),
ConfirmationStateTime: blkMetadata.ConfirmationStateTime().UnixNano(),
ConfirmationState: blkMetadata.ConfirmationState().String(),
}
})
if ret.IsTx {
ret.TxID = block.Payload().(*devnetvm.Transaction).ID().Base58()
}
return
}
func newUTXOVertex(blkID mesh_old.BlockID, tx *devnetvm.Transaction) (ret *utxoVertex) {
inputs := make([]*jsonmodels.Input, len(tx.Essence().Inputs()))
for i, input := range tx.Essence().Inputs() {
inputs[i] = jsonmodels.NewInput(input)
}
outputs := make([]string, len(tx.Essence().Outputs()))
for i, output := range tx.Essence().Outputs() {
outputs[i] = output.ID().Base58()
}
var confirmationState string
var confirmedTime int64
var conflictIDs []string
deps.Mesh.Ledger.Storage.CachedTransactionMetadata(tx.ID()).Consume(func(txMetadata *ledger.TransactionMetadata) {
confirmationState = txMetadata.ConfirmationState().String()
confirmedTime = txMetadata.ConfirmationStateTime().UnixNano()
conflictIDs = lo.Map(txMetadata.ConflictIDs().Slice(), utxo.TransactionID.Base58)
})
ret = &utxoVertex{
BlkID: blkID.Base58(),
ID: tx.ID().Base58(),
Inputs: inputs,
Outputs: outputs,
IsConfirmed: deps.AcceptanceGadget.IsTransactionConfirmed(tx.ID()),
ConflictIDs: conflictIDs,
ConfirmationState: confirmationState,
ConfirmationStateTime: confirmedTime,
}
return ret
}
func newConflictVertex(conflictID utxo.TransactionID) (ret *conflictVertex) {
deps.Mesh.Ledger.ConflictDAG.Storage.CachedConflict(conflictID).Consume(func(conflict *conflictdag.Conflict[utxo.TransactionID, utxo.OutputID]) {
conflicts := make(map[utxo.OutputID][]utxo.TransactionID)
// get conflicts of a conflict
for it := conflict.ConflictSetIDs().Iterator(); it.HasNext(); {
conflictID := it.Next()
conflicts[conflictID] = make([]utxo.TransactionID, 0)
deps.Mesh.Ledger.ConflictDAG.Storage.CachedConflictMembers(conflictID).Consume(func(conflictMember *conflictdag.ConflictMember[utxo.OutputID, utxo.TransactionID]) {
conflicts[conflictID] = append(conflicts[conflictID], conflictMember.ConflictID())
})
}
ret = &conflictVertex{
ID: conflictID.Base58(),
Parents: lo.Map(conflict.Parents().Slice(), utxo.TransactionID.Base58),
Conflicts: jsonmodels.NewGetConflictConflictsResponse(conflict.ID(), conflicts),
IsConfirmed: deps.AcceptanceGadget.IsConflictConfirmed(conflictID),
ConfirmationState: deps.Mesh.Ledger.ConflictDAG.ConfirmationState(utxo.NewTransactionIDs(conflictID)).String(),
AW: deps.Mesh.ApprovalWeightManager.WeightOfConflict(conflictID),
}
})
return
}
func storeWsBlock(blk *wsBlock) {
bufferMutex.Lock()
defer bufferMutex.Unlock()
if len(buffer) >= maxWsBlockBufferSize {
buffer = buffer[1:]
}
buffer = append(buffer, blk)
}
func getConflictsToMaster(vertex *conflictVertex, parents map[string]*conflictVertex) {
for _, IDBase58 := range vertex.Parents {
if _, ok := parents[IDBase58]; !ok {
var ID utxo.TransactionID
if err := ID.FromBase58(IDBase58); err == nil {
parentVertex := newConflictVertex(ID)
parents[parentVertex.ID] = parentVertex
getConflictsToMaster(parentVertex, parents)
}
}
}
}