-
Notifications
You must be signed in to change notification settings - Fork 12
ETCM-416: Limit entries from the same subnet in the K-table #109
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
db16ed5
ETCM-416: Added InetAddressOps.truncate
aakoshh d73d1d0
ETCM-416: Wrap KBuckets to track subnet limits.
aakoshh f741ddb
ETCM-416: Implement subnet limits.
aakoshh 4d71734
ETCM-416: Test no-limits.
aakoshh 90ecded
Merge remote-tracking branch 'origin/develop' into ETCM-416-limit-sub…
aakoshh cd01758
ETCM-416: Disable subnet limits in tests.
aakoshh 6c968d8
Merge remote-tracking branch 'origin/develop' into ETCM-416-limit-sub…
aakoshh 18ef43f
ETCM-416: Don't filter twice when removing by ID.
aakoshh 75dadac
ETCM-416: Split up the count maintenance logic.
aakoshh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/KBucketsWithSubnetLimits.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| package io.iohk.scalanet.discovery.ethereum.v4 | ||
|
|
||
| import cats._ | ||
| import cats.implicits._ | ||
| import io.iohk.scalanet.discovery.hash.Hash | ||
| import io.iohk.scalanet.discovery.ethereum.Node | ||
| import io.iohk.scalanet.kademlia.{KBuckets, TimeSet} | ||
| import io.iohk.scalanet.peergroup.Addressable | ||
| import io.iohk.scalanet.peergroup.InetAddressOps._ | ||
| import java.net.InetAddress | ||
|
|
||
| case class KBucketsWithSubnetLimits[A: Addressable]( | ||
| table: KBuckets[Hash], | ||
| limits: KBucketsWithSubnetLimits.SubnetLimits, | ||
| tableLevelCounts: KBucketsWithSubnetLimits.TableLevelCounts, | ||
| bucketLevelCounts: KBucketsWithSubnetLimits.BucketLevelCounts | ||
| ) { | ||
| import DiscoveryNetwork.Peer | ||
| import KBucketsWithSubnetLimits._ | ||
|
|
||
| def contains(peer: Peer[A]): Boolean = | ||
| table.contains(peer.kademliaId) | ||
|
|
||
| def touch(peer: Peer[A]): KBucketsWithSubnetLimits[A] = | ||
| // Note that `KBuckets.touch` also adds, so if the the record | ||
| // isn't in the table already then use `add` to maintain counts. | ||
| if (contains(peer)) copy(table = table.touch(peer.kademliaId)) else add(peer) | ||
|
|
||
| /** Add the peer to the underlying K-table unless doing so would violate some limit. */ | ||
| def add(peer: Peer[A]): KBucketsWithSubnetLimits[A] = | ||
| if (contains(peer)) this | ||
| else { | ||
| val ip = subnet(peer) | ||
| val idx = getBucket(peer)._1 | ||
|
|
||
| // Upsert the counts of the index and/or IP in the maps, so that we can check the limits on them. | ||
| val tlc = incrementForTable(ip) | ||
| val blc = incrementForBucket(idx, ip) | ||
|
|
||
| val isOverAnyLimit = | ||
| limits.isOverLimitForTable(tlc(ip)) || | ||
| limits.isOverLimitForBucket(blc(idx)(ip)) | ||
|
|
||
| if (isOverAnyLimit) this | ||
| else { | ||
| copy( | ||
| table = table.add(peer.kademliaId), | ||
| tableLevelCounts = tlc, | ||
| bucketLevelCounts = blc | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| def remove(peer: Peer[A]): KBucketsWithSubnetLimits[A] = | ||
| if (!contains(peer)) this | ||
| else { | ||
| val ip = subnet(peer) | ||
| val idx = getBucket(peer)._1 | ||
|
|
||
| val tlc = decrementForTable(ip) | ||
| val blc = decrementForBucket(idx, ip) | ||
|
|
||
| copy(table = table.remove(peer.kademliaId), tableLevelCounts = tlc, bucketLevelCounts = blc) | ||
| } | ||
|
|
||
| def closestNodes(targetKademliaId: Hash, n: Int): List[Hash] = | ||
| table.closestNodes(targetKademliaId, n) | ||
|
|
||
| def getBucket(peer: Peer[A]): (Int, TimeSet[Hash]) = | ||
| table.getBucket(peer.kademliaId) | ||
|
|
||
| private def subnet(peer: Peer[A]): InetAddress = | ||
| Addressable[A].getAddress(peer.address).getAddress.truncate(limits.prefixLength) | ||
|
|
||
| /** Increase the table level count for the IP of a subnet. */ | ||
| private def incrementForTable(ip: InetAddress): TableLevelCounts = | ||
| tableLevelCounts |+| Map(ip -> 1) | ||
|
|
||
| /** Increase the bucket level count for the IP of a subnet. */ | ||
| private def incrementForBucket(idx: Int, ip: InetAddress): BucketLevelCounts = | ||
| bucketLevelCounts |+| Map(idx -> Map(ip -> 1)) | ||
|
|
||
| /** Decrement the table level count for the IP of a subnet and remove the entry if it's zero. */ | ||
| private def decrementForTable(ip: InetAddress): TableLevelCounts = | ||
| tableLevelCounts |+| Map(ip -> -1) match { | ||
| case counts if counts(ip) <= 0 => counts - ip | ||
| case counts => counts | ||
| } | ||
|
|
||
| /** Decrement the bucket level count for the IP of a subnet and remove the entry if it's zero | ||
| * for the subnet itself, or the whole bucket. | ||
| */ | ||
| private def decrementForBucket(idx: Int, ip: InetAddress): BucketLevelCounts = | ||
| bucketLevelCounts |+| Map(idx -> Map(ip -> -1)) match { | ||
| case counts if counts(idx)(ip) <= 0 && counts(idx).size > 1 => | ||
| // The subnet count in the bucket is zero, but there are other subnets in the bucket, | ||
| // so keep the bucket level count and just remove the subnet from it. | ||
| counts.updated(idx, counts(idx) - ip) | ||
| case counts if counts(idx)(ip) <= 0 => | ||
| // The subnet count is zero, and it's the only subnet in the bucket, so remove the bucket. | ||
| counts - idx | ||
| case counts => | ||
| counts | ||
| } | ||
| } | ||
|
|
||
| object KBucketsWithSubnetLimits { | ||
| type SubnetCounts = Map[InetAddress, Int] | ||
| type TableLevelCounts = SubnetCounts | ||
| type BucketLevelCounts = Map[Int, SubnetCounts] | ||
|
|
||
| case class SubnetLimits( | ||
| // Number of leftmost bits of the IP address that counts as a subnet, serving as its ID. | ||
| prefixLength: Int, | ||
| // Limit of nodes from the same subnet within any given bucket in the K-table. | ||
| forBucket: Int, | ||
| // Limit of nodes from the same subnet across all buckets in the K-table. | ||
| forTable: Int | ||
| ) { | ||
|
|
||
| /** All limits can be disabled by setting the subnet prefix length to 0. */ | ||
| def isEnabled: Boolean = prefixLength > 0 | ||
|
|
||
| def isEnabledForBucket: Boolean = | ||
| isEnabled && forBucket > 0 | ||
|
|
||
| def isEnabledForTable: Boolean = | ||
| isEnabled && forTable > 0 | ||
|
|
||
| def isOverLimitForBucket(count: Int): Boolean = | ||
| isEnabledForBucket && count > forBucket | ||
|
|
||
| def isOverLimitForTable(count: Int): Boolean = | ||
| isEnabledForTable && count > forTable | ||
| } | ||
|
|
||
| object SubnetLimits { | ||
| val Unlimited = SubnetLimits(0, 0, 0) | ||
|
|
||
| def fromConfig(config: DiscoveryConfig): SubnetLimits = | ||
| SubnetLimits( | ||
| prefixLength = config.subnetLimitPrefixLength, | ||
| forBucket = config.subnetLimitForBucket, | ||
| forTable = config.subnetLimitForTable | ||
| ) | ||
| } | ||
|
|
||
| def apply[A: Addressable]( | ||
| node: Node, | ||
| limits: SubnetLimits | ||
| ): KBucketsWithSubnetLimits[A] = { | ||
| KBucketsWithSubnetLimits[A]( | ||
| new KBuckets[Hash](node.kademliaId, clock = java.time.Clock.systemUTC()), | ||
| limits, | ||
| tableLevelCounts = Map.empty[InetAddress, Int], | ||
| bucketLevelCounts = Map.empty[Int, Map[InetAddress, Int]] | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems wasteful that we are itereating
lastPongTimestampMapandbondingResultsMapmaps two times here when finding all records of peer, and later when fitering this peer out. From what i known this collections can be fairly large, maybe we could do it in one pass ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I can change that. But this method won't be actually called, I just kept it for backwards compatibility with KRouter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.