Skip to content

Commit

Permalink
simulators/eth2: Add bn multiaddrs in $HIVE_ETH2_STATIC_PEERS (ethere…
Browse files Browse the repository at this point in the history
…um#641)

* simulators/eth2/testnet: Add bn multiaddrs in $HIVE_ETH2_STATIC_PEERS

* clients/teku: Use $HIVE_ETH2_STATIC_PEERS for persistent BN connection

* simulators/eth2/engine: add static peers

Co-authored-by: marioevz <[email protected]>
  • Loading branch information
zilm13 and marioevz authored Sep 1, 2022
1 parent 619a7dd commit f75003c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
4 changes: 3 additions & 1 deletion clients/teku-bn/teku_bn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ case "$HIVE_LOGLEVEL" in
esac

echo "bootnodes: ${HIVE_ETH2_BOOTNODE_ENRS}"
echo "staticpeers: ${HIVE_ETH2_STATIC_PEERS}"

CONTAINER_IP=`hostname -i | awk '{print $1;}'`
eth1_option=$([[ "$HIVE_ETH2_ETH1_RPC_ADDRS" == "" ]] && echo "" || echo "--eth1-endpoints=$HIVE_ETH2_ETH1_RPC_ADDRS")
metrics_option=$([[ "$HIVE_ETH2_METRICS_PORT" == "" ]] && echo "" || echo "--metrics-enabled=true --metrics-interface=0.0.0.0 --metrics-port=$HIVE_ETH2_METRICS_PORT --metrics-host-allowlist=*")
enr_option=$([[ "$HIVE_ETH2_BOOTNODE_ENRS" == "" ]] && echo "" || echo --p2p-discovery-bootnodes="$HIVE_ETH2_BOOTNODE_ENRS")
static_option=$([[ "$HIVE_ETH2_STATIC_PEERS" == "" ]] && echo "" || echo --p2p-static-peers="$HIVE_ETH2_STATIC_PEERS")
opt_sync_option=$([[ "$HIVE_ETH2_SAFE_SLOTS_TO_IMPORT_OPTIMISTICALLY" == "" ]] && echo "" || echo "--Xnetwork-safe-slots-to-import-optimistically=$HIVE_ETH2_SAFE_SLOTS_TO_IMPORT_OPTIMISTICALLY")

if [ "$HIVE_ETH2_MERGE_ENABLED" != "" ]; then
Expand All @@ -59,7 +61,7 @@ echo Starting Teku Beacon Node
--eth1-deposit-contract-address="${HIVE_ETH2_CONFIG_DEPOSIT_CONTRACT_ADDRESS:-0x1111111111111111111111111111111111111111}" \
--log-destination console \
--logging="$LOG" \
$metrics_option $eth1_option $merge_option $enr_option $opt_sync_option \
$metrics_option $eth1_option $merge_option $enr_option $static_option $opt_sync_option \
--validators-proposer-default-fee-recipient="0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b" \
--p2p-port="${HIVE_ETH2_P2P_TCP_PORT:-9000}" \
--p2p-udp-port="${HIVE_ETH2_P2P_UDP_PORT:-9000}" \
Expand Down
27 changes: 27 additions & 0 deletions simulators/eth2/engine/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ func (bn *BeaconClient) ENR() (string, error) {
return out.ENR, nil
}

func (bn *BeaconClient) P2PAddr() (string, error) {
ctx, _ := context.WithTimeout(context.Background(), time.Second*10)
var out eth2api.NetworkIdentity
if err := nodeapi.Identity(ctx, bn.API, &out); err != nil {
return "", err
}
return out.P2PAddresses[0], nil
}

func (bn *BeaconClient) EnodeURL() (string, error) {
return "", errors.New("beacon node does not have an discv4 Enode URL, use ENR or multi-address instead")
}
Expand Down Expand Up @@ -265,6 +274,24 @@ func (beacons BeaconClients) ENRs() (string, error) {
return strings.Join(enrs, ","), nil
}

// Returns comma-separated P2PAddr of all running beacon nodes
func (beacons BeaconClients) P2PAddrs() (string, error) {
if len(beacons) == 0 {
return "", nil
}
staticPeers := make([]string, 0)
for _, bn := range beacons {
if bn.IsRunning() {
p2p, err := bn.P2PAddr()
if err != nil {
return "", err
}
staticPeers = append(staticPeers, p2p)
}
}
return strings.Join(staticPeers, ","), nil
}

func (b *BeaconClient) PrintAllBeaconBlocks(ctx context.Context) error {
var headInfo eth2api.BeaconBlockHeaderAndInfo
if exists, err := beaconapi.BlockHeader(ctx, b.API, eth2api.BlockHead, &headInfo); err != nil {
Expand Down
12 changes: 8 additions & 4 deletions simulators/eth2/engine/prepared_testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,18 @@ func (p *PreparedTestnet) prepareBeaconNode(testnet *Testnet, beaconDef *hivesim
"HIVE_ETH2_BEACON_NODE_INDEX": fmt.Sprintf("%d", beaconIndex),
})

bootnodeENRs, err := testnet.BeaconClients().Running().ENRs()
if err != nil {
if bootnodeENRs, err := testnet.BeaconClients().Running().ENRs(); err != nil {
return nil, fmt.Errorf("failed to get ENR as bootnode for every beacon node: %v", err)
}
if bootnodeENRs != "" {
} else if bootnodeENRs != "" {
opts = append(opts, hivesim.Params{"HIVE_ETH2_BOOTNODE_ENRS": bootnodeENRs})
}

if staticPeers, err := testnet.BeaconClients().Running().P2PAddrs(); err != nil {
return nil, fmt.Errorf("failed to get p2paddr for every beacon node: %v", err)
} else if staticPeers != "" {
opts = append(opts, hivesim.Params{"HIVE_ETH2_STATIC_PEERS": staticPeers})
}

if ttd != nil {
opts = append(opts, hivesim.Params{"HIVE_TERMINAL_TOTAL_DIFFICULTY": fmt.Sprintf("%d", ttd)})
}
Expand Down
9 changes: 9 additions & 0 deletions simulators/eth2/testnet/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ func (bn *BeaconNode) ENR() (string, error) {
return out.ENR, nil
}

func (bn *BeaconNode) P2PAddr() (string, error) {
ctx, _ := context.WithTimeout(context.Background(), time.Second*10)
var out eth2api.NetworkIdentity
if err := nodeapi.Identity(ctx, bn.API, &out); err != nil {
return "", err
}
return out.P2PAddresses[0], nil
}

func (bn *BeaconNode) EnodeURL() (string, error) {
return "", errors.New("beacon node does not have an discv4 Enode URL, use ENR or multi-address instead")
}
Expand Down
12 changes: 12 additions & 0 deletions simulators/eth2/testnet/prepared_testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ func (p *PreparedTestnet) startBeaconNode(testnet *Testnet, beaconDef *hivesim.C
opts = append(opts, hivesim.Params{"HIVE_ETH2_BOOTNODE_ENRS": bootnodeENR})
}

if len(testnet.beacons) > 0 {
var staticPeers []string
for _, bn := range testnet.beacons {
staticPeer, err := bn.P2PAddr()
if err != nil {
testnet.t.Fatalf("failed to get multiaddr for beacon node: %v", err)
}
staticPeers = append(staticPeers, staticPeer)
}
opts = append(opts, hivesim.Params{"HIVE_ETH2_STATIC_PEERS": strings.Join(staticPeers, ",")})
}

// TODO
//if p.configName != "mainnet" && hasBuildTarget(beaconDef, p.configName) {
// opts = append(opts, hivesim.WithBuildTarget(p.configName))
Expand Down

0 comments on commit f75003c

Please sign in to comment.