container: connect all validators

This commit is contained in:
Edwin 2017-08-19 16:00:59 +08:00
parent 9c0fa56cfc
commit 28ac722bb2
5 changed files with 40 additions and 4 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

@ -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(),