Skip to content

Commit

Permalink
Merge branch 'rampup' into optimization/verifier-proof-dedup
Browse files Browse the repository at this point in the history
  • Loading branch information
deepthiskumar authored Nov 21, 2023
2 parents 88529de + 44af5c2 commit 88fec9b
Show file tree
Hide file tree
Showing 24 changed files with 537 additions and 316 deletions.
2 changes: 1 addition & 1 deletion src/app/libp2p_helper/src/bitswap_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"fmt"

"github.com/ipfs/boxo/blockstore"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipfs/boxo/blockstore"
"github.com/ledgerwatch/lmdb-go/lmdb"
"github.com/multiformats/go-multihash"
lmdbbs "github.com/o1-labs/go-bs-lmdb"
Expand Down
131 changes: 93 additions & 38 deletions src/app/libp2p_helper/src/codanet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ import (
"sync"
"time"

"github.com/ipfs/boxo/bitswap"
"github.com/ipfs/boxo/bitswap"
bitnet "github.com/ipfs/boxo/bitswap/network"
dsb "github.com/ipfs/go-ds-badger"
logging "github.com/ipfs/go-log/v2"
p2p "github.com/libp2p/go-libp2p"

dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p-kad-dht/dual"
pubsub "github.com/libp2p/go-libp2p-pubsub"
record "github.com/libp2p/go-libp2p-record"
p2pconfig "github.com/libp2p/go-libp2p/config"
"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/control"
"github.com/libp2p/go-libp2p/core/crypto"
Expand All @@ -25,14 +30,9 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/core/routing"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p-kad-dht/dual"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoreds"
pubsub "github.com/libp2p/go-libp2p-pubsub"
record "github.com/libp2p/go-libp2p-record"
p2pconfig "github.com/libp2p/go-libp2p/config"
mdns "github.com/libp2p/go-libp2p/p2p/discovery/mdns"
discovery "github.com/libp2p/go-libp2p/p2p/discovery/routing"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoreds"
libp2pyamux "github.com/libp2p/go-libp2p/p2p/muxer/yamux"
p2pconnmgr "github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
Expand Down Expand Up @@ -89,26 +89,32 @@ func isPrivateAddr(addr ma.Multiaddr) bool {
}

type CodaConnectionManager struct {
p2pManager *p2pconnmgr.BasicConnMgr
OnConnect func(network.Network, network.Conn)
OnDisconnect func(network.Network, network.Conn)
p2pManager *p2pconnmgr.BasicConnMgr
onConnectMutex sync.RWMutex
onConnect func(network.Network, network.Conn)
onDisconnectMutex sync.RWMutex
onDisconnect func(network.Network, network.Conn)
// protectedMirror is a map of protected peer ids/tags, mirroring the structure in
// BasicConnMgr which is not accessible from CodaConnectionManager
protectedMirror map[peer.ID]map[string]interface{}
protectedMirrorLock sync.Mutex
}

func (cm *CodaConnectionManager) AddOnConnectHandler(f func(network.Network, network.Conn)) {
prevOnConnect := cm.OnConnect
cm.OnConnect = func(net network.Network, c network.Conn) {
cm.onConnectMutex.Lock()
defer cm.onConnectMutex.Unlock()
prevOnConnect := cm.onConnect
cm.onConnect = func(net network.Network, c network.Conn) {
prevOnConnect(net, c)
f(net, c)
}
}

func (cm *CodaConnectionManager) AddOnDisconnectHandler(f func(network.Network, network.Conn)) {
prevOnDisconnect := cm.OnDisconnect
cm.OnDisconnect = func(net network.Network, c network.Conn) {
cm.onDisconnectMutex.Lock()
defer cm.onDisconnectMutex.Unlock()
prevOnDisconnect := cm.onDisconnect
cm.onDisconnect = func(net network.Network, c network.Conn) {
prevOnDisconnect(net, c)
f(net, c)
}
Expand All @@ -122,8 +128,8 @@ func newCodaConnectionManager(minConnections, maxConnections int, grace time.Dur
}
return &CodaConnectionManager{
p2pManager: connmgr,
OnConnect: noop,
OnDisconnect: noop,
onConnect: noop,
onDisconnect: noop,
protectedMirror: make(map[peer.ID]map[string]interface{}),
}, nil
}
Expand Down Expand Up @@ -191,14 +197,27 @@ func (cm *CodaConnectionManager) Listen(net network.Network, addr ma.Multiaddr)
func (cm *CodaConnectionManager) ListenClose(net network.Network, addr ma.Multiaddr) {
cm.p2pManager.Notifee().ListenClose(net, addr)
}

func (cm *CodaConnectionManager) onConnectHandler() func(net network.Network, c network.Conn) {
cm.onConnectMutex.RLock()
defer cm.onConnectMutex.RUnlock()
return cm.onConnect
}

func (cm *CodaConnectionManager) Connected(net network.Network, c network.Conn) {
logger.Debugf("%s connected to %s", c.LocalPeer(), c.RemotePeer())
cm.OnConnect(net, c)
cm.onConnectHandler()(net, c)
cm.p2pManager.Notifee().Connected(net, c)
}

func (cm *CodaConnectionManager) onDisconnectHandler() func(net network.Network, c network.Conn) {
cm.onDisconnectMutex.RLock()
defer cm.onDisconnectMutex.RUnlock()
return cm.onDisconnect
}

func (cm *CodaConnectionManager) Disconnected(net network.Network, c network.Conn) {
cm.OnDisconnect(net, c)
cm.onDisconnectHandler()(net, c)
cm.p2pManager.Notifee().Disconnected(net, c)
}

Expand All @@ -224,7 +243,6 @@ type Helper struct {
ConnectionManager *CodaConnectionManager
BandwidthCounter *metrics.BandwidthCounter
MsgStats *MessageStats
Seeds []peer.AddrInfo
NodeStatus []byte
HeartbeatPeer func(peer.ID)
}
Expand Down Expand Up @@ -273,8 +291,28 @@ func (ms *MessageStats) GetStats() *safeStats {
}
}

func (h *Helper) ResetGatingConfigTrustedAddrFilters() {
h.gatingState.TrustedAddrFilters = ma.NewFilters()
func (h *Helper) SetBannedPeers(newP map[peer.ID]struct{}) {
h.gatingState.bannedPeersMutex.Lock()
defer h.gatingState.bannedPeersMutex.Unlock()
h.gatingState.bannedPeers = newP
}

func (h *Helper) SetTrustedPeers(newP map[peer.ID]struct{}) {
h.gatingState.trustedPeersMutex.Lock()
defer h.gatingState.trustedPeersMutex.Unlock()
h.gatingState.trustedPeers = newP
}

func (h *Helper) SetTrustedAddrFilters(newF *ma.Filters) {
h.gatingState.trustedAddrFiltersMutex.Lock()
defer h.gatingState.trustedAddrFiltersMutex.Unlock()
h.gatingState.trustedAddrFilters = newF
}

func (h *Helper) SetBannedAddrFilters(newF *ma.Filters) {
h.gatingState.bannedAddrFiltersMutex.Lock()
defer h.gatingState.bannedAddrFiltersMutex.Unlock()
h.gatingState.bannedAddrFilters = newF
}

// this type implements the ConnectionGating interface
Expand All @@ -283,10 +321,14 @@ func (h *Helper) ResetGatingConfigTrustedAddrFilters() {
type CodaGatingState struct {
logger logging.EventLogger
KnownPrivateAddrFilters *ma.Filters
BannedAddrFilters *ma.Filters
TrustedAddrFilters *ma.Filters
BannedPeers map[peer.ID]struct{}
TrustedPeers map[peer.ID]struct{}
bannedAddrFiltersMutex sync.RWMutex
bannedAddrFilters *ma.Filters
trustedAddrFiltersMutex sync.RWMutex
trustedAddrFilters *ma.Filters
bannedPeersMutex sync.RWMutex
bannedPeers map[peer.ID]struct{}
trustedPeersMutex sync.RWMutex
trustedPeers map[peer.ID]struct{}
}

type CodaGatingConfig struct {
Expand Down Expand Up @@ -322,11 +364,11 @@ func NewCodaGatingState(config *CodaGatingConfig, knownPrivateAddrFilters *ma.Fi

return &CodaGatingState{
logger: logger,
BannedAddrFilters: bannedAddrFilters,
TrustedAddrFilters: trustedAddrFilters,
bannedAddrFilters: bannedAddrFilters,
trustedAddrFilters: trustedAddrFilters,
KnownPrivateAddrFilters: knownPrivateAddrFilters,
BannedPeers: bannedPeers,
TrustedPeers: trustedPeers,
bannedPeers: bannedPeers,
trustedPeers: trustedPeers,
}
}

Expand All @@ -335,10 +377,10 @@ func (h *Helper) GatingState() *CodaGatingState {
}

func (h *Helper) SetGatingState(gs *CodaGatingConfig) {
h.gatingState.TrustedPeers = gs.TrustedPeers
h.gatingState.BannedPeers = gs.BannedPeers
h.gatingState.TrustedAddrFilters = gs.TrustedAddrFilters
h.gatingState.BannedAddrFilters = gs.BannedAddrFilters
h.SetTrustedPeers(gs.TrustedPeers)
h.SetBannedPeers(gs.BannedPeers)
h.SetTrustedAddrFilters(gs.TrustedAddrFilters)
h.SetBannedAddrFilters(gs.BannedAddrFilters)
for _, c := range h.Host.Network().Conns() {
pid := c.RemotePeer()
maddr := c.RemoteMultiaddr()
Expand All @@ -352,6 +394,12 @@ func (h *Helper) SetGatingState(gs *CodaGatingConfig) {
}
}

func (gs *CodaGatingState) TrustPeer(p peer.ID) {
gs.trustedPeersMutex.Lock()
defer gs.trustedPeersMutex.Unlock()
gs.trustedPeers[p] = struct{}{}
}

func (gs *CodaGatingState) MarkPrivateAddrAsKnown(addr ma.Multiaddr) {
if isPrivateAddr(addr) && gs.KnownPrivateAddrFilters.AddrBlocked(addr) {
gs.logger.Infof("marking private addr %v as known", addr)
Expand Down Expand Up @@ -397,15 +445,19 @@ func (c connectionAllowance) isDeny() bool {
}

func (gs *CodaGatingState) checkPeerTrusted(p peer.ID) connectionAllowance {
_, isTrusted := gs.TrustedPeers[p]
gs.trustedPeersMutex.RLock()
defer gs.trustedPeersMutex.RUnlock()
_, isTrusted := gs.trustedPeers[p]
if isTrusted {
return Accept
}
return Undecided
}

func (gs *CodaGatingState) checkPeerBanned(p peer.ID) connectionAllowance {
_, isBanned := gs.BannedPeers[p]
gs.bannedPeersMutex.RLock()
defer gs.bannedPeersMutex.RUnlock()
_, isBanned := gs.bannedPeers[p]
if isBanned {
return DenyBannedPeer
}
Expand Down Expand Up @@ -440,14 +492,18 @@ func (gs *CodaGatingState) checkAllowedPeer(p peer.ID) connectionAllowance {
}

func (gs *CodaGatingState) checkAddrTrusted(addr ma.Multiaddr) connectionAllowance {
if !gs.TrustedAddrFilters.AddrBlocked(addr) {
gs.trustedAddrFiltersMutex.RLock()
defer gs.trustedAddrFiltersMutex.RUnlock()
if !gs.trustedAddrFilters.AddrBlocked(addr) {
return Accept
}
return Undecided
}

func (gs *CodaGatingState) checkAddrBanned(addr ma.Multiaddr) connectionAllowance {
if gs.BannedAddrFilters.AddrBlocked(addr) {
gs.bannedAddrFiltersMutex.RLock()
defer gs.bannedAddrFiltersMutex.RUnlock()
if gs.bannedAddrFilters.AddrBlocked(addr) {
return DenyBannedAddress
}
return Undecided
Expand Down Expand Up @@ -721,7 +777,6 @@ func MakeHelper(ctx context.Context, listenOn []ma.Multiaddr, externalAddr ma.Mu
ConnectionManager: connManager,
BandwidthCounter: bandwidthCounter,
MsgStats: &MessageStats{min: math.MaxUint64},
Seeds: seeds,
HeartbeatPeer: func(p peer.ID) {
lanPatcher.Heartbeat(p)
wanPatcher.Heartbeat(p)
Expand Down
2 changes: 1 addition & 1 deletion src/app/libp2p_helper/src/codanet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestTrustedPrivateConnectionGating(t *testing.T) {
allowed := gs.InterceptAddrDial(testInfo.ID, testMa)
require.False(t, allowed)

gs.TrustedPeers[testInfo.ID] = struct{}{}
gs.TrustPeer(testInfo.ID)
allowed = gs.InterceptAddrDial(testInfo.ID, testMa)
require.True(t, allowed)
}
Expand Down
Loading

0 comments on commit 88fec9b

Please sign in to comment.