|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/ethereum/go-ethereum/common" |
| 10 | + "github.com/statechannels/go-nitro-testground/config" |
| 11 | + "github.com/statechannels/go-nitro-testground/peer" |
| 12 | + "github.com/statechannels/go-nitro/protocols" |
| 13 | + "github.com/testground/sdk-go/sync" |
| 14 | + |
| 15 | + "gonum.org/v1/gonum/graph/formats/gexf12" |
| 16 | +) |
| 17 | + |
| 18 | +type objectiveStatus string |
| 19 | + |
| 20 | +const ( |
| 21 | + Starting objectiveStatus = "Starting" |
| 22 | + Completed objectiveStatus = "Completed" |
| 23 | +) |
| 24 | + |
| 25 | +// ObjectiveStatusInfo contains information about the status change of an objective |
| 26 | +type ObjectiveStatusInfo struct { |
| 27 | + Id protocols.ObjectiveId |
| 28 | + Time time.Time |
| 29 | + Participants []common.Address |
| 30 | + ChannelId string |
| 31 | + Status objectiveStatus |
| 32 | +} |
| 33 | + |
| 34 | +// GraphRecorder is a utility for recording the state of the network in GEXF file format. |
| 35 | +type GraphRecorder struct { |
| 36 | + me peer.PeerInfo |
| 37 | + peers []peer.PeerInfo |
| 38 | + config config.RunConfig |
| 39 | + graph *gexf12.Graph |
| 40 | + syncClient sync.Client |
| 41 | +} |
| 42 | + |
| 43 | +// Graph returns the GEXF graph struct |
| 44 | +func (gr *GraphRecorder) Graph() *gexf12.Graph { |
| 45 | + return gr.graph |
| 46 | +} |
| 47 | + |
| 48 | +// NewGraphRecorder creates a new GraphRecorder |
| 49 | +func NewGraphRecorder(me peer.PeerInfo, peers []peer.PeerInfo, config config.RunConfig, syncClient sync.Client) *GraphRecorder { |
| 50 | + var graph *gexf12.Graph |
| 51 | + if peer.IsGraphRecorder(me.Seq, config) { |
| 52 | + graph = &gexf12.Graph{} |
| 53 | + graph.TimeFormat = "dateTime" |
| 54 | + graph.Start = time.Now().Format( |
| 55 | + "2006-01-02T15:04:05-0700") |
| 56 | + graph.DefaultEdgeType = "directed" |
| 57 | + |
| 58 | + // Set up attributes on the node and edges for channels and participants |
| 59 | + graph.Attributes = []gexf12.Attributes{{ |
| 60 | + Class: "node", |
| 61 | + Attributes: []gexf12.Attribute{ |
| 62 | + {ID: "address", Title: "address", Type: "string"}, {ID: "role", Title: "role", Type: "string"}}}, |
| 63 | + { |
| 64 | + Class: "edge", |
| 65 | + Attributes: []gexf12.Attribute{ |
| 66 | + {ID: "channelId", Title: "ChannelId", Type: "string"}, {ID: "channelType", Title: "ChannelType", Type: "string"}}}} |
| 67 | + |
| 68 | + // Add nodes for each participant |
| 69 | + for _, p := range append(peers, me) { |
| 70 | + attValues := gexf12.AttValues{AttValues: []gexf12.AttValue{ |
| 71 | + {For: "address", Value: p.Address.String()}, |
| 72 | + {For: "role", Value: fmt.Sprintf("%d", p.Role)}}} |
| 73 | + |
| 74 | + node := gexf12.Node{ID: p.Address.String(), |
| 75 | + Label: p.Address.String()[0:6], |
| 76 | + AttValues: &attValues, |
| 77 | + Start: time.Now().Format( |
| 78 | + "2006-01-02T15:04:05-0700")} |
| 79 | + |
| 80 | + graph.Nodes.Nodes = append(graph.Nodes.Nodes, node) |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + gr := &GraphRecorder{ |
| 86 | + me: me, |
| 87 | + peers: peers, |
| 88 | + config: config, |
| 89 | + graph: graph, |
| 90 | + syncClient: syncClient, |
| 91 | + } |
| 92 | + // Start listening for changes from other participants |
| 93 | + go gr.listenForShared(context.Background()) |
| 94 | + |
| 95 | + return gr |
| 96 | +} |
| 97 | + |
| 98 | +// objectiveStatusChangedTopic returns the topic to share objective status changes on |
| 99 | +func (gr *GraphRecorder) objectiveStatusChangedTopic() *sync.Topic { |
| 100 | + return sync.NewTopic("objective-status-change", ObjectiveStatusInfo{}) |
| 101 | +} |
| 102 | + |
| 103 | +// listenForShared listens for objective status changes from other participants |
| 104 | +func (gr *GraphRecorder) listenForShared(ctx context.Context) { |
| 105 | + if !peer.IsGraphRecorder(gr.me.Seq, gr.config) { |
| 106 | + return |
| 107 | + } |
| 108 | + started := make(chan ObjectiveStatusInfo) |
| 109 | + |
| 110 | + gr.syncClient.MustSubscribe(ctx, gr.objectiveStatusChangedTopic(), started) |
| 111 | + for { |
| 112 | + select { |
| 113 | + case s := <-started: |
| 114 | + gr.ObjectiveStatusUpdated(s) |
| 115 | + case <-ctx.Done(): |
| 116 | + return |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// ObjectiveStatusUpdated is called when an objective status changes |
| 122 | +// It uses this information to build a graph of the network |
| 123 | +func (gr *GraphRecorder) ObjectiveStatusUpdated(info ObjectiveStatusInfo) { |
| 124 | + if !peer.IsGraphRecorder(gr.me.Seq, gr.config) { |
| 125 | + gr.syncClient.MustPublish(context.Background(), gr.objectiveStatusChangedTopic(), info) |
| 126 | + } |
| 127 | + |
| 128 | + isCreateChannel := (strings.Contains(string(info.Id), "VirtualFund") || strings.Contains(string(info.Id), "DirectFund")) && info.Status == Starting |
| 129 | + isCloseChannel := (strings.Contains(string(info.Id), "VirtualDefund") || strings.Contains(string(info.Id), "DirectDefund")) && info.Status == Completed |
| 130 | + |
| 131 | + if isCreateChannel { |
| 132 | + channelType := "ledger" |
| 133 | + if strings.Contains(string(info.Id), "VirtualFund") { |
| 134 | + channelType = "virtual" |
| 135 | + } |
| 136 | + |
| 137 | + attValues := gexf12.AttValues{ |
| 138 | + AttValues: []gexf12.AttValue{ |
| 139 | + {For: "channelId", Value: info.ChannelId}, |
| 140 | + {For: "channelType", Value: channelType}}} |
| 141 | + |
| 142 | + for i := 0; (i + 1) < len(info.Participants); i++ { |
| 143 | + gr.graph.Edges.Edges = append(gr.graph.Edges.Edges, |
| 144 | + gexf12.Edge{ |
| 145 | + ID: fmt.Sprintf("%s_%s", info.ChannelId, info.Participants[i]), |
| 146 | + Label: string(info.ChannelId)[0:6], |
| 147 | + Source: info.Participants[i].String(), |
| 148 | + Target: info.Participants[i+1].String(), |
| 149 | + Start: info.Time.Format( |
| 150 | + "2006-01-02T15:04:05-0700"), |
| 151 | + AttValues: &attValues, |
| 152 | + }, |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | + if isCloseChannel { |
| 158 | + for i := 0; i < len(gr.graph.Edges.Edges); i++ { |
| 159 | + |
| 160 | + if strings.Contains(gr.graph.Edges.Edges[i].ID, info.ChannelId) { |
| 161 | + |
| 162 | + gr.graph.Edges.Edges[i].End = info.Time.Format( |
| 163 | + "2006-01-02T15:04:05-0700") |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
0 commit comments