add logs for forkchoice

This commit is contained in:
Termina1
2022-12-02 19:21:04 +02:00
parent 60ba6358ad
commit 86b23af6c6

View File

@@ -3,12 +3,17 @@ package io.emeraldpay.dshackle.upstream.forkchoice
import io.emeraldpay.dshackle.data.BlockContainer
import io.emeraldpay.dshackle.data.BlockId
import io.emeraldpay.dshackle.data.RingSet
import org.slf4j.LoggerFactory
import java.util.concurrent.atomic.AtomicReference
class PriorityForkChoice : ForkChoice {
private val head = AtomicReference<BlockContainer>(null)
private val seenBlocks = RingSet<BlockId>(10)
companion object {
private val log = LoggerFactory.getLogger(PriorityForkChoice::class.java)
}
override fun getHead(): BlockContainer? {
return head.get()
}
@@ -19,17 +24,24 @@ class PriorityForkChoice : ForkChoice {
}
override fun choose(block: BlockContainer): ForkChoice.ChoiceResult {
head.get()?.let {
log.debug("Candidate for priority forkchoice (${block.height}, ${block.nodeRating}), current is (${it.height},${it.nodeRating}")
}
val nwhead = head.updateAndGet { curr ->
if (!filter(block)) {
log.debug("Preparing to deny block ${block.height}")
curr
} else {
log.debug("Preparing to accept block ${block.height}")
seenBlocks.add(block.hash)
block
}
}
if (nwhead.hash == block.hash) {
log.debug("Accepted block ${block.height}")
return ForkChoice.ChoiceResult.Updated(nwhead)
}
log.debug("Denied block ${block.height}")
return ForkChoice.ChoiceResult.Same(nwhead)
}
}