Skip to content
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

Mario/p2p #2258

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ node3:
--p2p-private-key="54a695e2a5d5717d5ba8730efcafe6f17251a1955733cffc55a4085fbf7f5d2c1b4009314092069ef7ca9b364ce3eb3072531c64dfb2799c6bad76720a5bdff0" \
--metrics-port=9093

nodep2p1: juno-cached
./build/juno \
--network=sepolia \
--disable-l1-verification \
--log-level=info \
--db-path=./p2p-dbs/nodep2p1 \
--p2p \
--p2p-peers=/ip4/35.231.95.227/tcp/7777/p2p/12D3KooWNKz9BJmyWVFUnod6SQYLG4dYZNhs3GrMpiot63Y1DLYS \
--p2p-private-key=5f6cdc3aebcc74af494df054876100368ef6126e3a33fa65b90c765b381ffc37a0a63bbeeefab0740f24a6a38dabb513b9233254ad0020c721c23e69bc820089

pathfinder: juno-cached
./build/juno \
--network=sepolia \
Expand Down
103 changes: 76 additions & 27 deletions p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package p2p
import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"strings"
"sync"
"time"
Expand All @@ -26,6 +29,7 @@ import (
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/multiformats/go-multiaddr"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -73,6 +77,37 @@ func New(addr, publicAddr, version, peers, privKeyStr string, feederNode bool, b
if err != nil {
return nil, err
}
} else {
timeout := 5 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://ip-api.com/json/", http.NoBody)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var ip struct {
Query string
}
err = json.Unmarshal(body, &ip)
if err != nil {
return nil, err
}
guessedPublicAddr := ip.Query
addrPort := addr[strings.LastIndex(addr, "/")+1:]
guessedPublicAddr = fmt.Sprintf("/ip4/%s/tcp/%s", guessedPublicAddr, addrPort)
publicMultiAddr, err = multiaddr.NewMultiaddr(guessedPublicAddr)
if err != nil {
return nil, err
}
}

prvKey, err := privateKey(privKeyStr)
Expand All @@ -92,15 +127,34 @@ func New(addr, publicAddr, version, peers, privKeyStr string, feederNode bool, b
return addrs
}

var peersAddrInfoS []peer.AddrInfo
peersAddrInfoS, err = loadPeers(database)
if err != nil {
log.Warnw("Failed to load peers", "err", err)
}
splitted := strings.Split(peers, ",")
for _, peerStr := range splitted {
var peerAddr *peer.AddrInfo
peerAddr, err = peer.AddrInfoFromString(peerStr)
if err != nil {
return nil, fmt.Errorf("addr info from %q: %w", peerStr, err)
}

peersAddrInfoS = append(peersAddrInfoS, *peerAddr)
}

