-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate drpc-logs-oracle (#1096) (#335)
* add oracle jar to build (#1096) * add oracle config (#1096) * add eth_getLogsEstimate (#1096) * add scheduler for oracle (#1096) * fix tests :[ (#1096)
- Loading branch information
1 parent
a7f74bb
commit 5a154ac
Showing
33 changed files
with
369 additions
and
73 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
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
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/io/emeraldpay/dshackle/config/IndexConfig.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,21 @@ | ||
package io.emeraldpay.dshackle.config | ||
|
||
import io.emeraldpay.dshackle.Chain | ||
|
||
class IndexConfig { | ||
var items: HashMap<Chain, Index> = HashMap<Chain, Index>() | ||
|
||
class Index( | ||
var rpc: String, | ||
var store: String, | ||
var ram_limit: Long?, | ||
) | ||
|
||
fun isChainEnabled(chain: Chain): Boolean { | ||
return items.containsKey(chain) | ||
} | ||
|
||
fun getByChain(chain: Chain): Index? { | ||
return items.get(chain) | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/kotlin/io/emeraldpay/dshackle/config/IndexConfigReader.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,62 @@ | ||
package io.emeraldpay.dshackle.config | ||
|
||
import io.emeraldpay.dshackle.Chain | ||
import io.emeraldpay.dshackle.Global | ||
import io.emeraldpay.dshackle.foundation.YamlConfigReader | ||
import org.apache.commons.lang3.StringUtils | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.util.unit.DataSize | ||
import org.yaml.snakeyaml.nodes.MappingNode | ||
|
||
class IndexConfigReader : YamlConfigReader<IndexConfig>() { | ||
|
||
companion object { | ||
private val log = LoggerFactory.getLogger(IndexConfigReader::class.java) | ||
} | ||
|
||
override fun read(input: MappingNode?): IndexConfig? { | ||
return getList<MappingNode>(input, "index")?.let { items -> | ||
val config = IndexConfig() | ||
|
||
items.value.map { | ||
val blockchainRaw = getValueAsString(it, "chain") | ||
if (blockchainRaw == null || StringUtils.isEmpty(blockchainRaw) || Global.chainById(blockchainRaw) == Chain.UNSPECIFIED) { | ||
throw InvalidConfigYamlException(filename, it.startMark, "Invalid blockchain or not specified") | ||
} | ||
|
||
val blockchain = Global.chainById(blockchainRaw) | ||
if (config.items.containsKey(blockchain)) { | ||
throw InvalidConfigYamlException(filename, it.startMark, "Duplicated indexes") | ||
} | ||
|
||
val rpc = getValueAsString(it, "rpc") | ||
if (rpc == null || StringUtils.isEmpty(rpc)) { | ||
throw InvalidConfigYamlException(filename, it.startMark, "Invalid rpc specified") | ||
} | ||
|
||
val store = getValueAsString(it, "store") | ||
if (store == null || StringUtils.isEmpty(store)) { | ||
throw InvalidConfigYamlException(filename, it.startMark, "Invalid store directory or not specified") | ||
} | ||
|
||
val limit = getMapping(it, "limit") | ||
val ram_limit = limit?.let { | ||
val raw = getValueAsString(limit, "ram") | ||
if (raw == null || StringUtils.isEmpty(raw)) { | ||
return null | ||
} | ||
|
||
try { | ||
DataSize.parse(raw).toBytes() | ||
} catch (e: IllegalArgumentException) { | ||
throw InvalidConfigYamlException(filename, it.startMark, "Invalid limit for index") | ||
} | ||
} | ||
|
||
config.items.put(blockchain, IndexConfig.Index(rpc, store, ram_limit)) | ||
} | ||
|
||
config | ||
} | ||
} | ||
} |
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
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
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/io/emeraldpay/dshackle/upstream/LogsOracle.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,42 @@ | ||
package io.emeraldpay.dshackle.upstream | ||
|
||
import io.emeraldpay.dshackle.config.IndexConfig | ||
import org.slf4j.LoggerFactory | ||
import reactor.core.Disposable | ||
import reactor.core.publisher.Mono | ||
import reactor.core.scheduler.Scheduler | ||
|
||
class LogsOracle( | ||
private val config: IndexConfig.Index, | ||
private val upstream: Multistream, | ||
private val scheduler: Scheduler, | ||
) { | ||
|
||
private val log = LoggerFactory.getLogger(LogsOracle::class.java) | ||
|
||
private var subscription: Disposable? = null | ||
private val db = org.drpc.logsoracle.LogsOracle(config.store, config.store, config.ram_limit ?: 0L) | ||
|
||
fun start() { | ||
subscription = upstream.getHead().getFlux() | ||
.doOnError { t -> log.warn("Failed to subscribe head for oracle", t) } | ||
.subscribe { println(it.height); db.UpdateHeight(it.height) } | ||
} | ||
|
||
fun stop() { | ||
db.close() | ||
|
||
subscription?.dispose() | ||
subscription = null | ||
} | ||
|
||
fun estimate( | ||
fromBlock: Long?, | ||
toBlock: Long?, | ||
address: List<String>, | ||
topics: List<List<String>>, | ||
): Mono<Long> { | ||
return Mono.fromCallable { db.Query(fromBlock, toBlock, address, topics) } | ||
.subscribeOn(scheduler) | ||
} | ||
} |
Oops, something went wrong.