Skip to content

Commit

Permalink
p2pnet: use identity tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
algorandskiy committed Jun 19, 2024
1 parent 8a97776 commit e529195
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
10 changes: 3 additions & 7 deletions network/hybridNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,9 @@ func NewHybridP2PNetwork(log logging.Logger, cfg config.Local, datadir string, p
if err != nil {
return nil, err
}
fmt.Printf("NewHybridP2PNetwork: p2pnet peerID: %v\n", p2pnet.PeerID())
// TODO: p2p peer ID not working since it is a domain string + pub key => will not match the challenge's peerID
// TODO: add NewIdentityChallengeScheme construction tests:
// 1 vs 2 DedupNames
// empty peerID, PublicAddress
// empty signer
identityOpts.scheme = NewIdentityChallengeScheme(DedupNames(cfg.PublicAddress, string(p2pnet.PeerID())), Signer(p2pnet.PeerIDSigner()))
fmt.Printf("NewHybridP2PNetwork: p2pnet peerID: %v\n", p2pnet.PeerID().String())
// identityOpts.scheme = NewIdentityChallengeScheme(DedupNames(cfg.PublicAddress, string(p2pnet.PeerID())), Signer(p2pnet.PeerIDSigner()))
identityOpts.scheme = NewIdentityChallengeScheme(DedupNames(cfg.PublicAddress, p2pnet.PeerID().String()), Signer(p2pnet.PeerIDSigner()))
wsnet, err := NewWebsocketNetwork(log, cfg, phonebookAddresses, genesisID, networkID, nodeInfo, &identityOpts)
if err != nil {
return nil, err
Expand Down
32 changes: 26 additions & 6 deletions network/p2pNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

"github.com/algorand/go-algorand/config"
algocrypto "github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/logging/telemetryspec"
"github.com/algorand/go-algorand/network/limitcaller"
Expand Down Expand Up @@ -790,28 +791,51 @@ func (n *P2PNetwork) wsStreamHandler(ctx context.Context, p2pPeer peer.ID, strea
ma := stream.Conn().RemoteMultiaddr()
addr := ma.String()
if addr == "" {
n.log.Warnf("Could not get address for peer %s", p2pPeer)
n.log.Warnf("Cannot get address for peer %s", p2pPeer)
}
// create a wsPeer for this stream and added it to the peers map.

// create a wsPeer for this stream and added it to the peers map.
addrInfo := &peer.AddrInfo{ID: p2pPeer, Addrs: []multiaddr.Multiaddr{ma}}
maxIdleConnsPerHost := int(n.config.ConnectionsRateLimitingCount)
client, err := p2p.MakeHTTPClientWithRateLimit(addrInfo, n.pstore, limitcaller.DefaultQueueingTimeout, maxIdleConnsPerHost)
if err != nil {
client = nil
}
var netIdentPeerID algocrypto.PublicKey
if p2pPeerPubKey, err0 := p2pPeer.ExtractPublicKey(); err0 == nil {
if b, err0 := p2pPeerPubKey.Raw(); err0 == nil {
netIdentPeerID = algocrypto.PublicKey(b)
} else {
n.log.Warnf("Cannot get raw pubkey for peer %s", p2pPeer)
}
} else {
n.log.Warnf("Cannot get pubkey for peer %s", p2pPeer)
}
peerCore := makePeerCore(ctx, n, n.log, n.handler.readBuffer, addr, client, addr)
wsp := &wsPeer{
wsPeerCore: peerCore,
conn: &wsPeerConnP2PImpl{stream: stream},
outgoing: !incoming,
identity: netIdentPeerID,
}
protos, err := n.pstore.GetProtocols(p2pPeer)
if err != nil {
n.log.Warnf("Error getting protocols for peer %s: %v", p2pPeer, err)
}
wsp.TelemetryGUID, wsp.InstanceName = p2p.GetPeerTelemetryInfo(protos)

localAddr, has := n.Address()
if !has {
n.log.Warn("Could not get local address")
}

ok := n.identityTracker.setIdentity(wsp)
if !ok {
networkPeerIdentityDisconnect.Inc(nil)
n.log.With("remote", addr).With("local", localAddr).Warn("peer deduplicated before adding because the identity is already known")
stream.Close()
}

wsp.init(n.config, outgoingMessagesBufferSize)
n.wsPeersLock.Lock()
n.wsPeers[p2pPeer] = wsp
Expand All @@ -825,10 +849,6 @@ func (n *P2PNetwork) wsStreamHandler(ctx context.Context, p2pPeer peer.ID, strea
event = "ConnectedIn"
msg = "Accepted incoming connection from peer %s"
}
localAddr, has := n.Address()
if !has {
n.log.Warn("Could not get local address")
}
n.log.With("event", event).With("remote", addr).With("local", localAddr).Infof(msg, p2pPeer.String())

if n.log.GetLevel() >= logging.Debug {
Expand Down

0 comments on commit e529195

Please sign in to comment.