-
Notifications
You must be signed in to change notification settings - Fork 422
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
NODE-2596 Optimised active leases #3884
Merged
Merged
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
110690c
Clean-up
xrtm000 e84c0fd
Added LeaseInfo.fromLeaseDetails()
xrtm000 a14cd43
Better code
xrtm000 f98f602
NODE-2596 Optimised active leases
xrtm000 57e02bc
Better code
xrtm000 deb3c07
Merge remote-tracking branch 'origin/version-1.5.x' into node-2596-op…
xrtm000 ba42ada
Removed old implementation
xrtm000 20ec312
Removed unused field leaseStatesAreStoredByAddress
xrtm000 90ef2e9
Merge remote-tracking branch 'origin/version-1.5.x' into node-2596-op…
xrtm000 52d4e01
Store only lease id
xrtm000 46c5098
Rollback test
xrtm000 d7c88f7
Rollback
xrtm000 cdb8495
Adapted database.proto
xrtm000 7d9433e
Improved rollback
xrtm000 95956ca
Better code
xrtm000 3893fee
Merge branch 'version-1.5.x' into node-2596-optimised-active-leases
phearnot 5eea031
Merge remote-tracking branch 'origin/version-1.5.x' into node-2596-op…
xrtm000 9c31748
Fix after merge
xrtm000 0576a1e
Adapted test
xrtm000 a12f838
Merge remote-tracking branch 'origin/version-1.5.x' into node-2596-op…
xrtm000 807a895
Fixes
xrtm000 aa254d6
Merge branch 'version-1.5.x' into node-2596-optimised-active-leases
phearnot d1e4bbd
qase fixes
phearnot 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 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 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 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 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 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 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 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 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
47 changes: 47 additions & 0 deletions
47
node/src/main/scala/com/wavesplatform/api/common/lease/AddressLeaseInfo.scala
This file contains 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,47 @@ | ||
package com.wavesplatform.api.common.lease | ||
|
||
import com.wavesplatform.account.Address | ||
import com.wavesplatform.api.common.LeaseInfo.Status.Active | ||
import com.wavesplatform.api.common.LeaseInfo | ||
import com.wavesplatform.common.state.ByteStr | ||
import com.wavesplatform.database.{AddressId, DBExt, DBResource, Keys, RDB} | ||
import com.wavesplatform.state.reader.LeaseDetails | ||
import com.wavesplatform.state.{Blockchain, StateSnapshot} | ||
import monix.eval.Task | ||
import monix.reactive.Observable | ||
|
||
import scala.jdk.CollectionConverters.IteratorHasAsScala | ||
|
||
object AddressLeaseInfo { | ||
def activeLeases( | ||
rdb: RDB, | ||
snapshot: StateSnapshot, | ||
blockchain: Blockchain, | ||
subject: Address | ||
): Observable[LeaseInfo] = { | ||
val snapshotLeases = leasesFromSnapshot(snapshot, blockchain, subject) | ||
val dbLeases = leasesFromDb(rdb, blockchain, subject) | ||
(Observable.fromIterable(snapshotLeases) ++ dbLeases.filterNot(info => snapshotLeases.exists(_.id == info.id))) | ||
.filter(_.status == Active) | ||
} | ||
|
||
private def leasesFromSnapshot(snapshot: StateSnapshot, blockchain: Blockchain, subject: Address): Seq[LeaseInfo] = | ||
snapshot.leaseStates.collect { | ||
case (id, details) if subject == details.sender.toAddress || blockchain.resolveAlias(details.recipient).exists(subject == _) => | ||
LeaseInfo.fromLeaseDetails(id, details, blockchain) | ||
}.toSeq | ||
|
||
private def leasesFromDb(rdb: RDB, blockchain: Blockchain, subject: Address): Observable[LeaseInfo] = | ||
for { | ||
dbResource <- rdb.db.resourceObservable | ||
(leaseId, details) <- dbResource | ||
.get(Keys.addressId(subject)) | ||
.map(fromLeaseDbIterator(dbResource, _)) | ||
.getOrElse(Observable.empty) | ||
} yield LeaseInfo.fromLeaseDetails(leaseId, details, blockchain) | ||
|
||
private def fromLeaseDbIterator(dbResource: DBResource, addressId: AddressId): Observable[(ByteStr, LeaseDetails)] = | ||
Observable | ||
.fromIterator(Task(new LeaseByAddressIterator(dbResource, addressId).asScala)) | ||
.concatMapIterable(identity) | ||
} |
30 changes: 30 additions & 0 deletions
30
node/src/main/scala/com/wavesplatform/api/common/lease/LeaseByAddressIterator.scala
This file contains 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,30 @@ | ||
package com.wavesplatform.api.common.lease | ||
|
||
import com.google.common.collect.AbstractIterator | ||
import com.wavesplatform.common.state.ByteStr | ||
import com.wavesplatform.database.{AddressId, DBResource, Keys, readLeaseSeq} | ||
import com.wavesplatform.state.reader.LeaseDetails | ||
|
||
import scala.collection.mutable | ||
|
||
private class LeaseByAddressIterator(db: DBResource, addressId: AddressId) extends AbstractIterator[Seq[(ByteStr, LeaseDetails)]] { | ||
private val seqNr = db.get(Keys.addressLeaseSeqNr(addressId)) | ||
db.withSafePrefixIterator(_.seekForPrev(Keys.addressLeaseSeq(addressId, seqNr).keyBytes))() | ||
|
||
final override def computeNext(): Seq[(ByteStr, LeaseDetails)] = | ||
db.withSafePrefixIterator { dbIterator => | ||
val buffer = mutable.Map[ByteStr, LeaseDetails]() | ||
while (dbIterator.isValid) { | ||
readLeaseSeq(dbIterator.value()).foreach { case (id, newDetails) => | ||
buffer.updateWith(id)(_.fold(Option(newDetails))(details => if (details.isActive) Some(newDetails) else None)) | ||
} | ||
dbIterator.prev() | ||
} | ||
if (buffer.nonEmpty) | ||
buffer.toSeq | ||
else | ||
endOfData() | ||
}( | ||
endOfData() | ||
) | ||
} |
This file contains 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 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 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
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.
unused?
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.
Removed