Skip to content

Commit

Permalink
Merge pull request #1947 from angusscott/feature/support-withdrawals-…
Browse files Browse the repository at this point in the history
…eth-block

Adding support for Withdrawals in EthBlock as part of EIP-4895
  • Loading branch information
gtebrean authored Aug 22, 2023
2 parents fd9be0b + 3506f7b commit 84d1387
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
Expand Down Expand Up @@ -81,6 +82,8 @@ public static class Block {
private List<String> uncles;
private List<String> sealFields;
private String baseFeePerGas;
private String withdrawalsRoot;
private List<Withdrawal> withdrawals;

public Block() {}

Expand All @@ -107,7 +110,9 @@ public Block(
List<TransactionResult> transactions,
List<String> uncles,
List<String> sealFields,
String baseFeePerGas) {
String baseFeePerGas,
String withdrawalsRoot,
List<Withdrawal> withdrawals) {
this.number = number;
this.hash = hash;
this.parentHash = parentHash;
Expand All @@ -131,6 +136,8 @@ public Block(
this.uncles = uncles;
this.sealFields = sealFields;
this.baseFeePerGas = baseFeePerGas;
this.withdrawalsRoot = withdrawalsRoot;
this.withdrawals = withdrawals;
}

public BigInteger getNumber() {
Expand Down Expand Up @@ -354,6 +361,22 @@ public String getBaseFeePerGasRaw() {
return baseFeePerGas;
}

public String getWithdrawalsRoot() {
return withdrawalsRoot;
}

public void setWithdrawalsRoot(String withdrawalsRoot) {
this.withdrawalsRoot = withdrawalsRoot;
}

public List<Withdrawal> getWithdrawals() {
return withdrawals;
}

public void setWithdrawals(List<Withdrawal> withdrawals) {
this.withdrawals = withdrawals;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -475,9 +498,21 @@ public boolean equals(Object o) {
return false;
}

return getSealFields() != null
? getSealFields().equals(block.getSealFields())
: block.getSealFields() == null;
if (getSealFields() != null
? !getSealFields().equals(block.getSealFields())
: block.getSealFields() != null) {
return false;
}

if (getWithdrawalsRoot() != null
? !getWithdrawalsRoot().equals(block.getWithdrawalsRoot())
: block.getWithdrawalsRoot() != null) {
return false;
}

return getWithdrawals() != null
? getWithdrawals().equals(block.getWithdrawals())
: block.getWithdrawals() == null;
}

@Override
Expand Down Expand Up @@ -517,6 +552,10 @@ public int hashCode() {
+ (getBaseFeePerGasRaw() != null
? getBaseFeePerGasRaw().hashCode()
: 0);
result =
31 * result
+ (getWithdrawalsRoot() != null ? getWithdrawalsRoot().hashCode() : 0);
result = 31 * result + (getWithdrawals() != null ? getWithdrawals().hashCode() : 0);
return result;
}
}
Expand Down Expand Up @@ -702,6 +741,70 @@ public List<TransactionResult> deserialize(
}
}

public static class Withdrawal {
private String index;
private String validatorIndex;
private String address;
private String amount;

public Withdrawal() {}

public Withdrawal(String index, String validatorIndex, String address, String amount) {
this.index = index;
this.validatorIndex = validatorIndex;
this.address = address;
this.amount = amount;
}

public String getIndex() {
return index;
}

public void setIndex(String index) {
this.index = index;
}

public String getValidatorIndex() {
return validatorIndex;
}

public void setValidatorIndex(String validatorIndex) {
this.validatorIndex = validatorIndex;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public BigInteger getAmount() {
return Numeric.decodeQuantity(amount);
}

public void setAmount(String amount) {
this.amount = amount;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Withdrawal that = (Withdrawal) o;
return Objects.equals(index, that.index)
&& Objects.equals(validatorIndex, that.validatorIndex)
&& Objects.equals(address, that.address)
&& Objects.equals(amount, that.amount);
}

@Override
public int hashCode() {
return Objects.hash(index, validatorIndex, address, amount);
}
}

public static class ResponseDeserialiser extends JsonDeserializer<Block> {

private ObjectReader objectReader = ObjectMapperFactory.getObjectReader();
Expand Down
60 changes: 54 additions & 6 deletions core/src/test/java/org/web3j/protocol/core/ResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,16 @@ public void testEthBlockTransactionHashes() {
+ " \"0x57919c4e72e79ad7705a26e7ecd5a08ff546ac4fa37882e9cc57be87a3dab26b\",\n"
+ " \"0x39a3eb432fbef1fc\"\n"
+ " ],\n"
+ " \"baseFeePerGas\": \"0x7\"\n"
+ " \"baseFeePerGas\": \"0x7\",\n"
+ " \"withdrawalsRoot\": \"0x1b1f845cd61c375a89ef101fd1bd86355f372a6a3dfe1960f2355e70bd5cd8a2\",\n"
+ " \"withdrawals\": [\n"
+ " {\n"
+ " \"index\": \"0x68ba80\",\n"
+ " \"validatorIndex\": \"0x65285\",\n"
+ " \"address\": \"0x1e09b4199780a45792f4ff195ef68410a091b047\",\n"
+ " \"amount\": \"0xd1f129\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "}");

Expand Down Expand Up @@ -692,7 +701,14 @@ public void testEthBlockTransactionHashes() {
Arrays.asList(
"0x57919c4e72e79ad7705a26e7ecd5a08ff546ac4fa37882e9cc57be87a3dab26b",
"0x39a3eb432fbef1fc"),
"0x7");
"0x7",
"0x1b1f845cd61c375a89ef101fd1bd86355f372a6a3dfe1960f2355e70bd5cd8a2",
Arrays.asList(
new EthBlock.Withdrawal(
"0x68ba80",
"0x65285",
"0x1e09b4199780a45792f4ff195ef68410a091b047",
"0xd1f129")));
assertEquals(ethBlock.getBlock(), (block));
}

Expand Down Expand Up @@ -760,7 +776,16 @@ public void testEthBlockFullTransactionsParity() {
+ " \"0x57919c4e72e79ad7705a26e7ecd5a08ff546ac4fa37882e9cc57be87a3dab26b\",\n"
+ " \"0x39a3eb432fbef1fc\"\n"
+ " ],\n"
+ " \"baseFeePerGas\": \"0x7\"\n"
+ " \"baseFeePerGas\": \"0x7\",\n"
+ " \"withdrawalsRoot\": \"0x1b1f845cd61c375a89ef101fd1bd86355f372a6a3dfe1960f2355e70bd5cd8a2\",\n"
+ " \"withdrawals\": [\n"
+ " {\n"
+ " \"index\": \"0x68ba80\",\n"
+ " \"validatorIndex\": \"0x65285\",\n"
+ " \"address\": \"0x1e09b4199780a45792f4ff195ef68410a091b047\",\n"
+ " \"amount\": \"0xd1f129\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "}");

Expand Down Expand Up @@ -820,7 +845,14 @@ public void testEthBlockFullTransactionsParity() {
Arrays.asList(
"0x57919c4e72e79ad7705a26e7ecd5a08ff546ac4fa37882e9cc57be87a3dab26b",
"0x39a3eb432fbef1fc"),
"0x7");
"0x7",
"0x1b1f845cd61c375a89ef101fd1bd86355f372a6a3dfe1960f2355e70bd5cd8a2",
Arrays.asList(
new EthBlock.Withdrawal(
"0x68ba80",
"0x65285",
"0x1e09b4199780a45792f4ff195ef68410a091b047",
"0xd1f129")));

