Skip to content

Commit 84d1387

Browse files
authored
Merge pull request #1947 from angusscott/feature/support-withdrawals-eth-block
Adding support for Withdrawals in EthBlock as part of EIP-4895
2 parents fd9be0b + 3506f7b commit 84d1387

File tree

3 files changed

+165
-12
lines changed

3 files changed

+165
-12
lines changed

core/src/main/java/org/web3j/protocol/core/methods/response/EthBlock.java

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.ArrayList;
1818
import java.util.Iterator;
1919
import java.util.List;
20+
import java.util.Objects;
2021

2122
import com.fasterxml.jackson.core.JsonParser;
2223
import com.fasterxml.jackson.core.JsonToken;
@@ -81,6 +82,8 @@ public static class Block {
8182
private List<String> uncles;
8283
private List<String> sealFields;
8384
private String baseFeePerGas;
85+
private String withdrawalsRoot;
86+
private List<Withdrawal> withdrawals;
8487

8588
public Block() {}
8689

@@ -107,7 +110,9 @@ public Block(
107110
List<TransactionResult> transactions,
108111
List<String> uncles,
109112
List<String> sealFields,
110-
String baseFeePerGas) {
113+
String baseFeePerGas,
114+
String withdrawalsRoot,
115+
List<Withdrawal> withdrawals) {
111116
this.number = number;
112117
this.hash = hash;
113118
this.parentHash = parentHash;
@@ -131,6 +136,8 @@ public Block(
131136
this.uncles = uncles;
132137
this.sealFields = sealFields;
133138
this.baseFeePerGas = baseFeePerGas;
139+
this.withdrawalsRoot = withdrawalsRoot;
140+
this.withdrawals = withdrawals;
134141
}
135142

136143
public BigInteger getNumber() {
@@ -354,6 +361,22 @@ public String getBaseFeePerGasRaw() {
354361
return baseFeePerGas;
355362
}
356363

364+
public String getWithdrawalsRoot() {
365+
return withdrawalsRoot;
366+
}
367+
368+
public void setWithdrawalsRoot(String withdrawalsRoot) {
369+
this.withdrawalsRoot = withdrawalsRoot;
370+
}
371+
372+
public List<Withdrawal> getWithdrawals() {
373+
return withdrawals;
374+
}
375+
376+
public void setWithdrawals(List<Withdrawal> withdrawals) {
377+
this.withdrawals = withdrawals;
378+
}
379+
357380
@Override
358381
public boolean equals(Object o) {
359382
if (this == o) {
@@ -475,9 +498,21 @@ public boolean equals(Object o) {
475498
return false;
476499
}
477500

478-
return getSealFields() != null
479-
? getSealFields().equals(block.getSealFields())
480-
: block.getSealFields() == null;
501+
if (getSealFields() != null
502+
? !getSealFields().equals(block.getSealFields())
503+
: block.getSealFields() != null) {
504+
return false;
505+
}
506+
507+
if (getWithdrawalsRoot() != null
508+
? !getWithdrawalsRoot().equals(block.getWithdrawalsRoot())
509+
: block.getWithdrawalsRoot() != null) {
510+
return false;
511+
}
512+
513+
return getWithdrawals() != null
514+
? getWithdrawals().equals(block.getWithdrawals())
515+
: block.getWithdrawals() == null;
481516
}
482517

483518
@Override
@@ -517,6 +552,10 @@ public int hashCode() {
517552
+ (getBaseFeePerGasRaw() != null
518553
? getBaseFeePerGasRaw().hashCode()
519554
: 0);
555+
result =
556+
31 * result
557+
+ (getWithdrawalsRoot() != null ? getWithdrawalsRoot().hashCode() : 0);
558+
result = 31 * result + (getWithdrawals() != null ? getWithdrawals().hashCode() : 0);
520559
return result;
521560
}
522561
}
@@ -702,6 +741,70 @@ public List<TransactionResult> deserialize(
702741
}
703742
}
704743

744+
public static class Withdrawal {
745+
private String index;
746+
private String validatorIndex;
747+
private String address;
748+
private String amount;
749+
750+
public Withdrawal() {}
751+
752+
public Withdrawal(String index, String validatorIndex, String address, String amount) {
753+
this.index = index;
754+
this.validatorIndex = validatorIndex;
755+
this.address = address;
756+
this.amount = amount;
757+
}
758+
759+
public String getIndex() {
760+
return index;
761+
}
762+
763+
public void setIndex(String index) {
764+
this.index = index;
765+
}
766+
767+
public String getValidatorIndex() {
768+
return validatorIndex;
769+
}
770+
771+
public void setValidatorIndex(String validatorIndex) {
772+
this.validatorIndex = validatorIndex;
773+
}
774+
775+
public String getAddress() {
776+
return address;
777+
}
778+
779+
public void setAddress(String address) {
780+
this.address = address;
781+
}
782+
783+
public BigInteger getAmount() {
784+
return Numeric.decodeQuantity(amount);
785+
}
786+
787+
public void setAmount(String amount) {
788+
this.amount = amount;
789+
}
790+
791+
@Override
792+
public boolean equals(Object o) {
793+
if (this == o) return true;
794+
if (o == null || getClass() != o.getClass()) return false;
795+
Withdrawal that = (Withdrawal) o;
796+
return Objects.equals(index, that.index)
797+
&& Objects.equals(validatorIndex, that.validatorIndex)
798+
&& Objects.equals(address, that.address)
799+
&& Objects.equals(amount, that.amount);
800+
}
801+
802+
@Override
803+
public int hashCode() {
804+
return Objects.hash(index, validatorIndex, address, amount);
805+
}
806+
}
807+
705808
public static class ResponseDeserialiser extends JsonDeserializer<Block> {
706809

707810
private ObjectReader objectReader = ObjectMapperFactory.getObjectReader();

core/src/test/java/org/web3j/protocol/core/ResponseTest.java

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,16 @@ public void testEthBlockTransactionHashes() {
654654
+ " \"0x57919c4e72e79ad7705a26e7ecd5a08ff546ac4fa37882e9cc57be87a3dab26b\",\n"
655655
+ " \"0x39a3eb432fbef1fc\"\n"
656656
+ " ],\n"
657-
+ " \"baseFeePerGas\": \"0x7\"\n"
657+
+ " \"baseFeePerGas\": \"0x7\",\n"
658+
+ " \"withdrawalsRoot\": \"0x1b1f845cd61c375a89ef101fd1bd86355f372a6a3dfe1960f2355e70bd5cd8a2\",\n"
659+
+ " \"withdrawals\": [\n"
660+
+ " {\n"
661+
+ " \"index\": \"0x68ba80\",\n"
662+
+ " \"validatorIndex\": \"0x65285\",\n"
663+
+ " \"address\": \"0x1e09b4199780a45792f4ff195ef68410a091b047\",\n"
664+
+ " \"amount\": \"0xd1f129\"\n"
665+
+ " }\n"
666+
+ " ]\n"
658667
+ " }\n"
659668
+ "}");
660669

@@ -692,7 +701,14 @@ public void testEthBlockTransactionHashes() {
692701
Arrays.asList(
693702
"0x57919c4e72e79ad7705a26e7ecd5a08ff546ac4fa37882e9cc57be87a3dab26b",
694703
"0x39a3eb432fbef1fc"),
695-
"0x7");
704+
"0x7",
705+
"0x1b1f845cd61c375a89ef101fd1bd86355f372a6a3dfe1960f2355e70bd5cd8a2",
706+
Arrays.asList(
707+
new EthBlock.Withdrawal(
708+
"0x68ba80",
709+
"0x65285",
710+
"0x1e09b4199780a45792f4ff195ef68410a091b047",
711+
"0xd1f129")));
696712
assertEquals(ethBlock.getBlock(), (block));
697713
}
698714

@@ -760,7 +776,16 @@ public void testEthBlockFullTransactionsParity() {
760776
+ " \"0x57919c4e72e79ad7705a26e7ecd5a08ff546ac4fa37882e9cc57be87a3dab26b\",\n"
761777
+ " \"0x39a3eb432fbef1fc\"\n"
762778
+ " ],\n"
763-
+ " \"baseFeePerGas\": \"0x7\"\n"
779+
+ " \"baseFeePerGas\": \"0x7\",\n"
780+
+ " \"withdrawalsRoot\": \"0x1b1f845cd61c375a89ef101fd1bd86355f372a6a3dfe1960f2355e70bd5cd8a2\",\n"
781+
+ " \"withdrawals\": [\n"
782+
+ " {\n"
783+
+ " \"index\": \"0x68ba80\",\n"
784+
+ " \"validatorIndex\": \"0x65285\",\n"
785+
+ " \"address\": \"0x1e09b4199780a45792f4ff195ef68410a091b047\",\n"
786+
+ " \"amount\": \"0xd1f129\"\n"
787+
+ " }\n"
788+
+ " ]\n"
764789
+ " }\n"
765790
+ "}");
766791

@@ -820,7 +845,14 @@ public void testEthBlockFullTransactionsParity() {
820845
Arrays.asList(
821846
"0x57919c4e72e79ad7705a26e7ecd5a08ff546ac4fa37882e9cc57be87a3dab26b",
822847
"0x39a3eb432fbef1fc"),
823-
"0x7");
848+
"0x7",
849+
"0x1b1f845cd61c375a89ef101fd1bd86355f372a6a3dfe1960f2355e70bd5cd8a2",
850+
Arrays.asList(
851+
new EthBlock.Withdrawal(
852+
"0x68ba80",
853+
"0x65285",
854+
"0x1e09b4199780a45792f4ff195ef68410a091b047",
855+
"0xd1f129")));
824856

