Skip to content

Commit

Permalink
Add withdrawals to BlockResult for eth_getBlockByHash and eth_getBloc…
Browse files Browse the repository at this point in the history
…kByNumber

As per ethereum/execution-apis#334

Signed-off-by: Simon Dudley <simon.dudley@consensys.net>
  • Loading branch information
siladu committed Dec 20, 2022
1 parent 46f4390 commit 161b70c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.results;

import static java.util.stream.Collectors.toList;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.WithdrawalParameter;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.Withdrawal;

import java.util.List;
import java.util.Optional;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down Expand Up @@ -49,7 +54,8 @@
"timestamp",
"uncles",
"transactions",
"withdrawalsRoot"
"withdrawalsRoot",
"withdrawals"
})
public class BlockResult implements JsonRpcResult {

Expand All @@ -76,14 +82,15 @@ public class BlockResult implements JsonRpcResult {
private final List<JsonNode> ommers;
private final String coinbase;
private final String withdrawalsRoot;
private final List<WithdrawalParameter> withdrawals;

public <T extends TransactionResult> BlockResult(
final BlockHeader header,
final List<TransactionResult> transactions,
final List<JsonNode> ommers,
final Difficulty totalDifficulty,
final int size) {
this(header, transactions, ommers, totalDifficulty, size, false);
this(header, transactions, ommers, totalDifficulty, size, false, Optional.empty());
}

public <T extends TransactionResult> BlockResult(
Expand All @@ -92,7 +99,8 @@ public <T extends TransactionResult> BlockResult(
final List<JsonNode> ommers,
final Difficulty totalDifficulty,
final int size,
final boolean includeCoinbase) {
final boolean includeCoinbase,
final Optional<List<Withdrawal>> withdrawals) {
this.number = Quantity.create(header.getNumber());
this.hash = header.getHash().toString();
this.mixHash = header.getMixHash().toString();
Expand All @@ -119,6 +127,10 @@ public <T extends TransactionResult> BlockResult(
!header.getWithdrawalRoot().equals(Hash.EMPTY)
? header.getWithdrawalRoot().toString()
: null;
this.withdrawals =
withdrawals
.map(w -> w.stream().map(WithdrawalParameter::fromWithdrawal).collect(toList()))
.orElse(null);
}

@JsonGetter(value = "number")
Expand Down Expand Up @@ -236,4 +248,9 @@ public String getCoinbase() {
public String getWithdrawalsRoot() {
return withdrawalsRoot;
}

@JsonGetter(value = "withdrawals")
public List<WithdrawalParameter> getWithdrawals() {
return withdrawals;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public BlockResult transactionComplete(
ommers,
blockWithMetadata.getTotalDifficulty(),
blockWithMetadata.getSize(),
includeCoinbase);
includeCoinbase,
blockWithMetadata.getWithdrawals());
}

public BlockResult transactionComplete(final Block block) {
Expand Down Expand Up @@ -138,6 +139,7 @@ public BlockResult transactionHash(
ommers,
blockWithMetadata.getTotalDifficulty(),
blockWithMetadata.getSize(),
includeCoinbase);
includeCoinbase,
blockWithMetadata.getWithdrawals());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hyperledger.besu.ethereum.core.Difficulty;

import java.util.List;
import java.util.Optional;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
Expand Down Expand Up @@ -57,7 +58,7 @@ public ConsensusBlockResult(
final Difficulty totalDifficulty,
final int size,
final boolean includeCoinbase) {
super(header, null, ommers, totalDifficulty, size, includeCoinbase);
super(header, null, ommers, totalDifficulty, size, includeCoinbase, Optional.empty());
this.opaqueTransactions = transactions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.Withdrawal;

import java.util.List;
import java.util.Optional;

public class BlockWithMetadata<T, O> {

Expand All @@ -26,6 +28,7 @@ public class BlockWithMetadata<T, O> {
private final List<O> ommers;
private final Difficulty totalDifficulty;
private final int size;
private final Optional<List<Withdrawal>> withdrawals;

/**
* @param header The block header
Expand All @@ -40,11 +43,22 @@ public BlockWithMetadata(
final List<O> ommers,
final Difficulty totalDifficulty,
final int size) {
this(header, transactions, ommers, totalDifficulty, size, Optional.empty());
}

public BlockWithMetadata(
final BlockHeader header,
final List<T> transactions,
final List<O> ommers,
final Difficulty totalDifficulty,
final int size,
final Optional<List<Withdrawal>> withdrawals) {
this.header = header;
this.transactions = transactions;
this.ommers = ommers;
this.totalDifficulty = totalDifficulty;
this.size = size;
this.withdrawals = withdrawals;
}

public BlockHeader getHeader() {
Expand All @@ -66,4 +80,8 @@ public Difficulty getTotalDifficulty() {
public int getSize() {
return size;
}

public Optional<List<Withdrawal>> getWithdrawals() {
return withdrawals;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,12 @@ public Optional<BlockWithMetadata<TransactionWithMetadata, Hash>> blockByHash(
.collect(Collectors.toList());
final int size = new Block(header, body).calculateSize();
return new BlockWithMetadata<>(
header, formattedTxs, ommers, td, size);
header,
formattedTxs,
ommers,
td,
size,
body.getWithdrawals());
})));
}

Expand Down Expand Up @@ -468,7 +473,8 @@ public Optional<BlockWithMetadata<Hash, Hash>> blockByHashWithTxHashes(
.map(BlockHeader::getHash)
.collect(Collectors.toList());
final int size = new Block(header, body).calculateSize();
return new BlockWithMetadata<>(header, txs, ommers, td, size);
return new BlockWithMetadata<>(
header, txs, ommers, td, size, body.getWithdrawals());
})));
}

Expand Down

0 comments on commit 161b70c

Please sign in to comment.