From c82b8d22af9eb6b29a97616814011f1c4825c8a1 Mon Sep 17 00:00:00 2001 From: Ivan Danyliuk Date: Wed, 19 Sep 2018 18:17:16 +0300 Subject: [PATCH] Change graph-experiments to graphx dependency --- cmd/propagation_simulator/main.go | 4 ++-- propagation/naivep2p/links.go | 24 ++++++++++++------------ propagation/naivep2p/logentry.go | 4 ++-- propagation/naivep2p/simulator.go | 2 +- propagation/whisperv6/logentry.go | 11 +++++++++++ propagation/whisperv6/simulator.go | 30 +++++++++++++----------------- stats/stats.go | 2 +- stats/stats_test.go | 14 +++++++------- 8 files changed, 49 insertions(+), 42 deletions(-) diff --git a/cmd/propagation_simulator/main.go b/cmd/propagation_simulator/main.go index 808c83d..c29db98 100644 --- a/cmd/propagation_simulator/main.go +++ b/cmd/propagation_simulator/main.go @@ -7,7 +7,7 @@ import ( "os" "time" - "github.com/divan/graph-experiments/graph" + "github.com/divan/graphx/formats" gethlog "github.com/ethereum/go-ethereum/log" "github.com/status-im/simulation/propagation" "github.com/status-im/simulation/propagation/naivep2p" @@ -27,7 +27,7 @@ func main() { ) flag.Parse() - data, err := graph.NewGraphFromJSON(*input) + data, err := formats.FromD3JSON(*input) if err != nil { log.Fatal("Opening input file failed: ", err) } diff --git a/propagation/naivep2p/links.go b/propagation/naivep2p/links.go index 73d1d03..2a57063 100644 --- a/propagation/naivep2p/links.go +++ b/propagation/naivep2p/links.go @@ -1,7 +1,7 @@ package naivep2p import ( - "github.com/divan/graph-experiments/graph" + "github.com/divan/graphx/graph" ) // LinkIndex stores link information in form of indexes, rather than nodes IP. @@ -16,23 +16,23 @@ func PrecalculatePeers(data *graph.Graph) map[int][]int { ret := make(map[int][]int) for _, link := range links { - if link.From == link.To { + if link.From() == link.To() { continue } - if _, ok := ret[link.From]; !ok { - ret[link.From] = make([]int, 0) + if _, ok := ret[link.FromIdx()]; !ok { + ret[link.FromIdx()] = make([]int, 0) } - if _, ok := ret[link.To]; !ok { - ret[link.To] = make([]int, 0) + if _, ok := ret[link.ToIdx()]; !ok { + ret[link.ToIdx()] = make([]int, 0) } - peers := ret[link.From] - peers = append(peers, link.To) - ret[link.From] = peers + peers := ret[link.FromIdx()] + peers = append(peers, link.ToIdx()) + ret[link.FromIdx()] = peers - peers = ret[link.To] - peers = append(peers, link.From) - ret[link.To] = peers + peers = ret[link.ToIdx()] + peers = append(peers, link.FromIdx()) + ret[link.ToIdx()] = peers } return ret } diff --git a/propagation/naivep2p/logentry.go b/propagation/naivep2p/logentry.go index 4d157a5..84fc5f3 100644 --- a/propagation/naivep2p/logentry.go +++ b/propagation/naivep2p/logentry.go @@ -37,8 +37,8 @@ func (s *Simulator) logEntries2PropagationLog(entries []*LogEntry) *propagation. findLink := func(from, to int) int { links := s.data.Links() for i := range links { - if links[i].From == from && links[i].To == to || - links[i].To == from && links[i].From == to { + if links[i].FromIdx() == from && links[i].ToIdx() == to || + links[i].ToIdx() == from && links[i].FromIdx() == to { return i } } diff --git a/propagation/naivep2p/simulator.go b/propagation/naivep2p/simulator.go index 3653d5f..0276c1d 100644 --- a/propagation/naivep2p/simulator.go +++ b/propagation/naivep2p/simulator.go @@ -4,7 +4,7 @@ import ( "sync" "time" - "github.com/divan/graph-experiments/graph" + "github.com/divan/graphx/graph" "github.com/status-im/simulation/propagation" ) diff --git a/propagation/whisperv6/logentry.go b/propagation/whisperv6/logentry.go index 5adc714..b437ef2 100644 --- a/propagation/whisperv6/logentry.go +++ b/propagation/whisperv6/logentry.go @@ -26,3 +26,14 @@ func newlogEntry(start time.Time, from, to int) *logEntry { To: to, } } + +/* +// newlogEntry creates new log entry. +func newlogEntry(start time.Time, from, to int) *logEntry { + return &logEntry{ + Ts: time.Since(start) / time.Millisecond, + From: from, + To: to, + } +} +*/ diff --git a/propagation/whisperv6/simulator.go b/propagation/whisperv6/simulator.go index eea2e16..94dda48 100644 --- a/propagation/whisperv6/simulator.go +++ b/propagation/whisperv6/simulator.go @@ -7,7 +7,7 @@ import ( "math/rand" "time" - "github.com/divan/graph-experiments/graph" + "github.com/divan/graphx/graph" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/node" @@ -84,9 +84,9 @@ func NewSimulator(data *graph.Graph) *Simulator { go func() { log.Println("Connecting nodes...") for _, link := range data.Links() { - err := sim.connectNodes(link.From, link.To) + err := sim.connectNodes(link.FromIdx(), link.ToIdx()) if err != nil && err != ErrLinkExists { - log.Fatalf("[ERROR] Can't connect nodes %d and %d: %s", link.From, link.To, err) + log.Fatalf("[ERROR] Can't connect nodes %s and %s: %s", link.From(), link.To(), err) } else if err == nil { count++ } @@ -204,22 +204,11 @@ func (s *Simulator) SendMessage(startNodeIdx, ttl int) *propagation.Log { // aggregating by timestamps and converting nodes indices to link indices. // We expect that timestamps already bucketed into Nms groups. func (s *Simulator) logEntries2PropagationLog(entries []*logEntry) *propagation.Log { - links := s.data.Links() - findLink := func(from, to int) int { - for i := range links { - if links[i].From == from && links[i].To == to || - links[i].To == from && links[i].From == to { - return i - } - } - return -1 - } - tss := make(map[time.Duration][]int) tsnodes := make(map[time.Duration][]int) for _, entry := range entries { - idx := findLink(entry.From, entry.To) - if idx == -1 { + idx, err := s.data.LinkByIndices(entry.From, entry.To) + if err != nil { log.Println("[EE] Wrong link", entry) continue } @@ -286,12 +275,19 @@ func findNode(nodes []graph.Node, ID string) (int, error) { return i, nil } } - return -1, fmt.Errorf("Node with ID '%s' not found", ID) + return -1, fmt.Errorf("node with ID '%s' not found", ID) } func (sim *Simulator) connectNodes(from, to int) error { + // TODO(divan): check if we have IDs in from/to strings node1 := sim.network.Nodes[from] + if node1 == nil { + return fmt.Errorf("node with ID '%v' not found", from) + } node2 := sim.network.Nodes[to] + if node2 == nil { + return fmt.Errorf("node with ID '%v' not found", to) + } // if connection already exists, skip it, as network.Connect will fail if sim.network.GetConn(node1.ID(), node2.ID()) != nil { return ErrLinkExists diff --git a/stats/stats.go b/stats/stats.go index d4689ba..d6dc8f2 100644 --- a/stats/stats.go +++ b/stats/stats.go @@ -5,7 +5,7 @@ import ( "log" "time" - "github.com/divan/graph-experiments/graph" + "github.com/divan/graphx/graph" "github.com/status-im/simulation/propagation" ) diff --git a/stats/stats_test.go b/stats/stats_test.go index 046649c..306a3b2 100644 --- a/stats/stats_test.go +++ b/stats/stats_test.go @@ -3,8 +3,8 @@ package stats import ( "testing" - "github.com/divan/graph-experiments/graph" - "github.com/status-im/simulation/simulation" + "github.com/divan/graphx/graph" + "github.com/status-im/simulation/propagation" ) // node implements string-only graph.Node @@ -19,15 +19,15 @@ func TestAnalyze(t *testing.T) { g.AddNode(node("2")) g.AddNode(node("3")) - g.AddLink(0, 1) - g.AddLink(1, 2) - g.AddLink(2, 0) - g.AddLink(0, 3) + g.AddLink("0", "1") + g.AddLink("1", "2") + g.AddLink("2", "0") + g.AddLink("0", "3") // example propagation log // three timestamps: 10, 20 and 30 ms // with first node hit 1 time, second and third - 3 times - plog := &simulation.Log{ + plog := &propagation.Log{ Timestamps: []int{10, 20, 30}, Nodes: [][]int{ []int{0, 1, 2},