Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOTICK - Claim debug #4571

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package net.corda.ledger.utxo.token.cache.services

import net.corda.data.ledger.utxo.token.selection.data.TokenAmount
import net.corda.data.ledger.utxo.token.selection.event.TokenPoolCacheEvent
import net.corda.data.ledger.utxo.token.selection.key.TokenPoolCacheKey
import net.corda.data.ledger.utxo.token.selection.state.TokenPoolCacheState
import net.corda.ledger.utxo.token.cache.converters.EntityConverter
import net.corda.ledger.utxo.token.cache.converters.EventConverter
import net.corda.ledger.utxo.token.cache.entities.PoolCacheState
import net.corda.ledger.utxo.token.cache.entities.TokenCache
import net.corda.ledger.utxo.token.cache.entities.TokenEvent
import net.corda.ledger.utxo.token.cache.handlers.TokenEventHandler
import net.corda.messaging.api.processor.StateAndEventProcessor
import net.corda.messaging.api.records.Record
import org.slf4j.LoggerFactory
import java.math.BigDecimal
import java.math.BigInteger

class TokenCacheEventProcessor constructor(
private val eventConverter: EventConverter,
Expand Down Expand Up @@ -48,16 +53,64 @@ class TokenCacheEventProcessor constructor(
"Received an event with and unrecognized payload '${tokenEvent.javaClass}'"
}

val sb = StringBuffer()
sb.appendLine(
"Token Selection Request type = ${handler.javaClass.simpleName} Pool = ${nonNullableState.poolKey}"
)
sb.appendLine("Before Handler:")
sb.append(getTokenSummary(tokenCache, poolCacheState))

val result = handler.handle(tokenCache, poolCacheState, tokenEvent)
?: return StateAndEventProcessor.Response(poolCacheState.toAvro(), listOf())

return StateAndEventProcessor.Response(
poolCacheState.toAvro(),
listOf(result)
)
sb.appendLine("After Handler:")
sb.append(getTokenSummary(tokenCache, poolCacheState))
log.info(sb.toString())

return if (result == null) {
StateAndEventProcessor.Response(poolCacheState.toAvro(), listOf())
} else {
StateAndEventProcessor.Response(
poolCacheState.toAvro(),
listOf(result)
)
}

} catch (e: Exception) {
log.error("Unexpected error while processing event '${event}'. The event will be sent to the DLQ.", e)
return StateAndEventProcessor.Response(state, listOf(), markForDLQ = true)
}
}

private fun getTokenSummary(tokenCache: TokenCache, state: PoolCacheState): String {
val cachedTokenValue = tokenCache.sumOf { it.amount }
val cachedTokenCount = tokenCache.count()
val avroState = state.toAvro()
val sb = StringBuffer()
sb.appendLine("Token Cache Summary:")
sb.appendLine("Token Balance: $cachedTokenValue")
sb.appendLine("Token Count : $cachedTokenCount")
val tokenIndex = tokenCache.associateBy { it.stateRef }

avroState.tokenClaims.forEach {
val claimedTokens = it.claimedTokenStateRefs.mapNotNull { ref -> tokenIndex[ref] }
val claimCount = claimedTokens.size
val claimBalance = claimedTokens.sumOf { token -> token.amount }
val tokens = claimedTokens.joinToString(" ") { token -> "(${token.amount})" }
sb.appendLine(
"Claim : ${it.claimId} Token Count $claimCount Token Balance $claimBalance Tokens:${tokens}"
)
}
return sb.toString()
}

private fun amountToBigDecimal(avroTokenAmount: TokenAmount): BigDecimal {
val unscaledValueBytes = ByteArray(avroTokenAmount.unscaledValue.remaining())
.apply { avroTokenAmount.unscaledValue.get(this) }

avroTokenAmount.unscaledValue.position(0)
return BigDecimal(
BigInteger(unscaledValueBytes),
avroTokenAmount.scale
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import net.corda.data.ledger.utxo.token.selection.state.TokenPoolCacheState
import net.corda.messaging.api.records.Record
import net.corda.ledger.utxo.token.cache.converters.EntityConverter
import net.corda.ledger.utxo.token.cache.converters.EventConverter
import net.corda.ledger.utxo.token.cache.entities.CachedToken
import net.corda.ledger.utxo.token.cache.entities.PoolCacheState
import net.corda.ledger.utxo.token.cache.entities.TokenCache
import net.corda.ledger.utxo.token.cache.entities.TokenEvent
Expand All @@ -28,7 +29,10 @@ class TokenCacheEventProcessorTest {
private val mockHandler = mock<TokenEventHandler<FakeTokenEvent>>()
private val tokenCacheEventHandlerMap = mutableMapOf<Class<*>, TokenEventHandler<in TokenEvent>>()
private val event = FakeTokenEvent()
private val tokenCache = mock<TokenCache>()
private val tokenCache = mock<TokenCache>().apply {
whenever(iterator()).thenReturn(listOf<CachedToken>().iterator())
}

private val cachePoolState = mock<PoolCacheState>()
private val stateIn = TokenPoolCacheState()
private val tokenPoolCacheEvent = TokenPoolCacheEvent(POOL_CACHE_KEY, null)
Expand Down Expand Up @@ -86,7 +90,7 @@ class TokenCacheEventProcessorTest {
fun `forward an event to its handler and return the response`() {
tokenPoolCacheEvent.payload = "message"

val outputState = TokenPoolCacheState()
val outputState = TokenPoolCacheState().apply { tokenClaims = listOf() }
val handlerResponse = Record<String, FlowEvent>("", "", null)

whenever(entityConverter.toTokenCache(stateIn)).thenReturn(tokenCache)
Expand All @@ -109,7 +113,7 @@ class TokenCacheEventProcessorTest {
fun `null state will be defaulted to an empty state before processing`() {
tokenPoolCacheEvent.payload = "message"

val outputState = TokenPoolCacheState()
val outputState = TokenPoolCacheState().apply { tokenClaims = listOf() }
val handlerResponse = Record<String, FlowEvent>("", "", null)

whenever(entityConverter.toTokenCache(any())).thenReturn(tokenCache)
Expand Down