Skip to content

Commit

Permalink
Use BlockId in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bast committed Nov 10, 2023
1 parent 5e8543d commit 10e49e7
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)))
Expand All @@ -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)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -647,6 +655,7 @@ object JsonSerializers {
ByteVectorSerializer +
ByteVector32Serializer +
TxIdSerializer +
BlockIdSerializer +
BlockHashSerializer +
ByteVector64Serializer +
ChannelEventSerializer +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 10e49e7

Please sign in to comment.