Skip to content

Commit

Permalink
P2P handshake handling (#2306)
Browse files Browse the repository at this point in the history
* proto: illegal wireType 7 :(

* set addr to todo for now so somethign gets sent

* push latest progress

* Add feedback from @raulk. Stream never connects

* working handshake handler

* add exclusions for relay/bootstrap node

* fix tests, still need to add new ones

* remove race, fails coverage

* Add test for negotiation

* gazelle

* regen pb

* Update shared/p2p/handshake_handler.go

Co-Authored-By: prestonvanloon <preston@prysmaticlabs.com>
  • Loading branch information
prestonvanloon authored and rauljordan committed Apr 27, 2019
1 parent 7a04af7 commit 210edfc
Show file tree
Hide file tree
Showing 13 changed files with 515 additions and 95 deletions.
1 change: 1 addition & 0 deletions beacon-chain/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"fetch_contract_address.go",
"node.go",
"p2p_config.go",
],
Expand Down
41 changes: 41 additions & 0 deletions beacon-chain/node/fetch_contract_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package node

import (
"io/ioutil"
"net/http"
"sync"

"github.com/prysmaticlabs/prysm/shared/params"
)

var cachedDepositAddress string
var fetchLock sync.Mutex

// fetchDepositContract from the cluster endpoint.
func fetchDepositContract() (string, error) {
fetchLock.Lock()
defer fetchLock.Unlock()

if cachedDepositAddress != "" {
return cachedDepositAddress, nil
}

log.WithField(
"endpoint",
params.BeaconConfig().TestnetContractEndpoint,
).Info("Fetching testnet cluster address")
resp, err := http.Get(params.BeaconConfig().TestnetContractEndpoint)
if err != nil {
return "", err
}
contractResponse, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if err := resp.Body.Close(); err != nil {
return "", err
}

cachedDepositAddress = string(contractResponse)
return cachedDepositAddress, nil
}
19 changes: 3 additions & 16 deletions beacon-chain/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package node
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"path"
Expand Down Expand Up @@ -248,22 +246,11 @@ func (b *BeaconNode) registerPOWChainService(cliCtx *cli.Context) error {
depAddress := cliCtx.GlobalString(utils.DepositContractFlag.Name)

if depAddress == "" {
log.Infof("Fetching testnet cluster address from %s...", params.BeaconConfig().TestnetContractEndpoint)
resp, err := http.Get(params.BeaconConfig().TestnetContractEndpoint)
var err error
depAddress, err = fetchDepositContract()
if err != nil {
log.Fatalf("Could not get latest deposit contract address: %v", err)
log.WithError(err).Fatal("Cannot fetch deposit contract")
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Fatal(err)
}
}()

contractResponse, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
depAddress = string(contractResponse)
}

if !common.IsHexAddress(depAddress) {
Expand Down
19 changes: 15 additions & 4 deletions beacon-chain/node/p2p_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package node

import (
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/utils"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/p2p"
Expand All @@ -27,11 +28,21 @@ var topicMappings = map[pb.Topic]proto.Message{
}

func configureP2P(ctx *cli.Context) (*p2p.Server, error) {
contractAddress := ctx.GlobalString(utils.DepositContractFlag.Name)
if contractAddress == "" {
var err error
contractAddress, err = fetchDepositContract()
if err != nil {
return nil, err
}
}

s, err := p2p.NewServer(&p2p.ServerConfig{
NoDiscovery: ctx.GlobalBool(cmd.NoDiscovery.Name),
BootstrapNodeAddr: ctx.GlobalString(cmd.BootstrapNode.Name),
RelayNodeAddr: ctx.GlobalString(cmd.RelayNode.Name),
Port: ctx.GlobalInt(cmd.P2PPort.Name),
NoDiscovery: ctx.GlobalBool(cmd.NoDiscovery.Name),
BootstrapNodeAddr: ctx.GlobalString(cmd.BootstrapNode.Name),
RelayNodeAddr: ctx.GlobalString(cmd.RelayNode.Name),
Port: ctx.GlobalInt(cmd.P2PPort.Name),
DepositContractAddress: contractAddress,
})
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 210edfc

Please sign in to comment.