problem: race condition on a new block on head

This commit is contained in:
Igor Artamonov
2020-05-16 22:59:07 -04:00
parent 8d49856ffd
commit c742a62090
9 changed files with 186 additions and 13 deletions

View File

@@ -109,7 +109,6 @@ class TrackEthereumAddressSpec extends Specification {
.expectNext(exp1).as("First block")
.then {
upstreamMock.nextBlock(BlockContainer.from(block2, TestingCommons.objectMapper()))
Thread.sleep(50)
}
.expectNext(exp2).as("Second block")
.thenCancel()

View File

@@ -18,6 +18,7 @@ package io.emeraldpay.dshackle.test
import io.emeraldpay.dshackle.data.BlockContainer
import io.emeraldpay.dshackle.upstream.Head
import org.jetbrains.annotations.NotNull
import org.reactivestreams.Publisher
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
@@ -28,8 +29,12 @@ class EthereumHeadMock implements Head {
private TopicProcessor<BlockContainer> bus = TopicProcessor.create()
private Publisher<BlockContainer> predefined = null
private BlockContainer latest
private List<Runnable> handlers = []
void nextBlock(BlockContainer block) {
handlers.forEach {
it.run()
}
assert block != null
println("New block: ${block.height} / ${block.hash}")
latest = block
@@ -52,4 +57,9 @@ class EthereumHeadMock implements Head {
return Flux.concat(Mono.justOrEmpty(latest), bus).distinctUntilChanged()
}
}
@Override
void onBeforeBlock(@NotNull Runnable handler) {
handlers.add(handler)
}
}

View File

@@ -0,0 +1,117 @@
/**
* Copyright (c) 2020 EmeraldPay, Inc
*
* 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
import io.emeraldpay.dshackle.data.BlockContainer
import io.emeraldpay.dshackle.data.BlockId
import reactor.core.publisher.Flux
import reactor.core.publisher.TopicProcessor
import reactor.test.StepVerifier
import spock.lang.Specification
import java.time.Duration
import java.time.Instant
class AbstractHeadSpec extends Specification {
def blocks = [1L, 2, 3, 4].collect { i ->
byte[] hash = new byte[32]
hash[0] = i as byte
new BlockContainer(i, BlockId.from(hash), BigInteger.valueOf(i), Instant.now(), false, null, null, [])
}
def "Calls beforeBlock on each block"() {
setup:
TopicProcessor<BlockContainer> source = TopicProcessor.create()
def head = new TestHead()
def called = false
when:
head.follow(Flux.from(source))
head.onBeforeBlock {
called = true
}
def act = head.flux
source.onNext(blocks[0])
then:
StepVerifier.create(act)
.expectNext(blocks[0])
.then {
assert called
called = false
source.onNext(blocks[1])
}
.expectNext(blocks[1])
.then {
assert called
source.onComplete()
}
.expectComplete()
.verify(Duration.ofSeconds(1))
}
def "Follows source"() {
setup:
TopicProcessor<BlockContainer> source = TopicProcessor.create()
def head = new TestHead()
when:
head.follow(Flux.from(source))
def act = head.flux
source.onNext(blocks[0])
then:
StepVerifier.create(act)
.expectNext(blocks[0])
.then { source.onNext(blocks[1]) }
.expectNext(blocks[1])
.then { source.onNext(blocks[2]) }
.expectNext(blocks[2])
.then { source.onNext(blocks[3]) }
.expectNext(blocks[3])
.then { source.onComplete() }
.expectComplete()
.verify(Duration.ofSeconds(1))
}
def "Ignores block will less difficulty"() {
setup:
TopicProcessor<BlockContainer> source = TopicProcessor.create()
def head = new TestHead()
def wrongblock = new BlockContainer(
blocks[1].height, BlockId.from(blocks[1].hash.value.clone().tap { it[1] = 0xff as byte }),
blocks[1].difficulty - 1,
Instant.now(),
false, null, null, []
)
when:
head.follow(Flux.from(source))
def act = head.flux
source.onNext(blocks[0])
then:
StepVerifier.create(act)
.expectNext(blocks[0])
.then { source.onNext(blocks[1]) }
.expectNext(blocks[1])
.then { source.onNext(wrongblock) }
.then { source.onNext(blocks[3]) }
.expectNext(blocks[3])
.then { source.onComplete() }
.expectComplete()
.verify(Duration.ofSeconds(1))
}
class TestHead extends AbstractHead {
}
}