solution: refactoring, make ObjectMapper as a global object, not bean

This commit is contained in:
Igor Artamonov
2020-05-18 19:23:48 -04:00
parent 2cfdf392ad
commit 2fc6896a83
73 changed files with 345 additions and 368 deletions

View File

@@ -16,6 +16,7 @@
package io.emeraldpay.dshackle.cache
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.data.BlockContainer
import io.emeraldpay.dshackle.test.TestingCommons
import io.infinitape.etherjar.domain.BlockHash
@@ -31,7 +32,7 @@ class BlockByHeightSpec extends Specification {
String hash1 = "0xd3f34def3c56ba4e701540d15edaff9acd2a1c968a7ff83b3300ab5dfd5f6aab"
String hash2 = "0x4aabdaff9acd2f30d15e00ab5dfd5f6c56ba4ea1c968a7ff8d3f34de70153b33"
ObjectMapper objectMapper = TestingCommons.objectMapper()
ObjectMapper objectMapper = Global.objectMapper
def "Fetch with all data available"() {
setup:
@@ -46,7 +47,7 @@ class BlockByHeightSpec extends Specification {
block.uncles = []
block.transactions = []
BlockContainer.from(block, objectMapper).with {
BlockContainer.from(block).with {
blocks.add(it)
heights.add(it)
}
@@ -81,11 +82,11 @@ class BlockByHeightSpec extends Specification {
block2.transactions = []
BlockContainer.from(block1, objectMapper).with {
BlockContainer.from(block1).with {
blocks.add(it)
heights.add(it)
}
BlockContainer.from(block2, objectMapper).with {
BlockContainer.from(block2).with {
blocks.add(it)
heights.add(it)
}
@@ -124,11 +125,11 @@ class BlockByHeightSpec extends Specification {
block2.uncles = []
block2.transactions = []
BlockContainer.from(block1, objectMapper).with {
BlockContainer.from(block1).with {
blocks.add(it)
heights.add(it)
}
BlockContainer.from(block2, objectMapper).with {
BlockContainer.from(block2).with {
blocks.add(it)
heights.add(it)
}
@@ -153,7 +154,7 @@ class BlockByHeightSpec extends Specification {
block.timestamp = Instant.now()
// add only to heights
BlockContainer.from(block, objectMapper).with {
BlockContainer.from(block).with {
heights.add(it)
}
@@ -177,7 +178,7 @@ class BlockByHeightSpec extends Specification {
block.timestamp = Instant.now()
// add only to blocks
BlockContainer.from(block, objectMapper).with {
BlockContainer.from(block).with {
blocks.add(it)
}

View File

@@ -17,6 +17,7 @@
package io.emeraldpay.dshackle.cache
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.data.BlockContainer
import io.emeraldpay.dshackle.data.BlockId
import io.emeraldpay.dshackle.test.TestingCommons
@@ -35,8 +36,6 @@ class BlocksMemCacheSpec extends Specification {
String hash3 = "0x40d15edaff9acdabd2a1c96fd5f683b3300aad34e7015f34def3c56ba8a7ffb5"
String hash4 = "0xa4e7a75dfd5f6a83b3304dc56bfa0abfd3fef01540d15edafc9683f9acd2a13b"
ObjectMapper objectMapper = TestingCommons.objectMapper()
def "Add and read"() {
setup:
def cache = new BlocksMemCache()
@@ -49,10 +48,10 @@ class BlocksMemCacheSpec extends Specification {
block.transactions = []
when:
cache.add(BlockContainer.from(block, objectMapper))
cache.add(BlockContainer.from(block))
def act = cache.read(BlockId.from(hash1)).block()
then:
objectMapper.readValue(act.json, BlockJson) == block
Global.objectMapper.readValue(act.json, BlockJson) == block
}
def "Keeps only configured amount"() {
@@ -70,7 +69,7 @@ class BlocksMemCacheSpec extends Specification {
block.uncles = []
block.transactions = []
cache.add(BlockContainer.from(block, objectMapper))
cache.add(BlockContainer.from(block))
}
def act1 = cache.read(BlockId.from(hash1)).block()

View File

@@ -16,6 +16,7 @@
package io.emeraldpay.dshackle.cache
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.data.BlockContainer
import io.emeraldpay.dshackle.data.BlockId
import io.emeraldpay.dshackle.data.TxId
@@ -44,7 +45,7 @@ class BlocksRedisCacheSpec extends Specification {
String hash3 = "0x40d15edaff9acdabd2a1c96fd5f683b3300aad34e7015f34def3c56ba8a7ffb5"
String hash4 = "0xa4e7a75dfd5f6a83b3304dc56bfa0abfd3fef01540d15edafc9683f9acd2a13b"
ObjectMapper objectMapper = TestingCommons.objectMapper()
ObjectMapper objectMapper = Global.objectMapper
def setup() {
redis = IntegrationTestingCommons.redisConnection()
@@ -94,7 +95,7 @@ class BlocksRedisCacheSpec extends Specification {
block.uncles = []
when:
cache.add(BlockContainer.from(block, objectMapper)).subscribe()
cache.add(BlockContainer.from(block)).subscribe()
def act = cache.read(BlockId.from(hash1)).block()
then:
act != null
@@ -112,7 +113,7 @@ class BlocksRedisCacheSpec extends Specification {
block.uncles = []
when:
cache.add(BlockContainer.from(block, objectMapper)).subscribe()
cache.add(BlockContainer.from(block)).subscribe()
def act = cache.read(BlockId.from(hash2)).block()
then:
objectMapper.readValue(act.json, BlockJson) == block
@@ -136,7 +137,7 @@ class BlocksRedisCacheSpec extends Specification {
block.uncles = []
when:
cache.add(BlockContainer.from(block, objectMapper)).subscribe()
cache.add(BlockContainer.from(block)).subscribe()
def act = cache.read(BlockId.from(hash2)).block()
then:
act != null

View File

@@ -16,6 +16,7 @@
package io.emeraldpay.dshackle.cache
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.data.BlockContainer
import io.emeraldpay.dshackle.data.TxContainer
import io.emeraldpay.dshackle.test.TestingCommons
@@ -33,15 +34,12 @@ class CachesSpec extends Specification {
String hash1 = "0xd3f34def3c56ba4e701540d15edaff9acd2a1c968a7ff83b3300ab5dfd5f6aab"
String hash2 = "0x4aabdaff9acd2f30d15e00ab5dfd5f6c56ba4ea1c968a7ff8d3f34de70153b33"
ObjectMapper objectMapper = TestingCommons.objectMapper()
def "Evict txes if block updated"() {
setup:
TxMemCache txCache = Mock()
HeightCache heightCache = Mock()
BlocksMemCache blocksCache = Mock()
def caches = Caches.newBuilder()
.setObjectMapper(objectMapper)
.setTxByHash(txCache)
.setBlockByHeight(heightCache)
.setBlockByHash(blocksCache)
@@ -53,7 +51,7 @@ class CachesSpec extends Specification {
block1.totalDifficulty = BigInteger.ONE
block1.timestamp = Instant.now()
block1.transactions = []
block1 = BlockContainer.from(block1, objectMapper)
block1 = BlockContainer.from(block1)
def block2 = new BlockJson()
block2.number = 100
@@ -61,7 +59,7 @@ class CachesSpec extends Specification {
block2.totalDifficulty = BigInteger.ONE
block2.timestamp = Instant.now()
block2.transactions = []
block2 = BlockContainer.from(block2, objectMapper)
block2 = BlockContainer.from(block2)
when:
caches.cache(Caches.Tag.LATEST, block1)
@@ -84,7 +82,6 @@ class CachesSpec extends Specification {
HeightCache heightCache = Mock()
BlocksMemCache blocksCache = Mock()
def caches = Caches.newBuilder()
.setObjectMapper(objectMapper)
.setTxByHash(txCache)
.setBlockByHeight(heightCache)
.setBlockByHash(blocksCache)
@@ -95,14 +92,14 @@ class CachesSpec extends Specification {
block1.hash = BlockHash.from(hash1)
block1.totalDifficulty = BigInteger.ONE
block1.timestamp = Instant.now()
block1 = BlockContainer.from(block1, objectMapper)
block1 = BlockContainer.from(block1)
def block2 = new BlockJson()
block2.number = 100
block2.hash = BlockHash.from(hash2)
block2.totalDifficulty = BigInteger.ONE
block2.timestamp = Instant.now()
block2 = BlockContainer.from(block2, objectMapper)
block2 = BlockContainer.from(block2)
when:
caches.cache(Caches.Tag.LATEST, block1)
@@ -125,7 +122,6 @@ class CachesSpec extends Specification {
HeightCache heightCache = Mock()
BlocksMemCache blocksCache = Mock()
def caches = Caches.newBuilder()
.setObjectMapper(TestingCommons.objectMapper())
.setTxByHash(txCache)
.setBlockByHeight(heightCache)
.setBlockByHash(blocksCache)
@@ -142,7 +138,7 @@ class CachesSpec extends Specification {
]
when:
caches.cache(Caches.Tag.REQUESTED, BlockContainer.from(block, objectMapper))
caches.cache(Caches.Tag.REQUESTED, BlockContainer.from(block))
then:
0 * txCache.add(_)
}
@@ -153,7 +149,6 @@ class CachesSpec extends Specification {
HeightCache heightCache = Mock()
BlocksMemCache blocksCache = Mock()
def caches = Caches.newBuilder()
.setObjectMapper(TestingCommons.objectMapper())
.setTxByHash(txCache)
.setBlockByHeight(heightCache)
.setBlockByHash(blocksCache)
@@ -179,12 +174,12 @@ class CachesSpec extends Specification {
block.totalDifficulty = BigInteger.ONE
block.transactions = [tx1, tx2]
block.timestamp = Instant.now()
block = BlockContainer.from(block, objectMapper)
block = BlockContainer.from(block)
when:
caches.cache(Caches.Tag.REQUESTED, block)
then:
1 * txCache.add(TxContainer.from(tx1, objectMapper))
1 * txCache.add(TxContainer.from(tx2, objectMapper))
1 * txCache.add(TxContainer.from(tx1))
1 * txCache.add(TxContainer.from(tx2))
}
}

View File

@@ -16,6 +16,7 @@
package io.emeraldpay.dshackle.cache
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.data.BlockContainer
import io.emeraldpay.dshackle.test.TestingCommons
import io.infinitape.etherjar.domain.BlockHash
@@ -32,8 +33,6 @@ class HeightCacheSpec extends Specification {
String hash3 = "0x40d15edaff9acdabd2a1c96fd5f683b3300aad34e7015f34def3c56ba8a7ffb5"
String hash4 = "0xa4e7a75dfd5f6a83b3304dc56bfa0abfd3fef01540d15edafc9683f9acd2a13b"
ObjectMapper objectMapper = TestingCommons.objectMapper()
def "Add and read"() {
setup:
def cache = new HeightCache()
@@ -45,7 +44,7 @@ class HeightCacheSpec extends Specification {
block.hash = BlockHash.from(hash)
block.totalDifficulty = BigInteger.ONE
block.timestamp = Instant.now()
cache.add(BlockContainer.from(block, objectMapper))
cache.add(BlockContainer.from(block))
}
def act1 = cache.read(100).block()
@@ -71,7 +70,7 @@ class HeightCacheSpec extends Specification {
block.hash = BlockHash.from(hash)
block.totalDifficulty = BigInteger.ONE
block.timestamp = Instant.now()
cache.add(BlockContainer.from(block, objectMapper))
cache.add(BlockContainer.from(block))
}
def act1 = cache.read(100).block()

View File

@@ -16,6 +16,7 @@
package io.emeraldpay.dshackle.cache
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.data.BlockContainer
import io.emeraldpay.dshackle.data.BlockId
import io.emeraldpay.dshackle.data.TxContainer
@@ -37,7 +38,7 @@ class TxMemCacheSpec extends Specification {
String hash3 = "0x40d15edaff9acdabd2a1c96fd5f683b3300aad34e7015f34def3c56ba8a7ffb5"
String hash4 = "0xa4e7a75dfd5f6a83b3304dc56bfa0abfd3fef01540d15edafc9683f9acd2a13b"
ObjectMapper objectMapper = TestingCommons.objectMapper()
ObjectMapper objectMapper = Global.objectMapper
def "Add and read"() {
setup:
@@ -48,7 +49,7 @@ class TxMemCacheSpec extends Specification {
tx.blockNumber = 100
when:
cache.add(TxContainer.from(tx, objectMapper))
cache.add(TxContainer.from(tx))
def act = cache.read(TxId.from(hash1)).block()
then:
objectMapper.readValue(act.json, TransactionJson.class) == tx
@@ -64,7 +65,7 @@ class TxMemCacheSpec extends Specification {
tx.blockNumber = 100 + i
tx.blockHash = BlockHash.from(hash)
tx.hash = TransactionId.from(hash)
cache.add(TxContainer.from(tx, objectMapper))
cache.add(TxContainer.from(tx))
}
def act1 = cache.read(TxId.from(hash1)).block()
@@ -88,14 +89,14 @@ class TxMemCacheSpec extends Specification {
tx.blockNumber = 100
tx.blockHash = BlockHash.from(hash1)
tx.hash = TransactionId.from(hash)
cache.add(TxContainer.from(tx, objectMapper))
cache.add(TxContainer.from(tx))
}
[hash3, hash4].eachWithIndex { String hash, int i ->
def tx = new TransactionJson()
tx.blockNumber = 101
tx.blockHash = BlockHash.from(hash2)
tx.hash = TransactionId.from(hash)
cache.add(TxContainer.from(tx, objectMapper))
cache.add(TxContainer.from(tx))
}
cache.evict(BlockId.from(hash1))
@@ -122,14 +123,14 @@ class TxMemCacheSpec extends Specification {
tx.blockNumber = 100
tx.blockHash = BlockHash.from(hash1)
tx.hash = TransactionId.from(hash)
cache.add(TxContainer.from(tx, objectMapper))
cache.add(TxContainer.from(tx))
}
[hash3, hash4].eachWithIndex{ String hash, int i ->
def tx = new TransactionJson()
tx.blockNumber = 100
tx.blockHash = BlockHash.from(hash2)
tx.hash = TransactionId.from(hash)
cache.add(TxContainer.from(tx, objectMapper))
cache.add(TxContainer.from(tx))
}
def block = new BlockJson<TransactionRefJson>()
@@ -142,7 +143,7 @@ class TxMemCacheSpec extends Specification {
new TransactionRefJson(TransactionId.from(hash2)),
]
cache.evict(BlockContainer.from(block, objectMapper))
cache.evict(BlockContainer.from(block))
def act1 = cache.read(TxId.from(hash1)).block()
def act2 = cache.read(TxId.from(hash2)).block()

View File

@@ -15,7 +15,8 @@
*/
package io.emeraldpay.dshackle.cache
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.data.BlockContainer
import io.emeraldpay.dshackle.data.BlockId
import io.emeraldpay.dshackle.data.TxContainer
@@ -49,7 +50,7 @@ class TxRedisCacheSpec extends Specification {
String hash4 = "0xa4e7a75dfd5f6a83b3304dc56bfa0abfd3fef01540d15edafc9683f9acd2a13b"
TxRedisCache cache
def objectMapper = TestingCommons.objectMapper()
ObjectMapper objectMapper = Global.objectMapper
def setup() {
StatefulRedisConnection<String, byte[]> redis = IntegrationTestingCommons.redisConnection()
@@ -96,7 +97,7 @@ class TxRedisCacheSpec extends Specification {
tx.nonce = 0
when:
cache.add(TxContainer.from(tx, objectMapper), BlockContainer.from(block, objectMapper)).subscribe()
cache.add(TxContainer.from(tx), BlockContainer.from(block)).subscribe()
def act = cache.read(TxId.from(hash1)).block()
then:
act != null
@@ -121,7 +122,7 @@ class TxRedisCacheSpec extends Specification {
tx.nonce = 0
when:
cache.add(TxContainer.from(tx, objectMapper), BlockContainer.from(block, objectMapper)).subscribe()
cache.add(TxContainer.from(tx), BlockContainer.from(block)).subscribe()
def act = cache.read(TxId.from(tx.hash)).block()
then:
act != null
@@ -162,7 +163,7 @@ class TxRedisCacheSpec extends Specification {
tx.hash = TransactionId.from(hash)
tx.value = Wei.ofEthers(i)
tx.nonce = 0
cache.add(TxContainer.from(tx, objectMapper), BlockContainer.from(block1, objectMapper)).subscribe()
cache.add(TxContainer.from(tx), BlockContainer.from(block1)).subscribe()
}
[hash3, hash4].eachWithIndex{ String hash, int i ->
def tx = new TransactionJson()
@@ -171,11 +172,11 @@ class TxRedisCacheSpec extends Specification {
tx.hash = TransactionId.from(hash)
tx.value = Wei.ofEthers(i)
tx.nonce = 0
cache.add(TxContainer.from(tx, objectMapper), BlockContainer.from(block2, objectMapper)).subscribe()
cache.add(TxContainer.from(tx), BlockContainer.from(block2)).subscribe()
}
cache.evict(BlockContainer.from(block1, objectMapper)).subscribe()
cache.evict(BlockContainer.from(block1)).subscribe()
def act1 = cache.read(TxId.from(hash1)).block()
def act2 = cache.read(TxId.from(hash2)).block()

View File

@@ -42,7 +42,7 @@ class ProxyServerSpec extends Specification {
ProxyServer server = new ProxyServer(
new ProxyConfig(),
new ReadRpcJson(TestingCommons.objectMapper()),
new ReadRpcJson(),
writeRpcJson,
nativeCall,
new TlsSetup(TestingCommons.fileResolver())

View File

@@ -22,7 +22,7 @@ import spock.lang.Specification
class ReadRpcJsonSpec extends Specification {
ReadRpcJson reader = new ReadRpcJson(TestingCommons.objectMapper())
ReadRpcJson reader = new ReadRpcJson()
def "Get first symbol"() {
expect:

View File

@@ -26,7 +26,7 @@ import java.time.Duration
class WriteRpcJsonSpec extends Specification {
WriteRpcJson writer = new WriteRpcJson(TestingCommons.objectMapper())
WriteRpcJson writer = new WriteRpcJson()
def "Write empty array"() {
when:

View File

@@ -16,6 +16,8 @@
*/
package io.emeraldpay.dshackle.quorum
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.test.TestingCommons
import io.emeraldpay.dshackle.upstream.Head
import io.emeraldpay.dshackle.upstream.Upstream
@@ -25,11 +27,11 @@ import spock.lang.Specification
class BroadcastQuorumSpec extends Specification {
def objectMapper = TestingCommons.objectMapper()
ObjectMapper objectMapper = Global.objectMapper
def "Resolved with first after 3 tries"() {
setup:
def q = Spy(new BroadcastQuorum(objectMapper, 3))
def q = Spy(new BroadcastQuorum(3))
def upstream1 = Stub(Upstream)
def upstream2 = Stub(Upstream)
def upstream3 = Stub(Upstream)
@@ -61,7 +63,7 @@ class BroadcastQuorumSpec extends Specification {
def "Remembers first response"() {
setup:
def q = Spy(new BroadcastQuorum(objectMapper, 3))
def q = Spy(new BroadcastQuorum(3))
def upstream1 = Stub(Upstream)
def upstream2 = Stub(Upstream)
def upstream3 = Stub(Upstream)

View File

@@ -25,7 +25,7 @@ class NonEmptyQuorumSpec extends Specification {
def "Fail if too many errors"() {
setup:
def q = Spy(new NonEmptyQuorum(TestingCommons.objectMapper(), 3))
def q = Spy(new NonEmptyQuorum(3))
def upstream1 = Stub(Upstream)
def upstream2 = Stub(Upstream)
def upstream3 = Stub(Upstream)
@@ -57,7 +57,7 @@ class NonEmptyQuorumSpec extends Specification {
def "Fail first if not error"() {
setup:
def q = Spy(new NonEmptyQuorum(TestingCommons.objectMapper(), 3))
def q = Spy(new NonEmptyQuorum(3))
def upstream1 = Stub(Upstream)
def upstream2 = Stub(Upstream)
def upstream3 = Stub(Upstream)
@@ -77,7 +77,7 @@ class NonEmptyQuorumSpec extends Specification {
def "Fail second if first is error"() {
setup:
def q = Spy(new NonEmptyQuorum(TestingCommons.objectMapper(), 3))
def q = Spy(new NonEmptyQuorum(3))
def upstream1 = Stub(Upstream)
def upstream2 = Stub(Upstream)
def upstream3 = Stub(Upstream)
@@ -104,7 +104,7 @@ class NonEmptyQuorumSpec extends Specification {
def "Fail second if first is null"() {
setup:
def q = Spy(new NonEmptyQuorum(TestingCommons.objectMapper(), 3))
def q = Spy(new NonEmptyQuorum(3))
def upstream1 = Stub(Upstream)
def upstream2 = Stub(Upstream)
def upstream3 = Stub(Upstream)

View File

@@ -16,6 +16,8 @@
*/
package io.emeraldpay.dshackle.quorum
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.test.TestingCommons
import io.emeraldpay.dshackle.upstream.Head
import io.emeraldpay.dshackle.upstream.Upstream
@@ -25,11 +27,11 @@ import spock.lang.Specification
class NonceQuorumSpec extends Specification {
def objectMapper = TestingCommons.objectMapper()
ObjectMapper objectMapper = Global.objectMapper
def "Gets max value"() {
setup:
def q = Spy(new NonceQuorum(objectMapper, 3))
def q = Spy(new NonceQuorum(3))
def upstream1 = Stub(Upstream)
def upstream2 = Stub(Upstream)
def upstream3 = Stub(Upstream)
@@ -61,7 +63,7 @@ class NonceQuorumSpec extends Specification {
def "Ignores errors"() {
setup:
def q = Spy(new NonceQuorum(objectMapper, 3))
def q = Spy(new NonceQuorum(3))
def upstream1 = Stub(Upstream)
def upstream2 = Stub(Upstream)
def upstream3 = Stub(Upstream)
@@ -99,7 +101,7 @@ class NonceQuorumSpec extends Specification {
def "Fail if too many errors"() {
setup:
def q = Spy(new NonceQuorum(objectMapper, 3))
def q = Spy(new NonceQuorum(3))
def upstream1 = Stub(Upstream)
def upstream2 = Stub(Upstream)
def upstream3 = Stub(Upstream)

View File

@@ -99,7 +99,7 @@ class QuorumRpcReaderSpec extends Specification {
def apis = new FilteredApis(
[up], Selector.empty
)
def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(TestingCommons.objectMapper(), 3))
def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(3))
when:
def act = reader.read(new JsonRpcRequest("eth_test", []))
@@ -129,7 +129,7 @@ class QuorumRpcReaderSpec extends Specification {
def apis = new FilteredApis(
[up], Selector.empty
)
def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(TestingCommons.objectMapper(), 3))
def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(3))
when:
def act = reader.read(new JsonRpcRequest("eth_test", []))
@@ -159,7 +159,7 @@ class QuorumRpcReaderSpec extends Specification {
def apis = new FilteredApis(
[up], Selector.empty
)
def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(TestingCommons.objectMapper(), 3))
def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(3))
when:
def act = reader.read(new JsonRpcRequest("eth_test", []))
@@ -189,7 +189,7 @@ class QuorumRpcReaderSpec extends Specification {
def apis = new FilteredApis(
[up], Selector.empty
)
def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(TestingCommons.objectMapper(), 3))
def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(3))
when:
def act = reader.read(new JsonRpcRequest("eth_test", []))

View File

@@ -62,7 +62,7 @@ class ValueAwareQuorumSpec extends Specification {
class ValueAwareQuorumImpl extends ValueAwareQuorum {
ValueAwareQuorumImpl() {
super(TestingCommons.objectMapper(), Object)
super(Object)
}
@Override

View File

@@ -16,8 +16,9 @@
*/
package io.emeraldpay.dshackle.rpc
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.api.proto.BlockchainOuterClass
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.quorum.BroadcastQuorum
import io.emeraldpay.dshackle.quorum.QuorumReaderFactory
import io.emeraldpay.dshackle.quorum.QuorumRpcReader
@@ -45,7 +46,7 @@ import java.util.concurrent.TimeoutException
class NativeCallSpec extends Specification {
def objectMapper = TestingCommons.objectMapper()
ObjectMapper objectMapper = Global.objectMapper
def "Tries router first"() {
def routedApi = Mock(Reader) {
@@ -56,7 +57,7 @@ class NativeCallSpec extends Specification {
}
def upstreams = Stub(MultistreamHolder)
def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper())
def nativeCall = new NativeCall(upstreams)
def ctx = new NativeCall.CallContext<NativeCall.ParsedCallDetails>(
1, upstream, Selector.empty, new AlwaysQuorum(),
new NativeCall.ParsedCallDetails("eth_test", [])
@@ -77,7 +78,7 @@ class NativeCallSpec extends Specification {
}
def upstreams = Stub(MultistreamHolder)
def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper())
def nativeCall = new NativeCall(upstreams)
def ctx = new NativeCall.CallContext<NativeCall.ParsedCallDetails>(
15, upstream, Selector.empty, new AlwaysQuorum(),
new NativeCall.ParsedCallDetails("eth_test", [])
@@ -100,7 +101,7 @@ class NativeCallSpec extends Specification {
setup:
def quorum = new AlwaysQuorum()
def nativeCall = new NativeCall(Stub(MultistreamHolder), TestingCommons.objectMapper())
def nativeCall = new NativeCall(Stub(MultistreamHolder))
nativeCall.quorumReaderFactory = Mock(QuorumReaderFactory) {
1 * create(_, _) >> Mock(Reader) {
1 * read(_) >> Mono.just(new QuorumRpcReader.Result("\"foo\"".bytes, 1))
@@ -120,7 +121,7 @@ class NativeCallSpec extends Specification {
setup:
def quorum = new AlwaysQuorum()
def nativeCall = new NativeCall(Stub(MultistreamHolder), TestingCommons.objectMapper())
def nativeCall = new NativeCall(Stub(MultistreamHolder))
nativeCall.quorumReaderFactory = Mock(QuorumReaderFactory) {
1 * create(_, _) >> Mock(Reader) {
1 * read(_) >> Mono.empty()
@@ -140,7 +141,7 @@ class NativeCallSpec extends Specification {
def "Packs call exception into response with id"() {
setup:
def upstreams = Stub(MultistreamHolder)
def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper())
def nativeCall = new NativeCall(upstreams)
when:
def resp = nativeCall.processException(new NativeCall.CallFailure(5, new IllegalArgumentException("test test")))
then:
@@ -157,7 +158,7 @@ class NativeCallSpec extends Specification {
def "Packs unknown exception into response"() {
setup:
def upstreams = Stub(MultistreamHolder)
def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper())
def nativeCall = new NativeCall(upstreams)
when:
def resp = nativeCall.processException(new IllegalArgumentException("test test"))
then:
@@ -173,7 +174,7 @@ class NativeCallSpec extends Specification {
def "Builds normal response"() {
setup:
def upstreams = Stub(MultistreamHolder)
def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper())
def nativeCall = new NativeCall(upstreams)
def json = [jsonrpc:"2.0", id:1, result: "foo"]
when:
@@ -189,7 +190,7 @@ class NativeCallSpec extends Specification {
def "Returns error for invalid chain"() {
setup:
def upstreams = Stub(MultistreamHolder)
def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper())
def nativeCall = new NativeCall(upstreams)
def req = BlockchainOuterClass.NativeCallRequest.newBuilder()
.setChainValue(0)
@@ -212,7 +213,7 @@ class NativeCallSpec extends Specification {
def "Returns error for unsupported chain"() {
setup:
def upstreams = Mock(MultistreamHolder)
def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper())
def nativeCall = new NativeCall(upstreams)
def req = BlockchainOuterClass.NativeCallRequest.newBuilder()
.setChainValue(Chain.TESTNET_MORDEN.id)
@@ -238,7 +239,7 @@ class NativeCallSpec extends Specification {
def "Calls cache before remote"() {
setup:
def upstreams = Stub(MultistreamHolder)
def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper())
def nativeCall = new NativeCall(upstreams)
def api = TestingCommons.api()
def upstream = TestingCommons.aggregatedUpstream(api)
@@ -257,7 +258,7 @@ class NativeCallSpec extends Specification {
def "Uses cached value"() {
setup:
def upstreams = Stub(MultistreamHolder)
def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper())
def nativeCall = new NativeCall(upstreams)
def upstream = TestingCommons.aggregatedUpstream(TestingCommons.api())
def ctx = new NativeCall.CallContext<NativeCall.ParsedCallDetails>(10,

View File

@@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.google.protobuf.ByteString
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.test.EthereumUpstreamMock
import io.emeraldpay.dshackle.test.TestingCommons
@@ -38,7 +39,7 @@ import java.time.Instant
class StreamHeadSpec extends Specification {
ObjectMapper objectMapper = TestingCommons.objectMapper()
ObjectMapper objectMapper = Global.objectMapper
def "Errors on unavailable chain"() {
setup:
@@ -86,9 +87,9 @@ class StreamHeadSpec extends Specification {
)
then:
StepVerifier.create(flux.take(2))
.then { upstream.nextBlock(BlockContainer.from(blocks[0], objectMapper)) }
.then { upstream.nextBlock(BlockContainer.from(blocks[0])) }
.expectNext(heads[0])
.then { upstream.nextBlock(BlockContainer.from(blocks[1], objectMapper)) }
.then { upstream.nextBlock(BlockContainer.from(blocks[1])) }
.expectNext(heads[1])
.expectComplete()
.verify(Duration.ofSeconds(1))

View File

@@ -15,8 +15,10 @@
*/
package io.emeraldpay.dshackle.rpc
import com.fasterxml.jackson.databind.ObjectMapper
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.data.BlockId
import io.emeraldpay.dshackle.test.TestingCommons
@@ -38,11 +40,12 @@ import java.time.Instant
class TrackBitcoinAddressSpec extends Specification {
String hash1 = "0xa0e65cbc1b52a8ca60562112c6060552d882f16f34a9dba2ccdc05c0a6a27c22"
ObjectMapper objectMapper = Global.objectMapper
def "Correct sum from multiple"() {
setup:
def json = this.class.getClassLoader().getResourceAsStream("bitcoin/unspent-one-addr.json")
def unspents = TestingCommons.objectMapper().readValue(json, List)
def unspents = objectMapper.readValue(json, List)
TrackBitcoinAddress track = new TrackBitcoinAddress(Stub(MultistreamHolder))
when:
def total = track.getTotal(Chain.BITCOIN, ["1K7xkspJg7DDKNwzXgoRSDCUxiFsRegsSK"], unspents)
@@ -57,7 +60,7 @@ class TrackBitcoinAddressSpec extends Specification {
def "Correct sum when other addresses"() {
setup:
def json = this.class.getClassLoader().getResourceAsStream("bitcoin/unspent-two-addr.json")
def unspents = TestingCommons.objectMapper().readValue(json, List)
def unspents = objectMapper.readValue(json, List)
TrackBitcoinAddress track = new TrackBitcoinAddress(Stub(MultistreamHolder))
when:
def total = track.getTotal(Chain.BITCOIN, ["1K7xkspJg7DDKNwzXgoRSDCUxiFsRegsSK"], unspents)
@@ -72,7 +75,7 @@ class TrackBitcoinAddressSpec extends Specification {
def "Sum for two addresses"() {
setup:
def json = this.class.getClassLoader().getResourceAsStream("bitcoin/unspent-two-addr.json")
def unspents = TestingCommons.objectMapper().readValue(json, List)
def unspents = objectMapper.readValue(json, List)
TrackBitcoinAddress track = new TrackBitcoinAddress(Stub(MultistreamHolder))
when:
def total = track.getTotal(Chain.BITCOIN, ["1K7xkspJg7DDKNwzXgoRSDCUxiFsRegsSK", "35hK24tcLEWcgNA4JxpvbkNkoAcDGqQPsP"], unspents).sort { it.address.address }
@@ -107,7 +110,7 @@ class TrackBitcoinAddressSpec extends Specification {
def "Zero for unknown address"() {
setup:
def json = this.class.getClassLoader().getResourceAsStream("bitcoin/unspent-two-addr.json")
def unspents = TestingCommons.objectMapper().readValue(json, List)
def unspents = objectMapper.readValue(json, List)
TrackBitcoinAddress track = new TrackBitcoinAddress(Stub(MultistreamHolder))
when:
def total = track.getTotal(Chain.BITCOIN, ["16rCmCmbuWDhPjWTrpQGaU3EPdZF7MTdUk", "1K7xkspJg7DDKNwzXgoRSDCUxiFsRegsSK"], unspents).sort { it.address.address }

View File

@@ -108,7 +108,7 @@ class TrackEthereumAddressSpec extends Specification {
StepVerifier.create(flux)
.expectNext(exp1).as("First block")
.then {
upstreamMock.nextBlock(BlockContainer.from(block2, TestingCommons.objectMapper()))
upstreamMock.nextBlock(BlockContainer.from(block2))
}
.expectNext(exp2).as("Second block")
.thenCancel()

View File

@@ -103,7 +103,7 @@ class TrackEthereumTxSpec extends Specification {
apiMock.answer("eth_getTransactionByHash", [txId], txJson)
apiMock.answer("eth_getBlockByHash", [blockJson.hash.toHex(), false], blockJson)
upstreamMock.nextBlock(BlockContainer.from(blockHeadJson, TestingCommons.objectMapper()))
upstreamMock.nextBlock(BlockContainer.from(blockHeadJson))
when:
def flux = trackTx.subscribe(req)
@@ -301,7 +301,7 @@ class TrackEthereumTxSpec extends Specification {
upstreamMock.blocks = Flux.fromIterable(blocks)
.map { block ->
BlockContainer.from(block, TestingCommons.objectMapper())
BlockContainer.from(block)
}
when:

View File

@@ -19,6 +19,7 @@ package io.emeraldpay.dshackle.test
import com.fasterxml.jackson.databind.ObjectMapper
import com.google.protobuf.ByteString
import io.emeraldpay.api.proto.BlockchainOuterClass
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.reader.Reader
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse
@@ -37,12 +38,11 @@ class EthereumApiMock implements Reader<JsonRpcRequest, JsonRpcResponse> {
private static final Logger log = LoggerFactory.getLogger(this)
List<PredefinedResponse> predefined = []
private ObjectMapper objectMapper
private final ObjectMapper objectMapper = Global.objectMapper
String id = "default"
EthereumApiMock(@NotNull ObjectMapper objectMapper) {
this.objectMapper = objectMapper
EthereumApiMock() {
}
EthereumApiMock answerOnce(@NotNull String method, List<Object> params, Object result) {

View File

@@ -41,8 +41,8 @@ class EthereumUpstreamMock extends EthereumUpstream {
static CallMethods allMethods() {
new AggregatedCallMethods([
new DefaultEthereumMethods(TestingCommons.objectMapper(), Chain.ETHEREUM),
new DefaultBitcoinMethods(TestingCommons.objectMapper()),
new DefaultEthereumMethods(Chain.ETHEREUM),
new DefaultBitcoinMethods(),
new DirectCallMethods(["eth_test"])
])
}
@@ -62,7 +62,7 @@ class EthereumUpstreamMock extends EthereumUpstream {
EthereumUpstreamMock(@NotNull String id, @NotNull Chain chain, @NotNull Reader<JsonRpcRequest, JsonRpcResponse> api, CallMethods methods) {
super(id, chain, api, null,
UpstreamsConfig.Options.getDefaults(), new QuorumForLabels.QuorumItem(1, new UpstreamsConfig.Labels()),
methods, TestingCommons.objectMapper())
methods)
setLag(0)
setStatus(UpstreamAvailability.OK)
start()

View File

@@ -47,7 +47,7 @@ class MultistreamHolderMock implements MultistreamHolder {
if (up instanceof EthereumMultistream) {
upstreams[chain] = up
} else if (up instanceof EthereumUpstream) {
upstreams[chain] = new EthereumMultistreamMock(chain, [up as EthereumUpstream], Caches.default(TestingCommons.objectMapper()))
upstreams[chain] = new EthereumMultistreamMock(chain, [up as EthereumUpstream], Caches.default())
} else {
throw new IllegalArgumentException("Unsupported upstream type ${up.class}")
}
@@ -56,7 +56,7 @@ class MultistreamHolderMock implements MultistreamHolder {
if (up instanceof BitcoinMultistream) {
upstreams[chain] = up
} else if (up instanceof BitcoinUpstream) {
upstreams[chain] = new BitcoinMultistream(chain, [up as BitcoinUpstream], Caches.default(TestingCommons.objectMapper()), TestingCommons.objectMapper())
upstreams[chain] = new BitcoinMultistream(chain, [up as BitcoinUpstream], Caches.default())
} else {
throw new IllegalArgumentException("Unsupported upstream type ${up.class}")
}
@@ -86,7 +86,7 @@ class MultistreamHolderMock implements MultistreamHolder {
@Override
DefaultEthereumMethods getDefaultMethods(@NotNull Chain chain) {
if (target[chain] == null) {
DefaultEthereumMethods targets = new DefaultEthereumMethods(TestingCommons.objectMapper(), chain)
DefaultEthereumMethods targets = new DefaultEthereumMethods(chain)
target[chain] = targets
}
return target[chain]
@@ -102,7 +102,7 @@ class MultistreamHolderMock implements MultistreamHolder {
EthereumReader customReader = null
EthereumMultistreamMock(@NotNull Chain chain, @NotNull List<EthereumUpstream> upstreams, @NotNull Caches caches) {
super(chain, upstreams, caches, TestingCommons.objectMapper())
super(chain, upstreams, caches)
}
@Override

View File

@@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import io.emeraldpay.dshackle.FileResolver
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.cache.Caches
import io.emeraldpay.dshackle.cache.CachesFactory
import io.emeraldpay.dshackle.config.CacheConfig
@@ -38,27 +39,9 @@ import java.text.SimpleDateFormat
class TestingCommons {
static ObjectMapper objectMapper() {
def module = new SimpleModule("EmeraldDShackle", new Version(1, 0, 0, null, null, null))
def objectMapper = new ObjectMapper()
objectMapper.registerModule(module)
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
objectMapper
.setDateFormat(new SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss.SSS"))
.setTimeZone(TimeZone.getTimeZone("UTC"))
return objectMapper
}
static EthereumApiMock api() {
return new EthereumApiMock(objectMapper())
return new EthereumApiMock()
}
static JacksonRpcConverter rpcConverter() {
return new JacksonRpcConverter(objectMapper())
}
static EthereumUpstreamMock upstream(Reader<JsonRpcRequest, JsonRpcResponse> api) {
return new EthereumUpstreamMock(Chain.ETHEREUM, api)
}
@@ -76,13 +59,13 @@ class TestingCommons {
}
static Multistream aggregatedUpstream(EthereumUpstream up) {
return new EthereumMultistream(Chain.ETHEREUM, [up], Caches.default(objectMapper()), objectMapper()).tap {
return new EthereumMultistream(Chain.ETHEREUM, [up], Caches.default()).tap {
start()
}
}
static CachesFactory emptyCaches() {
return new CachesFactory(objectMapper(), new CacheConfig())
return new CachesFactory(new CacheConfig())
}
static FileResolver fileResolver() {

View File

@@ -25,7 +25,7 @@ class CurrentMultistreamHolderSpec extends Specification {
def "add upstream"() {
setup:
def current = new CurrentMultistreamHolder(TestingCommons.objectMapper(), TestingCommons.emptyCaches())
def current = new CurrentMultistreamHolder(TestingCommons.emptyCaches())
def up = new EthereumUpstreamMock("test", Chain.ETHEREUM, TestingCommons.api())
when:
current.update(new UpstreamChange(Chain.ETHEREUM, up, UpstreamChange.ChangeType.ADDED))
@@ -36,7 +36,7 @@ class CurrentMultistreamHolderSpec extends Specification {
def "add multiple upstreams"() {
setup:
def current = new CurrentMultistreamHolder(TestingCommons.objectMapper(), TestingCommons.emptyCaches())
def current = new CurrentMultistreamHolder(TestingCommons.emptyCaches())
def up1 = new EthereumUpstreamMock("test1", Chain.ETHEREUM, TestingCommons.api())
def up2 = new EthereumUpstreamMock("test2", Chain.ETHEREUM_CLASSIC, TestingCommons.api())
def up3 = new EthereumUpstreamMock("test3", Chain.ETHEREUM, TestingCommons.api())
@@ -52,7 +52,7 @@ class CurrentMultistreamHolderSpec extends Specification {
def "remove upstream"() {
setup:
def current = new CurrentMultistreamHolder(TestingCommons.objectMapper(), TestingCommons.emptyCaches())
def current = new CurrentMultistreamHolder(TestingCommons.emptyCaches())
def up1 = new EthereumUpstreamMock("test1", Chain.ETHEREUM, TestingCommons.api())
def up2 = new EthereumUpstreamMock("test2", Chain.ETHEREUM_CLASSIC, TestingCommons.api())
def up3 = new EthereumUpstreamMock("test3", Chain.ETHEREUM, TestingCommons.api())
@@ -70,7 +70,7 @@ class CurrentMultistreamHolderSpec extends Specification {
def "available after adding"() {
setup:
def current = new CurrentMultistreamHolder(TestingCommons.objectMapper(), TestingCommons.emptyCaches())
def current = new CurrentMultistreamHolder(TestingCommons.emptyCaches())
def up1 = new EthereumUpstreamMock("test1", Chain.ETHEREUM, TestingCommons.api())
when:

View File

@@ -16,7 +16,6 @@
*/
package io.emeraldpay.dshackle.upstream
import io.emeraldpay.dshackle.cache.Caches
import io.emeraldpay.dshackle.config.UpstreamsConfig
import io.emeraldpay.dshackle.startup.QuorumForLabels
import io.emeraldpay.dshackle.test.EthereumApiStub
@@ -25,7 +24,6 @@ import io.emeraldpay.dshackle.upstream.calls.DefaultEthereumMethods
import io.emeraldpay.dshackle.upstream.ethereum.EthereumUpstream
import io.emeraldpay.dshackle.upstream.ethereum.EthereumWsFactory
import io.emeraldpay.grpc.Chain
import io.infinitape.etherjar.rpc.ReactorRpcClient
import reactor.test.StepVerifier
import spock.lang.Retry
import spock.lang.Specification
@@ -34,9 +32,7 @@ import java.time.Duration
class FilteredApisSpec extends Specification {
def rpcClient = Stub(ReactorRpcClient)
def objectMapper = TestingCommons.objectMapper()
def ethereumTargets = new DefaultEthereumMethods(objectMapper, Chain.ETHEREUM)
def ethereumTargets = new DefaultEthereumMethods(Chain.ETHEREUM)
def "Verifies labels"() {
setup:
@@ -55,7 +51,7 @@ class FilteredApisSpec extends Specification {
(EthereumWsFactory) null,
new UpstreamsConfig.Options(),
new QuorumForLabels.QuorumItem(1, UpstreamsConfig.Labels.fromMap(it)),
ethereumTargets, TestingCommons.objectMapper()
ethereumTargets
)
}
def matcher = new Selector.LabelMatcher("test", ["foo"])

View File

@@ -31,7 +31,7 @@ class MultistreamSpec extends Specification {
setup:
def up1 = new EthereumUpstreamMock("test1", Chain.ETHEREUM, TestingCommons.api(), new DirectCallMethods(["eth_test1", "eth_test2"]))
def up2 = new EthereumUpstreamMock("test1", Chain.ETHEREUM, TestingCommons.api(), new DirectCallMethods(["eth_test2", "eth_test3"]))
def aggr = new EthereumMultistream(Chain.ETHEREUM, [up1, up2], Caches.default(TestingCommons.objectMapper()), TestingCommons.objectMapper())
def aggr = new EthereumMultistream(Chain.ETHEREUM, [up1, up2], Caches.default())
when:
aggr.onUpstreamsUpdated()
def act = aggr.getMethods()

View File

@@ -86,7 +86,7 @@ class BitcoinRpcHeadSpec extends Specification {
_ * read(new JsonRpcRequest("getblock", [hash1])) >> Mono.just(new JsonRpcResponse(block1.bytes, null))
_ * read(new JsonRpcRequest("getblock", [hash2])) >> Mono.just(new JsonRpcResponse(block2.bytes, null))
}
BitcoinRpcHead head = new BitcoinRpcHead(api, new ExtractBlock(TestingCommons.objectMapper()), Duration.ofMillis(200))
BitcoinRpcHead head = new BitcoinRpcHead(api, new ExtractBlock(), Duration.ofMillis(200))
when:
def act = head.flux.take(2)

View File

@@ -20,7 +20,7 @@ import spock.lang.Specification
class ExtractBlockSpec extends Specification {
ExtractBlock extractBlock = new ExtractBlock(TestingCommons.objectMapper())
ExtractBlock extractBlock = new ExtractBlock()
def "Extract standard block"() {
setup:

View File

@@ -17,6 +17,7 @@
package io.emeraldpay.dshackle.upstream.ethereum
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.data.BlockContainer
import io.emeraldpay.dshackle.test.TestingCommons
import io.infinitape.etherjar.domain.BlockHash
@@ -30,17 +31,16 @@ import java.time.Instant
class DefaultEthereumHeadSpec extends Specification {
DefaultEthereumHead head = new DefaultEthereumHead()
ObjectMapper objectMapper = TestingCommons.objectMapper()
ObjectMapper objectMapper = Global.objectMapper
def blocks = (10L..20L).collect { i ->
BlockContainer.from(
new BlockJson().with {
new BlockJson().tap {
it.number = 10000L + i
it.hash = BlockHash.from("0x3ec2ebf5d0ec474d0ac6bc50d2770d8409ad76e119968e7919f85d5ec89152" + i)
it.totalDifficulty = 11 * i
it.timestamp = Instant.now()
return it
}, objectMapper)
})
}
def "Starts to follow"() {
@@ -90,13 +90,12 @@ class DefaultEthereumHeadSpec extends Specification {
def "Ignores less difficult"() {
when:
def block3less = BlockContainer.from(
new BlockJson().with {
new BlockJson().tap {
it.number = blocks[3].height
it.hash = BlockHash.from(blocks[3].hash.value)
it.totalDifficulty = blocks[3].difficulty - 1
it.timestamp = Instant.now()
return it
}, objectMapper)
})
head.follow(Flux.just(blocks[0], blocks[3], block3less))
def act = head.flux
then:
@@ -109,13 +108,12 @@ class DefaultEthereumHeadSpec extends Specification {
def "Replaces with more difficult"() {
when:
def block3less = BlockContainer.from(
new BlockJson().with {
new BlockJson().tap {
it.number = blocks[3].height
it.hash = BlockHash.from(blocks[3].hash.value)
it.totalDifficulty = blocks[3].difficulty + 1
it.timestamp = Instant.now()
return it
}, objectMapper)
})
head.follow(Flux.just(blocks[0], blocks[3], block3less))
def act = head.flux
then:

View File

@@ -16,6 +16,7 @@
package io.emeraldpay.dshackle.upstream.ethereum
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.cache.BlocksMemCache
import io.emeraldpay.dshackle.cache.TxMemCache
import io.emeraldpay.dshackle.data.BlockContainer
@@ -39,7 +40,7 @@ class EthereumFullBlocksReaderSpec extends Specification {
String hash3 = "0xa4e7a75dfd5f6a83b3304dc56bfa0abfd3fef01540d15edafc9683f9acd2a13b"
String hash4 = "0xd3f34def3c56ba4e701540d15edaff9acd2a1c968a7ff83b3300ab5dfd5f6aab"
ObjectMapper objectMapper = TestingCommons.objectMapper()
ObjectMapper objectMapper = Global.objectMapper
def tx1 = new TransactionJson().with {
it.blockNumber = 100
@@ -113,15 +114,15 @@ class EthereumFullBlocksReaderSpec extends Specification {
def txes = new TxMemCache()
def blocks = new BlocksMemCache()
txes.add(TxContainer.from(tx1, objectMapper))
txes.add(TxContainer.from(tx2, objectMapper))
txes.add(TxContainer.from(tx3, objectMapper))
txes.add(TxContainer.from(tx4, objectMapper))
blocks.add(BlockContainer.from(block1, objectMapper))
blocks.add(BlockContainer.from(block2, objectMapper))
blocks.add(BlockContainer.from(block3, objectMapper))
txes.add(TxContainer.from(tx1))
txes.add(TxContainer.from(tx2))
txes.add(TxContainer.from(tx3))
txes.add(TxContainer.from(tx4))
blocks.add(BlockContainer.from(block1))
blocks.add(BlockContainer.from(block2))
blocks.add(BlockContainer.from(block3))
def full = new EthereumFullBlocksReader(objectMapper, blocks, txes)
def full = new EthereumFullBlocksReader(blocks, txes)
when:
def act = full.read(BlockId.from(block1.hash)).block()
@@ -179,15 +180,15 @@ class EthereumFullBlocksReaderSpec extends Specification {
def txes = new TxMemCache()
def blocks = new BlocksMemCache()
txes.add(TxContainer.from(tx1, objectMapper))
txes.add(TxContainer.from(tx2, objectMapper))
txes.add(TxContainer.from(tx3, objectMapper))
txes.add(TxContainer.from(tx4, objectMapper))
blocks.add(BlockContainer.from(block1, objectMapper))
blocks.add(BlockContainer.from(block2, objectMapper))
blocks.add(BlockContainer.from(block3, objectMapper))
txes.add(TxContainer.from(tx1))
txes.add(TxContainer.from(tx2))
txes.add(TxContainer.from(tx3))
txes.add(TxContainer.from(tx4))
blocks.add(BlockContainer.from(block1))
blocks.add(BlockContainer.from(block2))
blocks.add(BlockContainer.from(block3))
def full = new EthereumFullBlocksReader(objectMapper, blocks, txes)
def full = new EthereumFullBlocksReader(blocks, txes)
when:
def act = full.read(BlockId.from(block3.hash)).block()
@@ -204,10 +205,10 @@ class EthereumFullBlocksReaderSpec extends Specification {
def txes = new TxMemCache()
def blocks = new BlocksMemCache()
txes.add(TxContainer.from(tx1, objectMapper))
blocks.add(BlockContainer.from(block1, objectMapper)) //missing tx2 in cache
txes.add(TxContainer.from(tx1))
blocks.add(BlockContainer.from(block1)) //missing tx2 in cache
def full = new EthereumFullBlocksReader(objectMapper, blocks, txes)
def full = new EthereumFullBlocksReader(blocks, txes)
when:
def act = full.read(BlockId.from(block1.hash)).block()
@@ -221,11 +222,11 @@ class EthereumFullBlocksReaderSpec extends Specification {
def txes = new TxMemCache()
def blocks = new BlocksMemCache()
txes.add(TxContainer.from(tx1, objectMapper))
txes.add(TxContainer.from(tx2, objectMapper))
txes.add(TxContainer.from(tx3, objectMapper))
txes.add(TxContainer.from(tx1))
txes.add(TxContainer.from(tx2))
txes.add(TxContainer.from(tx3))
def full = new EthereumFullBlocksReader(objectMapper, blocks, txes)
def full = new EthereumFullBlocksReader(blocks, txes)
when:
def act = full.read(BlockId.from(block1.hash)).block()

View File

@@ -16,9 +16,7 @@
*/
package io.emeraldpay.dshackle.upstream.ethereum
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.data.BlockContainer
import io.emeraldpay.dshackle.test.TestingCommons
import io.emeraldpay.dshackle.upstream.Head
import io.emeraldpay.dshackle.upstream.HeadLagObserver
import io.emeraldpay.dshackle.upstream.Upstream
@@ -35,8 +33,6 @@ import java.time.Instant
class EthereumHeadLagObserverSpec extends Specification {
ObjectMapper objectMapper = TestingCommons.objectMapper()
def "Updates lag distance"() {
setup:
Head master = Mock()
@@ -53,14 +49,12 @@ class EthereumHeadLagObserverSpec extends Specification {
def blocks = [100, 101, 102].collect { i ->
return BlockContainer.from(
new BlockJson().with {
new BlockJson().tap {
it.number = i
it.totalDifficulty = 2000 + i
it.hash = BlockHash.from("0x3ec2ebf5d0ec474d0ac6bc50d2770d8409ad76e119968e7919f85d5ec8915" + i)
it.timestamp = Instant.now()
return it
},
objectMapper)
})
}
def masterBus = TopicProcessor.create()
@@ -97,14 +91,12 @@ class EthereumHeadLagObserverSpec extends Specification {
def blocks = [100, 101, 102].collect { i ->
return BlockContainer.from(
new BlockJson().with {
new BlockJson().tap {
it.number = i
it.totalDifficulty = 2000 + i
it.hash = BlockHash.from("0x3ec2ebf5d0ec474d0ac6bc50d2770d8409ad76e119968e7919f85d5ec8915" + i)
it.timestamp = Instant.now()
return it
},
objectMapper)
})
}
def upblocks = Flux.fromIterable(blocks)
@@ -137,7 +129,7 @@ class EthereumHeadLagObserverSpec extends Specification {
it.timestamp = Instant.now()
return it
}
delta as Long == observer.extractDistance(BlockContainer.from(top, objectMapper), BlockContainer.from(curr, objectMapper))
delta as Long == observer.extractDistance(BlockContainer.from(top), BlockContainer.from(curr))
where:
topHeight | topDiff | currHeight | currDiff | delta
100 | 1000 | 100 | 1000 | 0

View File

@@ -58,13 +58,12 @@ class EthereumReaderSpec extends Specification {
def "Block by Id reads from cache"() {
setup:
def memCache = Mock(BlocksMemCache) {
1 * read(blockId) >> Mono.just(BlockContainer.from(blockJson, TestingCommons.objectMapper()))
1 * read(blockId) >> Mono.just(BlockContainer.from(blockJson))
}
def caches = Caches.newBuilder()
.setBlockByHash(memCache)
.setObjectMapper(TestingCommons.objectMapper())
.build()
def reader = new EthereumReader(Stub(Multistream), caches, TestingCommons.objectMapper())
def reader = new EthereumReader(Stub(Multistream), caches)
when:
def act = reader.blocksById().read(blockId).block()
@@ -80,13 +79,12 @@ class EthereumReaderSpec extends Specification {
}
def caches = Caches.newBuilder()
.setBlockByHash(memCache)
.setObjectMapper(TestingCommons.objectMapper())
.build()
def api = TestingCommons.api()
api.answer("eth_getBlockByHash", ["0xf85b826fdf98ee0f48f7db001be00472e63ceb056846f4ecac5f0c32878b8ab2", false], blockJson)
def upstream = TestingCommons.aggregatedUpstream(api)
def reader = new EthereumReader(upstream, caches, TestingCommons.objectMapper())
def reader = new EthereumReader(upstream, caches)
when:
def act = reader.blocksById().read(blockId).block()
@@ -102,13 +100,12 @@ class EthereumReaderSpec extends Specification {
}
def caches = Caches.newBuilder()
.setBlockByHash(memCache)
.setObjectMapper(TestingCommons.objectMapper())
.build()
def api = TestingCommons.api()
api.answer("eth_getBlockByHash", ["0xf85b826fdf98ee0f48f7db001be00472e63ceb056846f4ecac5f0c32878b8ab2", false], blockJson)
def upstream = TestingCommons.aggregatedUpstream(api)
def reader = new EthereumReader(upstream, caches, TestingCommons.objectMapper())
def reader = new EthereumReader(upstream, caches)
when:
def act = reader.blocksById().read(blockId).block()
@@ -120,13 +117,12 @@ class EthereumReaderSpec extends Specification {
def "Block by Hash reads from cache"() {
setup:
def memCache = Mock(BlocksMemCache) {
1 * read(blockId) >> Mono.just(BlockContainer.from(blockJson, TestingCommons.objectMapper()))
1 * read(blockId) >> Mono.just(BlockContainer.from(blockJson))
}
def caches = Caches.newBuilder()
.setBlockByHash(memCache)
.setObjectMapper(TestingCommons.objectMapper())
.build()
def reader = new EthereumReader(Stub(Multistream), caches, TestingCommons.objectMapper())
def reader = new EthereumReader(Stub(Multistream), caches)
when:
def act = reader.blocksByHash().read(blockJson.hash).block()
@@ -142,12 +138,11 @@ class EthereumReaderSpec extends Specification {
}
def caches = Caches.newBuilder()
.setBlockByHash(memCache)
.setObjectMapper(TestingCommons.objectMapper())
.build()
def api = TestingCommons.api()
api.answer("eth_getBlockByHash", ["0xf85b826fdf98ee0f48f7db001be00472e63ceb056846f4ecac5f0c32878b8ab2", false], blockJson)
def upstream = TestingCommons.aggregatedUpstream(api)
def reader = new EthereumReader(upstream, caches, TestingCommons.objectMapper())
def reader = new EthereumReader(upstream, caches)
when:
def act = reader.blocksByHash().read(blockJson.hash).block()
@@ -159,13 +154,12 @@ class EthereumReaderSpec extends Specification {
def "Tx by Hash reads from cache"() {
setup:
def memCache = Mock(TxMemCache) {
1 * read(txId) >> Mono.just(TxContainer.from(txJson, TestingCommons.objectMapper()))
1 * read(txId) >> Mono.just(TxContainer.from(txJson))
}
def caches = Caches.newBuilder()
.setTxByHash(memCache)
.setObjectMapper(TestingCommons.objectMapper())
.build()
def reader = new EthereumReader(Stub(Multistream), caches, TestingCommons.objectMapper())
def reader = new EthereumReader(Stub(Multistream), caches)
when:
def act = reader.txByHash().read(txJson.hash).block()
@@ -181,13 +175,12 @@ class EthereumReaderSpec extends Specification {
}
def caches = Caches.newBuilder()
.setTxByHash(memCache)
.setObjectMapper(TestingCommons.objectMapper())
.build()
def api = TestingCommons.api()
api.answer("eth_getTransactionByHash", [txJson.hash.toHex()], txJson)
def upstream = TestingCommons.aggregatedUpstream(api)
def reader = new EthereumReader(upstream, caches, TestingCommons.objectMapper())
def reader = new EthereumReader(upstream, caches)
when:
def act = reader.txByHash().read(txJson.hash).block()
@@ -203,7 +196,7 @@ class EthereumReaderSpec extends Specification {
api.answerOnce("eth_getBalance", ["0x70b91ff87a902b53dc6e2f6bda8bb9b330ccd30c", "latest"], "0xff")
EthereumUpstreamMock upstream = new EthereumUpstreamMock(Chain.ETHEREUM, api)
def upstreams = TestingCommons.aggregatedUpstream(upstream)
def reader = new EthereumReader(upstreams, Caches.default(TestingCommons.objectMapper()), TestingCommons.objectMapper())
def reader = new EthereumReader(upstreams, Caches.default())
reader.start()
when:
@@ -225,7 +218,7 @@ class EthereumReaderSpec extends Specification {
it.number++
it.totalDifficulty = BigInteger.TWO
}
upstream.nextBlock(BlockContainer.from(block2, TestingCommons.objectMapper()))
upstream.nextBlock(BlockContainer.from(block2))
Thread.sleep(50)
act = reader.balance().read(Address.from("0x70b91ff87a902b53dc6e2f6bda8bb9b330ccd30c")).block()

View File

@@ -15,17 +15,12 @@
*/
package io.emeraldpay.dshackle.upstream.ethereum
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.cache.BlocksMemCache
import io.emeraldpay.dshackle.cache.Caches
import io.emeraldpay.dshackle.data.BlockContainer
import io.emeraldpay.dshackle.data.BlockId
import io.emeraldpay.dshackle.test.TestingCommons
import io.infinitape.etherjar.domain.BlockHash
import io.infinitape.etherjar.rpc.ReactorRpcClient
import io.infinitape.etherjar.rpc.json.BlockJson
import io.infinitape.etherjar.rpc.json.TransactionRefJson
import reactor.core.publisher.Mono
import reactor.test.StepVerifier
import spock.lang.Specification
@@ -35,11 +30,9 @@ import java.time.temporal.ChronoUnit
class EthereumWsFactorySpec extends Specification {
ObjectMapper objectMapper = TestingCommons.objectMapper()
def "Fetch block"() {
setup:
def wsf = new EthereumWsFactory(new URI("http://localhost"), new URI("http://localhost"), objectMapper)
def wsf = new EthereumWsFactory(new URI("http://localhost"), new URI("http://localhost"))
def blocksCache = Mock(BlocksMemCache)
def block = new BlockJson<TransactionRefJson>()
@@ -61,7 +54,7 @@ class EthereumWsFactorySpec extends Specification {
then:
StepVerifier.create(ws.flux.take(1))
.expectNext(BlockContainer.from(block, objectMapper))
.expectNext(BlockContainer.from(block))
.expectComplete()
.verify(Duration.ofSeconds(1))
}

View File

@@ -1,11 +1,9 @@
package io.emeraldpay.dshackle.upstream.ethereum
import io.emeraldpay.dshackle.cache.Caches
import io.emeraldpay.dshackle.reader.EmptyReader
import io.emeraldpay.dshackle.test.TestingCommons
import io.emeraldpay.dshackle.upstream.calls.DefaultEthereumMethods
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse
import io.emeraldpay.grpc.Chain
import spock.lang.Specification
@@ -15,13 +13,11 @@ class NativeCallRouterSpec extends Specification {
def "Calls hardcoded"() {
setup:
def methods = new DefaultEthereumMethods(TestingCommons.objectMapper(), Chain.ETHEREUM)
def methods = new DefaultEthereumMethods(Chain.ETHEREUM)
def router = new NativeCallRouter(
TestingCommons.objectMapper(),
new EthereumReader(
TestingCommons.aggregatedUpstream(TestingCommons.api()),
Caches.default(TestingCommons.objectMapper()),
TestingCommons.objectMapper()
Caches.default()
),
methods
)

View File

@@ -21,6 +21,7 @@ import com.google.protobuf.ByteString
import io.emeraldpay.api.proto.BlockchainGrpc
import io.emeraldpay.api.proto.BlockchainOuterClass
import io.emeraldpay.api.proto.Common
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.data.BlockId
import io.emeraldpay.dshackle.test.MockGrpcServer
import io.emeraldpay.dshackle.test.TestingCommons
@@ -39,7 +40,7 @@ import java.util.concurrent.CompletableFuture
class EthereumGrpcUpstreamSpec extends Specification {
MockGrpcServer mockServer = new MockGrpcServer()
ObjectMapper objectMapper = TestingCommons.objectMapper()
ObjectMapper objectMapper = Global.objectMapper
def "Subscribe to head"() {
setup:
@@ -72,7 +73,7 @@ class EthereumGrpcUpstreamSpec extends Specification {
)
}
})
def upstream = new EthereumGrpcUpstream("test", chain, client, objectMapper, new JsonRpcGrpcClient(client, chain, objectMapper))
def upstream = new EthereumGrpcUpstream("test", chain, client, new JsonRpcGrpcClient(client, chain))
upstream.setLag(0)
upstream.init(BlockchainOuterClass.DescribeChain.newBuilder()
.addAllSupportedMethods(["eth_getBlockByHash"])
@@ -129,7 +130,7 @@ class EthereumGrpcUpstreamSpec extends Specification {
)
}
})
def upstream = new EthereumGrpcUpstream("test", Chain.ETHEREUM, client, objectMapper, new JsonRpcGrpcClient(client, Chain.ETHEREUM, objectMapper))
def upstream = new EthereumGrpcUpstream("test", Chain.ETHEREUM, client, new JsonRpcGrpcClient(client, Chain.ETHEREUM))
upstream.setLag(0)
upstream.init(BlockchainOuterClass.DescribeChain.newBuilder()
.addAllSupportedMethods(["eth_getBlockByHash"])
@@ -190,7 +191,7 @@ class EthereumGrpcUpstreamSpec extends Specification {
finished.complete(true)
}
})
def upstream = new EthereumGrpcUpstream("test", chain, client, objectMapper, new JsonRpcGrpcClient(client, chain, objectMapper))
def upstream = new EthereumGrpcUpstream("test", chain, client, new JsonRpcGrpcClient(client, chain))
upstream.setLag(0)
upstream.init(BlockchainOuterClass.DescribeChain.newBuilder()
.addAllSupportedMethods(["eth_getBlockByHash"])

View File

@@ -40,7 +40,7 @@ class JsonRpcHttpClientSpec extends Specification {
def "Make a request"() {
setup:
JsonRpcHttpClient client = new JsonRpcHttpClient("localhost:18332", TestingCommons.objectMapper(), null, null)
JsonRpcHttpClient client = new JsonRpcHttpClient("localhost:18332", null, null)
def resp = '{' +
' "jsonrpc": "2.0",' +
' "result": "0x98de45",' +
@@ -62,7 +62,7 @@ class JsonRpcHttpClientSpec extends Specification {
def "Make request with basic auth"() {
setup:
def auth = new AuthConfig.ClientBasicAuth("user", "passwd")
def client = new JsonRpcHttpClient("localhost:18332", TestingCommons.objectMapper(), auth, null)
def client = new JsonRpcHttpClient("localhost:18332", auth, null)
mockServer.when(
HttpRequest.request()

View File

@@ -24,7 +24,7 @@ class JsonRpcRequestSpec extends Specification {
setup:
def req = new JsonRpcRequest("test_foo", [])
when:
def act = req.toJson(TestingCommons.objectMapper())
def act = req.toJson()
then:
new String(act) == '{"jsonrpc":"2.0","id":1,"method":"test_foo","params":[]}'
}
@@ -33,7 +33,7 @@ class JsonRpcRequestSpec extends Specification {
setup:
def req = new JsonRpcRequest("test_foo", ["0x0000"])
when:
def act = req.toJson(TestingCommons.objectMapper())
def act = req.toJson()
then:
new String(act) == '{"jsonrpc":"2.0","id":1,"method":"test_foo","params":["0x0000"]}'
}
@@ -42,7 +42,7 @@ class JsonRpcRequestSpec extends Specification {
setup:
def req = new JsonRpcRequest("test_foo", ["0x0000", false])
when:
def act = req.toJson(TestingCommons.objectMapper())
def act = req.toJson()
then:
new String(act) == '{"jsonrpc":"2.0","id":1,"method":"test_foo","params":["0x0000",false]}'
}