@@ -34,6 +34,7 @@ import io.iohk.ethereum.jsonrpc.{FilterManager => FM}
34
34
import monix .eval .Task
35
35
import org .bouncycastle .util .encoders .Hex
36
36
37
+ import scala .collection .concurrent .{Map => ConcurrentMap , TrieMap }
37
38
import scala .concurrent .duration .FiniteDuration
38
39
import scala .language .existentials
39
40
import scala .reflect .ClassTag
@@ -226,8 +227,7 @@ class EthService(
226
227
227
228
import EthService ._
228
229
229
- val hashRate : AtomicReference [Map [ByteString , (BigInt , Date )]] =
230
- new AtomicReference [Map [ByteString , (BigInt , Date )]](Map ())
230
+ val hashRate : ConcurrentMap [ByteString , (BigInt , Date )] = new TrieMap [ByteString , (BigInt , Date )]()
231
231
val lastActive = new AtomicReference [Option [Date ]](None )
232
232
233
233
private [this ] def consensus = ledger.consensus
@@ -481,11 +481,9 @@ class EthService(
481
481
def submitHashRate (req : SubmitHashRateRequest ): ServiceResponse [SubmitHashRateResponse ] =
482
482
ifEthash(req) { req =>
483
483
reportActive()
484
- hashRate.updateAndGet((t : Map [ByteString , (BigInt , Date )]) => {
485
- val now = new Date
486
- removeObsoleteHashrates(now, t + (req.id -> (req.hashRate, now)))
487
- })
488
-
484
+ val now = new Date
485
+ removeObsoleteHashrates(now)
486
+ hashRate.put(req.id, (req.hashRate -> now))
489
487
SubmitHashRateResponse (true )
490
488
}
491
489
@@ -527,20 +525,14 @@ class EthService(
527
525
528
526
def getHashRate (req : GetHashRateRequest ): ServiceResponse [GetHashRateResponse ] =
529
527
ifEthash(req) { _ =>
530
- val hashRates : Map [ByteString , (BigInt , Date )] = hashRate.updateAndGet((t : Map [ByteString , (BigInt , Date )]) => {
531
- removeObsoleteHashrates(new Date , t)
532
- })
533
-
528
+ removeObsoleteHashrates(new Date )
534
529
// sum all reported hashRates
535
- GetHashRateResponse (hashRates.mapValues { case (hr, _) => hr }.values .sum)
530
+ GetHashRateResponse (hashRate.map { case (_, ( hr, _)) => hr }.sum)
536
531
}
537
532
538
533
// NOTE This is called from places that guarantee we are running Ethash consensus.
539
- private def removeObsoleteHashrates (
540
- now : Date ,
541
- rates : Map [ByteString , (BigInt , Date )]
542
- ): Map [ByteString , (BigInt , Date )] = {
543
- rates.filter { case (_, (_, reported)) =>
534
+ private def removeObsoleteHashrates (now : Date ): Unit = {
535
+ hashRate.retain { case (_, (_, reported)) =>
544
536
Duration .between(reported.toInstant, now.toInstant).toMillis < jsonRpcConfig.minerActiveTimeout.toMillis
545
537
}
546
538
}
0 commit comments