Skip to content

Commit

Permalink
filter before dialing
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic committed Jun 21, 2024
1 parent 6852231 commit c1c5beb
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 35 deletions.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,8 @@ type P2PConfig struct { //nolint: maligned
SameRegion bool `mapstructure:"same_region"`
MyRegion string `mapstructure:"my_region"`
MaxPercentPeersInSameRegion float64 `mapstructure:"max_percent_peers_in_same_region"`
CurrentNumOutboundPeersInOtherRegion float64 `mapstructure:"current_num_outbound_peers_in_other_region"`
CurrentNumInboundPeersInOtherRegion float64 `mapstructure:"current_num_inbound_peers_in_other_region"`
CurrentNumOutboundPeersInOtherRegion int `mapstructure:"current_num_outbound_peers_in_other_region"`
CurrentNumInboundPeersInOtherRegion int `mapstructure:"current_num_inbound_peers_in_other_region"`
}

// DefaultP2PConfig returns a default configuration for the peer-to-peer layer
Expand Down
138 changes: 105 additions & 33 deletions p2p/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,45 @@ func (sw *Switch) acceptRoutine() {

}

if sw.config.SameRegion {
// Note if the new peer is in the same region as us
fmt.Println("Checking if peer is same region. My region: ", sw.config.MyRegion, " Peer region: ", p.GetRegion())
isSameRegion := p.GetRegion() == sw.config.MyRegion
fmt.Println("isSameRegion", isSameRegion)

if !isSameRegion {
// If this peer is not in our same region and we have no room to dial peers outside of our region, return error
// TODO check this formula
if p.IsOutbound() {
fmt.Println("peer is outbound")
fmt.Println("sw.config.CurrentNumOutboundPeersInOtherRegion", sw.config.CurrentNumOutboundPeersInOtherRegion)
fmt.Println("sw.config.MaxPercentPeersInSameRegion", sw.config.MaxPercentPeersInSameRegion)
fmt.Println("sw.config.MaxNumOutboundPeers", sw.config.MaxNumOutboundPeers)
fmt.Println("sw.config.CurrentNumOutboundPeersInOtherRegion+1", sw.config.CurrentNumOutboundPeersInOtherRegion+1)
fmt.Println("1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumOutboundPeers", (1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumOutboundPeers))
maxOutboundPeersInOtherRegion := sw.config.MaxNumOutboundPeers - int(sw.config.MaxPercentPeersInSameRegion*float64(sw.config.MaxNumOutboundPeers))
if sw.config.CurrentNumOutboundPeersInOtherRegion+1 > maxOutboundPeersInOtherRegion {
sw.Logger.Error("exceeds max percent peers in same region")
continue
// return ErrRejected{id: p.ID(), err: fmt.Errorf("exceeds max percent peers in same region")}
}
} else {
fmt.Println("peer is inbound")
fmt.Println("sw.config.CurrentNumInboundPeersInOtherRegion", sw.config.CurrentNumInboundPeersInOtherRegion)
fmt.Println("sw.config.MaxPercentPeersInSameRegion", sw.config.MaxPercentPeersInSameRegion)
fmt.Println("sw.config.MaxNumInboundPeers", sw.config.MaxNumInboundPeers)
fmt.Println("sw.config.CurrentNumInboundPeersInOtherRegion+1", sw.config.CurrentNumInboundPeersInOtherRegion+1)
fmt.Println("1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumInboundPeers", (1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumInboundPeers))
maxInboundPeersInOtherRegion := sw.config.MaxNumInboundPeers - int(sw.config.MaxPercentPeersInSameRegion*float64(sw.config.MaxNumInboundPeers))
if sw.config.CurrentNumInboundPeersInOtherRegion+1 > maxInboundPeersInOtherRegion {
sw.Logger.Error("exceeds max percent peers in same region")
continue
// return ErrRejected{id: p.ID(), err: fmt.Errorf("exceeds max percent peers in same region")}
}
}
}
}

