-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Produce GEFX graph for each run #135
Open
lalexgap
wants to merge
2
commits into
main
Choose a base branch
from
gexf-graph
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/statechannels/go-nitro-testground/config" | ||
"github.com/statechannels/go-nitro-testground/peer" | ||
"github.com/statechannels/go-nitro/protocols" | ||
"github.com/testground/sdk-go/sync" | ||
|
||
"gonum.org/v1/gonum/graph/formats/gexf12" | ||
) | ||
|
||
type objectiveStatus string | ||
|
||
const ( | ||
Starting objectiveStatus = "Starting" | ||
Completed objectiveStatus = "Completed" | ||
) | ||
|
||
// ObjectiveStatusInfo contains information about the status change of an objective | ||
type ObjectiveStatusInfo struct { | ||
Id protocols.ObjectiveId | ||
Time time.Time | ||
Participants []common.Address | ||
ChannelId string | ||
Status objectiveStatus | ||
} | ||
|
||
// GraphRecorder is a utility for recording the state of the network in GEXF file format. | ||
type GraphRecorder struct { | ||
me peer.PeerInfo | ||
peers []peer.PeerInfo | ||
config config.RunConfig | ||
graph *gexf12.Graph | ||
syncClient sync.Client | ||
} | ||
|
||
// Graph returns the GEXF graph struct | ||
func (gr *GraphRecorder) Graph() *gexf12.Graph { | ||
return gr.graph | ||
} | ||
|
||
// NewGraphRecorder creates a new GraphRecorder | ||
func NewGraphRecorder(me peer.PeerInfo, peers []peer.PeerInfo, config config.RunConfig, syncClient sync.Client) *GraphRecorder { | ||
var graph *gexf12.Graph | ||
if peer.IsGraphRecorder(me.Seq, config) { | ||
graph = &gexf12.Graph{} | ||
graph.TimeFormat = "dateTime" | ||
graph.Start = time.Now().Format( | ||
"2006-01-02T15:04:05-0700") | ||
graph.DefaultEdgeType = "directed" | ||
|
||
// Set up attributes on the node and edges for channels and participants | ||
graph.Attributes = []gexf12.Attributes{{ | ||
Class: "node", | ||
Attributes: []gexf12.Attribute{ | ||
{ID: "address", Title: "address", Type: "string"}, {ID: "role", Title: "role", Type: "string"}}}, | ||
{ | ||
Class: "edge", | ||
Attributes: []gexf12.Attribute{ | ||
{ID: "channelId", Title: "ChannelId", Type: "string"}, {ID: "channelType", Title: "ChannelType", Type: "string"}}}} | ||
|
||
// Add nodes for each participant | ||
for _, p := range append(peers, me) { | ||
attValues := gexf12.AttValues{AttValues: []gexf12.AttValue{ | ||
{For: "address", Value: p.Address.String()}, | ||
{For: "role", Value: fmt.Sprintf("%d", p.Role)}}} | ||
|
||
node := gexf12.Node{ID: p.Address.String(), | ||
Label: p.Address.String()[0:6], | ||
AttValues: &attValues, | ||
Start: time.Now().Format( | ||
"2006-01-02T15:04:05-0700")} | ||
|
||
graph.Nodes.Nodes = append(graph.Nodes.Nodes, node) | ||
} | ||
|
||
} | ||
|
||
gr := &GraphRecorder{ | ||
me: me, | ||
peers: peers, | ||
config: config, | ||
graph: graph, | ||
syncClient: syncClient, | ||
} | ||
if peer.IsGraphRecorder(gr.me.Seq, gr.config) { | ||
// Start listening for changes from other participants | ||
go gr.listenForShared(context.Background()) | ||
} | ||
|
||
return gr | ||
} | ||
|
||
// objectiveStatusChangedTopic returns the topic to share objective status changes on | ||
func (gr *GraphRecorder) objectiveStatusChangedTopic() *sync.Topic { | ||
return sync.NewTopic("objective-status-change", ObjectiveStatusInfo{}) | ||
} | ||
|
||
// listenForShared listens for objective status changes from other participants | ||
func (gr *GraphRecorder) listenForShared(ctx context.Context) { | ||
if !peer.IsGraphRecorder(gr.me.Seq, gr.config) { | ||
return | ||
} | ||
started := make(chan ObjectiveStatusInfo) | ||
|
||
gr.syncClient.MustSubscribe(ctx, gr.objectiveStatusChangedTopic(), started) | ||
for { | ||
select { | ||
case s := <-started: | ||
gr.ObjectiveStatusUpdated(s) | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
} | ||
|
||
// ObjectiveStatusUpdated is called when an objective status changes | ||
// It uses this information to build a graph of the network | ||
func (gr *GraphRecorder) ObjectiveStatusUpdated(info ObjectiveStatusInfo) { | ||
if !peer.IsGraphRecorder(gr.me.Seq, gr.config) { | ||
gr.syncClient.MustPublish(context.Background(), gr.objectiveStatusChangedTopic(), info) | ||
} else { | ||
|
||
isCreateChannel := (strings.Contains(string(info.Id), "VirtualFund") || strings.Contains(string(info.Id), "DirectFund")) && info.Status == Starting | ||
isCloseChannel := (strings.Contains(string(info.Id), "VirtualDefund") || strings.Contains(string(info.Id), "DirectDefund")) && info.Status == Completed | ||
|
||
if isCreateChannel { | ||
channelType := "ledger" | ||
if strings.Contains(string(info.Id), "VirtualFund") { | ||
channelType = "virtual" | ||
} | ||
|
||
attValues := gexf12.AttValues{ | ||
AttValues: []gexf12.AttValue{ | ||
{For: "channelId", Value: info.ChannelId}, | ||
{For: "channelType", Value: channelType}}} | ||
|
||
for i := 0; (i + 1) < len(info.Participants); i++ { | ||
|
||
gr.graph.Edges.Edges = append(gr.graph.Edges.Edges, | ||
gexf12.Edge{ | ||
ID: fmt.Sprintf("%s_%s", info.ChannelId, info.Participants[i]), | ||
Label: string(info.ChannelId)[0:6], | ||
Source: info.Participants[i].String(), | ||
Target: info.Participants[i+1].String(), | ||
Start: info.Time.Format( | ||
"2006-01-02T15:04:05-0700"), | ||
AttValues: &attValues, | ||
}, | ||
) | ||
} | ||
|
||
} | ||
if isCloseChannel { | ||
for i := 0; i < len(gr.graph.Edges.Edges); i++ { | ||
|
||
if strings.Contains(gr.graph.Edges.Edges[i].ID, info.ChannelId) { | ||
|
||
gr.graph.Edges.Edges[i].End = info.Time.Format( | ||
"2006-01-02T15:04:05-0700") | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use empty participants list here?