mirror of
https://github.com/status-im/simulation.git
synced 2025-02-23 04:18:09 +00:00
Change graph-experiments to graphx dependency
This commit is contained in:
parent
0a792009b6
commit
c82b8d22af
@ -7,7 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/divan/graph-experiments/graph"
|
"github.com/divan/graphx/formats"
|
||||||
gethlog "github.com/ethereum/go-ethereum/log"
|
gethlog "github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/status-im/simulation/propagation"
|
"github.com/status-im/simulation/propagation"
|
||||||
"github.com/status-im/simulation/propagation/naivep2p"
|
"github.com/status-im/simulation/propagation/naivep2p"
|
||||||
@ -27,7 +27,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
data, err := graph.NewGraphFromJSON(*input)
|
data, err := formats.FromD3JSON(*input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Opening input file failed: ", err)
|
log.Fatal("Opening input file failed: ", err)
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package naivep2p
|
package naivep2p
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/divan/graph-experiments/graph"
|
"github.com/divan/graphx/graph"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LinkIndex stores link information in form of indexes, rather than nodes IP.
|
// 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)
|
ret := make(map[int][]int)
|
||||||
for _, link := range links {
|
for _, link := range links {
|
||||||
if link.From == link.To {
|
if link.From() == link.To() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if _, ok := ret[link.From]; !ok {
|
if _, ok := ret[link.FromIdx()]; !ok {
|
||||||
ret[link.From] = make([]int, 0)
|
ret[link.FromIdx()] = make([]int, 0)
|
||||||
}
|
}
|
||||||
if _, ok := ret[link.To]; !ok {
|
if _, ok := ret[link.ToIdx()]; !ok {
|
||||||
ret[link.To] = make([]int, 0)
|
ret[link.ToIdx()] = make([]int, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
peers := ret[link.From]
|
peers := ret[link.FromIdx()]
|
||||||
peers = append(peers, link.To)
|
peers = append(peers, link.ToIdx())
|
||||||
ret[link.From] = peers
|
ret[link.FromIdx()] = peers
|
||||||
|
|
||||||
peers = ret[link.To]
|
peers = ret[link.ToIdx()]
|
||||||
peers = append(peers, link.From)
|
peers = append(peers, link.FromIdx())
|
||||||
ret[link.To] = peers
|
ret[link.ToIdx()] = peers
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
@ -37,8 +37,8 @@ func (s *Simulator) logEntries2PropagationLog(entries []*LogEntry) *propagation.
|
|||||||
findLink := func(from, to int) int {
|
findLink := func(from, to int) int {
|
||||||
links := s.data.Links()
|
links := s.data.Links()
|
||||||
for i := range links {
|
for i := range links {
|
||||||
if links[i].From == from && links[i].To == to ||
|
if links[i].FromIdx() == from && links[i].ToIdx() == to ||
|
||||||
links[i].To == from && links[i].From == to {
|
links[i].ToIdx() == from && links[i].FromIdx() == to {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/divan/graph-experiments/graph"
|
"github.com/divan/graphx/graph"
|
||||||
"github.com/status-im/simulation/propagation"
|
"github.com/status-im/simulation/propagation"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,3 +26,14 @@ func newlogEntry(start time.Time, from, to int) *logEntry {
|
|||||||
To: to,
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/divan/graph-experiments/graph"
|
"github.com/divan/graphx/graph"
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/node"
|
"github.com/ethereum/go-ethereum/node"
|
||||||
@ -84,9 +84,9 @@ func NewSimulator(data *graph.Graph) *Simulator {
|
|||||||
go func() {
|
go func() {
|
||||||
log.Println("Connecting nodes...")
|
log.Println("Connecting nodes...")
|
||||||
for _, link := range data.Links() {
|
for _, link := range data.Links() {
|
||||||
err := sim.connectNodes(link.From, link.To)
|
err := sim.connectNodes(link.FromIdx(), link.ToIdx())
|
||||||
if err != nil && err != ErrLinkExists {
|
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 {
|
} else if err == nil {
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
@ -204,22 +204,11 @@ func (s *Simulator) SendMessage(startNodeIdx, ttl int) *propagation.Log {
|
|||||||
// aggregating by timestamps and converting nodes indices to link indices.
|
// aggregating by timestamps and converting nodes indices to link indices.
|
||||||
// We expect that timestamps already bucketed into Nms groups.
|
// We expect that timestamps already bucketed into Nms groups.
|
||||||
func (s *Simulator) logEntries2PropagationLog(entries []*logEntry) *propagation.Log {
|
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)
|
tss := make(map[time.Duration][]int)
|
||||||
tsnodes := make(map[time.Duration][]int)
|
tsnodes := make(map[time.Duration][]int)
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
idx := findLink(entry.From, entry.To)
|
idx, err := s.data.LinkByIndices(entry.From, entry.To)
|
||||||
if idx == -1 {
|
if err != nil {
|
||||||
log.Println("[EE] Wrong link", entry)
|
log.Println("[EE] Wrong link", entry)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -286,12 +275,19 @@ func findNode(nodes []graph.Node, ID string) (int, error) {
|
|||||||
return i, nil
|
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 {
|
func (sim *Simulator) connectNodes(from, to int) error {
|
||||||
|
// TODO(divan): check if we have IDs in from/to strings
|
||||||
node1 := sim.network.Nodes[from]
|
node1 := sim.network.Nodes[from]
|
||||||
|
if node1 == nil {
|
||||||
|
return fmt.Errorf("node with ID '%v' not found", from)
|
||||||
|
}
|
||||||
node2 := sim.network.Nodes[to]
|
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 connection already exists, skip it, as network.Connect will fail
|
||||||
if sim.network.GetConn(node1.ID(), node2.ID()) != nil {
|
if sim.network.GetConn(node1.ID(), node2.ID()) != nil {
|
||||||
return ErrLinkExists
|
return ErrLinkExists
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/divan/graph-experiments/graph"
|
"github.com/divan/graphx/graph"
|
||||||
"github.com/status-im/simulation/propagation"
|
"github.com/status-im/simulation/propagation"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -3,8 +3,8 @@ package stats
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/divan/graph-experiments/graph"
|
"github.com/divan/graphx/graph"
|
||||||
"github.com/status-im/simulation/simulation"
|
"github.com/status-im/simulation/propagation"
|
||||||
)
|
)
|
||||||
|
|
||||||
// node implements string-only graph.Node
|
// node implements string-only graph.Node
|
||||||
@ -19,15 +19,15 @@ func TestAnalyze(t *testing.T) {
|
|||||||
g.AddNode(node("2"))
|
g.AddNode(node("2"))
|
||||||
g.AddNode(node("3"))
|
g.AddNode(node("3"))
|
||||||
|
|
||||||
g.AddLink(0, 1)
|
g.AddLink("0", "1")
|
||||||
g.AddLink(1, 2)
|
g.AddLink("1", "2")
|
||||||
g.AddLink(2, 0)
|
g.AddLink("2", "0")
|
||||||
g.AddLink(0, 3)
|
g.AddLink("0", "3")
|
||||||
|
|
||||||
// example propagation log
|
// example propagation log
|
||||||
// three timestamps: 10, 20 and 30 ms
|
// three timestamps: 10, 20 and 30 ms
|
||||||
// with first node hit 1 time, second and third - 3 times
|
// with first node hit 1 time, second and third - 3 times
|
||||||
plog := &simulation.Log{
|
plog := &propagation.Log{
|
||||||
Timestamps: []int{10, 20, 30},
|
Timestamps: []int{10, 20, 30},
|
||||||
Nodes: [][]int{
|
Nodes: [][]int{
|
||||||
[]int{0, 1, 2},
|
[]int{0, 1, 2},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user