optimise default getBlockByNumber - skip processing requests of tx bodies

This commit is contained in:
a10zn8
2022-12-12 17:57:59 +04:00
parent 6a252029f7
commit 25e2e32685
2 changed files with 26 additions and 11 deletions

View File

@@ -136,6 +136,11 @@ class LocalCallRouter(
throw RpcException(RpcResponseError.CODE_INVALID_METHOD_PARAMS, "Must provide 2 parameters")
}
val number: Long
val withTx = params[1].toString().toBoolean()
if (withTx) {
// with Tx request much more efficient in remote call
return null
}
try {
val blockRef = params[0].toString()
when {
@@ -165,16 +170,8 @@ class LocalCallRouter(
} catch (e: IllegalArgumentException) {
throw RpcException(RpcResponseError.CODE_INVALID_METHOD_PARAMS, "[0] must be a block number")
}
val withTx = params[1].toString().toBoolean()
var block = reader.blocksByHeightAsCont()
.read(number)
block = if (withTx) {
block.flatMap {
fullBlocksReader.read(it.hash)
}
} else {
block
}
return block.map { it.json!! }
return reader.blocksByHeightAsCont()
.read(number).map { it.json!! }
}
}

View File

@@ -138,4 +138,22 @@ class LocalCallRouterSpec extends Specification {
}
}
}
def "getBlockByNumber skips requests with tx bodies"() {
setup:
def head = Mock(Head)
def reader = Mock(EthereumCachingReader) {
_ * blocksByIdAsCont() >> new EmptyReader<>()
_ * txByHashAsCont() >> new EmptyReader<>()
_ * blocksByHeightAsCont() >> new EmptyReader<>()
}
def methods = new DefaultEthereumMethods(Chain.ETHEREUM)
def router = new LocalCallRouter(reader, methods, head, true)
when:
def act = router.getBlockByNumber(["0x0", true])
then:
act == null
}
}