if err := sw.addPeer(p); err != nil {
sw.transport.Cleanup(p)
if p.IsRunning() {
Expand Down Expand Up @@ -793,6 +832,37 @@ func (sw *Switch) addOutboundPeerWithConfig(
// peerRegion = peerRegionInternal
// }

// Check if adding this peer would exceed the percentage of in/outbound peers in the same region
if sw.config.SameRegion {
// // Note if the new peer is in the same region as us
// fmt.Println("Checking if peer is same region. My region: ", sw.config.MyRegion, " Peer region: ", p.GetRegion())
// isSameRegion := p.GetRegion() == sw.config.MyRegion
// fmt.Println("isSameRegion", isSameRegion)
peerRegion, err := getRegionFromIP(addr.IP.String())
if err != nil {
sw.Logger.Error("Failed to get region from IP", "err", err)
return err
}
fmt.Println("peerRegion", peerRegion)
isSameRegion := peerRegion == sw.config.MyRegion

if !isSameRegion {
// If this peer is not in our same region and we have no room to dial peers outside of our region, return error
// TODO check this formula
fmt.Println("peer is outbound")
fmt.Println("sw.config.CurrentNumOutboundPeersInOtherRegion", sw.config.CurrentNumOutboundPeersInOtherRegion)
fmt.Println("sw.config.MaxPercentPeersInSameRegion", sw.config.MaxPercentPeersInSameRegion)
fmt.Println("sw.config.MaxNumOutboundPeers", sw.config.MaxNumOutboundPeers)
fmt.Println("sw.config.CurrentNumOutboundPeersInOtherRegion+1", sw.config.CurrentNumOutboundPeersInOtherRegion+1)
fmt.Println("1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumOutboundPeers", (1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumOutboundPeers))
maxOutboundPeersInOtherRegion := sw.config.MaxNumOutboundPeers - int(sw.config.MaxPercentPeersInSameRegion*float64(sw.config.MaxNumOutboundPeers))
if sw.config.CurrentNumOutboundPeersInOtherRegion+1 > maxOutboundPeersInOtherRegion {
return ErrRejected{id: ID(addr.ID), err: fmt.Errorf("exceeds max percent peers in same region")}
// return ErrRejected{id: p.ID(), err: fmt.Errorf("exceeds max percent peers in same region")}
}
}
}

p, err := sw.transport.Dial(*addr, peerConfig{
chDescs: sw.chDescs,
onPeerError: sw.StopPeerForError,
Expand Down Expand Up @@ -869,39 +939,41 @@ func (sw *Switch) filterPeer(p Peer) error {
return ErrRejected{id: p.ID(), isDuplicate: true}
}

// Check if adding this peer would exceed the percentage of in/outbound peers in the same region
if sw.config.SameRegion {
// Note if the new peer is in the same region as us
fmt.Println("Checking if peer is same region. My region: ", sw.config.MyRegion, " Peer region: ", p.GetRegion())
isSameRegion := p.GetRegion() == sw.config.MyRegion
fmt.Println("isSameRegion", isSameRegion)

if !isSameRegion {
// If this peer is not in our same region and we have no room to dial peers outside of our region, return error
// TODO check this formula
if p.IsOutbound() {
fmt.Println("peer is outbound")
fmt.Println("sw.config.CurrentNumOutboundPeersInOtherRegion", sw.config.CurrentNumOutboundPeersInOtherRegion)
fmt.Println("sw.config.MaxPercentPeersInSameRegion", sw.config.MaxPercentPeersInSameRegion)
fmt.Println("sw.config.MaxNumOutboundPeers", sw.config.MaxNumOutboundPeers)
fmt.Println("sw.config.CurrentNumOutboundPeersInOtherRegion+1", sw.config.CurrentNumOutboundPeersInOtherRegion+1)
fmt.Println("1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumOutboundPeers", (1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumOutboundPeers))
if sw.config.CurrentNumOutboundPeersInOtherRegion+1 > (1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumOutboundPeers) {
return ErrRejected{id: p.ID(), err: fmt.Errorf("exceeds max percent peers in same region")}
}
} else {
fmt.Println("peer is inbound")
fmt.Println("sw.config.CurrentNumInboundPeersInOtherRegion", sw.config.CurrentNumInboundPeersInOtherRegion)
fmt.Println("sw.config.MaxPercentPeersInSameRegion", sw.config.MaxPercentPeersInSameRegion)
fmt.Println("sw.config.MaxNumInboundPeers", sw.config.MaxNumInboundPeers)
fmt.Println("sw.config.CurrentNumInboundPeersInOtherRegion+1", sw.config.CurrentNumInboundPeersInOtherRegion+1)
fmt.Println("1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumInboundPeers", (1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumInboundPeers))
if sw.config.CurrentNumInboundPeersInOtherRegion+1 > (1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumInboundPeers) {
return ErrRejected{id: p.ID(), err: fmt.Errorf("exceeds max percent peers in same region")}
}
}
}
}
// // Check if adding this peer would exceed the percentage of in/outbound peers in the same region
// if sw.config.SameRegion {
// // Note if the new peer is in the same region as us
// fmt.Println("Checking if peer is same region. My region: ", sw.config.MyRegion, " Peer region: ", p.GetRegion())
// isSameRegion := p.GetRegion() == sw.config.MyRegion
// fmt.Println("isSameRegion", isSameRegion)

// if !isSameRegion {
// // If this peer is not in our same region and we have no room to dial peers outside of our region, return error
// // TODO check this formula
// if p.IsOutbound() {
// fmt.Println("peer is outbound")
// fmt.Println("sw.config.CurrentNumOutboundPeersInOtherRegion", sw.config.CurrentNumOutboundPeersInOtherRegion)
// fmt.Println("sw.config.MaxPercentPeersInSameRegion", sw.config.MaxPercentPeersInSameRegion)
// fmt.Println("sw.config.MaxNumOutboundPeers", sw.config.MaxNumOutboundPeers)
// fmt.Println("sw.config.CurrentNumOutboundPeersInOtherRegion+1", sw.config.CurrentNumOutboundPeersInOtherRegion+1)
// fmt.Println("1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumOutboundPeers", (1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumOutboundPeers))
// maxOutboundPeersInOtherRegion := sw.config.MaxNumOutboundPeers - int(sw.config.MaxPercentPeersInSameRegion*float64(sw.config.MaxNumOutboundPeers))
// if sw.config.CurrentNumOutboundPeersInOtherRegion+1 > maxOutboundPeersInOtherRegion {
// return ErrRejected{id: p.ID(), err: fmt.Errorf("exceeds max percent peers in same region")}
// }
// } else {
// fmt.Println("peer is inbound")
// fmt.Println("sw.config.CurrentNumInboundPeersInOtherRegion", sw.config.CurrentNumInboundPeersInOtherRegion)
// fmt.Println("sw.config.MaxPercentPeersInSameRegion", sw.config.MaxPercentPeersInSameRegion)
// fmt.Println("sw.config.MaxNumInboundPeers", sw.config.MaxNumInboundPeers)
// fmt.Println("sw.config.CurrentNumInboundPeersInOtherRegion+1", sw.config.CurrentNumInboundPeersInOtherRegion+1)
// fmt.Println("1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumInboundPeers", (1-sw.config.MaxPercentPeersInSameRegion)*float64(sw.config.MaxNumInboundPeers))
// maxInboundPeersInOtherRegion := sw.config.MaxNumInboundPeers - int(sw.config.MaxPercentPeersInSameRegion*float64(sw.config.MaxNumInboundPeers))
// if sw.config.CurrentNumInboundPeersInOtherRegion+1 > maxInboundPeersInOtherRegion {
// return ErrRejected{id: p.ID(), err: fmt.Errorf("exceeds max percent peers in same region")}
// }
// }
// }
// }

errc := make(chan error, len(sw.peerFilters))

Expand Down

0 comments on commit c1c5beb

Please sign in to comment.