From 37cb7ceb81a9ce8d2a098b3b319b5fca616c2d2b Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Mon, 18 Mar 2019 23:20:20 -0700 Subject: [PATCH] server: Allow to create a GolombCodedSet from block addresses --- server/app/com/xsn/explorer/gcs/GolombEncoding.scala | 8 ++++++++ .../app/com/xsn/explorer/models/persisted/Block.scala | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/server/app/com/xsn/explorer/gcs/GolombEncoding.scala b/server/app/com/xsn/explorer/gcs/GolombEncoding.scala index 670aa2f..49156b1 100644 --- a/server/app/com/xsn/explorer/gcs/GolombEncoding.scala +++ b/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)) + } } diff --git a/server/app/com/xsn/explorer/models/persisted/Block.scala b/server/app/com/xsn/explorer/models/persisted/Block.scala index 450680f..982769a 100644 --- a/server/app/com/xsn/explorer/models/persisted/Block.scala +++ b/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 + } + } } }