mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
p2p/simulations: fix lock copying (found by go vet)
This commit is contained in:
parent
b3c772c5dd
commit
a471b8242b
4 changed files with 41 additions and 67 deletions
|
|
@ -73,8 +73,7 @@ func NewEvent(v interface{}) *Event {
|
||||||
switch v := v.(type) {
|
switch v := v.(type) {
|
||||||
case *Node:
|
case *Node:
|
||||||
event.Type = EventTypeNode
|
event.Type = EventTypeNode
|
||||||
node := *v
|
event.Node = v.copy()
|
||||||
event.Node = &node
|
|
||||||
case *Conn:
|
case *Conn:
|
||||||
event.Type = EventTypeConn
|
event.Type = EventTypeConn
|
||||||
conn := *v
|
conn := *v
|
||||||
|
|
|
||||||
|
|
@ -420,16 +420,8 @@ type expectEvents struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *expectEvents) nodeEvent(id string, up bool) *Event {
|
func (t *expectEvents) nodeEvent(id string, up bool) *Event {
|
||||||
node := Node{
|
config := &adapters.NodeConfig{ID: enode.HexID(id)}
|
||||||
Config: &adapters.NodeConfig{
|
return &Event{Type: EventTypeNode, Node: newNode(nil, config, up)}
|
||||||
ID: enode.HexID(id),
|
|
||||||
},
|
|
||||||
up: up,
|
|
||||||
}
|
|
||||||
return &Event{
|
|
||||||
Type: EventTypeNode,
|
|
||||||
Node: &node,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *expectEvents) connEvent(one, other string, up bool) *Event {
|
func (t *expectEvents) connEvent(one, other string, up bool) *Event {
|
||||||
|
|
@ -450,7 +442,7 @@ loop:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event := <-t.events:
|
case event := <-t.events:
|
||||||
t.Logf("received %s event: %s", event.Type, event)
|
t.Logf("received %s event: %v", event.Type, event)
|
||||||
|
|
||||||
if event.Type != EventTypeMsg || event.Msg.Received {
|
if event.Type != EventTypeMsg || event.Msg.Received {
|
||||||
continue loop
|
continue loop
|
||||||
|
|
@ -486,7 +478,7 @@ func (t *expectEvents) expect(events ...*Event) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event := <-t.events:
|
case event := <-t.events:
|
||||||
t.Logf("received %s event: %s", event.Type, event)
|
t.Logf("received %s event: %v", event.Type, event)
|
||||||
|
|
||||||
expected := events[i]
|
expected := events[i]
|
||||||
if event.Type != expected.Type {
|
if event.Type != expected.Type {
|
||||||
|
|
|
||||||
|
|
@ -119,10 +119,7 @@ func (net *Network) NewNodeWithConfig(conf *adapters.NodeConfig) (*Node, error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
node := &Node{
|
node := newNode(adapterNode, conf, false)
|
||||||
Node: adapterNode,
|
|
||||||
Config: conf,
|
|
||||||
}
|
|
||||||
log.Trace("Node created", "id", conf.ID)
|
log.Trace("Node created", "id", conf.ID)
|
||||||
|
|
||||||
nodeIndex := len(net.Nodes)
|
nodeIndex := len(net.Nodes)
|
||||||
|
|
@ -448,7 +445,7 @@ func (net *Network) GetNodeIDs(excludeIDs ...enode.ID) []enode.ID {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (net *Network) getNodeIDs(excludeIDs []enode.ID) []enode.ID {
|
func (net *Network) getNodeIDs(excludeIDs []enode.ID) []enode.ID {
|
||||||
// Get all curent nodeIDs
|
// Get all current nodeIDs
|
||||||
nodeIDs := make([]enode.ID, 0, len(net.nodeMap))
|
nodeIDs := make([]enode.ID, 0, len(net.nodeMap))
|
||||||
for id := range net.nodeMap {
|
for id := range net.nodeMap {
|
||||||
nodeIDs = append(nodeIDs, id)
|
nodeIDs = append(nodeIDs, id)
|
||||||
|
|
@ -735,7 +732,16 @@ type Node struct {
|
||||||
|
|
||||||
// up tracks whether or not the node is running
|
// up tracks whether or not the node is running
|
||||||
up bool
|
up bool
|
||||||
upMu sync.RWMutex
|
upMu *sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func newNode(an adapters.Node, ac *adapters.NodeConfig, up bool) *Node {
|
||||||
|
return &Node{Node: an, Config: ac, up: up, upMu: new(sync.RWMutex)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node) copy() *Node {
|
||||||
|
configCpy := *n.Config
|
||||||
|
return newNode(n.Node, &configCpy, n.Up())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Up returns whether the node is currently up (online)
|
// Up returns whether the node is currently up (online)
|
||||||
|
|
@ -787,22 +793,19 @@ func (n *Node) MarshalJSON() ([]byte, error) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON implements json.Unmarshaler interface so that we don't lose
|
// UnmarshalJSON implements json.Unmarshaler interface so that we don't lose Node.up
|
||||||
// Node.up status. IMPORTANT: The implementation is incomplete; we lose
|
// status. IMPORTANT: The implementation is incomplete; we lose p2p.NodeInfo.
|
||||||
// p2p.NodeInfo.
|
|
||||||
func (n *Node) UnmarshalJSON(raw []byte) error {
|
func (n *Node) UnmarshalJSON(raw []byte) error {
|
||||||
// TODO: How should we turn back NodeInfo into n.Node?
|
// TODO: How should we turn back NodeInfo into n.Node?
|
||||||
// Ticket: https://github.com/ethersphere/go-ethereum/issues/1177
|
// Ticket: https://github.com/ethersphere/go-ethereum/issues/1177
|
||||||
node := struct {
|
var node struct {
|
||||||
Config *adapters.NodeConfig `json:"config,omitempty"`
|
Config *adapters.NodeConfig `json:"config,omitempty"`
|
||||||
Up bool `json:"up"`
|
Up bool `json:"up"`
|
||||||
}{}
|
}
|
||||||
if err := json.Unmarshal(raw, &node); err != nil {
|
if err := json.Unmarshal(raw, &node); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
*n = *newNode(nil, node.Config, node.Up)
|
||||||
n.SetUp(node.Up)
|
|
||||||
n.Config = node.Config
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -899,7 +902,7 @@ func (net *Network) snapshot(addServices []string, removeServices []string) (*Sn
|
||||||
Nodes: make([]NodeSnapshot, len(net.Nodes)),
|
Nodes: make([]NodeSnapshot, len(net.Nodes)),
|
||||||
}
|
}
|
||||||
for i, node := range net.Nodes {
|
for i, node := range net.Nodes {
|
||||||
snap.Nodes[i] = NodeSnapshot{Node: *node}
|
snap.Nodes[i] = NodeSnapshot{Node: *node.copy()}
|
||||||
if !node.Up() {
|
if !node.Up() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -758,27 +758,22 @@ func benchmarkMinimalServiceTmp(b *testing.B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNode_UnmarshalJSON(t *testing.T) {
|
func TestNode_UnmarshalJSON(t *testing.T) {
|
||||||
t.Run(
|
t.Run("up_field", func(t *testing.T) {
|
||||||
"test unmarshal of Node up field",
|
|
||||||
func(t *testing.T) {
|
|
||||||
runNodeUnmarshalJSON(t, casesNodeUnmarshalJSONUpField())
|
runNodeUnmarshalJSON(t, casesNodeUnmarshalJSONUpField())
|
||||||
},
|
})
|
||||||
)
|
t.Run("config_field", func(t *testing.T) {
|
||||||
t.Run(
|
|
||||||
"test unmarshal of Node Config field",
|
|
||||||
func(t *testing.T) {
|
|
||||||
runNodeUnmarshalJSON(t, casesNodeUnmarshalJSONConfigField())
|
runNodeUnmarshalJSON(t, casesNodeUnmarshalJSONConfigField())
|
||||||
},
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runNodeUnmarshalJSON(t *testing.T, tests []nodeUnmarshalTestCase) {
|
func runNodeUnmarshalJSON(t *testing.T, tests []nodeUnmarshalTestCase) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
var got Node
|
var got *Node
|
||||||
if err := got.UnmarshalJSON([]byte(tt.marshaled)); err != nil {
|
if err := json.Unmarshal([]byte(tt.marshaled), &got); err != nil {
|
||||||
expectErrorMessageToContain(t, err, tt.wantErr)
|
expectErrorMessageToContain(t, err, tt.wantErr)
|
||||||
|
got = nil
|
||||||
}
|
}
|
||||||
expectNodeEquality(t, got, tt.want)
|
expectNodeEquality(t, got, tt.want)
|
||||||
})
|
})
|
||||||
|
|
@ -788,7 +783,7 @@ func runNodeUnmarshalJSON(t *testing.T, tests []nodeUnmarshalTestCase) {
|
||||||
type nodeUnmarshalTestCase struct {
|
type nodeUnmarshalTestCase struct {
|
||||||
name string
|
name string
|
||||||
marshaled string
|
marshaled string
|
||||||
want Node
|
want *Node
|
||||||
wantErr string
|
wantErr string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -812,7 +807,7 @@ func expectErrorMessageToContain(t *testing.T, got error, want string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func expectNodeEquality(t *testing.T, got Node, want Node) {
|
func expectNodeEquality(t *testing.T, got, want *Node) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if !reflect.DeepEqual(got, want) {
|
if !reflect.DeepEqual(got, want) {
|
||||||
t.Errorf("Node.UnmarshalJSON() = %v, want %v", got, want)
|
t.Errorf("Node.UnmarshalJSON() = %v, want %v", got, want)
|
||||||
|
|
@ -824,23 +819,17 @@ func casesNodeUnmarshalJSONUpField() []nodeUnmarshalTestCase {
|
||||||
{
|
{
|
||||||
name: "empty json",
|
name: "empty json",
|
||||||
marshaled: "{}",
|
marshaled: "{}",
|
||||||
want: Node{
|
want: newNode(nil, nil, false),
|
||||||
up: false,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "a stopped node",
|
name: "a stopped node",
|
||||||
marshaled: "{\"up\": false}",
|
marshaled: "{\"up\": false}",
|
||||||
want: Node{
|
want: newNode(nil, nil, false),
|
||||||
up: false,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "a running node",
|
name: "a running node",
|
||||||
marshaled: "{\"up\": true}",
|
marshaled: "{\"up\": true}",
|
||||||
want: Node{
|
want: newNode(nil, nil, true),
|
||||||
up: true,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid JSON value on valid key",
|
name: "invalid JSON value on valid key",
|
||||||
|
|
@ -867,26 +856,17 @@ func casesNodeUnmarshalJSONConfigField() []nodeUnmarshalTestCase {
|
||||||
{
|
{
|
||||||
name: "Config field is omitted",
|
name: "Config field is omitted",
|
||||||
marshaled: "{}",
|
marshaled: "{}",
|
||||||
want: Node{
|
want: newNode(nil, nil, false),
|
||||||
Config: nil,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Config field is nil",
|
name: "Config field is nil",
|
||||||
marshaled: "{\"config\": nil}",
|
marshaled: "{\"config\": null}",
|
||||||
want: Node{
|
want: newNode(nil, nil, false),
|
||||||
Config: nil,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "a non default Config field",
|
name: "a non default Config field",
|
||||||
marshaled: "{\"config\":{\"name\":\"node_ecdd0\",\"port\":44665}}",
|
marshaled: "{\"config\":{\"name\":\"node_ecdd0\",\"port\":44665}}",
|
||||||
want: Node{
|
want: newNode(nil, &adapters.NodeConfig{Name: "node_ecdd0", Port: 44665}, false),
|
||||||
Config: &adapters.NodeConfig{
|
|
||||||
Name: "node_ecdd0",
|
|
||||||
Port: 44665,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue