problem: duplicate logic for Head following
solution: refactor to default implementation
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* Copyright (c) 2019 ETCDEV GmbH
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.emeraldpay.dshackle.upstream.ethereum
|
||||
|
||||
import io.infinitape.etherjar.domain.BlockHash
|
||||
import io.infinitape.etherjar.rpc.json.BlockJson
|
||||
import reactor.core.publisher.Flux
|
||||
import reactor.test.StepVerifier
|
||||
import spock.lang.Specification
|
||||
|
||||
class DefaultEthereumHeadSpec extends Specification {
|
||||
|
||||
DefaultEthereumHead head = new DefaultEthereumHead()
|
||||
|
||||
def blocks = (10L..20L).collect { i ->
|
||||
new BlockJson().with {
|
||||
it.number = 10000L + i
|
||||
it.hash = BlockHash.from("0x3ec2ebf5d0ec474d0ac6bc50d2770d8409ad76e119968e7919f85d5ec89152" + i)
|
||||
it.totalDifficulty = 11 * i
|
||||
return it
|
||||
}
|
||||
}
|
||||
|
||||
def "Starts to follow"() {
|
||||
when:
|
||||
head.follow(Flux.just(blocks[0]))
|
||||
def act = head.flux
|
||||
then:
|
||||
StepVerifier.create(act)
|
||||
.expectNext(blocks[0])
|
||||
.expectComplete()
|
||||
}
|
||||
|
||||
def "Follows normal order"() {
|
||||
when:
|
||||
head.follow(Flux.just(blocks[0], blocks[1], blocks[3]))
|
||||
def act = head.flux
|
||||
then:
|
||||
StepVerifier.create(act)
|
||||
.expectNext(blocks[0])
|
||||
.expectNext(blocks[1])
|
||||
.expectNext(blocks[3])
|
||||
.expectComplete()
|
||||
}
|
||||
|
||||
def "Ignores old"() {
|
||||
when:
|
||||
head.follow(Flux.just(blocks[0], blocks[3], blocks[1]))
|
||||
def act = head.flux
|
||||
then:
|
||||
StepVerifier.create(act)
|
||||
.expectNext(blocks[0])
|
||||
.expectNext(blocks[3])
|
||||
.expectComplete()
|
||||
}
|
||||
|
||||
def "Ignores repeating"() {
|
||||
when:
|
||||
head.follow(Flux.just(blocks[0], blocks[3], blocks[3], blocks[3], blocks[2], blocks[3]))
|
||||
def act = head.flux
|
||||
then:
|
||||
StepVerifier.create(act)
|
||||
.expectNext(blocks[0])
|
||||
.expectNext(blocks[3])
|
||||
.expectComplete()
|
||||
}
|
||||
|
||||
def "Ignores less difficult"() {
|
||||
when:
|
||||
def block3less = new BlockJson().with {
|
||||
it.number = blocks[3].number
|
||||
it.hash = blocks[3].hash
|
||||
it.totalDifficulty = blocks[3].totalDifficulty - 1
|
||||
return it
|
||||
}
|
||||
head.follow(Flux.just(blocks[0], blocks[3], block3less))
|
||||
def act = head.flux
|
||||
then:
|
||||
StepVerifier.create(act)
|
||||
.expectNext(blocks[0])
|
||||
.expectNext(blocks[3])
|
||||
.expectComplete()
|
||||
}
|
||||
|
||||
def "Replaces with more difficult"() {
|
||||
when:
|
||||
def block3less = new BlockJson().with {
|
||||
it.number = blocks[3].number
|
||||
it.hash = blocks[3].hash
|
||||
it.totalDifficulty = blocks[3].totalDifficulty + 1
|
||||
return it
|
||||
}
|
||||
head.follow(Flux.just(blocks[0], blocks[3], block3less))
|
||||
def act = head.flux
|
||||
then:
|
||||
StepVerifier.create(act)
|
||||
.expectNext(blocks[0])
|
||||
.expectNext(block3less)
|
||||
.expectComplete()
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,7 @@ import io.infinitape.etherjar.rpc.JacksonRpcConverter
|
||||
import io.infinitape.etherjar.rpc.RpcClient
|
||||
import io.infinitape.etherjar.rpc.emerald.EmeraldGrpcTransport
|
||||
import io.infinitape.etherjar.rpc.json.BlockJson
|
||||
import reactor.test.StepVerifier
|
||||
import spock.lang.Specification
|
||||
|
||||
import java.time.Duration
|
||||
@@ -90,9 +91,6 @@ class GrpcUpstreamSpec extends Specification {
|
||||
|
||||
def "Follows difficulty, ignores less difficult"() {
|
||||
setup:
|
||||
def callData = [:]
|
||||
def finished = new CompletableFuture<Boolean>()
|
||||
def chain = Chain.ETHEREUM
|
||||
def api = TestingCommons.api(Stub(RpcClient))
|
||||
def block1 = new BlockJson().with {
|
||||
it.number = 650246
|
||||
@@ -130,19 +128,17 @@ class GrpcUpstreamSpec extends Specification {
|
||||
.setWeight(ByteString.copyFrom(block2.totalDifficulty.toByteArray()))
|
||||
.build()
|
||||
)
|
||||
finished.complete(true)
|
||||
}
|
||||
})
|
||||
def transport = EmeraldGrpcTransport.newBuilder().forChannel(client.channel).build()
|
||||
def upstream = new GrpcUpstream("test", chain, client, objectMapper, transport)
|
||||
def upstream = new GrpcUpstream("test", Chain.ETHEREUM, client, objectMapper, transport)
|
||||
upstream.setLag(0)
|
||||
upstream.init(BlockchainOuterClass.DescribeChain.newBuilder()
|
||||
.addAllSupportedMethods(["eth_getBlockByHash"])
|
||||
.build())
|
||||
when:
|
||||
upstream.start()
|
||||
finished.get()
|
||||
def h = upstream.head.getFlux().next().block(Duration.ofSeconds(1))
|
||||
def h = upstream.head.getFlux().take(Duration.ofSeconds(1)).last().block()
|
||||
then:
|
||||
upstream.status == UpstreamAvailability.OK
|
||||
h.hash == BlockHash.from("0x50d26e119968e791970d84a7bf5d0ec474d3ec2ef85d5ec8915210ac6bc09ad7")
|
||||
@@ -203,7 +199,7 @@ class GrpcUpstreamSpec extends Specification {
|
||||
when:
|
||||
upstream.start()
|
||||
finished.get()
|
||||
def h = upstream.head.getFlux().next().block(Duration.ofSeconds(1))
|
||||
def h = upstream.head.getFlux().take(Duration.ofSeconds(1)).last().block()
|
||||
then:
|
||||
upstream.status == UpstreamAvailability.OK
|
||||
h.hash == BlockHash.from("0x3ec2ebf5d0ec474d0ac6bc50d2770d8409ad76e119968e7919f85d5ec891521a")
|
||||
|
||||
Reference in New Issue
Block a user