825857
assertEquals(ethBlock.getBlock(), (block));
826858
}
@@ -890,7 +922,16 @@ public void testEthBlockFullTransactionsGeth() {
890922
+ " \"0x57919c4e72e79ad7705a26e7ecd5a08ff546ac4fa37882e9cc57be87a3dab26b\",\n"
891923
+ " \"0x39a3eb432fbef1fc\"\n"
892924
+ " ],\n"
893-
+ " \"baseFeePerGas\": \"0x7\"\n"
925+
+ " \"baseFeePerGas\": \"0x7\",\n"
926+
+ " \"withdrawalsRoot\": \"0x1b1f845cd61c375a89ef101fd1bd86355f372a6a3dfe1960f2355e70bd5cd8a2\",\n"
927+
+ " \"withdrawals\": [\n"
928+
+ " {\n"
929+
+ " \"index\": \"0x68ba80\",\n"
930+
+ " \"validatorIndex\": \"0x65285\",\n"
931+
+ " \"address\": \"0x1e09b4199780a45792f4ff195ef68410a091b047\",\n"
932+
+ " \"amount\": \"0xd1f129\"\n"
933+
+ " }\n"
934+
+ " ]\n"
894935
+ " }\n"
895936
+ "}");
896937

@@ -950,7 +991,14 @@ public void testEthBlockFullTransactionsGeth() {
950991
Arrays.asList(
951992
"0x57919c4e72e79ad7705a26e7ecd5a08ff546ac4fa37882e9cc57be87a3dab26b",
952993
"0x39a3eb432fbef1fc"),
953-
"0x7");
994+
"0x7",
995+
"0x1b1f845cd61c375a89ef101fd1bd86355f372a6a3dfe1960f2355e70bd5cd8a2",
996+
Arrays.asList(
997+
new EthBlock.Withdrawal(
998+
"0x68ba80",
999+
"0x65285",
1000+
"0x1e09b4199780a45792f4ff195ef68410a091b047",
1001+
"0xd1f129")));
9541002

9551003
assertEquals(ethBlock.getBlock(), (block));
9561004
}

core/src/test/java/org/web3j/protocol/core/methods/response/EthBlockTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public void testEthBlockNullSize() {
2525
EthBlock.Block ethBlock =
2626
new EthBlock.Block(
2727
null, null, null, null, null, null, null, null, null, null, null, null,
28-
null, null, null, null, null, null, null, null, null, null, null);
28+
null, null, null, null, null, null, null, null, null, null, null, null,
29+
null);
2930

3031
assertEquals(ethBlock.getSize(), BigInteger.ZERO);
3132
}
@@ -35,7 +36,8 @@ public void testEthBlockNotNullSize() {
3536
EthBlock.Block ethBlock =
3637
new EthBlock.Block(
3738
null, null, null, null, null, null, null, null, null, null, null, null,
38-
null, null, null, "0x3e8", null, null, null, null, null, null, null);
39+
null, null, null, "0x3e8", null, null, null, null, null, null, null, null,
40+
null);
3941

4042
assertEquals(ethBlock.getSize(), BigInteger.valueOf(1000));
4143
}

0 commit comments

Comments
 (0)