-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NODE-2596 Optimised active leases (#3884)
- Loading branch information
Showing
22 changed files
with
324 additions
and
159 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%date %-5level [%.15thread] %logger{26} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<logger name="scorex.crypto.signatures.Curve25519$" level="INFO"/> | ||
|
||
<root level="${logback.test.level:-DEBUG}"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
</configuration> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,3 +116,7 @@ message LeaseDetails { | |
Expired expired = 12; | ||
} | ||
} | ||
|
||
message LeaseIds { | ||
repeated bytes ids = 1; | ||
} |
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
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
package com.wavesplatform.api.common.lease | ||
|
||
import com.wavesplatform.account.Address | ||
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.{LeaseDetails, StateSnapshot} | ||
import monix.eval.Task | ||
import monix.reactive.Observable | ||
|
||
import scala.jdk.CollectionConverters.IteratorHasAsScala | ||
|
||
object AddressLeaseInfo { | ||
def activeLeases( | ||
rdb: RDB, | ||
snapshot: StateSnapshot, | ||
subject: Address | ||
): Observable[LeaseInfo] = { | ||
val snapshotLeases = leasesFromSnapshot(snapshot, subject) | ||
val dbLeases = leasesFromDb(rdb, subject) | ||
Observable.fromIterable(snapshotLeases) ++ dbLeases.filterNot(info => snapshot.cancelledLeases.contains(info.id)) | ||
} | ||
|
||
private def leasesFromSnapshot(snapshot: StateSnapshot, subject: Address): Seq[LeaseInfo] = | ||
snapshot.newLeases.collect { | ||
case (id, leaseStatic) | ||
if !snapshot.cancelledLeases.contains(id) && | ||
(subject == leaseStatic.sender.toAddress || subject == leaseStatic.recipientAddress) => | ||
LeaseInfo( | ||
id, | ||
leaseStatic.sourceId, | ||
leaseStatic.sender.toAddress, | ||
leaseStatic.recipientAddress, | ||
leaseStatic.amount.value, | ||
leaseStatic.height, | ||
LeaseInfo.Status.Active | ||
) | ||
}.toSeq | ||
|
||
private def leasesFromDb(rdb: RDB, 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) | ||
|
||
private def fromLeaseDbIterator(dbResource: DBResource, addressId: AddressId): Observable[(ByteStr, LeaseDetails)] = | ||
Observable | ||
.fromIterator(Task(new LeaseByAddressIterator(dbResource, addressId).asScala)) | ||
.concatMapIterable(identity) | ||
} |
32 changes: 32 additions & 0 deletions
32
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,32 @@ | ||
package com.wavesplatform.api.common.lease | ||
|
||
import com.google.common.collect.AbstractIterator | ||
import com.wavesplatform.common.state.ByteStr | ||
import com.wavesplatform.database | ||
import com.wavesplatform.database.{AddressId, DBResource, Keys} | ||
import com.wavesplatform.state.LeaseDetails | ||
|
||
import scala.collection.mutable | ||
|
||
private class LeaseByAddressIterator(resource: DBResource, addressId: AddressId) extends AbstractIterator[Seq[(ByteStr, LeaseDetails)]] { | ||
private val seqNr = resource.get(Keys.addressLeaseSeqNr(addressId)) | ||
resource.withSafePrefixIterator(_.seekForPrev(Keys.addressLeaseSeq(addressId, seqNr).keyBytes))() | ||
|
||
final override def computeNext(): Seq[(ByteStr, LeaseDetails)] = | ||
resource.withSafePrefixIterator { iterator => | ||
val buffer = mutable.Map[ByteStr, LeaseDetails]() | ||
while (iterator.isValid) { | ||
for { | ||
id <- database.readLeaseIdSeq(iterator.value()) | ||
details <- database.loadLease(resource, id) if details.isActive | ||
} buffer.update(id, details) | ||
iterator.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
Oops, something went wrong.