Skip to content

Commit

Permalink
replaced AtomicLong by Array[Long]
Browse files Browse the repository at this point in the history
  • Loading branch information
pm47 committed Sep 9, 2019
1 parent fd20150 commit c6390c2
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 28 deletions.
6 changes: 3 additions & 3 deletions eclair-core/src/main/scala/fr/acinq/eclair/NodeParams.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import scala.concurrent.duration.FiniteDuration
* Created by PM on 26/02/2017.
*/
case class NodeParams(keyManager: KeyManager,
private val blockCount: AtomicLong,
private val blockCount: Array[Long],
alias: String,
color: Color,
publicAddresses: List[NodeAddress],
Expand Down Expand Up @@ -82,7 +82,7 @@ case class NodeParams(keyManager: KeyManager,
maxPaymentAttempts: Int) {
val privateKey = keyManager.nodeKey.privateKey
val nodeId = keyManager.nodeId
def currentBlockHeight: Long = blockCount.get
def currentBlockHeight: Long = blockCount(0)
}

object NodeParams {
Expand Down Expand Up @@ -127,7 +127,7 @@ object NodeParams {
}
}

def makeNodeParams(config: Config, keyManager: KeyManager, torAddress_opt: Option[NodeAddress], database: Databases, blockCount: AtomicLong, feeEstimator: FeeEstimator): NodeParams = {
def makeNodeParams(config: Config, keyManager: KeyManager, torAddress_opt: Option[NodeAddress], database: Databases, blockCount: Array[Long], feeEstimator: FeeEstimator): NodeParams = {

val chain = config.getString("chain")
val chainHash = makeChainHash(chain)
Expand Down
2 changes: 1 addition & 1 deletion eclair-core/src/main/scala/fr/acinq/eclair/Setup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Setup(datadir: File,
* It is mainly used to calculate htlc expiries.
* The value is read by all actors, hence it needs to be thread-safe.
*/
val blockCount = new AtomicLong(0)
val blockCount: Array[Long] = Array(0L)

/**
* This holds the current feerates, in satoshi-per-kilobytes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import scala.util.Try
* - also uses bitcoin-core rpc api, most notably for tx confirmation count and blockcount (because reorgs)
* Created by PM on 21/02/2016.
*/
class ZmqWatcher(blockCount: AtomicLong, client: ExtendedBitcoinClient)(implicit ec: ExecutionContext = ExecutionContext.global) extends Actor with ActorLogging {
class ZmqWatcher(blockCount: Array[Long], client: ExtendedBitcoinClient)(implicit ec: ExecutionContext = ExecutionContext.global) extends Actor with ActorLogging {

import ZmqWatcher._

Expand Down Expand Up @@ -80,7 +80,7 @@ class ZmqWatcher(blockCount: AtomicLong, client: ExtendedBitcoinClient)(implicit
client.getBlockCount.map {
case count =>
log.debug(s"setting blockCount=$count")
blockCount.set(count)
blockCount(0) = count
context.system.eventStream.publish(CurrentBlockCount(count))
}
// TODO: beware of the herd effect
Expand Down Expand Up @@ -151,7 +151,7 @@ class ZmqWatcher(blockCount: AtomicLong, client: ExtendedBitcoinClient)(implicit
context become watching(watches + w, addWatchedUtxos(watchedUtxos, w), block2tx, nextTick)

case PublishAsap(tx) =>
val blockCount = this.blockCount.get()
val blockCount = this.blockCount(0)
val cltvTimeout = Scripts.cltvTimeout(tx)
val csvTimeout = Scripts.csvTimeout(tx)
if (csvTimeout > 0) {
Expand All @@ -168,7 +168,7 @@ class ZmqWatcher(blockCount: AtomicLong, client: ExtendedBitcoinClient)(implicit

case WatchEventConfirmed(BITCOIN_PARENT_TX_CONFIRMED(tx), blockHeight, _, _) =>
log.info(s"parent tx of txid=${tx.txid} has been confirmed")
val blockCount = this.blockCount.get()
val blockCount = this.blockCount(0)
val csvTimeout = Scripts.csvTimeout(tx)
val absTimeout = blockHeight + csvTimeout
if (absTimeout > blockCount) {
Expand Down Expand Up @@ -226,7 +226,7 @@ class ZmqWatcher(blockCount: AtomicLong, client: ExtendedBitcoinClient)(implicit

object ZmqWatcher {

def props(blockCount: AtomicLong, client: ExtendedBitcoinClient)(implicit ec: ExecutionContext = ExecutionContext.global) = Props(new ZmqWatcher(blockCount, client)(ec))
def props(blockCount: Array[Long], client: ExtendedBitcoinClient)(implicit ec: ExecutionContext = ExecutionContext.global) = Props(new ZmqWatcher(blockCount, client)(ec))

case object TickNewBlock

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import scala.concurrent.ExecutionContext
import scala.concurrent.duration._
import scala.util.Random

class ElectrumClientPool(blockCount: AtomicLong, serverAddresses: Set[ElectrumServerAddress])(implicit val ec: ExecutionContext) extends Actor with FSM[ElectrumClientPool.State, ElectrumClientPool.Data] {
class ElectrumClientPool(blockCount: Array[Long], serverAddresses: Set[ElectrumServerAddress])(implicit val ec: ExecutionContext) extends Actor with FSM[ElectrumClientPool.State, ElectrumClientPool.Data] {
import ElectrumClientPool._

val statusListeners = collection.mutable.HashSet.empty[ActorRef]
Expand Down Expand Up @@ -166,10 +166,10 @@ class ElectrumClientPool(blockCount: AtomicLong, serverAddresses: Set[ElectrumSe

private def updateBlockCount(blockCount: Long): Unit = {
// when synchronizing we don't want to advertise previous blocks
if (this.blockCount.get() < blockCount) {
if (this.blockCount(0) < blockCount) {
log.debug("current blockchain height={}", blockCount)
context.system.eventStream.publish(CurrentBlockCount(blockCount))
this.blockCount.set(blockCount)
this.blockCount(0) = blockCount
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package fr.acinq.eclair.blockchain.electrum

import java.util.concurrent.atomic.AtomicLong

import akka.actor.{Actor, ActorLogging, ActorRef, Stash, Terminated}
import fr.acinq.bitcoin.{BlockHeader, ByteVector32, Script, Transaction, TxIn, TxOut}
import fr.acinq.eclair.blockchain._
Expand All @@ -30,7 +28,7 @@ import scala.collection.SortedMap
import scala.collection.immutable.Queue


class ElectrumWatcher(blockCount: AtomicLong, client: ActorRef) extends Actor with Stash with ActorLogging {
class ElectrumWatcher(blockCount: Array[Long], client: ActorRef) extends Actor with Stash with ActorLogging {

client ! ElectrumClient.AddStatusListener(self)

Expand Down Expand Up @@ -163,7 +161,7 @@ class ElectrumWatcher(blockCount: AtomicLong, client: ActorRef) extends Actor wi
case ElectrumClient.ServerError(ElectrumClient.GetTransaction(txid, Some(origin: ActorRef)), _) => origin ! GetTxWithMetaResponse(txid, None, tip.time)

case PublishAsap(tx) =>
val blockCount = this.blockCount.get()
val blockCount = this.blockCount(0)
val cltvTimeout = Scripts.cltvTimeout(tx)
val csvTimeout = Scripts.csvTimeout(tx)
if (csvTimeout > 0) {
Expand All @@ -184,7 +182,7 @@ class ElectrumWatcher(blockCount: AtomicLong, client: ActorRef) extends Actor wi

case WatchEventConfirmed(BITCOIN_PARENT_TX_CONFIRMED(tx), blockHeight, _, _) =>
log.info(s"parent tx of txid=${tx.txid} has been confirmed")
val blockCount = this.blockCount.get()
val blockCount = this.blockCount(0)
val csvTimeout = Scripts.csvTimeout(tx)
val absTimeout = blockHeight + csvTimeout
if (absTimeout > blockCount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class StartupSpec extends FunSuite {
val conf = illegalAliasConf.withFallback(ConfigFactory.parseResources("reference.conf").getConfig("eclair"))
val keyManager = new LocalKeyManager(seed = randomBytes32, chainHash = Block.TestnetGenesisBlock.hash)

val blockCount = new AtomicLong(0)
val blockCount = Array(0L)

// try to create a NodeParams instance with a conf that contains an illegal alias
val nodeParamsAttempt = Try(NodeParams.makeNodeParams(conf, keyManager, None, TestConstants.inMemoryDb(), blockCount, new TestConstants.TestFeeEstimator))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ object TestConstants {
// This is a function, and not a val! When called will return a new NodeParams
def nodeParams = NodeParams(
keyManager = keyManager,
blockCount = new AtomicLong(400000),
blockCount = Array(400000L),
alias = "alice",
color = Color(1, 2, 3),
publicAddresses = NodeAddress.fromParts("localhost", 9731).get :: Nil,
Expand Down Expand Up @@ -143,7 +143,7 @@ object TestConstants {

def nodeParams = NodeParams(
keyManager = keyManager,
blockCount = new AtomicLong(400000),
blockCount = Array(400000L),
alias = "bob",
color = Color(4, 5, 6),
publicAddresses = NodeAddress.fromParts("localhost", 9732).get :: Nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ElectrumClientPoolSpec extends TestKit(ActorSystem("test")) with FunSuiteL
val addresses = random.shuffle(serverAddresses.toSeq).take(2).toSet + ElectrumClientPool.ElectrumServerAddress(new InetSocketAddress("electrum.acinq.co", 50002), SSL.STRICT)
stream.close()
assert(addresses.nonEmpty)
pool = system.actorOf(Props(new ElectrumClientPool(new AtomicLong(), addresses)), "electrum-client")
pool = system.actorOf(Props(new ElectrumClientPool(Array(0L), addresses)), "electrum-client")
}

test("connect to an electrumx mainnet server") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ElectrumWalletSpec extends TestKit(ActorSystem("test")) with FunSuiteLike
}

test("wait until wallet is ready") {
electrumClient = system.actorOf(Props(new ElectrumClientPool(new AtomicLong(), Set(ElectrumServerAddress(new InetSocketAddress("localhost", electrumPort), SSL.OFF)))))
electrumClient = system.actorOf(Props(new ElectrumClientPool(Array(0L), Set(ElectrumServerAddress(new InetSocketAddress("localhost", electrumPort), SSL.OFF)))))
wallet = system.actorOf(Props(new ElectrumWallet(seed, electrumClient, WalletParameters(Block.RegtestGenesisBlock.hash, new SqliteWalletDb(DriverManager.getConnection("jdbc:sqlite::memory:")), minimumFee = 5000 sat))), "wallet")
val probe = TestProbe()
awaitCond({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ElectrumWatcherSpec extends TestKit(ActorSystem("test")) with FunSuiteLike

test("watch for confirmed transactions") {
val probe = TestProbe()
val blockCount = new AtomicLong()
val blockCount = Array(0L)
val electrumClient = system.actorOf(Props(new ElectrumClientPool(blockCount, Set(electrumAddress))))
val watcher = system.actorOf(Props(new ElectrumWatcher(blockCount, electrumClient)))

Expand All @@ -82,7 +82,7 @@ class ElectrumWatcherSpec extends TestKit(ActorSystem("test")) with FunSuiteLike

test("watch for spent transactions") {
val probe = TestProbe()
val blockCount = new AtomicLong()
val blockCount = Array(0L)
val electrumClient = system.actorOf(Props(new ElectrumClientPool(blockCount, Set(electrumAddress))))
val watcher = system.actorOf(Props(new ElectrumWatcher(blockCount, electrumClient)))

Expand Down Expand Up @@ -127,7 +127,7 @@ class ElectrumWatcherSpec extends TestKit(ActorSystem("test")) with FunSuiteLike
}

test("get transaction") {
val blockCount = new AtomicLong()
val blockCount = Array(0L)
val mainnetAddress = ElectrumServerAddress(new InetSocketAddress("electrum.acinq.co", 50002), SSL.STRICT)
val electrumClient = system.actorOf(Props(new ElectrumClientPool(blockCount, Set(mainnetAddress))))
val watcher = system.actorOf(Props(new ElectrumWatcher(blockCount, electrumClient)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ThroughputSpec extends FunSuite {
ignore("throughput") {
implicit val system = ActorSystem()
val pipe = system.actorOf(Props[Pipe], "pipe")
val blockCount = new AtomicLong()
val blockCount = Array(0L)
val blockchain = system.actorOf(ZmqWatcher.props(blockCount, new TestBitcoinClient()), "blockchain")
val paymentHandler = system.actorOf(Props(new Actor() {
val random = new Random()
Expand Down Expand Up @@ -86,7 +86,7 @@ class ThroughputSpec extends FunSuite {
pipe ! (alice, bob)
latch.await()

var i = new AtomicLong(0)
var i = Array(0L)
val random = new Random()

def msg = random.nextInt(100) % 5 match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class RustyTestsSpec extends TestKit(ActorSystem("test")) with Matchers with fix
case class FixtureParam(ref: List[String], res: List[String])

override def withFixture(test: OneArgTest): Outcome = {
val blockCount = new AtomicLong(0)
val blockCount = Array(0L)
val latch = new CountDownLatch(1)
val pipe: ActorRef = system.actorOf(Props(new SynchronizationPipe(latch)))
val alice2blockchain = TestProbe()
Expand Down

0 comments on commit c6390c2

Please sign in to comment.