@@ -34,6 +34,7 @@ import io.iohk.ethereum.jsonrpc.{FilterManager => FM}
3434import monix .eval .Task
3535import org .bouncycastle .util .encoders .Hex
3636
37+ import scala .collection .concurrent .{Map => ConcurrentMap , TrieMap }
3738import scala .concurrent .duration .FiniteDuration
3839import scala .language .existentials
3940import scala .reflect .ClassTag
@@ -226,8 +227,7 @@ class EthService(
226227
227228 import EthService ._
228229
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 )]()
231231 val lastActive = new AtomicReference [Option [Date ]](None )
232232
233233 private [this ] def consensus = ledger.consensus
@@ -481,11 +481,9 @@ class EthService(
481481 def submitHashRate (req : SubmitHashRateRequest ): ServiceResponse [SubmitHashRateResponse ] =
482482 ifEthash(req) { req =>
483483 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))
489487 SubmitHashRateResponse (true )
490488 }
491489
@@ -527,20 +525,14 @@ class EthService(
527525
528526 def getHashRate (req : GetHashRateRequest ): ServiceResponse [GetHashRateResponse ] =
529527 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 )
534529 // sum all reported hashRates
535- GetHashRateResponse (hashRates.mapValues { case (hr, _) => hr }.values .sum)
530+ GetHashRateResponse (hashRate.map { case (_, ( hr, _)) => hr }.sum)
536531 }
537532
538533 // 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)) =>
544536 Duration .between(reported.toInstant, now.toInstant).toMillis < jsonRpcConfig.minerActiveTimeout.toMillis
545537 }
546538 }
0 commit comments