-
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
04acc7f
commit b43d282
Showing
11 changed files
with
515 additions
and
13 deletions.
There are no files selected for viewing
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
Submodule emerald-grpc
updated
from 2c7c32 to e65b28
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
69 changes: 69 additions & 0 deletions
69
src/main/kotlin/io/emeraldpay/dshackle/rpc/NativeCallStream.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,69 @@ | ||
package io.emeraldpay.dshackle.rpc | ||
|
||
import com.google.protobuf.ByteString | ||
import io.emeraldpay.api.proto.BlockchainOuterClass.NativeCallReplyItem | ||
import io.emeraldpay.api.proto.BlockchainOuterClass.NativeCallRequest | ||
import org.springframework.stereotype.Service | ||
import reactor.core.publisher.Flux | ||
import reactor.core.publisher.Mono | ||
import kotlin.math.min | ||
|
||
@Service | ||
class NativeCallStream( | ||
private val nativeCall: NativeCall, | ||
) { | ||
|
||
fun nativeCall( | ||
requestMono: Mono<NativeCallRequest> | ||
): Flux<NativeCallReplyItem> { | ||
return requestMono.flatMapMany { req -> | ||
nativeCall.nativeCall(Mono.just(req)) | ||
.map { StreamNativeResult(it, req.chunkSize) } | ||
.transform { | ||
if (!req.sorted || req.itemsList.size == 1) { | ||
it | ||
} else { | ||
it.sort { o1, o2 -> o1.response.id - o2.response.id } | ||
} | ||
} | ||
}.concatMap { | ||
val chunkSize = it.chunkSize | ||
val response = it.response | ||
if (chunkSize == 0 || response.payload.size() <= chunkSize || !response.succeed) { | ||
Mono.just(response) | ||
} else { | ||
Flux.fromIterable(chunks(response, chunkSize)) | ||
} | ||
} | ||
} | ||
|
||
private fun chunks(response: NativeCallReplyItem, chunkSize: Int): List<NativeCallReplyItem> { | ||
val chunks = mutableListOf<ByteString>() | ||
val responseBytes = response.payload | ||
|
||
for (i in 0 until responseBytes.size() step+chunkSize) { | ||
chunks.add(responseBytes.substring(i, min(i + chunkSize, responseBytes.size()))) | ||
} | ||
|
||
return chunks | ||
.mapIndexed { index, bytes -> | ||
NativeCallReplyItem.newBuilder() | ||
.apply { | ||
id = response.id | ||
payload = bytes | ||
succeed = true | ||
upstreamId = response.upstreamId | ||
chunked = true | ||
finalChunk = index == chunks.size - 1 | ||
if (this.finalChunk && response.hasSignature()) { | ||
signature = response.signature | ||
} | ||
}.build() | ||
} | ||
} | ||
|
||
private data class StreamNativeResult( | ||
val response: NativeCallReplyItem, | ||
val chunkSize: Int | ||
) | ||
} |
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
173 changes: 173 additions & 0 deletions
173
src/test/kotlin/io/emeraldpay/dshackle/rpc/NativeCallStreamTest.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,173 @@ | ||
package io.emeraldpay.dshackle.rpc | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.google.protobuf.ByteString | ||
import io.emeraldpay.api.proto.BlockchainOuterClass | ||
import io.emeraldpay.api.proto.BlockchainOuterClass.NativeCallRequest | ||
import io.emeraldpay.dshackle.Global | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.doReturn | ||
import org.mockito.kotlin.mock | ||
import org.springframework.util.ResourceUtils | ||
import reactor.core.publisher.Flux | ||
import reactor.core.publisher.Mono | ||
import reactor.test.StepVerifier | ||
import java.time.Duration | ||
|
||
class NativeCallStreamTest { | ||
private val upstreamId = "upstreamId" | ||
private val mapper = Global.objectMapper | ||
|
||
@Test | ||
fun `streaming response is equal to the original response`() { | ||
val responseFile = ResourceUtils.getFile("classpath:responses/get-by-number-response.json") | ||
val response = mapper.writeValueAsBytes(mapper.readValue(responseFile, JsonNode::class.java)) | ||
val nativeCallResponse = BlockchainOuterClass.NativeCallReplyItem.newBuilder() | ||
.setId(1) | ||
.setSucceed(true) | ||
.setUpstreamId(upstreamId) | ||
.setPayload(ByteString.copyFrom(response)) | ||
.build() | ||
val nativeCallMock = mock<NativeCall> { | ||
on { nativeCall(any()) } doReturn Flux.just(nativeCallResponse) | ||
} | ||
val nativeCallStream = NativeCallStream(nativeCallMock) | ||
val req = Mono.just( | ||
NativeCallRequest.newBuilder() | ||
.setChunkSize(1000) | ||
.build() | ||
) | ||
|
||
val result = nativeCallStream.nativeCall(req) | ||
.collectList() | ||
.block()!! | ||
.map { it.payload.toByteArray() } | ||
.reduce { acc, bytes -> acc.plus(bytes) } | ||
|
||
assertTrue(response.contentEquals(result)) | ||
} | ||
|
||
@Test | ||
fun `streaming responses is correct`() { | ||
val response = "\"0x1126938\"".toByteArray() | ||
val nativeCallResponse = BlockchainOuterClass.NativeCallReplyItem.newBuilder() | ||
.setId(15) | ||
.setSucceed(true) | ||
.setUpstreamId(upstreamId) | ||
.setPayload(ByteString.copyFrom(response)) | ||
.build() | ||
val nativeCallMock = mock<NativeCall> { | ||
on { nativeCall(any()) } doReturn Flux.just(nativeCallResponse) | ||
} | ||
val nativeCallStream = NativeCallStream(nativeCallMock) | ||
val req = Mono.just( | ||
NativeCallRequest.newBuilder() | ||
.setChunkSize(5) | ||
.build() | ||
) | ||
|
||
val chunkResponse: (Int) -> BlockchainOuterClass.NativeCallReplyItem.Builder = { id -> | ||
BlockchainOuterClass.NativeCallReplyItem.newBuilder() | ||
.setId(id) | ||
.setChunked(true) | ||
.setSucceed(true) | ||
.setUpstreamId(upstreamId) | ||
} | ||
|
||
val result = nativeCallStream.nativeCall(req) | ||
|
||
StepVerifier.create(result) | ||
.expectNext( | ||
chunkResponse(15) | ||
.setPayload(ByteString.copyFrom("\"0x11".toByteArray())) | ||
.build() | ||
) | ||
.expectNext( | ||
chunkResponse(15) | ||
.setPayload(ByteString.copyFrom("26938".toByteArray())) | ||
.build() | ||
) | ||
.expectNext( | ||
chunkResponse(15) | ||
.setFinalChunk(true) | ||
.setPayload(ByteString.copyFrom("\"".toByteArray())) | ||
.build() | ||
) | ||
.expectComplete() | ||
.verify(Duration.ofSeconds(3)) | ||
} | ||
|
||
@Test | ||
fun `no streaming if response is too small`() { | ||
val response = "\"0x1\"".toByteArray() | ||
val nativeCallResponse = BlockchainOuterClass.NativeCallReplyItem.newBuilder() | ||
.setId(15) | ||
.setSucceed(true) | ||
.setUpstreamId(upstreamId) | ||
.setPayload(ByteString.copyFrom(response)) | ||
.build() | ||
val nativeCallMock = mock<NativeCall> { | ||
on { nativeCall(any()) } doReturn Flux.just(nativeCallResponse) | ||
} | ||
val nativeCallStream = NativeCallStream(nativeCallMock) | ||
val req = Mono.just( | ||
NativeCallRequest.newBuilder() | ||
.setChunkSize(1000) | ||
.build() | ||
) | ||
|
||
val result = nativeCallStream.nativeCall(req) | ||
|
||
StepVerifier.create(result) | ||
.expectNext( | ||
nativeCallResponse | ||
) | ||
.expectComplete() | ||
.verify(Duration.ofSeconds(3)) | ||
} | ||
|
||
@Test | ||
fun `sort responses by request id is correct`() { | ||
val response = "\"0x1\"".toByteArray() | ||
val response2 = "\"0x2\"".toByteArray() | ||
val response3 = "\"0x3\"".toByteArray() | ||
|
||
val nativeCallResponse: (Int, ByteArray) -> BlockchainOuterClass.NativeCallReplyItem = { id, resp -> | ||
BlockchainOuterClass.NativeCallReplyItem.newBuilder() | ||
.setId(id) | ||
.setChunked(true) | ||
.setSucceed(true) | ||
.setUpstreamId(upstreamId) | ||
.setPayload(ByteString.copyFrom(resp)) | ||
.build() | ||
} | ||
val nativeCallMock = mock<NativeCall> { | ||
on { nativeCall(any()) } doReturn Flux.just( | ||
nativeCallResponse(1, response), nativeCallResponse(2, response2), nativeCallResponse(3, response3) | ||
).flatMap { | ||
when (it.id) { | ||
1 -> Mono.just(it).delayElement(Duration.ofMillis(200)) | ||
2 -> Mono.just(it).delayElement(Duration.ofMillis(100)) | ||
else -> Mono.just(it) | ||
} | ||
} | ||
} | ||
val nativeCallStream = NativeCallStream(nativeCallMock) | ||
val req = Mono.just( | ||
NativeCallRequest.newBuilder() | ||
.setSorted(true) | ||
.build() | ||
) | ||
|
||
val result = nativeCallStream.nativeCall(req) | ||
|
||
StepVerifier.create(result) | ||
.expectNextMatches { it.payload.toByteArray().contentEquals(response) } | ||
.expectNextMatches { it.payload.toByteArray().contentEquals(response2) } | ||
.expectNextMatches { it.payload.toByteArray().contentEquals(response3) } | ||
.expectComplete() | ||
.verify(Duration.ofSeconds(3)) | ||
} | ||
} |
Oops, something went wrong.