problem: doesn't parse block selector specified as object EIP-1898

rel: #81
This commit is contained in:
Igor Artamonov
2021-03-19 22:04:03 -04:00
parent c3f0f1f589
commit c3c1a51a52
2 changed files with 30 additions and 9 deletions

View File

@@ -61,24 +61,37 @@ class EthereumCallSelector {
log.debug("Tag is not specified. Ignoring")
return null
}
// integer block number, or the string "latest", "earliest" or "pending"
val minHeight = when (val tag = list[pos].toString()) {
"latest" -> head.getCurrentHeight() ?: 0
// integer block number, a string "latest", "earliest" or "pending", or an object with block reference
val minHeight: Long? = when (val tag = list[pos].toString()) {
"latest" -> head.getCurrentHeight()
// for earliest it doesn't nothing, we expect to have 0 block
"earliest" -> 0L
else -> if (tag.startsWith("0x")) {
try {
HexQuantity.from(tag).value.toLong()
} catch (t: Throwable) {
log.debug("Invalid tag: $tag. ${t.javaClass}: ${t.message}")
null
}
} else if (tag.startsWith("{") && list[pos] is Map<*, *>) {
// see https://eips.ethereum.org/EIPS/eip-1898
val obj = list[pos] as Map<*, *>
if (obj.containsKey("blockNumber")) {
try {
HexQuantity.from(tag).value.toLong()
HexQuantity.from(obj["blockNumber"].toString()).value.toLong()
} catch (t: Throwable) {
log.debug("Invalid tag: $tag. ${t.javaClass}: ${t.message}")
0L
log.warn("Invalid blockNumber: $tag")
null
}
} else {
log.debug("Invalid tag: $tag")
0L
null
}
} else {
log.debug("Invalid tag: $tag")
null
}
}
return if (minHeight > 0) {
return if (minHeight != null && minHeight >= 0) {
Selector.HeightMatcher(minHeight)
} else {
null

View File

@@ -78,4 +78,12 @@ class EthereumCallSelectorSpec extends Specification {
act == null
}
def "Get height matcher with EIP-1898"() {
setup:
def head = Stub(Head)
when:
def act = callSelector.getMatcher("eth_call", '["0x0000", {"blockNumber": "0x100"}]', head)
then:
act == new Selector.HeightMatcher(0x100)
}
}