Merge pull request #23 from getamis/feature/add-peer-in-each-validator

container: add peer in each validator
This commit is contained in:
bailantaotao 2017-08-21 11:39:50 +08:00 committed by GitHub
commit 2c403967a6
7 changed files with 77 additions and 11 deletions

View File

@ -82,7 +82,7 @@ func (bc *blockchain) Start() error {
}
}
return nil
return bc.connectAll()
}
func (bc *blockchain) Stop() error {
@ -167,3 +167,19 @@ func (bc *blockchain) setupValidators(keys []*ecdsa.PrivateKey, options ...Optio
bc.validators = append(bc.validators, geth)
}
}
func (bc *blockchain) connectAll() error {
for i, v := range bc.validators {
istClient := v.NewIstanbulClient()
for j, v := range bc.validators {
if i == j {
continue
}
err := istClient.AddPeer(context.Background(), v.NodeAddress())
if err != nil {
return err
}
}
}
return nil
}

View File

@ -29,7 +29,7 @@ func TestEthereumBlockchain(t *testing.T) {
DataDir("/data"),
WebSocket(),
WebSocketAddress("0.0.0.0"),
WebSocketAPI("eth,net,web3,personal"),
WebSocketAPI("admin,eth,net,web3,personal"),
WebSocketOrigin("*"),
NoDiscover(),
Logging(true),

View File

@ -40,6 +40,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/getamis/istanbul-tools/genesis"
"github.com/getamis/istanbul-tools/istclient"
)
const (
@ -57,6 +58,7 @@ type Ethereum interface {
ContainerID() string
Host() string
NewClient() *ethclient.Client
NewIstanbulClient() *istclient.Client
}
func NewEthereum(c *client.Client, options ...Option) *ethereum {
@ -331,6 +333,24 @@ func (eth *ethereum) NewClient() *ethclient.Client {
return client
}
func (eth *ethereum) NewIstanbulClient() *istclient.Client {
var scheme, port string
if eth.rpcPort != "" {
scheme = "http://"
port = eth.rpcPort
}
if eth.wsPort != "" {
scheme = "ws://"
port = eth.wsPort
}
client, err := istclient.Dial(scheme + eth.Host() + ":" + port)
if err != nil {
return nil
}
return client
}
func (eth *ethereum) NodeAddress() string {
if eth.node != nil {
return eth.node.String()

View File

@ -37,7 +37,7 @@ func TestEthereumContainer(t *testing.T) {
HostPort(freeport.GetPort()),
WebSocket(),
WebSocketAddress("0.0.0.0"),
WebSocketAPI("eth,net,web3,personal"),
WebSocketAPI("admin,eth,net,web3,personal"),
HostWebSocketPort(freeport.GetPort()),
WebSocketOrigin("*"),
NoDiscover(),

View File

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

View File

@ -19,6 +19,7 @@ package istclient
import (
"context"
"log"
"fmt"
"github.com/ethereum/go-ethereum/common"
)
@ -48,7 +49,7 @@ func ExampleProposerValidator() {
}
func ExampleGetValidators() {
client, err := Dial("ws://192.168.99.100:53257")
client, err := Dial("ws://127.0.0.1:53257")
if err != nil {
log.Fatal("failed to dial, err:", err)
}
@ -58,6 +59,35 @@ func ExampleGetValidators() {
log.Fatal("failed to get validators, err:", err)
}
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)
}
}

View File

@ -43,7 +43,7 @@ var _ = Describe("4 validators Istanbul", func() {
container.DataDir("/data"),
container.WebSocket(),
container.WebSocketAddress("0.0.0.0"),
container.WebSocketAPI("eth,net,web3,personal,miner"),
container.WebSocketAPI("admin,eth,net,web3,personal,miner"),
container.WebSocketOrigin("*"),
container.NAT("any"),
container.NoDiscover(),