-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5590f6
commit eddaf98
Showing
22 changed files
with
1,276 additions
and
232 deletions.
There are no files selected for viewing
Submodule emerald-grpc
updated
from 088e30 to a81672
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
src/main/kotlin/io/emeraldpay/dshackle/rpc/ChainEventMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package io.emeraldpay.dshackle.rpc | ||
|
||
import com.google.protobuf.ByteString | ||
import io.emeraldpay.api.proto.BlockchainOuterClass | ||
import io.emeraldpay.api.proto.BlockchainOuterClass.SupportedMethodsEvent | ||
import io.emeraldpay.api.proto.Common | ||
import io.emeraldpay.dshackle.data.BlockContainer | ||
import io.emeraldpay.dshackle.startup.QuorumForLabels | ||
import io.emeraldpay.dshackle.upstream.Capability | ||
import io.emeraldpay.dshackle.upstream.UpstreamAvailability | ||
import io.emeraldpay.dshackle.upstream.finalization.FinalizationData | ||
import io.emeraldpay.dshackle.upstream.lowerbound.LowerBoundData | ||
import io.emeraldpay.dshackle.upstream.lowerbound.LowerBoundType | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class ChainEventMapper { | ||
|
||
fun mapHead(head: BlockContainer): BlockchainOuterClass.ChainEvent { | ||
return BlockchainOuterClass.ChainEvent.newBuilder() | ||
.setHead( | ||
BlockchainOuterClass.HeadEvent.newBuilder() | ||
.setHeight(head.height) | ||
.setSlot(head.slot) | ||
.setTimestamp(head.timestamp.toEpochMilli()) | ||
.setWeight(ByteString.copyFrom(head.difficulty.toByteArray())) | ||
.setBlockId(head.hash.toHex()) | ||
.setParentBlockId(head.parentHash?.toHex() ?: "") | ||
.build(), | ||
) | ||
.build() | ||
} | ||
|
||
fun mapCapabilities(capabilities: Collection<Capability>): BlockchainOuterClass.ChainEvent { | ||
val caps = capabilities.map { | ||
when (it) { | ||
Capability.RPC -> BlockchainOuterClass.Capabilities.CAP_CALLS | ||
Capability.BALANCE -> BlockchainOuterClass.Capabilities.CAP_BALANCE | ||
Capability.WS_HEAD -> BlockchainOuterClass.Capabilities.CAP_WS_HEAD | ||
} | ||
} | ||
|
||
return BlockchainOuterClass.ChainEvent.newBuilder() | ||
.setCapabilitiesEvent( | ||
BlockchainOuterClass.CapabilitiesEvent.newBuilder() | ||
.addAllCapabilities(caps) | ||
.build(), | ||
) | ||
.build() | ||
} | ||
|
||
fun mapNodeDetails(nodeDetails: Collection<QuorumForLabels.QuorumItem>): BlockchainOuterClass.ChainEvent { | ||
val details = nodeDetails.map { | ||
BlockchainOuterClass.NodeDetails.newBuilder() | ||
.setQuorum(it.quorum) | ||
.addAllLabels( | ||
it.labels.entries.map { label -> | ||
BlockchainOuterClass.Label.newBuilder() | ||
.setName(label.key) | ||
.setValue(label.value) | ||
.build() | ||
}, | ||
).build() | ||
} | ||
|
||
return BlockchainOuterClass.ChainEvent.newBuilder() | ||
.setNodesEvent( | ||
BlockchainOuterClass.NodeDetailsEvent.newBuilder() | ||
.addAllNodes(details) | ||
.build(), | ||
) | ||
.build() | ||
} | ||
|
||
fun mapFinalizationData(finalizationData: Collection<FinalizationData>): BlockchainOuterClass.ChainEvent { | ||
val data = finalizationData.map { | ||
Common.FinalizationData.newBuilder() | ||
.setHeight(it.height) | ||
.setType(it.type.toProtoFinalizationType()) | ||
.build() | ||
} | ||
|
||
return BlockchainOuterClass.ChainEvent.newBuilder() | ||
.setFinalizationDataEvent( | ||
BlockchainOuterClass.FinalizationDataEvent.newBuilder() | ||
.addAllFinalizationData(data) | ||
.build(), | ||
) | ||
.build() | ||
} | ||
|
||
fun mapLowerBounds(lowerBounds: Collection<LowerBoundData>): BlockchainOuterClass.ChainEvent { | ||
val data = lowerBounds | ||
.map { | ||
BlockchainOuterClass.LowerBound.newBuilder() | ||
.setLowerBoundTimestamp(it.timestamp) | ||
.setLowerBoundType(mapLowerBoundType(it.type)) | ||
.setLowerBoundValue(it.lowerBound) | ||
.build() | ||
} | ||
|
||
return BlockchainOuterClass.ChainEvent.newBuilder() | ||
.setLowerBoundsEvent( | ||
BlockchainOuterClass.LowerBoundEvent.newBuilder() | ||
.addAllLowerBounds(data) | ||
.build(), | ||
) | ||
.build() | ||
} | ||
|
||
fun chainStatus(status: UpstreamAvailability): BlockchainOuterClass.ChainEvent { | ||
return BlockchainOuterClass.ChainEvent.newBuilder() | ||
.setStatus( | ||
BlockchainOuterClass.ChainStatus.newBuilder() | ||
.setAvailability(Common.AvailabilityEnum.forNumber(status.grpcId)) | ||
.build(), | ||
) | ||
.build() | ||
} | ||
|
||
fun supportedMethods(methods: Collection<String>): BlockchainOuterClass.ChainEvent { | ||
return BlockchainOuterClass.ChainEvent.newBuilder() | ||
.setSupportedMethodsEvent( | ||
SupportedMethodsEvent.newBuilder() | ||
.addAllMethods(methods) | ||
.build(), | ||
) | ||
.build() | ||
} | ||
|
||
fun supportedSubs(subs: Collection<String>): BlockchainOuterClass.ChainEvent { | ||
return BlockchainOuterClass.ChainEvent.newBuilder() | ||
.setSupportedSubscriptionsEvent( | ||
BlockchainOuterClass.SupportedSubscriptionsEvent.newBuilder() | ||
.addAllSubs(subs) | ||
.build(), | ||
) | ||
.build() | ||
} | ||
|
||
private fun mapLowerBoundType(type: LowerBoundType): BlockchainOuterClass.LowerBoundType { | ||
return when (type) { | ||
LowerBoundType.SLOT -> BlockchainOuterClass.LowerBoundType.LOWER_BOUND_SLOT | ||
LowerBoundType.UNKNOWN -> BlockchainOuterClass.LowerBoundType.LOWER_BOUND_UNSPECIFIED | ||
LowerBoundType.STATE -> BlockchainOuterClass.LowerBoundType.LOWER_BOUND_STATE | ||
LowerBoundType.BLOCK -> BlockchainOuterClass.LowerBoundType.LOWER_BOUND_BLOCK | ||
LowerBoundType.TX -> BlockchainOuterClass.LowerBoundType.LOWER_BOUND_TX | ||
LowerBoundType.LOGS -> BlockchainOuterClass.LowerBoundType.LOWER_BOUND_LOGS | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
src/main/kotlin/io/emeraldpay/dshackle/rpc/SubscribeChainStatus.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package io.emeraldpay.dshackle.rpc | ||
|
||
import io.emeraldpay.api.proto.BlockchainOuterClass | ||
import io.emeraldpay.api.proto.Common | ||
import io.emeraldpay.dshackle.Global | ||
import io.emeraldpay.dshackle.data.BlockContainer | ||
import io.emeraldpay.dshackle.upstream.Multistream | ||
import io.emeraldpay.dshackle.upstream.MultistreamHolder | ||
import io.emeraldpay.dshackle.upstream.state.MultistreamStateEvent | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.stereotype.Service | ||
import reactor.core.publisher.Flux | ||
import reactor.core.publisher.Mono | ||
import reactor.kotlin.core.publisher.switchIfEmpty | ||
|
||
@Service | ||
class SubscribeChainStatus( | ||
private val multistreamHolder: MultistreamHolder, | ||
private val chainEventMapper: ChainEventMapper, | ||
) { | ||
|
||
companion object { | ||
private val log = LoggerFactory.getLogger(SubscribeChainStatus::class.java) | ||
} | ||
|
||
fun chainStatuses(): Flux<BlockchainOuterClass.SubscribeChainStatusResponse> { | ||
return Flux.merge( | ||
// we need to track not only multistreams with upstreams but all of them | ||
// because upstreams can be added in runtime with hot config reload | ||
multistreamHolder.all() | ||
.filter { Common.ChainRef.forNumber(it.chain.id) != null } | ||
.map { ms -> | ||
Flux.concat( | ||
// the first event must be filled with all fields | ||
firstFullEvent(ms), | ||
Flux.merge( | ||
// head events are separated from others | ||
headEvents(ms), | ||
multistreamEvents(ms), | ||
), | ||
) | ||
}, | ||
).doOnError { | ||
log.error("Error during sending chain statuses", it) | ||
} | ||
} | ||
|
||
private fun multistreamEvents(ms: Multistream): Flux<BlockchainOuterClass.SubscribeChainStatusResponse> { | ||
return ms.stateEvents() | ||
.filter { it.isNotEmpty() } | ||
.map { events -> | ||
val response = BlockchainOuterClass.SubscribeChainStatusResponse.newBuilder() | ||
val chainDescription = BlockchainOuterClass.ChainDescription.newBuilder() | ||
.setChain(Common.ChainRef.forNumber(ms.chain.id)) | ||
|
||
events.forEach { | ||
chainDescription.addChainEvent(processMsEvent(it)) | ||
} | ||
|
||
response.setChainDescription(chainDescription.build()) | ||
response.build() | ||
} | ||
} | ||
|
||
private fun processMsEvent(event: MultistreamStateEvent): BlockchainOuterClass.ChainEvent { | ||
return when (event) { | ||
is MultistreamStateEvent.CapabilitiesEvent -> chainEventMapper.mapCapabilities(event.caps) | ||
is MultistreamStateEvent.FinalizationEvent -> chainEventMapper.mapFinalizationData(event.finalizationData) | ||
is MultistreamStateEvent.LowerBoundsEvent -> chainEventMapper.mapLowerBounds(event.lowerBounds) | ||
is MultistreamStateEvent.MethodsEvent -> chainEventMapper.supportedMethods(event.methods) | ||
is MultistreamStateEvent.NodeDetailsEvent -> chainEventMapper.mapNodeDetails(event.details) | ||
is MultistreamStateEvent.StatusEvent -> chainEventMapper.chainStatus(event.status) | ||
is MultistreamStateEvent.SubsEvent -> chainEventMapper.supportedSubs(event.subs) | ||
} | ||
} | ||
|
||
private fun firstFullEvent(ms: Multistream): Mono<BlockchainOuterClass.SubscribeChainStatusResponse> { | ||
return Mono.justOrEmpty(ms.getHead().getCurrent()) | ||
.map { toFullResponse(it!!, ms) } | ||
.switchIfEmpty { | ||
// in case if there is still no head we mush wait until we get it | ||
ms.getHead() | ||
.getFlux() | ||
.next() | ||
.map { toFullResponse(it!!, ms) } | ||
} | ||
} | ||
|
||
private fun headEvents(ms: Multistream): Flux<BlockchainOuterClass.SubscribeChainStatusResponse> { | ||
return ms.getHead() | ||
.getFlux() | ||
.skip(1) | ||
.map { | ||
BlockchainOuterClass.SubscribeChainStatusResponse.newBuilder() | ||
.setChainDescription( | ||
BlockchainOuterClass.ChainDescription.newBuilder() | ||
.setChain(Common.ChainRef.forNumber(ms.chain.id)) | ||
.addChainEvent(chainEventMapper.mapHead(it)) | ||
.build(), | ||
) | ||
.build() | ||
} | ||
} | ||
|
||
private fun toFullResponse(head: BlockContainer, ms: Multistream): BlockchainOuterClass.SubscribeChainStatusResponse { | ||
return BlockchainOuterClass.SubscribeChainStatusResponse.newBuilder() | ||
.setChainDescription( | ||
BlockchainOuterClass.ChainDescription.newBuilder() | ||
.setChain(Common.ChainRef.forNumber(ms.chain.id)) | ||
.addChainEvent(chainEventMapper.chainStatus(ms.getStatus())) | ||
.addChainEvent(chainEventMapper.mapHead(head)) | ||
.addChainEvent(chainEventMapper.supportedMethods(ms.getMethods().getSupportedMethods())) | ||
.addChainEvent(chainEventMapper.supportedSubs(ms.getEgressSubscription().getAvailableTopics())) | ||
.addChainEvent(chainEventMapper.mapCapabilities(ms.getCapabilities())) | ||
.addChainEvent(chainEventMapper.mapLowerBounds(ms.getLowerBounds())) | ||
.addChainEvent(chainEventMapper.mapFinalizationData(ms.getFinalizations())) | ||
.addChainEvent(chainEventMapper.mapNodeDetails(ms.getQuorumLabels())) | ||
.build(), | ||
) | ||
.setBuildInfo( | ||
BlockchainOuterClass.BuildInfo.newBuilder() | ||
.setVersion(Global.version) | ||
.build(), | ||
) | ||
.setFullResponse(true) | ||
.build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.