assertEquals(ethBlock.getBlock(), (block));
}
Expand Down Expand Up @@ -890,7 +922,16 @@ public void testEthBlockFullTransactionsGeth() {
+ " \"0x57919c4e72e79ad7705a26e7ecd5a08ff546ac4fa37882e9cc57be87a3dab26b\",\n"
+ " \"0x39a3eb432fbef1fc\"\n"
+ " ],\n"
+ " \"baseFeePerGas\": \"0x7\"\n"
+ " \"baseFeePerGas\": \"0x7\",\n"
+ " \"withdrawalsRoot\": \"0x1b1f845cd61c375a89ef101fd1bd86355f372a6a3dfe1960f2355e70bd5cd8a2\",\n"
+ " \"withdrawals\": [\n"
+ " {\n"
+ " \"index\": \"0x68ba80\",\n"
+ " \"validatorIndex\": \"0x65285\",\n"
+ " \"address\": \"0x1e09b4199780a45792f4ff195ef68410a091b047\",\n"
+ " \"amount\": \"0xd1f129\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "}");

Expand Down Expand Up @@ -950,7 +991,14 @@ public void testEthBlockFullTransactionsGeth() {
Arrays.asList(
"0x57919c4e72e79ad7705a26e7ecd5a08ff546ac4fa37882e9cc57be87a3dab26b",
"0x39a3eb432fbef1fc"),
"0x7");
"0x7",
"0x1b1f845cd61c375a89ef101fd1bd86355f372a6a3dfe1960f2355e70bd5cd8a2",
Arrays.asList(
new EthBlock.Withdrawal(
"0x68ba80",
"0x65285",
"0x1e09b4199780a45792f4ff195ef68410a091b047",
"0xd1f129")));

assertEquals(ethBlock.getBlock(), (block));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public void testEthBlockNullSize() {
EthBlock.Block ethBlock =
new EthBlock.Block(
null, null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null, null);
null, null, null, null, null, null, null, null, null, null, null, null,
null);

assertEquals(ethBlock.getSize(), BigInteger.ZERO);
}
Expand All @@ -35,7 +36,8 @@ public void testEthBlockNotNullSize() {
EthBlock.Block ethBlock =
new EthBlock.Block(
null, null, null, null, null, null, null, null, null, null, null, null,
null, null, null, "0x3e8", null, null, null, null, null, null, null);
null, null, null, "0x3e8", null, null, null, null, null, null, null, null,
null);

assertEquals(ethBlock.getSize(), BigInteger.valueOf(1000));
}
Expand Down

0 comments on commit 84d1387

Please sign in to comment.