Skip to content

Commit

Permalink
feat(core): add support for eip-4788 parentBeaconBlockRoot header f…
Browse files Browse the repository at this point in the history
…ield
  • Loading branch information
ArtificialPB committed Jan 1, 2024
1 parent eac1a5b commit 687b3fc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 11 additions & 0 deletions ethers-core/src/main/kotlin/io/ethers/core/types/Block.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ data class BlockWithHashes(
override val withdrawalsRoot: Hash?,
override val blobGasUsed: Long,
override val excessBlobGas: Long,
override val parentBeaconBlockRoot: Hash?,
override val otherFields: Map<String, JsonNode> = emptyMap(),
) : Block<Hash>

Expand Down Expand Up @@ -75,6 +76,7 @@ data class BlockWithTransactions(
override val withdrawalsRoot: Hash?,
override val blobGasUsed: Long,
override val excessBlobGas: Long,
override val parentBeaconBlockRoot: Hash?,
override val otherFields: Map<String, JsonNode> = emptyMap(),
) : Block<RPCTransaction>

Expand Down Expand Up @@ -104,6 +106,7 @@ interface Block<T> {
val withdrawalsRoot: Hash?
val blobGasUsed: Long
val excessBlobGas: Long
val parentBeaconBlockRoot: Hash?
val otherFields: Map<String, JsonNode>
}

Expand Down Expand Up @@ -149,6 +152,7 @@ private class BlockWithHashesDeserializer : GenericBlockDeserializer<Hash, Block
withdrawalsRoot: Hash?,
blobGasUsed: Long,
excessBlobGas: Long,
parentBeaconBlockRoot: Hash?,
otherFields: Map<String, JsonNode>,
): BlockWithHashes {
return BlockWithHashes(
Expand Down Expand Up @@ -177,6 +181,7 @@ private class BlockWithHashesDeserializer : GenericBlockDeserializer<Hash, Block
withdrawalsRoot,
blobGasUsed,
excessBlobGas,
parentBeaconBlockRoot,
otherFields,
)
}
Expand Down Expand Up @@ -213,6 +218,7 @@ private class BlockWithTransactionDeserialize : GenericBlockDeserializer<RPCTran
withdrawalsRoot: Hash?,
blobGasUsed: Long,
excessBlobGas: Long,
parentBeaconBlockRoot: Hash?,
otherFields: Map<String, JsonNode>,
): BlockWithTransactions {
return BlockWithTransactions(
Expand Down Expand Up @@ -241,6 +247,7 @@ private class BlockWithTransactionDeserialize : GenericBlockDeserializer<RPCTran
withdrawalsRoot,
blobGasUsed,
excessBlobGas,
parentBeaconBlockRoot,
otherFields,
)
}
Expand Down Expand Up @@ -277,6 +284,7 @@ private abstract class GenericBlockDeserializer<TX, T : Block<TX>> : JsonDeseria
var withdrawalsRoot: Hash? = null
var blobGasUsed: Long = -1L
var excessBlobGas: Long = -1L
var parentBeaconBlockRoot: Hash? = null
var otherFields: MutableMap<String, JsonNode>? = null

p.forEachObjectField { field ->
Expand Down Expand Up @@ -309,6 +317,7 @@ private abstract class GenericBlockDeserializer<TX, T : Block<TX>> : JsonDeseria
"withdrawalsRoot" -> withdrawalsRoot = p.readOrNull { readHash() }
"blobGasUsed" -> blobGasUsed = p.readHexLong()
"excessBlobGas" -> excessBlobGas = p.readHexLong()
"parentBeaconBlockRoot" -> parentBeaconBlockRoot = p.readOrNull { readHash() }
else -> {
if (otherFields == null) {
otherFields = HashMap()
Expand Down Expand Up @@ -344,6 +353,7 @@ private abstract class GenericBlockDeserializer<TX, T : Block<TX>> : JsonDeseria
withdrawalsRoot,
blobGasUsed,
excessBlobGas,
parentBeaconBlockRoot,
otherFields ?: emptyMap(),
)
}
Expand Down Expand Up @@ -376,6 +386,7 @@ private abstract class GenericBlockDeserializer<TX, T : Block<TX>> : JsonDeseria
withdrawalsRoot: Hash?,
blobGasUsed: Long,
excessBlobGas: Long,
parentBeaconBlockRoot: Hash?,
otherFields: Map<String, JsonNode>,
): T
}
Expand Down
9 changes: 7 additions & 2 deletions ethers-core/src/test/kotlin/io/ethers/core/types/BlockTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class BlockTest : FunSpec({
"withdrawalsRoot": "0x1276b1e90b9f4a76b24e5fad4adce2a5e6ffef7351bfaa2e128955950a9027b0",
"baseFeePerGas": "0x2e22eacf",
"miner": "0x3b64216ad1a58f61538b4fa1b27327675ab7ed67",
"blobGasUsed": "0x1276b0",
"excessBlobGas": "0x01276b",
"parentBeaconBlockRoot": "0xc74b35721eec9b338589ea735f8d322b3e27f3259d9e924ef354a4336fb715a8",
"transactions": [
"0xc74b35721eec9b338589ea735f8d322b3e27f3259d9e924ef354a4336fb715a8",
"0x5c4ca94fca565ef1c983e3a3cd4bc6d6da1091a487f5d64b8a1bb3434a12d876",
Expand Down Expand Up @@ -106,8 +109,9 @@ class BlockTest : FunSpec({
Withdrawal(20789864, 599445, Address("0xe839a3e9efb32c6a56ab7128e51056585275506c"), 16657613L),
),
withdrawalsRoot = Hash("0x1276b1e90b9f4a76b24e5fad4adce2a5e6ffef7351bfaa2e128955950a9027b0"),
blobGasUsed = -1L,
excessBlobGas = -1L,
blobGasUsed = 1210032,
excessBlobGas = 75627,
parentBeaconBlockRoot = Hash("0xc74b35721eec9b338589ea735f8d322b3e27f3259d9e924ef354a4336fb715a8"),
otherFields = mapOf(
"test" to Jackson.MAPPER.readTree("""{"k1":"v1","k2":"v2"}"""),
),
Expand Down Expand Up @@ -278,6 +282,7 @@ class BlockTest : FunSpec({
withdrawalsRoot = Hash("0x1276b1e90b9f4a76b24e5fad4adce2a5e6ffef7351bfaa2e128955950a9027b0"),
blobGasUsed = -1L,
excessBlobGas = -1L,
parentBeaconBlockRoot = null,
otherFields = mapOf(
"test" to Jackson.MAPPER.readTree("""{"k1":"v1","k2":"v2"}"""),
),
Expand Down

0 comments on commit 687b3fc

Please sign in to comment.