Browse Source

server: Allow to create a GolombCodedSet from block addresses

master
Alexis Hernandez 6 years ago
parent
commit
37cb7ceb81
  1. 8
      server/app/com/xsn/explorer/gcs/GolombEncoding.scala
  2. 11
      server/app/com/xsn/explorer/models/persisted/Block.scala

8
server/app/com/xsn/explorer/gcs/GolombEncoding.scala

@ -1,6 +1,7 @@
package com.xsn.explorer.gcs
import com.google.common.hash.Hashing
import com.xsn.explorer.models.persisted.Block
import scala.collection.SortedSet
@ -180,4 +181,11 @@ object GolombEncoding {
def default(key: SipHashKey): GolombEncoding = {
new GolombEncoding(p = DefaultP, m = DefaultM, key = key)
}
def encode(block: Block.HasTransactions): Option[GolombCodedSet] = {
val key = SipHashKey.fromBtcutil(block.hash)
val encoder = default(key)
val addresses = block.collectAddresses
encoder.encode(addresses.map(_.string))
}
}

11
server/app/com/xsn/explorer/models/persisted/Block.scala

@ -37,5 +37,16 @@ object Block {
def height: Height = block.height
def previousBlockhash: Option[Blockhash] = block.previousBlockhash
def asTip: HasTransactions = HasTransactions(block.copy(nextBlockhash = None), transactions)
/**
* Collect the addresses involved in the block.
*/
def collectAddresses: Set[Address] = {
transactions.foldLeft(Set.empty[Address]) { case (acc, tx) =>
val spending = tx.inputs.map(_.address)
val receiving = tx.outputs.map(_.address)
spending.toSet ++ receiving.toSet ++ acc
}
}
}
}

Loading…
Cancel
Save