-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add NodeInfo implementation * replace magic value with a constant. * update dependencies * bump minor version * add nodes deduplication logic. * shuffle values in test cases a little.
- Loading branch information
Showing
11 changed files
with
299 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package commands | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/services" | ||
"github.com/ledgerwatch/erigon/p2p" | ||
) | ||
|
||
// AdminAPI the interface for the admin_* RPC commands. | ||
type AdminAPI interface { | ||
// NodeInfo returns a collection of metadata known about the host. | ||
NodeInfo(ctx context.Context) (*p2p.NodeInfo, error) | ||
} | ||
|
||
// AdminAPIImpl data structure to store things needed for admin_* commands. | ||
type AdminAPIImpl struct { | ||
ethBackend services.ApiBackend | ||
} | ||
|
||
// NewAdminAPI returns AdminAPIImpl instance. | ||
func NewAdminAPI(eth services.ApiBackend) *AdminAPIImpl { | ||
return &AdminAPIImpl{ | ||
ethBackend: eth, | ||
} | ||
} | ||
|
||
func (api *AdminAPIImpl) NodeInfo(ctx context.Context) (*p2p.NodeInfo, error) { | ||
nodes, err := api.ethBackend.NodeInfo(ctx, 1) | ||
if err != nil { | ||
return nil, fmt.Errorf("node info request error: %w", err) | ||
} | ||
|
||
if len(nodes) == 0 { | ||
return nil, errors.New("empty nodesInfo response") | ||
} | ||
|
||
return &nodes[0], nil | ||
} |
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,16 @@ | ||
package commands | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ledgerwatch/erigon/p2p" | ||
) | ||
|
||
const ( | ||
// allNodesInfo used in NodeInfo request to receive meta data from all running sentries. | ||
allNodesInfo = 0 | ||
) | ||
|
||
func (api *ErigonImpl) NodeInfo(ctx context.Context) ([]p2p.NodeInfo, error) { | ||
return api.ethBackend.NodeInfo(ctx, allNodesInfo) | ||
} |
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,108 @@ | ||
package eth | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ledgerwatch/erigon-lib/direct" | ||
"github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" | ||
"github.com/ledgerwatch/erigon-lib/gointerfaces/types" | ||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/grpc" | ||
"google.golang.org/protobuf/types/known/emptypb" | ||
) | ||
|
||
func TestNodesInfo_Deduplication(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
limit int | ||
nodes []*types.NodeInfoReply | ||
want []*types.NodeInfoReply | ||
}{ | ||
{ | ||
name: "one node", | ||
nodes: []*types.NodeInfoReply{{Name: "name", Enode: "enode"}}, | ||
want: []*types.NodeInfoReply{{Name: "name", Enode: "enode"}}, | ||
}, | ||
{ | ||
name: "two different nodes", | ||
nodes: []*types.NodeInfoReply{ | ||
{Name: "name1", Enode: "enode1"}, | ||
{Name: "name", Enode: "enode"}, | ||
}, | ||
want: []*types.NodeInfoReply{ | ||
{Name: "name", Enode: "enode"}, | ||
{Name: "name1", Enode: "enode1"}, | ||
}, | ||
}, | ||
{ | ||
name: "two same nodes", | ||
nodes: []*types.NodeInfoReply{ | ||
{Name: "name", Enode: "enode"}, | ||
{Name: "name", Enode: "enode"}, | ||
}, | ||
want: []*types.NodeInfoReply{ | ||
{Name: "name", Enode: "enode"}, | ||
}, | ||
}, | ||
{ | ||
name: "three nodes", | ||
nodes: []*types.NodeInfoReply{ | ||
{Name: "name2", Enode: "enode2"}, | ||
{Name: "name", Enode: "enode"}, | ||
{Name: "name1", Enode: "enode1"}, | ||
}, | ||
want: []*types.NodeInfoReply{ | ||
{Name: "name", Enode: "enode"}, | ||
{Name: "name1", Enode: "enode1"}, | ||
{Name: "name2", Enode: "enode2"}, | ||
}, | ||
}, | ||
{ | ||
name: "three nodes with repeats", | ||
nodes: []*types.NodeInfoReply{ | ||
{Name: "name", Enode: "enode"}, | ||
{Name: "name1", Enode: "enode1"}, | ||
{Name: "name", Enode: "enode"}, | ||
}, | ||
want: []*types.NodeInfoReply{ | ||
{Name: "name", Enode: "enode"}, | ||
{Name: "name1", Enode: "enode1"}, | ||
}, | ||
}, | ||
{ | ||
name: "three same nodes", | ||
nodes: []*types.NodeInfoReply{ | ||
{Name: "name", Enode: "enode"}, | ||
{Name: "name", Enode: "enode"}, | ||
{Name: "name", Enode: "enode"}, | ||
}, | ||
want: []*types.NodeInfoReply{ | ||
{Name: "name", Enode: "enode"}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
eth := Ethereum{} | ||
|
||
for _, n := range tt.nodes { | ||
n := n | ||
eth.sentries = append(eth.sentries, | ||
direct.NewSentryClientRemote(&sentry.SentryClientMock{ | ||
NodeInfoFunc: func(context.Context, *emptypb.Empty, ...grpc.CallOption) (*types.NodeInfoReply, error) { | ||
return n, nil | ||
}, | ||
}), | ||
) | ||
} | ||
|
||
got, err := eth.NodesInfo(tt.limit) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
assert.Equal(t, tt.want, got.NodesInfo) | ||
}) | ||
} | ||
} |
Oops, something went wrong.