-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor compact filter matching (#838)
* Refactor compact filter matching * cleanup * some more changes * addressed the PR comments * cleanup * cleanup
- Loading branch information
1 parent
2527354
commit 804f18f
Showing
8 changed files
with
252 additions
and
202 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
72 changes: 72 additions & 0 deletions
72
core/src/main/scala/org/bitcoins/core/gcs/BlockFilter.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,72 @@ | ||
package org.bitcoins.core.gcs | ||
|
||
import org.bitcoins.core.crypto.DoubleSha256Digest | ||
import org.bitcoins.core.protocol.CompactSizeUInt | ||
import org.bitcoins.core.protocol.blockchain.Block | ||
import org.bitcoins.core.protocol.script.{EmptyScriptPubKey, ScriptPubKey} | ||
import org.bitcoins.core.protocol.transaction.{Transaction, TransactionOutput} | ||
import org.bitcoins.core.script.control.OP_RETURN | ||
import org.bitcoins.core.util.BitcoinSUtil | ||
import scodec.bits.ByteVector | ||
|
||
object BlockFilter { | ||
|
||
/** | ||
* Returns all ScriptPubKeys from a Block's outputs that are relevant | ||
* to BIP 158 Basic Block Filters | ||
* @see [[https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki#contents]] | ||
*/ | ||
def getOutputScriptPubKeysFromBlock(block: Block): Vector[ScriptPubKey] = { | ||
val transactions: Vector[Transaction] = block.transactions.toVector | ||
|
||
val newOutputs: Vector[TransactionOutput] = transactions.flatMap(_.outputs) | ||
|
||
newOutputs | ||
.filterNot(_.scriptPubKey.asm.contains(OP_RETURN)) | ||
.filterNot(_.scriptPubKey == EmptyScriptPubKey) | ||
.map(_.scriptPubKey) | ||
} | ||
|
||
/** | ||
* Given a Block and access to the previous output scripts, constructs a Block Filter for that block | ||
* @see [[https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki#block-filters]] | ||
*/ | ||
def apply( | ||
block: Block, | ||
prevOutputScripts: Vector[ScriptPubKey]): GolombFilter = { | ||
val keyBytes: ByteVector = block.blockHeader.hash.bytes.take(16) | ||
|
||
val key: SipHashKey = SipHashKey(keyBytes) | ||
|
||
val newScriptPubKeys: Vector[ByteVector] = | ||
getOutputScriptPubKeysFromBlock(block).map(_.asmBytes) | ||
|
||
val prevOutputScriptBytes: Vector[ByteVector] = | ||
prevOutputScripts | ||
.filterNot(_ == EmptyScriptPubKey) | ||
.map(_.asmBytes) | ||
|
||
val allOutputs = (prevOutputScriptBytes ++ newScriptPubKeys).distinct | ||
|
||
GCS.buildBasicBlockFilter(allOutputs, key) | ||
} | ||
|
||
def fromBytes( | ||
bytes: ByteVector, | ||
blockHash: DoubleSha256Digest): GolombFilter = { | ||
val n = CompactSizeUInt.fromBytes(bytes) | ||
val filterBytes = bytes.drop(n.bytes.length) | ||
val keyBytes: ByteVector = blockHash.bytes.take(16) | ||
val key: SipHashKey = SipHashKey(keyBytes) | ||
|
||
GolombFilter(key, | ||
FilterType.Basic.M, | ||
FilterType.Basic.P, | ||
n, | ||
filterBytes.toBitVector) | ||
} | ||
|
||
def fromHex(hex: String, blockHash: DoubleSha256Digest): GolombFilter = { | ||
fromBytes(BitcoinSUtil.decodeHex(hex), blockHash) | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
core/src/main/scala/org/bitcoins/core/gcs/BlockFilterMatcher.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,128 @@ | ||
package org.bitcoins.core.gcs | ||
|
||
import org.bitcoins.core.number.UInt64 | ||
import scodec.bits.ByteVector | ||
|
||
import scala.annotation.tailrec | ||
|
||
sealed trait BlockFilterMatcher { | ||
|
||
/** | ||
* Checks if the underlying filter matches the given data | ||
*/ | ||
def matches(data: ByteVector): Boolean | ||
|
||
/** | ||
* Checks if the underlying filter matches any item from the given collection | ||
*/ | ||
def matchesAny(data: Vector[ByteVector]): Boolean | ||
} | ||
|
||
case class SimpleFilterMatcher(filter: GolombFilter) | ||
extends BlockFilterMatcher { | ||
|
||
override def matches(data: ByteVector): Boolean = { | ||
val hash = filter.hashToRange(data) | ||
matchesHash(hash) | ||
} | ||
|
||
/** Hashes the given vector of data and calls [[matchesAnyHash()]] to find a match */ | ||
override def matchesAny(data: Vector[ByteVector]): Boolean = { | ||
val hashes = data.map(filter.hashToRange) | ||
matchesAnyHash(hashes) | ||
} | ||
|
||
def matchesHash(hash: UInt64): Boolean = { | ||
var matches = false | ||
GCS.golombDecodeSetsWithPredicate(filter.encodedData, filter.p) { | ||
decodedHash => | ||
if (hash > decodedHash) { | ||
true | ||
} else { | ||
if (hash == decodedHash) { | ||
matches = true | ||
} | ||
false | ||
} | ||
} | ||
matches | ||
} | ||
|
||
/** It implements https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki#golomb-coded-set-multi-match */ | ||
def matchesAnyHash(hashes: Vector[UInt64]): Boolean = { | ||
val sortedHashes = hashes.sorted | ||
var matches = false | ||
var i = 0 | ||
|
||
def predicate(decodedHash: UInt64): Boolean = { | ||
while (i < sortedHashes.size) { | ||
val hash = sortedHashes(i) | ||
if (hash == decodedHash) { | ||
matches = true | ||
return false | ||
} else if (hash > decodedHash) { | ||
return true | ||
} else { | ||
i += 1 | ||
} | ||
} | ||
false | ||
} | ||
|
||
GCS.golombDecodeSetsWithPredicate(filter.encodedData, filter.p)(predicate) | ||
|
||
matches | ||
} | ||
|
||
} | ||
|
||
case class BinarySearchFilterMatcher(filter: GolombFilter) | ||
extends BlockFilterMatcher { | ||
|
||
lazy val decodedHashes: Vector[UInt64] = | ||
GCS.golombDecodeSet(filter.encodedData, filter.p) | ||
|
||
override def matches(data: ByteVector): Boolean = { | ||
val hash = filter.hashToRange(data) | ||
|
||
matchesHash(hash) | ||
} | ||
|
||
/** Hashes the given vector of data and calls [[matchesAnyHash()]] to find a match */ | ||
override def matchesAny(data: Vector[ByteVector]): Boolean = { | ||
val hashes = data.map(filter.hashToRange) | ||
matchesAnyHash(hashes) | ||
} | ||
|
||
def matchesHash(hash: UInt64): Boolean = { | ||
@tailrec | ||
def binarySearch( | ||
from: Int, | ||
to: Int, | ||
hash: UInt64, | ||
set: Vector[UInt64]): Boolean = { | ||
if (to < from) { | ||
false | ||
} else { | ||
val index = (to + from) / 2 | ||
val otherHash = set(index) | ||
|
||
if (hash == otherHash) { | ||
true | ||
} else if (hash < otherHash) { | ||
binarySearch(from, index - 1, hash, set) | ||
} else { | ||
binarySearch(index + 1, to, hash, set) | ||
} | ||
} | ||
} | ||
|
||
binarySearch(from = 0, to = filter.n.toInt - 1, hash, decodedHashes) | ||
} | ||
|
||
/** Checks whether there's a match for at least one of the given hashes | ||
*/ | ||
def matchesAnyHash(hashes: Vector[UInt64]): Boolean = | ||
hashes.exists(matchesHash) | ||
|
||
} |
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.