From 10e49e768dd0aeab735abfaf74920d19f100d569 Mon Sep 17 00:00:00 2001 From: t-bast Date: Fri, 27 Oct 2023 14:34:08 +0200 Subject: [PATCH] Use BlockId in more places --- .../fr/acinq/eclair/blockchain/BlockchainEvents.scala | 4 ++-- .../acinq/eclair/blockchain/bitcoind/ZmqWatcher.scala | 4 ++-- .../bitcoind/rpc/BasicBitcoinJsonRPCClient.scala | 3 ++- .../blockchain/bitcoind/rpc/BitcoinCoreClient.scala | 8 ++++---- .../eclair/blockchain/bitcoind/zmq/ZMQActor.scala | 8 ++++---- .../fr/acinq/eclair/crypto/WeakEntropyPool.scala | 8 ++++---- .../scala/fr/acinq/eclair/json/JsonSerializers.scala | 11 ++++++++++- .../scala/fr/acinq/eclair/TestBitcoinCoreClient.scala | 4 ++-- .../acinq/eclair/transactions/TransactionsSpec.scala | 2 +- 9 files changed, 31 insertions(+), 21 deletions(-) diff --git a/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/BlockchainEvents.scala b/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/BlockchainEvents.scala index 03d815948b..f65769062a 100644 --- a/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/BlockchainEvents.scala +++ b/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/BlockchainEvents.scala @@ -16,7 +16,7 @@ package fr.acinq.eclair.blockchain -import fr.acinq.bitcoin.scalacompat.{ByteVector32, Transaction} +import fr.acinq.bitcoin.scalacompat.{BlockId, Transaction} import fr.acinq.eclair.BlockHeight import fr.acinq.eclair.blockchain.fee.FeeratesPerKw @@ -26,7 +26,7 @@ import fr.acinq.eclair.blockchain.fee.FeeratesPerKw sealed trait BlockchainEvent -case class NewBlock(blockHash: ByteVector32) extends BlockchainEvent +case class NewBlock(blockId: BlockId) extends BlockchainEvent case class NewTransaction(tx: Transaction) extends BlockchainEvent diff --git a/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/ZmqWatcher.scala b/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/ZmqWatcher.scala index 3aed6261b2..2ec66eaf1d 100644 --- a/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/ZmqWatcher.scala +++ b/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/ZmqWatcher.scala @@ -61,7 +61,7 @@ object ZmqWatcher { private case class GetBlockCountFailed(t: Throwable) extends Command private case class CheckBlockHeight(current: BlockHeight) extends Command private case class PublishBlockHeight(current: BlockHeight) extends Command - private case class ProcessNewBlock(blockHash: ByteVector32) extends Command + private case class ProcessNewBlock(blockId: BlockId) extends Command private case class ProcessNewTransaction(tx: Transaction) extends Command final case class ValidateRequest(replyTo: ActorRef[ValidateResult], ann: ChannelAnnouncement) extends Command @@ -171,7 +171,7 @@ object ZmqWatcher { def apply(nodeParams: NodeParams, blockCount: AtomicLong, client: BitcoinCoreClient): Behavior[Command] = Behaviors.setup { context => - context.system.eventStream ! EventStream.Subscribe(context.messageAdapter[NewBlock](b => ProcessNewBlock(b.blockHash))) + context.system.eventStream ! EventStream.Subscribe(context.messageAdapter[NewBlock](b => ProcessNewBlock(b.blockId))) context.system.eventStream ! EventStream.Subscribe(context.messageAdapter[NewTransaction](t => ProcessNewTransaction(t.tx))) Behaviors.withTimers { timers => // we initialize block count diff --git a/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/rpc/BasicBitcoinJsonRPCClient.scala b/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/rpc/BasicBitcoinJsonRPCClient.scala index 82d5d154d4..4c05b98036 100644 --- a/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/rpc/BasicBitcoinJsonRPCClient.scala +++ b/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/rpc/BasicBitcoinJsonRPCClient.scala @@ -37,7 +37,8 @@ class BasicBitcoinJsonRPCClient(rpcAuthMethod: BitcoinJsonRPCAuthMethod, host: S implicit val formats: Formats = DefaultFormats.withBigDecimal + ByteVector32Serializer + ByteVector32KmpSerializer + TxIdSerializer + TxIdKmpSerializer + - BlockHashSerializer + BlockHashKmpSerializer + BlockHashSerializer + BlockHashKmpSerializer + + BlockIdSerializer + BlockIdKmpSerializer private val scheme = if (ssl) "https" else "http" private val serviceUri = wallet match { diff --git a/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/rpc/BitcoinCoreClient.scala b/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/rpc/BitcoinCoreClient.scala index 2d4774b2ee..25689e6346 100644 --- a/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/rpc/BitcoinCoreClient.scala +++ b/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/rpc/BitcoinCoreClient.scala @@ -88,9 +88,9 @@ class BitcoinCoreClient(val rpcClient: BitcoinJsonRPCClient, val onChainKeyManag } /** Get the hash of the block containing a given transaction. */ - private def getTxBlockHash(txid: TxId)(implicit ec: ExecutionContext): Future[Option[ByteVector32]] = + private def getTxBlockId(txid: TxId)(implicit ec: ExecutionContext): Future[Option[BlockId]] = rpcClient.invoke("getrawtransaction", txid, 1 /* verbose output is needed to get the block hash */) - .map(json => (json \ "blockhash").extractOpt[String].map(ByteVector32.fromValidHex)) + .map(json => (json \ "blockhash").extractOpt[String].map(b => BlockId(ByteVector32.fromValidHex(b)))) .recover { case t: JsonRPCError if t.error.code == -5 => None // Invalid or non-wallet transaction id (code: -5) } @@ -101,8 +101,8 @@ class BitcoinCoreClient(val rpcClient: BitcoinJsonRPCClient, val onChainKeyManag */ def getTransactionShortId(txid: TxId)(implicit ec: ExecutionContext): Future[(BlockHeight, Int)] = for { - Some(blockHash) <- getTxBlockHash(txid) - json <- rpcClient.invoke("getblock", blockHash) + Some(blockId) <- getTxBlockId(txid) + json <- rpcClient.invoke("getblock", blockId) JInt(height) = json \ "height" JArray(txs) = json \ "tx" index = txs.indexOf(JString(txid.value.toHex)) diff --git a/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/zmq/ZMQActor.scala b/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/zmq/ZMQActor.scala index fe8384d6e9..b2713c7f4b 100644 --- a/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/zmq/ZMQActor.scala +++ b/eclair-core/src/main/scala/fr/acinq/eclair/blockchain/bitcoind/zmq/ZMQActor.scala @@ -18,7 +18,7 @@ package fr.acinq.eclair.blockchain.bitcoind.zmq import akka.Done import akka.actor.{Actor, ActorLogging} -import fr.acinq.bitcoin.scalacompat.{ByteVector32, Transaction} +import fr.acinq.bitcoin.scalacompat.{BlockHash, BlockId, ByteVector32, Transaction} import fr.acinq.eclair.blockchain.{NewBlock, NewTransaction} import org.zeromq.ZMQ.Event import org.zeromq.{SocketType, ZContext, ZMQ, ZMsg} @@ -92,9 +92,9 @@ class ZMQActor(address: String, topic: String, connected: Option[Promise[Done]] case msg: ZMsg => msg.popString() match { case "hashblock" => - val blockHash = ByteVector32(ByteVector(msg.pop().getData)) - log.debug("received blockhash={}", blockHash) - context.system.eventStream.publish(NewBlock(blockHash)) + val blockId = BlockId(ByteVector32(ByteVector(msg.pop().getData))) + log.debug("received blockId={}", blockId) + context.system.eventStream.publish(NewBlock(blockId)) case "rawtx" => val tx = Transaction.read(msg.pop().getData) log.debug("received txid={}", tx.txid) diff --git a/eclair-core/src/main/scala/fr/acinq/eclair/crypto/WeakEntropyPool.scala b/eclair-core/src/main/scala/fr/acinq/eclair/crypto/WeakEntropyPool.scala index 9cadda404b..7f588cf692 100644 --- a/eclair-core/src/main/scala/fr/acinq/eclair/crypto/WeakEntropyPool.scala +++ b/eclair-core/src/main/scala/fr/acinq/eclair/crypto/WeakEntropyPool.scala @@ -20,7 +20,7 @@ import akka.actor.typed.Behavior import akka.actor.typed.eventstream.EventStream import akka.actor.typed.scaladsl.Behaviors import fr.acinq.bitcoin.scalacompat.Crypto.PublicKey -import fr.acinq.bitcoin.scalacompat.{ByteVector32, ByteVector64, Crypto} +import fr.acinq.bitcoin.scalacompat.{BlockId, ByteVector32, ByteVector64, Crypto} import fr.acinq.eclair.TimestampMilli import fr.acinq.eclair.blockchain.NewBlock import fr.acinq.eclair.channel.ChannelSignatureReceived @@ -47,7 +47,7 @@ object WeakEntropyPool { // @formatter:off sealed trait Command private case object FlushEntropy extends Command - private case class WrappedNewBlock(blockHash: ByteVector32) extends Command + private case class WrappedNewBlock(blockId: BlockId) extends Command private case class WrappedPaymentRelayed(paymentHash: ByteVector32, relayedAt: TimestampMilli) extends Command private case class WrappedPeerConnected(nodeId: PublicKey) extends Command private case class WrappedChannelSignature(wtxid: ByteVector32) extends Command @@ -56,7 +56,7 @@ object WeakEntropyPool { def apply(collector: EntropyCollector): Behavior[Command] = { Behaviors.setup { context => - context.system.eventStream ! EventStream.Subscribe(context.messageAdapter[NewBlock](e => WrappedNewBlock(e.blockHash))) + context.system.eventStream ! EventStream.Subscribe(context.messageAdapter[NewBlock](e => WrappedNewBlock(e.blockId))) context.system.eventStream ! EventStream.Subscribe(context.messageAdapter[ChannelPaymentRelayed](e => WrappedPaymentRelayed(e.paymentHash, e.timestamp))) context.system.eventStream ! EventStream.Subscribe(context.messageAdapter[PeerConnected](e => WrappedPeerConnected(e.nodeId))) context.system.eventStream ! EventStream.Subscribe(context.messageAdapter[NodeUpdated](e => WrappedNodeUpdated(e.ann.signature))) @@ -79,7 +79,7 @@ object WeakEntropyPool { Behaviors.same } - case WrappedNewBlock(blockHash) => collecting(collector, collect(entropy_opt, blockHash ++ ByteVector.fromLong(System.currentTimeMillis()))) + case WrappedNewBlock(blockHash) => collecting(collector, collect(entropy_opt, blockHash.value ++ ByteVector.fromLong(System.currentTimeMillis()))) case WrappedPaymentRelayed(paymentHash, relayedAt) => collecting(collector, collect(entropy_opt, paymentHash ++ ByteVector.fromLong(relayedAt.toLong))) diff --git a/eclair-core/src/main/scala/fr/acinq/eclair/json/JsonSerializers.scala b/eclair-core/src/main/scala/fr/acinq/eclair/json/JsonSerializers.scala index 12df4da1ee..a1d072c89b 100644 --- a/eclair-core/src/main/scala/fr/acinq/eclair/json/JsonSerializers.scala +++ b/eclair-core/src/main/scala/fr/acinq/eclair/json/JsonSerializers.scala @@ -19,7 +19,7 @@ package fr.acinq.eclair.json import com.google.common.net.HostAndPort import fr.acinq.bitcoin.scalacompat.Crypto.{PrivateKey, PublicKey} import fr.acinq.bitcoin.scalacompat.DeterministicWallet.KeyPath -import fr.acinq.bitcoin.scalacompat.{BlockHash, Btc, ByteVector32, ByteVector64, OutPoint, Satoshi, Transaction, TxId} +import fr.acinq.bitcoin.scalacompat.{BlockHash, BlockId, Btc, ByteVector32, ByteVector64, OutPoint, Satoshi, Transaction, TxId} import fr.acinq.eclair.balance.CheckBalance.{CorrectedOnChainBalance, GlobalBalance, OffChainBalance} import fr.acinq.eclair.blockchain.fee.{ConfirmationTarget, FeeratePerKw} import fr.acinq.eclair.channel._ @@ -130,6 +130,14 @@ object TxIdKmpSerializer extends MinimalSerializer({ case x: fr.acinq.bitcoin.TxId => JString(x.value.toHex) }) +object BlockIdSerializer extends MinimalSerializer({ + case x: BlockId => JString(x.value.toHex) +}) + +object BlockIdKmpSerializer extends MinimalSerializer({ + case x: fr.acinq.bitcoin.BlockId => JString(x.value.toHex) +}) + object BlockHashSerializer extends MinimalSerializer({ case x: BlockHash => JString(x.value.toHex) }) @@ -647,6 +655,7 @@ object JsonSerializers { ByteVectorSerializer + ByteVector32Serializer + TxIdSerializer + + BlockIdSerializer + BlockHashSerializer + ByteVector64Serializer + ChannelEventSerializer + diff --git a/eclair-core/src/test/scala/fr/acinq/eclair/TestBitcoinCoreClient.scala b/eclair-core/src/test/scala/fr/acinq/eclair/TestBitcoinCoreClient.scala index f6c66be7b7..0cb8057a76 100644 --- a/eclair-core/src/test/scala/fr/acinq/eclair/TestBitcoinCoreClient.scala +++ b/eclair-core/src/test/scala/fr/acinq/eclair/TestBitcoinCoreClient.scala @@ -17,7 +17,7 @@ package fr.acinq.eclair import akka.actor.ActorSystem -import fr.acinq.bitcoin.scalacompat.{Transaction, TxId} +import fr.acinq.bitcoin.scalacompat.{BlockId, Transaction, TxId} import fr.acinq.eclair.blockchain._ import fr.acinq.eclair.blockchain.bitcoind.rpc.BitcoinJsonRPCAuthMethod.UserPassword import fr.acinq.eclair.blockchain.bitcoind.rpc.{BasicBitcoinJsonRPCClient, BitcoinCoreClient} @@ -32,7 +32,7 @@ class TestBitcoinCoreClient()(implicit system: ActorSystem) extends BitcoinCoreC import scala.concurrent.ExecutionContext.Implicits.global - system.scheduler.scheduleWithFixedDelay(100 milliseconds, 100 milliseconds)(() => system.eventStream.publish(NewBlock(randomBytes32()))) + system.scheduler.scheduleWithFixedDelay(100 milliseconds, 100 milliseconds)(() => system.eventStream.publish(NewBlock(BlockId(randomBytes32())))) override def publishTransaction(tx: Transaction)(implicit ec: ExecutionContext): Future[TxId] = { system.eventStream.publish(NewTransaction(tx)) diff --git a/eclair-core/src/test/scala/fr/acinq/eclair/transactions/TransactionsSpec.scala b/eclair-core/src/test/scala/fr/acinq/eclair/transactions/TransactionsSpec.scala index 502878a703..09264cca83 100644 --- a/eclair-core/src/test/scala/fr/acinq/eclair/transactions/TransactionsSpec.scala +++ b/eclair-core/src/test/scala/fr/acinq/eclair/transactions/TransactionsSpec.scala @@ -19,7 +19,7 @@ package fr.acinq.eclair.transactions import fr.acinq.bitcoin.SigHash._ import fr.acinq.bitcoin.scalacompat.Crypto.{PrivateKey, ripemd160, sha256} import fr.acinq.bitcoin.scalacompat.Script.{pay2wpkh, pay2wsh, write} -import fr.acinq.bitcoin.scalacompat.{Btc, ByteVector32, Crypto, MilliBtc, MilliBtcDouble, OutPoint, Protocol, Satoshi, SatoshiLong, Script, ScriptWitness, Transaction, TxId, TxIn, TxOut, millibtc2satoshi} +import fr.acinq.bitcoin.scalacompat.{Block, Btc, ByteVector32, Crypto, MilliBtc, MilliBtcDouble, OutPoint, Protocol, Satoshi, SatoshiLong, Script, ScriptWitness, Transaction, TxId, TxIn, TxOut, millibtc2satoshi} import fr.acinq.eclair.TestUtils.randomTxId import fr.acinq.eclair._ import fr.acinq.eclair.blockchain.fee.{ConfirmationTarget, FeeratePerKw}