Skip to content

P2P handshake handling #2306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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