istclient: cast the response by *p2p.PeerInfo

This commit is contained in:
Edwin 2017-08-19 15:59:22 +08:00
parent f99cf47060
commit 9c0fa56cfc
2 changed files with 37 additions and 7 deletions

View File

@ -23,6 +23,7 @@ import (
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
@ -55,15 +56,14 @@ func (ic *Client) AddPeer(ctx context.Context, nodeURL string) error {
return err return err
} }
func (ic *Client) AdminPeers(ctx context.Context) error { func (ic *Client) AdminPeers(ctx context.Context) ([]*p2p.PeerInfo, error) {
var r bool var r []*p2p.PeerInfo
// TODO: Result needs to be verified
// The response data type are bytes, but we cannot parse... // The response data type are bytes, but we cannot parse...
err := ic.c.CallContext(ctx, &r, "admin_peers") err := ic.c.CallContext(ctx, &r, "admin_peers")
if err != nil { if err != nil {
return err return nil, err
} }
return err return r, err
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -19,6 +19,7 @@ package istclient
import ( import (
"context" "context"
"log" "log"
"fmt"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
@ -48,7 +49,7 @@ func ExampleProposerValidator() {
} }
func ExampleGetValidators() { func ExampleGetValidators() {
client, err := Dial("ws://192.168.99.100:53257") client, err := Dial("ws://127.0.0.1:53257")
if err != nil { if err != nil {
log.Fatal("failed to dial, err:", err) log.Fatal("failed to dial, err:", err)
} }
@ -58,6 +59,35 @@ func ExampleGetValidators() {
log.Fatal("failed to get validators, err:", err) log.Fatal("failed to get validators, err:", err)
} }
for _, addr := range addrs { for _, addr := range addrs {
log.Println("address:",addr.Hex()) log.Println("address:", addr.Hex())
}
}
func ExampleAdminPeers() {
client, err := Dial("ws://127.0.0.1:62975")
if err != nil {
log.Fatal("failed to dial, err:", err)
}
peersInfo, err := client.AdminPeers(context.Background())
if err != nil {
log.Fatal("failed to get validators, err:", err)
}
fmt.Println("connected peer length:", len(peersInfo))
for _, peer := range peersInfo {
fmt.Println("address:", peer)
}
}
func ExampleAddPeer() {
client, err := Dial("ws://127.0.0.1:62975")
if err != nil {
log.Fatal("failed to dial, err:", err)
}
err = client.AddPeer(context.Background(), "enode://ad5b4b201cc0ef5cd6ce27e32c223d1852a8b7d6069de5c3c597601e94841a5811a354261726da7b8f851e9042d5aeaed580dbb7493d22a5d922206dce3ccdb8@192.168.99.100:63040?discport=0")
if err != nil {
log.Fatal("failed to get validators, err:", err)
} }
} }