p2pHost, err := libp2p.New(
libp2p.ListenAddrs(sourceMultiAddr),
libp2p.Identity(prvKey),
libp2p.UserAgent(makeAgentName(version)),
// Use address factory to add the public address to the list of
// addresses that the node will advertise.
libp2p.AddrsFactory(addressFactory),
// If we know the public ip, enable the relay service.
libp2p.EnableRelayService(),
// Automatically advertise the relay'ed address when it detects that
// the node is running behind a NAT.
libp2p.EnableAutoRelayWithStaticRelays(peersAddrInfoS),
// // If we know the public ip, enable the relay service.
// libp2p.EnableRelayService(),
// When listening behind NAT, enable peers to try to poke thought the
// NAT in order to reach the node.
libp2p.EnableHolePunching(),
Expand All @@ -110,38 +164,33 @@ func New(addr, publicAddr, version, peers, privKeyStr string, feederNode bool, b
if err != nil {
return nil, err
}
// Update public address port to match the listen address.
if publicAddr == "" {
// Update the port of publicMultiAddr to match the port of p2pHost.Addrs()[0]
port := p2pHost.Addrs()[0].String()
port = port[strings.LastIndex(port, "/")+1:]
publicMultiAddr, err = multiaddr.NewMultiaddr(strings.ReplaceAll(publicMultiAddr.String(), "/tcp/0", "/tcp/" + port))
if err != nil {
return nil, err
}
}

// Force all nodes to be able to act as a relay node
_, err = relay.New(p2pHost)
if err != nil {
log.Warnw("Failed to instantiate the relay: %v", err)
}

// Todo: try to understand what will happen if user passes a multiaddr with p2p public and a private key which doesn't match.
// For example, a user passes the following multiaddr: --p2p-addr=/ip4/0.0.0.0/tcp/7778/p2p/(SomePublicKey) and also passes a
// --p2p-private-key="SomePrivateKey". However, the private public key pair don't match, in this case what will happen?
return NewWithHost(p2pHost, peers, feederNode, bc, snNetwork, log, database)
return NewWithHost(p2pHost, peersAddrInfoS, feederNode, bc, snNetwork, log, database)
}

func NewWithHost(p2phost host.Host, peers string, feederNode bool, bc *blockchain.Blockchain, snNetwork *utils.Network,
func NewWithHost(p2phost host.Host, peersAddrInfoS []peer.AddrInfo, feederNode bool, bc *blockchain.Blockchain, snNetwork *utils.Network,
log utils.SimpleLogger, database db.DB,
) (*Service, error) {
var (
peersAddrInfoS []peer.AddrInfo
err error
)

peersAddrInfoS, err = loadPeers(database)
if err != nil {
log.Warnw("Failed to load peers", "err", err)
}

if peers != "" {
splitted := strings.Split(peers, ",")
for _, peerStr := range splitted {
var peerAddr *peer.AddrInfo
peerAddr, err = peer.AddrInfoFromString(peerStr)
if err != nil {
return nil, fmt.Errorf("addr info from %q: %w", peerStr, err)
}

peersAddrInfoS = append(peersAddrInfoS, *peerAddr)
}
}

var err error
p2pdht, err := makeDHT(p2phost, peersAddrInfoS)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions p2p/starknet/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"iter"
"sync"
"time"

"github.com/NethermindEth/juno/adapters/core2p2p"
"github.com/NethermindEth/juno/adapters/p2p2core"
Expand Down Expand Up @@ -442,6 +443,8 @@ func (h *Handler) processIterationRequestMulti(iteration *spec.Iteration, finMsg

type yieldFunc = func(proto.Message) bool
return func(yield yieldFunc) {
time.Sleep(3 * time.Second)
return
// while iterator is valid
for it.Valid() {
// pass it to handler function (some might be interested in header, others in entire block)
Expand Down
8 changes: 6 additions & 2 deletions p2p/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (s *syncService) start(ctx context.Context) {
continue
}

s.sleep(3 * time.Second)
s.log.Infow("Start Pipeline", "Current height", nextHeight-1, "Start", nextHeight)

// todo change iteration to fetch several objects uint64(min(blockBehind, maxBlocks))
Expand Down Expand Up @@ -125,6 +126,8 @@ func (s *syncService) processBlock(ctx context.Context, blockNumber uint64) erro
)))

for b := range blocksCh {
continue

if b.err != nil {
return fmt.Errorf("failed to process block: %w", b.err)
}
Expand Down Expand Up @@ -636,8 +639,9 @@ func (s *syncService) randomPeer() peer.ID {

p := peers[rand.Intn(len(peers))] //nolint:gosec

s.log.Debugw("Number of peers", "len", len(peers))
s.log.Debugw("Random chosen peer's info", "peerInfo", store.PeerInfo(p))
s.log.Infow("Number of peers", "len", len(peers))
s.log.Infow("Peers", "peers", peers)
s.log.Infow("Random chosen peer's info", "peerInfo", store.PeerInfo(p))

return p
}
Expand Down
Loading