mirror of https://github.com/hyperledger/besu
Change execution requests to use flat encoding (#7771)
Signed-off-by: Jason Frame <jason.frame@consensys.net>pull/7813/head
parent
516559fadc
commit
f16d3526db
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -1,102 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.Address; |
|
||||||
import org.hyperledger.besu.datatypes.BLSPublicKey; |
|
||||||
import org.hyperledger.besu.ethereum.core.ConsolidationRequest; |
|
||||||
|
|
||||||
import java.util.Objects; |
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonCreator; |
|
||||||
import com.fasterxml.jackson.annotation.JsonGetter; |
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty; |
|
||||||
|
|
||||||
public class ConsolidationRequestParameter { |
|
||||||
|
|
||||||
private final String sourceAddress; |
|
||||||
private final String sourcePubkey; |
|
||||||
private final String targetPubkey; |
|
||||||
|
|
||||||
@JsonCreator |
|
||||||
public ConsolidationRequestParameter( |
|
||||||
@JsonProperty("sourceAddress") final String sourceAddress, |
|
||||||
@JsonProperty("sourcePubkey") final String sourcePubkey, |
|
||||||
@JsonProperty("targetPubkey") final String targetPubkey) { |
|
||||||
this.sourceAddress = sourceAddress; |
|
||||||
this.sourcePubkey = sourcePubkey; |
|
||||||
this.targetPubkey = targetPubkey; |
|
||||||
} |
|
||||||
|
|
||||||
public static ConsolidationRequestParameter fromConsolidationRequest( |
|
||||||
final ConsolidationRequest consolidationRequest) { |
|
||||||
return new ConsolidationRequestParameter( |
|
||||||
consolidationRequest.getSourceAddress().toHexString(), |
|
||||||
consolidationRequest.getSourcePubkey().toHexString(), |
|
||||||
consolidationRequest.getTargetPubkey().toHexString()); |
|
||||||
} |
|
||||||
|
|
||||||
public ConsolidationRequest toConsolidationRequest() { |
|
||||||
return new ConsolidationRequest( |
|
||||||
Address.fromHexString(sourceAddress), |
|
||||||
BLSPublicKey.fromHexString(sourcePubkey), |
|
||||||
BLSPublicKey.fromHexString(targetPubkey)); |
|
||||||
} |
|
||||||
|
|
||||||
@JsonGetter |
|
||||||
public String getSourceAddress() { |
|
||||||
return sourceAddress; |
|
||||||
} |
|
||||||
|
|
||||||
@JsonGetter |
|
||||||
public String getSourcePubkey() { |
|
||||||
return sourcePubkey; |
|
||||||
} |
|
||||||
|
|
||||||
@JsonGetter |
|
||||||
public String getTargetPubkey() { |
|
||||||
return targetPubkey; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean equals(final Object o) { |
|
||||||
if (this == o) return true; |
|
||||||
if (o == null || getClass() != o.getClass()) return false; |
|
||||||
final ConsolidationRequestParameter that = (ConsolidationRequestParameter) o; |
|
||||||
return Objects.equals(sourceAddress, that.sourceAddress) |
|
||||||
&& Objects.equals(sourcePubkey, that.sourcePubkey) |
|
||||||
&& Objects.equals(targetPubkey, that.targetPubkey); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public int hashCode() { |
|
||||||
return Objects.hash(sourceAddress, sourcePubkey, targetPubkey); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public String toString() { |
|
||||||
return "ConsolidationRequestParameter{" |
|
||||||
+ "sourceAddress='" |
|
||||||
+ sourceAddress |
|
||||||
+ '\'' |
|
||||||
+ ", sourcePubkey='" |
|
||||||
+ sourcePubkey |
|
||||||
+ '\'' |
|
||||||
+ ", targetPubkey='" |
|
||||||
+ targetPubkey |
|
||||||
+ '\'' |
|
||||||
+ '}'; |
|
||||||
} |
|
||||||
} |
|
@ -1,144 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.BLSPublicKey; |
|
||||||
import org.hyperledger.besu.datatypes.BLSSignature; |
|
||||||
import org.hyperledger.besu.datatypes.GWei; |
|
||||||
import org.hyperledger.besu.ethereum.core.DepositRequest; |
|
||||||
|
|
||||||
import java.util.Objects; |
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonCreator; |
|
||||||
import com.fasterxml.jackson.annotation.JsonGetter; |
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty; |
|
||||||
import io.vertx.core.json.JsonObject; |
|
||||||
import org.apache.tuweni.bytes.Bytes32; |
|
||||||
import org.apache.tuweni.units.bigints.UInt64; |
|
||||||
|
|
||||||
public class DepositRequestParameter { |
|
||||||
|
|
||||||
private final String pubkey; |
|
||||||
|
|
||||||
private final String withdrawalCredentials; |
|
||||||
private final String amount; |
|
||||||
|
|
||||||
private final String signature; |
|
||||||
private final String index; |
|
||||||
|
|
||||||
@JsonCreator |
|
||||||
public DepositRequestParameter( |
|
||||||
@JsonProperty("pubkey") final String pubkey, |
|
||||||
@JsonProperty("withdrawalCredentials") final String withdrawalCredentials, |
|
||||||
@JsonProperty("amount") final String amount, |
|
||||||
@JsonProperty("signature") final String signature, |
|
||||||
@JsonProperty("index") final String index) { |
|
||||||
this.pubkey = pubkey; |
|
||||||
this.withdrawalCredentials = withdrawalCredentials; |
|
||||||
this.amount = amount; |
|
||||||
this.signature = signature; |
|
||||||
this.index = index; |
|
||||||
} |
|
||||||
|
|
||||||
public static DepositRequestParameter fromDeposit(final DepositRequest depositRequest) { |
|
||||||
return new DepositRequestParameter( |
|
||||||
depositRequest.getPubkey().toString(), |
|
||||||
depositRequest.getWithdrawalCredentials().toString(), |
|
||||||
depositRequest.getAmount().toShortHexString(), |
|
||||||
depositRequest.getSignature().toString(), |
|
||||||
depositRequest.getIndex().toBytes().toQuantityHexString()); |
|
||||||
} |
|
||||||
|
|
||||||
public DepositRequest toDeposit() { |
|
||||||
return new DepositRequest( |
|
||||||
BLSPublicKey.fromHexString(pubkey), |
|
||||||
Bytes32.fromHexString(withdrawalCredentials), |
|
||||||
GWei.fromHexString(amount), |
|
||||||
BLSSignature.fromHexString(signature), |
|
||||||
UInt64.fromHexString(index)); |
|
||||||
} |
|
||||||
|
|
||||||
public JsonObject asJsonObject() { |
|
||||||
return new JsonObject() |
|
||||||
.put("pubkey", pubkey) |
|
||||||
.put("withdrawalCredentials", withdrawalCredentials) |
|
||||||
.put("amount", amount) |
|
||||||
.put("signature", signature) |
|
||||||
.put("index", index); |
|
||||||
} |
|
||||||
|
|
||||||
@JsonGetter |
|
||||||
public String getPubkey() { |
|
||||||
return pubkey; |
|
||||||
} |
|
||||||
|
|
||||||
@JsonGetter |
|
||||||
public String getWithdrawalCredentials() { |
|
||||||
return withdrawalCredentials; |
|
||||||
} |
|
||||||
|
|
||||||
@JsonGetter |
|
||||||
public String getAmount() { |
|
||||||
return amount; |
|
||||||
} |
|
||||||
|
|
||||||
@JsonGetter |
|
||||||
public String getSignature() { |
|
||||||
return signature; |
|
||||||
} |
|
||||||
|
|
||||||
@JsonGetter |
|
||||||
public String getIndex() { |
|
||||||
return index; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean equals(final Object o) { |
|
||||||
if (this == o) return true; |
|
||||||
if (o == null || getClass() != o.getClass()) return false; |
|
||||||
final DepositRequestParameter that = (DepositRequestParameter) o; |
|
||||||
return Objects.equals(pubkey, that.pubkey) |
|
||||||
&& Objects.equals(withdrawalCredentials, that.withdrawalCredentials) |
|
||||||
&& Objects.equals(amount, that.amount) |
|
||||||
&& Objects.equals(signature, that.signature) |
|
||||||
&& Objects.equals(index, that.index); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public int hashCode() { |
|
||||||
return Objects.hash(pubkey, withdrawalCredentials, amount, signature, index); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public String toString() { |
|
||||||
return "DepositRequestParameter{" |
|
||||||
+ "pubKey='" |
|
||||||
+ pubkey |
|
||||||
+ '\'' |
|
||||||
+ ", withdrawalCredentials='" |
|
||||||
+ withdrawalCredentials |
|
||||||
+ '\'' |
|
||||||
+ ", amount='" |
|
||||||
+ amount |
|
||||||
+ '\'' |
|
||||||
+ ", signature='" |
|
||||||
+ signature |
|
||||||
+ '\'' |
|
||||||
+ ", index='" |
|
||||||
+ index |
|
||||||
+ '\'' |
|
||||||
+ '}'; |
|
||||||
} |
|
||||||
} |
|
@ -1,103 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.Address; |
|
||||||
import org.hyperledger.besu.datatypes.BLSPublicKey; |
|
||||||
import org.hyperledger.besu.datatypes.GWei; |
|
||||||
import org.hyperledger.besu.ethereum.core.WithdrawalRequest; |
|
||||||
|
|
||||||
import java.util.Objects; |
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonCreator; |
|
||||||
import com.fasterxml.jackson.annotation.JsonGetter; |
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty; |
|
||||||
|
|
||||||
public class WithdrawalRequestParameter { |
|
||||||
|
|
||||||
private final String sourceAddress; |
|
||||||
private final String validatorPubkey; |
|
||||||
private final String amount; |
|
||||||
|
|
||||||
@JsonCreator |
|
||||||
public WithdrawalRequestParameter( |
|
||||||
@JsonProperty("sourceAddress") final String sourceAddress, |
|
||||||
@JsonProperty("validatorPubkey") final String validatorPubkey, |
|
||||||
@JsonProperty("amount") final String amount) { |
|
||||||
this.sourceAddress = sourceAddress; |
|
||||||
this.validatorPubkey = validatorPubkey; |
|
||||||
this.amount = amount; |
|
||||||
} |
|
||||||
|
|
||||||
public static WithdrawalRequestParameter fromWithdrawalRequest( |
|
||||||
final WithdrawalRequest withdrawalRequest) { |
|
||||||
return new WithdrawalRequestParameter( |
|
||||||
withdrawalRequest.getSourceAddress().toHexString(), |
|
||||||
withdrawalRequest.getValidatorPubkey().toHexString(), |
|
||||||
withdrawalRequest.getAmount().toShortHexString()); |
|
||||||
} |
|
||||||
|
|
||||||
public WithdrawalRequest toWithdrawalRequest() { |
|
||||||
return new WithdrawalRequest( |
|
||||||
Address.fromHexString(sourceAddress), |
|
||||||
BLSPublicKey.fromHexString(validatorPubkey), |
|
||||||
GWei.fromHexString(amount)); |
|
||||||
} |
|
||||||
|
|
||||||
@JsonGetter |
|
||||||
public String getSourceAddress() { |
|
||||||
return sourceAddress; |
|
||||||
} |
|
||||||
|
|
||||||
@JsonGetter |
|
||||||
public String getValidatorPubkey() { |
|
||||||
return validatorPubkey; |
|
||||||
} |
|
||||||
|
|
||||||
@JsonGetter |
|
||||||
public String getAmount() { |
|
||||||
return amount; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean equals(final Object o) { |
|
||||||
if (this == o) return true; |
|
||||||
if (o == null || getClass() != o.getClass()) return false; |
|
||||||
final WithdrawalRequestParameter that = (WithdrawalRequestParameter) o; |
|
||||||
return Objects.equals(sourceAddress, that.sourceAddress) |
|
||||||
&& Objects.equals(validatorPubkey, that.validatorPubkey) |
|
||||||
&& Objects.equals(amount, that.amount); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public int hashCode() { |
|
||||||
return Objects.hash(sourceAddress, validatorPubkey, amount); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public String toString() { |
|
||||||
return "WithdrawalRequestParameter{" |
|
||||||
+ "sourceAddress='" |
|
||||||
+ sourceAddress |
|
||||||
+ '\'' |
|
||||||
+ ", validatorPubkey='" |
|
||||||
+ validatorPubkey |
|
||||||
+ '\'' |
|
||||||
+ ", amount='" |
|
||||||
+ amount |
|
||||||
+ '\'' |
|
||||||
+ '}'; |
|
||||||
} |
|
||||||
} |
|
@ -1,42 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters; |
|
||||||
|
|
||||||
public class DepositParameterTestFixture { |
|
||||||
|
|
||||||
public static final DepositRequestParameter DEPOSIT_PARAM_1 = |
|
||||||
createDeposit( |
|
||||||
"0xb10a4a15bf67b328c9b101d09e5c6ee6672978fdad9ef0d9e2ceffaee99223555d8601f0cb3bcc4ce1af9864779a416e", |
|
||||||
"0x0017a7fcf06faf493d30bbe2632ea7c2383cd86825e12797165de7aa35589483", |
|
||||||
"0x773594000", |
|
||||||
"0xa889db8300194050a2636c92a95bc7160515867614b7971a9500cdb62f9c0890217d2901c3241f86fac029428fc106930606154bd9e406d7588934a5f15b837180b17194d6e44bd6de23e43b163dfe12e369dcc75a3852cd997963f158217eb5", |
|
||||||
"0x1"); |
|
||||||
static final DepositRequestParameter DEPOSIT_PARAM_2 = |
|
||||||
createDeposit( |
|
||||||
"0x8706d19a62f28a6a6549f96c5adaebac9124a61d44868ec94f6d2d707c6a2f82c9162071231dfeb40e24bfde4ffdf243", |
|
||||||
"0x006a8dc800c6d8dd6977ef53264e2d030350f0145a91bcd167b4f1c3ea21b271", |
|
||||||
"0x773594000", |
|
||||||
"0x801b08ca107b623eca32ee9f9111b4e50eb9cfe19e38204b72de7dc04c5a5e00f61bab96f10842576f66020ce851083f1583dd9a6b73301bea6c245cf51f27cf96aeb018852c5f70bf485d16b957cfe49ca008913346b431e7653ae3ddb23b07", |
|
||||||
"0x3"); |
|
||||||
|
|
||||||
private static DepositRequestParameter createDeposit( |
|
||||||
final String pubKey, |
|
||||||
final String withdrawalCredentials, |
|
||||||
final String amount, |
|
||||||
final String signature, |
|
||||||
final String index) { |
|
||||||
return new DepositRequestParameter(pubKey, withdrawalCredentials, amount, signature, index); |
|
||||||
} |
|
||||||
} |
|
@ -1,61 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters; |
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat; |
|
||||||
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.DepositParameterTestFixture.DEPOSIT_PARAM_1; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.BLSPublicKey; |
|
||||||
import org.hyperledger.besu.datatypes.BLSSignature; |
|
||||||
import org.hyperledger.besu.datatypes.GWei; |
|
||||||
import org.hyperledger.besu.ethereum.core.DepositRequest; |
|
||||||
|
|
||||||
import org.apache.tuweni.bytes.Bytes32; |
|
||||||
import org.apache.tuweni.units.bigints.UInt64; |
|
||||||
import org.junit.jupiter.api.Test; |
|
||||||
|
|
||||||
public class DepositRequestRequestParameterTest { |
|
||||||
|
|
||||||
@Test |
|
||||||
public void toDeposit() { |
|
||||||
DepositRequest expected = |
|
||||||
new DepositRequest( |
|
||||||
BLSPublicKey.fromHexString( |
|
||||||
"0xb10a4a15bf67b328c9b101d09e5c6ee6672978fdad9ef0d9e2ceffaee99223555d8601f0cb3bcc4ce1af9864779a416e"), |
|
||||||
Bytes32.fromHexString( |
|
||||||
"0x0017a7fcf06faf493d30bbe2632ea7c2383cd86825e12797165de7aa35589483"), |
|
||||||
GWei.of(32000000000L), |
|
||||||
BLSSignature.fromHexString( |
|
||||||
"0xa889db8300194050a2636c92a95bc7160515867614b7971a9500cdb62f9c0890217d2901c3241f86fac029428fc106930606154bd9e406d7588934a5f15b837180b17194d6e44bd6de23e43b163dfe12e369dcc75a3852cd997963f158217eb5"), |
|
||||||
UInt64.ONE); |
|
||||||
assertThat(DEPOSIT_PARAM_1.toDeposit()).isEqualTo(expected); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void fromDeposit() { |
|
||||||
DepositRequest depositRequest = |
|
||||||
new DepositRequest( |
|
||||||
BLSPublicKey.fromHexString( |
|
||||||
"0xb10a4a15bf67b328c9b101d09e5c6ee6672978fdad9ef0d9e2ceffaee99223555d8601f0cb3bcc4ce1af9864779a416e"), |
|
||||||
Bytes32.fromHexString( |
|
||||||
"0x0017a7fcf06faf493d30bbe2632ea7c2383cd86825e12797165de7aa35589483"), |
|
||||||
GWei.of(32000000000L), |
|
||||||
BLSSignature.fromHexString( |
|
||||||
"0xa889db8300194050a2636c92a95bc7160515867614b7971a9500cdb62f9c0890217d2901c3241f86fac029428fc106930606154bd9e406d7588934a5f15b837180b17194d6e44bd6de23e43b163dfe12e369dcc75a3852cd997963f158217eb5"), |
|
||||||
UInt64.ONE); |
|
||||||
|
|
||||||
assertThat(DepositRequestParameter.fromDeposit(depositRequest)).isEqualTo(DEPOSIT_PARAM_1); |
|
||||||
} |
|
||||||
} |
|
@ -1,52 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters; |
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat; |
|
||||||
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.WithdrawalRequestTestFixture.WITHDRAWAL_REQUEST_PARAMETER_1; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.Address; |
|
||||||
import org.hyperledger.besu.datatypes.BLSPublicKey; |
|
||||||
import org.hyperledger.besu.datatypes.GWei; |
|
||||||
import org.hyperledger.besu.ethereum.core.WithdrawalRequest; |
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test; |
|
||||||
|
|
||||||
public class WithdrawalRequestParameterTest { |
|
||||||
|
|
||||||
@Test |
|
||||||
public void toWithdrawalRequest() { |
|
||||||
WithdrawalRequest expected = |
|
||||||
new WithdrawalRequest( |
|
||||||
Address.fromHexString("0x814FaE9f487206471B6B0D713cD51a2D35980000"), |
|
||||||
BLSPublicKey.fromHexString( |
|
||||||
"0xb10a4a15bf67b328c9b101d09e5c6ee6672978fdad9ef0d9e2ceffaee99223555d8601f0cb3bcc4ce1af9864779a416e"), |
|
||||||
GWei.ONE); |
|
||||||
assertThat(WITHDRAWAL_REQUEST_PARAMETER_1.toWithdrawalRequest()).isEqualTo(expected); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void fromWithdrawalRequest() { |
|
||||||
WithdrawalRequest withdrawalRequest = |
|
||||||
new WithdrawalRequest( |
|
||||||
Address.fromHexString("0x814FaE9f487206471B6B0D713cD51a2D35980000"), |
|
||||||
BLSPublicKey.fromHexString( |
|
||||||
"0xb10a4a15bf67b328c9b101d09e5c6ee6672978fdad9ef0d9e2ceffaee99223555d8601f0cb3bcc4ce1af9864779a416e"), |
|
||||||
GWei.ONE); |
|
||||||
|
|
||||||
assertThat(WithdrawalRequestParameter.fromWithdrawalRequest(withdrawalRequest)) |
|
||||||
.isEqualTo(WITHDRAWAL_REQUEST_PARAMETER_1); |
|
||||||
} |
|
||||||
} |
|
@ -1,31 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.GWei; |
|
||||||
|
|
||||||
public class WithdrawalRequestTestFixture { |
|
||||||
|
|
||||||
public static final WithdrawalRequestParameter WITHDRAWAL_REQUEST_PARAMETER_1 = |
|
||||||
new WithdrawalRequestParameter( |
|
||||||
"0x814fae9f487206471b6b0d713cd51a2d35980000", |
|
||||||
"0xb10a4a15bf67b328c9b101d09e5c6ee6672978fdad9ef0d9e2ceffaee99223555d8601f0cb3bcc4ce1af9864779a416e", |
|
||||||
GWei.ONE.toShortHexString()); |
|
||||||
static final WithdrawalRequestParameter WITHDRAWAL_REQUEST_PARAMETER_2 = |
|
||||||
new WithdrawalRequestParameter( |
|
||||||
"0x758b8178a9a4b7206d1f648c4a77c515cbac7000", |
|
||||||
"0x8706d19a62f28a6a6549f96c5adaebac9124a61d44868ec94f6d2d707c6a2f82c9162071231dfeb40e24bfde4ffdf243", |
|
||||||
GWei.ONE.toShortHexString()); |
|
||||||
} |
|
@ -1,90 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.core; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.Address; |
|
||||||
import org.hyperledger.besu.datatypes.BLSPublicKey; |
|
||||||
import org.hyperledger.besu.datatypes.PublicKey; |
|
||||||
import org.hyperledger.besu.datatypes.RequestType; |
|
||||||
|
|
||||||
import java.util.Objects; |
|
||||||
|
|
||||||
public class ConsolidationRequest extends Request |
|
||||||
implements org.hyperledger.besu.plugin.data.ConsolidationRequest { |
|
||||||
|
|
||||||
private final Address sourceAddress; |
|
||||||
private final BLSPublicKey sourcePubkey; |
|
||||||
private final BLSPublicKey targetPubkey; |
|
||||||
|
|
||||||
public ConsolidationRequest( |
|
||||||
final Address sourceAddress, |
|
||||||
final BLSPublicKey sourcePubkey, |
|
||||||
final BLSPublicKey targetPubkey) { |
|
||||||
this.sourceAddress = sourceAddress; |
|
||||||
this.sourcePubkey = sourcePubkey; |
|
||||||
this.targetPubkey = targetPubkey; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public RequestType getType() { |
|
||||||
return RequestType.CONSOLIDATION; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public Address getSourceAddress() { |
|
||||||
return sourceAddress; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public PublicKey getSourcePubkey() { |
|
||||||
return sourcePubkey; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public PublicKey getTargetPubkey() { |
|
||||||
return targetPubkey; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public String toString() { |
|
||||||
return "ConsolidationRequest{" |
|
||||||
+ "sourceAddress=" |
|
||||||
+ sourceAddress |
|
||||||
+ " sourcePubkey=" |
|
||||||
+ sourcePubkey |
|
||||||
+ " targetPubkey=" |
|
||||||
+ targetPubkey |
|
||||||
+ '}'; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean equals(final Object o) { |
|
||||||
if (this == o) { |
|
||||||
return true; |
|
||||||
} |
|
||||||
if (o == null || getClass() != o.getClass()) { |
|
||||||
return false; |
|
||||||
} |
|
||||||
final ConsolidationRequest that = (ConsolidationRequest) o; |
|
||||||
return Objects.equals(sourceAddress, that.sourceAddress) |
|
||||||
&& Objects.equals(sourcePubkey, that.sourcePubkey) |
|
||||||
&& Objects.equals(targetPubkey, that.targetPubkey); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public int hashCode() { |
|
||||||
return Objects.hash(sourceAddress, sourcePubkey, targetPubkey); |
|
||||||
} |
|
||||||
} |
|
@ -1,112 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.core; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.BLSPublicKey; |
|
||||||
import org.hyperledger.besu.datatypes.BLSSignature; |
|
||||||
import org.hyperledger.besu.datatypes.GWei; |
|
||||||
import org.hyperledger.besu.datatypes.PublicKey; |
|
||||||
import org.hyperledger.besu.datatypes.RequestType; |
|
||||||
|
|
||||||
import java.util.Objects; |
|
||||||
|
|
||||||
import org.apache.tuweni.bytes.Bytes32; |
|
||||||
import org.apache.tuweni.units.bigints.UInt64; |
|
||||||
|
|
||||||
public class DepositRequest extends Request |
|
||||||
implements org.hyperledger.besu.plugin.data.DepositRequest { |
|
||||||
|
|
||||||
private final BLSPublicKey pubkey; |
|
||||||
private final Bytes32 depositWithdrawalCredentials; |
|
||||||
private final GWei amount; |
|
||||||
private final BLSSignature signature; |
|
||||||
private final UInt64 index; |
|
||||||
|
|
||||||
public DepositRequest( |
|
||||||
final BLSPublicKey pubkey, |
|
||||||
final Bytes32 depositWithdrawalCredentials, |
|
||||||
final GWei amount, |
|
||||||
final BLSSignature signature, |
|
||||||
final UInt64 index) { |
|
||||||
this.pubkey = pubkey; |
|
||||||
this.depositWithdrawalCredentials = depositWithdrawalCredentials; |
|
||||||
this.amount = amount; |
|
||||||
this.signature = signature; |
|
||||||
this.index = index; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public RequestType getType() { |
|
||||||
return RequestType.DEPOSIT; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public PublicKey getPubkey() { |
|
||||||
return pubkey; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public Bytes32 getWithdrawalCredentials() { |
|
||||||
return depositWithdrawalCredentials; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public GWei getAmount() { |
|
||||||
return amount; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public BLSSignature getSignature() { |
|
||||||
return signature; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public UInt64 getIndex() { |
|
||||||
return index; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public String toString() { |
|
||||||
return "Deposit{" |
|
||||||
+ "pubKey=" |
|
||||||
+ pubkey |
|
||||||
+ ", withdrawalCredentials=" |
|
||||||
+ depositWithdrawalCredentials |
|
||||||
+ ", amount=" |
|
||||||
+ amount |
|
||||||
+ ", signature=" |
|
||||||
+ signature |
|
||||||
+ ", index=" |
|
||||||
+ index |
|
||||||
+ '}'; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean equals(final Object o) { |
|
||||||
if (this == o) return true; |
|
||||||
if (o == null || getClass() != o.getClass()) return false; |
|
||||||
final DepositRequest that = (DepositRequest) o; |
|
||||||
return Objects.equals(pubkey, that.pubkey) |
|
||||||
&& Objects.equals(depositWithdrawalCredentials, that.depositWithdrawalCredentials) |
|
||||||
&& Objects.equals(amount, that.amount) |
|
||||||
&& Objects.equals(signature, that.signature) |
|
||||||
&& Objects.equals(index, that.index); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public int hashCode() { |
|
||||||
return Objects.hash(pubkey, depositWithdrawalCredentials, amount, signature, index); |
|
||||||
} |
|
||||||
} |
|
@ -1,89 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.core; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.Address; |
|
||||||
import org.hyperledger.besu.datatypes.BLSPublicKey; |
|
||||||
import org.hyperledger.besu.datatypes.GWei; |
|
||||||
import org.hyperledger.besu.datatypes.PublicKey; |
|
||||||
import org.hyperledger.besu.datatypes.RequestType; |
|
||||||
|
|
||||||
import java.util.Objects; |
|
||||||
|
|
||||||
public class WithdrawalRequest extends Request |
|
||||||
implements org.hyperledger.besu.plugin.data.WithdrawalRequest { |
|
||||||
|
|
||||||
private final Address sourceAddress; |
|
||||||
private final BLSPublicKey validatorPubkey; |
|
||||||
private final GWei amount; |
|
||||||
|
|
||||||
public WithdrawalRequest( |
|
||||||
final Address sourceAddress, final BLSPublicKey validatorPubkey, final GWei amount) { |
|
||||||
this.sourceAddress = sourceAddress; |
|
||||||
this.validatorPubkey = validatorPubkey; |
|
||||||
this.amount = amount; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public RequestType getType() { |
|
||||||
return RequestType.WITHDRAWAL; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public Address getSourceAddress() { |
|
||||||
return sourceAddress; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public PublicKey getValidatorPubkey() { |
|
||||||
return validatorPubkey; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public GWei getAmount() { |
|
||||||
return amount; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public String toString() { |
|
||||||
return "WithdrawalRequest{" |
|
||||||
+ "sourceAddress=" |
|
||||||
+ sourceAddress |
|
||||||
+ " validatorPubkey=" |
|
||||||
+ validatorPubkey |
|
||||||
+ " amount=" |
|
||||||
+ amount |
|
||||||
+ '}'; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean equals(final Object o) { |
|
||||||
if (this == o) { |
|
||||||
return true; |
|
||||||
} |
|
||||||
if (o == null || getClass() != o.getClass()) { |
|
||||||
return false; |
|
||||||
} |
|
||||||
final WithdrawalRequest that = (WithdrawalRequest) o; |
|
||||||
return Objects.equals(sourceAddress, that.sourceAddress) |
|
||||||
&& Objects.equals(validatorPubkey, that.validatorPubkey) |
|
||||||
&& Objects.equals(amount, that.amount); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public int hashCode() { |
|
||||||
return Objects.hash(sourceAddress, validatorPubkey, amount); |
|
||||||
} |
|
||||||
} |
|
@ -1,40 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.core.encoding; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.Address; |
|
||||||
import org.hyperledger.besu.datatypes.BLSPublicKey; |
|
||||||
import org.hyperledger.besu.ethereum.core.ConsolidationRequest; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLP; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLPInput; |
|
||||||
|
|
||||||
import org.apache.tuweni.bytes.Bytes; |
|
||||||
|
|
||||||
public class ConsolidationRequestDecoder { |
|
||||||
|
|
||||||
public static ConsolidationRequest decode(final RLPInput rlpInput) { |
|
||||||
rlpInput.enterList(); |
|
||||||
final Address sourceAddress = Address.readFrom(rlpInput); |
|
||||||
final BLSPublicKey sourcePublicKey = BLSPublicKey.readFrom(rlpInput); |
|
||||||
final BLSPublicKey targetPublicKey = BLSPublicKey.readFrom(rlpInput); |
|
||||||
rlpInput.leaveList(); |
|
||||||
|
|
||||||
return new ConsolidationRequest(sourceAddress, sourcePublicKey, targetPublicKey); |
|
||||||
} |
|
||||||
|
|
||||||
public static ConsolidationRequest decodeOpaqueBytes(final Bytes input) { |
|
||||||
return decode(RLP.input(input)); |
|
||||||
} |
|
||||||
} |
|
@ -1,60 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.core.encoding; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.RequestType; |
|
||||||
import org.hyperledger.besu.ethereum.core.ConsolidationRequest; |
|
||||||
import org.hyperledger.besu.ethereum.core.Request; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLP; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLPOutput; |
|
||||||
|
|
||||||
import org.apache.tuweni.bytes.Bytes; |
|
||||||
|
|
||||||
public class ConsolidationRequestEncoder { |
|
||||||
|
|
||||||
/** |
|
||||||
* Encodes a Request into RLP format if it is a ConsolidationRequest. |
|
||||||
* |
|
||||||
* @param request The Request to encode, which must be a ConsolidationRequest. |
|
||||||
* @param rlpOutput The RLPOutput to write the encoded data to. |
|
||||||
* @throws IllegalArgumentException if the provided request is not a ConsolidationRequest. |
|
||||||
*/ |
|
||||||
public static void encode(final Request request, final RLPOutput rlpOutput) { |
|
||||||
if (!request.getType().equals(RequestType.CONSOLIDATION)) { |
|
||||||
throw new IllegalArgumentException( |
|
||||||
"The provided request is not of type ConsolidationRequest."); |
|
||||||
} |
|
||||||
encodeConsolidationRequest((ConsolidationRequest) request, rlpOutput); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Encodes the details of a ConsolidationRequest into RLP format. |
|
||||||
* |
|
||||||
* @param consolidationRequest The ConsolidationRequest to encode. |
|
||||||
* @param rlpOutput The RLPOutput to write the encoded data to. |
|
||||||
*/ |
|
||||||
private static void encodeConsolidationRequest( |
|
||||||
final ConsolidationRequest consolidationRequest, final RLPOutput rlpOutput) { |
|
||||||
rlpOutput.startList(); |
|
||||||
rlpOutput.writeBytes(consolidationRequest.getSourceAddress()); |
|
||||||
rlpOutput.writeBytes(consolidationRequest.getSourcePubkey()); |
|
||||||
rlpOutput.writeBytes(consolidationRequest.getTargetPubkey()); |
|
||||||
rlpOutput.endList(); |
|
||||||
} |
|
||||||
|
|
||||||
public static Bytes encodeOpaqueBytes(final Request consolidationRequest) { |
|
||||||
return RLP.encode(rlpOutput -> encode(consolidationRequest, rlpOutput)); |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,40 @@ |
|||||||
|
/* |
||||||
|
* Copyright contributors to Hyperledger Besu. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||||
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||||
|
* specific language governing permissions and limitations under the License. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.ethereum.core.encoding; |
||||||
|
|
||||||
|
import org.hyperledger.besu.ethereum.core.DepositContract; |
||||||
|
import org.hyperledger.besu.evm.log.Log; |
||||||
|
|
||||||
|
import org.apache.tuweni.bytes.Bytes; |
||||||
|
import org.web3j.tx.Contract; |
||||||
|
|
||||||
|
public class DepositLogDecoder { |
||||||
|
|
||||||
|
public static Bytes decodeFromLog(final Log log) { |
||||||
|
Contract.EventValuesWithLog eventValues = DepositContract.staticExtractDepositEventWithLog(log); |
||||||
|
final Bytes rawPublicKey = |
||||||
|
Bytes.wrap((byte[]) eventValues.getNonIndexedValues().get(0).getValue()); |
||||||
|
final Bytes rawWithdrawalCredential = |
||||||
|
Bytes.wrap((byte[]) eventValues.getNonIndexedValues().get(1).getValue()); |
||||||
|
final Bytes rawAmount = |
||||||
|
Bytes.wrap((byte[]) eventValues.getNonIndexedValues().get(2).getValue()); |
||||||
|
final Bytes rawSignature = |
||||||
|
Bytes.wrap((byte[]) eventValues.getNonIndexedValues().get(3).getValue()); |
||||||
|
final Bytes rawIndex = Bytes.wrap((byte[]) eventValues.getNonIndexedValues().get(4).getValue()); |
||||||
|
|
||||||
|
return Bytes.concatenate( |
||||||
|
rawPublicKey, rawWithdrawalCredential, rawAmount, rawSignature, rawIndex); |
||||||
|
} |
||||||
|
} |
@ -1,70 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.core.encoding; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.BLSPublicKey; |
|
||||||
import org.hyperledger.besu.datatypes.BLSSignature; |
|
||||||
import org.hyperledger.besu.datatypes.GWei; |
|
||||||
import org.hyperledger.besu.ethereum.core.DepositContract; |
|
||||||
import org.hyperledger.besu.ethereum.core.DepositRequest; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLP; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLPInput; |
|
||||||
import org.hyperledger.besu.evm.log.Log; |
|
||||||
|
|
||||||
import java.nio.ByteOrder; |
|
||||||
|
|
||||||
import org.apache.tuweni.bytes.Bytes; |
|
||||||
import org.apache.tuweni.bytes.Bytes32; |
|
||||||
import org.apache.tuweni.units.bigints.UInt64; |
|
||||||
import org.web3j.tx.Contract; |
|
||||||
|
|
||||||
public class DepositRequestDecoder { |
|
||||||
|
|
||||||
public static DepositRequest decode(final RLPInput rlpInput) { |
|
||||||
rlpInput.enterList(); |
|
||||||
final BLSPublicKey publicKey = BLSPublicKey.readFrom(rlpInput); |
|
||||||
final Bytes32 depositWithdrawalCredential = Bytes32.wrap(rlpInput.readBytes()); |
|
||||||
final GWei amount = GWei.of(rlpInput.readUInt64Scalar()); |
|
||||||
final BLSSignature signature = BLSSignature.readFrom(rlpInput); |
|
||||||
final UInt64 index = UInt64.valueOf(rlpInput.readBigIntegerScalar()); |
|
||||||
rlpInput.leaveList(); |
|
||||||
|
|
||||||
return new DepositRequest(publicKey, depositWithdrawalCredential, amount, signature, index); |
|
||||||
} |
|
||||||
|
|
||||||
public static DepositRequest decodeFromLog(final Log log) { |
|
||||||
Contract.EventValuesWithLog eventValues = DepositContract.staticExtractDepositEventWithLog(log); |
|
||||||
final byte[] rawPublicKey = (byte[]) eventValues.getNonIndexedValues().get(0).getValue(); |
|
||||||
final byte[] rawWithdrawalCredential = |
|
||||||
(byte[]) eventValues.getNonIndexedValues().get(1).getValue(); |
|
||||||
final byte[] rawAmount = (byte[]) eventValues.getNonIndexedValues().get(2).getValue(); |
|
||||||
final byte[] rawSignature = (byte[]) eventValues.getNonIndexedValues().get(3).getValue(); |
|
||||||
final byte[] rawIndex = (byte[]) eventValues.getNonIndexedValues().get(4).getValue(); |
|
||||||
|
|
||||||
return new DepositRequest( |
|
||||||
BLSPublicKey.wrap(Bytes.wrap(rawPublicKey)), |
|
||||||
Bytes32.wrap(Bytes.wrap(rawWithdrawalCredential)), |
|
||||||
GWei.of( |
|
||||||
Bytes.wrap(rawAmount) |
|
||||||
.toLong( |
|
||||||
ByteOrder.LITTLE_ENDIAN)), // Amount is little endian as per Deposit Contract
|
|
||||||
BLSSignature.wrap(Bytes.wrap(rawSignature)), |
|
||||||
UInt64.valueOf(Bytes.wrap(rawIndex).reverse().toLong())); |
|
||||||
} |
|
||||||
|
|
||||||
public static DepositRequest decodeOpaqueBytes(final Bytes input) { |
|
||||||
return decode(RLP.input(input)); |
|
||||||
} |
|
||||||
} |
|
@ -1,42 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.core.encoding; |
|
||||||
|
|
||||||
import org.hyperledger.besu.ethereum.core.DepositRequest; |
|
||||||
import org.hyperledger.besu.ethereum.core.Request; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLP; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLPOutput; |
|
||||||
|
|
||||||
import org.apache.tuweni.bytes.Bytes; |
|
||||||
|
|
||||||
public class DepositRequestEncoder { |
|
||||||
|
|
||||||
public static void encode(final Request request, final RLPOutput rlpOutput) { |
|
||||||
if (!(request instanceof DepositRequest depositRequest)) { |
|
||||||
throw new IllegalArgumentException("The provided request is not of type deposit."); |
|
||||||
} |
|
||||||
rlpOutput.startList(); |
|
||||||
rlpOutput.writeBytes(depositRequest.getPubkey()); |
|
||||||
rlpOutput.writeBytes(depositRequest.getWithdrawalCredentials()); |
|
||||||
rlpOutput.writeUInt64Scalar(depositRequest.getAmount()); |
|
||||||
rlpOutput.writeBytes(depositRequest.getSignature()); |
|
||||||
rlpOutput.writeUInt64Scalar(depositRequest.getIndex()); |
|
||||||
rlpOutput.endList(); |
|
||||||
} |
|
||||||
|
|
||||||
public static Bytes encodeOpaqueBytes(final Request deposit) { |
|
||||||
return RLP.encode(rlpOutput -> encode(deposit, rlpOutput)); |
|
||||||
} |
|
||||||
} |
|
@ -1,106 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.core.encoding; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.RequestType; |
|
||||||
import org.hyperledger.besu.ethereum.core.Request; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLP; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLPInput; |
|
||||||
|
|
||||||
import java.util.Optional; |
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap; |
|
||||||
import org.apache.tuweni.bytes.Bytes; |
|
||||||
|
|
||||||
/** |
|
||||||
* Decodes a request from its RLP encoded form. |
|
||||||
* |
|
||||||
* <p>This class provides functionality to decode requests based on their type. |
|
||||||
*/ |
|
||||||
public class RequestDecoder { |
|
||||||
|
|
||||||
@FunctionalInterface |
|
||||||
interface Decoder { |
|
||||||
Request decode(RLPInput input); |
|
||||||
} |
|
||||||
|
|
||||||
private static final ImmutableMap<RequestType, Decoder> DECODERS = |
|
||||||
ImmutableMap.of( |
|
||||||
RequestType.WITHDRAWAL, |
|
||||||
WithdrawalRequestDecoder::decode, |
|
||||||
RequestType.DEPOSIT, |
|
||||||
DepositRequestDecoder::decode, |
|
||||||
RequestType.CONSOLIDATION, |
|
||||||
ConsolidationRequestDecoder::decode); |
|
||||||
|
|
||||||
/** |
|
||||||
* Decodes a request from its RLP encoded bytes. |
|
||||||
* |
|
||||||
* <p>This method first determines the type of the request and then decodes the request data |
|
||||||
* according to the request type. |
|
||||||
* |
|
||||||
* @param rlpInput The RLP encoded request. |
|
||||||
* @return The decoded Request object. |
|
||||||
* @throws IllegalArgumentException if the request type is unsupported or invalid. |
|
||||||
*/ |
|
||||||
public static Request decode(final RLPInput rlpInput) { |
|
||||||
final Bytes requestBytes = rlpInput.readBytes(); |
|
||||||
return getRequestType(requestBytes) |
|
||||||
.map(type -> decodeRequest(requestBytes, type)) |
|
||||||
.orElseThrow(() -> new IllegalArgumentException("Unsupported or invalid request type")); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Decodes the request data according to the request type. |
|
||||||
* |
|
||||||
* @param requestBytes The bytes representing the request, including the request type byte. |
|
||||||
* @param requestType The type of the request to decode. |
|
||||||
* @return The decoded Request. |
|
||||||
* @throws IllegalStateException if no decoder is found for the specified request type. |
|
||||||
*/ |
|
||||||
private static Request decodeRequest(final Bytes requestBytes, final RequestType requestType) { |
|
||||||
// Skip the first byte which is the request type
|
|
||||||
RLPInput requestInput = RLP.input(requestBytes.slice(1)); |
|
||||||
Decoder decoder = |
|
||||||
Optional.ofNullable(DECODERS.get(requestType)) |
|
||||||
.orElseThrow( |
|
||||||
() -> |
|
||||||
new IllegalStateException( |
|
||||||
"Decoder not found for request type: " + requestType)); |
|
||||||
return decoder.decode(requestInput); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Extracts the request type from the given bytes. |
|
||||||
* |
|
||||||
* @param bytes The bytes from which to extract the request type. |
|
||||||
* @return An Optional containing the RequestType if it could be determined, or an empty Optional |
|
||||||
* otherwise. |
|
||||||
*/ |
|
||||||
private static Optional<RequestType> getRequestType(final Bytes bytes) { |
|
||||||
try { |
|
||||||
byte typeByte = bytes.get(0); |
|
||||||
return Optional.of(RequestType.of(typeByte)); |
|
||||||
} catch (IllegalArgumentException ex) { |
|
||||||
return Optional.empty(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public static Request decodeOpaqueBytes(final Bytes input) { |
|
||||||
|
|
||||||
RequestType type = getRequestType(input).orElseThrow(); |
|
||||||
return decodeRequest(input.slice(1), type); |
|
||||||
} |
|
||||||
} |
|
@ -1,83 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.core.encoding; |
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkNotNull; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.RequestType; |
|
||||||
import org.hyperledger.besu.ethereum.core.Request; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLP; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLPOutput; |
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap; |
|
||||||
import org.apache.tuweni.bytes.Bytes; |
|
||||||
|
|
||||||
/** Encodes Request objects into RLP format. */ |
|
||||||
public class RequestEncoder { |
|
||||||
|
|
||||||
@FunctionalInterface |
|
||||||
interface Encoder { |
|
||||||
void encode(Request request, RLPOutput output); |
|
||||||
} |
|
||||||
|
|
||||||
private static final ImmutableMap<RequestType, RequestEncoder.Encoder> ENCODERS = |
|
||||||
ImmutableMap.of( |
|
||||||
RequestType.WITHDRAWAL, |
|
||||||
WithdrawalRequestEncoder::encode, |
|
||||||
RequestType.DEPOSIT, |
|
||||||
DepositRequestEncoder::encode, |
|
||||||
RequestType.CONSOLIDATION, |
|
||||||
ConsolidationRequestEncoder::encode); |
|
||||||
|
|
||||||
/** |
|
||||||
* Encodes a Request into the provided RLPOutput. |
|
||||||
* |
|
||||||
* @param request The Request to encode. |
|
||||||
* @param rlpOutput The RLPOutput to write the encoded data to. |
|
||||||
*/ |
|
||||||
public static void encode(final Request request, final RLPOutput rlpOutput) { |
|
||||||
final RequestEncoder.Encoder encoder = getEncoder(request.getType()); |
|
||||||
Bytes requestBytes = RLP.encode(out -> encoder.encode(request, out)); |
|
||||||
rlpOutput.writeBytes( |
|
||||||
Bytes.concatenate(Bytes.of(request.getType().getSerializedType()), requestBytes)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Encodes a Request into a Bytes object representing the RLP-encoded data. |
|
||||||
* |
|
||||||
* @param request The Request to encode. |
|
||||||
* @return The RLP-encoded data as a Bytes object. |
|
||||||
*/ |
|
||||||
public static Bytes encodeOpaqueBytes(final Request request) { |
|
||||||
final RequestEncoder.Encoder encoder = getEncoder(request.getType()); |
|
||||||
final BytesValueRLPOutput out = new BytesValueRLPOutput(); |
|
||||||
out.writeByte(request.getType().getSerializedType()); |
|
||||||
encoder.encode(request, out); |
|
||||||
return out.encoded(); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Retrieves the encoder for the specified RequestType. |
|
||||||
* |
|
||||||
* @param requestType The type of the request. |
|
||||||
* @return The encoder for the specified type. |
|
||||||
* @throws NullPointerException if no encoder is found for the specified type. |
|
||||||
*/ |
|
||||||
private static RequestEncoder.Encoder getEncoder(final RequestType requestType) { |
|
||||||
return checkNotNull( |
|
||||||
ENCODERS.get(requestType), "Encoder not found for request type: %s", requestType); |
|
||||||
} |
|
||||||
} |
|
@ -1,41 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.core.encoding; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.Address; |
|
||||||
import org.hyperledger.besu.datatypes.BLSPublicKey; |
|
||||||
import org.hyperledger.besu.datatypes.GWei; |
|
||||||
import org.hyperledger.besu.ethereum.core.WithdrawalRequest; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLP; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLPInput; |
|
||||||
|
|
||||||
import org.apache.tuweni.bytes.Bytes; |
|
||||||
|
|
||||||
public class WithdrawalRequestDecoder { |
|
||||||
|
|
||||||
public static WithdrawalRequest decode(final RLPInput rlpInput) { |
|
||||||
rlpInput.enterList(); |
|
||||||
final Address sourceAddress = Address.readFrom(rlpInput); |
|
||||||
final BLSPublicKey validatorPubkey = BLSPublicKey.readFrom(rlpInput); |
|
||||||
final GWei amount = GWei.of(rlpInput.readUInt64Scalar()); |
|
||||||
rlpInput.leaveList(); |
|
||||||
|
|
||||||
return new WithdrawalRequest(sourceAddress, validatorPubkey, amount); |
|
||||||
} |
|
||||||
|
|
||||||
public static WithdrawalRequest decodeOpaqueBytes(final Bytes input) { |
|
||||||
return decode(RLP.input(input)); |
|
||||||
} |
|
||||||
} |
|
@ -1,59 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.core.encoding; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.RequestType; |
|
||||||
import org.hyperledger.besu.ethereum.core.Request; |
|
||||||
import org.hyperledger.besu.ethereum.core.WithdrawalRequest; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLP; |
|
||||||
import org.hyperledger.besu.ethereum.rlp.RLPOutput; |
|
||||||
|
|
||||||
import org.apache.tuweni.bytes.Bytes; |
|
||||||
|
|
||||||
public class WithdrawalRequestEncoder { |
|
||||||
|
|
||||||
/** |
|
||||||
* Encodes a Request into RLP format if it is a WithdrawalRequest. |
|
||||||
* |
|
||||||
* @param request The Request to encode, which must be a WithdrawalRequest. |
|
||||||
* @param rlpOutput The RLPOutput to write the encoded data to. |
|
||||||
* @throws IllegalArgumentException if the provided request is not a WithdrawalRequest. |
|
||||||
*/ |
|
||||||
public static void encode(final Request request, final RLPOutput rlpOutput) { |
|
||||||
if (!request.getType().equals(RequestType.WITHDRAWAL)) { |
|
||||||
throw new IllegalArgumentException("The provided request is not of type WithdrawalRequest."); |
|
||||||
} |
|
||||||
encodeWithdrawalRequest((WithdrawalRequest) request, rlpOutput); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Encodes the details of a WithdrawalRequest into RLP format. |
|
||||||
* |
|
||||||
* @param withdrawalRequest The WithdrawalRequest to encode. |
|
||||||
* @param rlpOutput The RLPOutput to write the encoded data to. |
|
||||||
*/ |
|
||||||
private static void encodeWithdrawalRequest( |
|
||||||
final WithdrawalRequest withdrawalRequest, final RLPOutput rlpOutput) { |
|
||||||
rlpOutput.startList(); |
|
||||||
rlpOutput.writeBytes(withdrawalRequest.getSourceAddress()); |
|
||||||
rlpOutput.writeBytes(withdrawalRequest.getValidatorPubkey()); |
|
||||||
rlpOutput.writeUInt64Scalar(withdrawalRequest.getAmount()); |
|
||||||
rlpOutput.endList(); |
|
||||||
} |
|
||||||
|
|
||||||
public static Bytes encodeOpaqueBytes(final Request withdrawalRequest) { |
|
||||||
return RLP.encode(rlpOutput -> encode(withdrawalRequest, rlpOutput)); |
|
||||||
} |
|
||||||
} |
|
@ -1,102 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.mainnet.requests; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.Address; |
|
||||||
import org.hyperledger.besu.ethereum.core.Request; |
|
||||||
import org.hyperledger.besu.ethereum.mainnet.SystemCallProcessor; |
|
||||||
|
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.List; |
|
||||||
import java.util.Optional; |
|
||||||
|
|
||||||
import org.apache.tuweni.bytes.Bytes; |
|
||||||
|
|
||||||
/** |
|
||||||
* Abstract base class for processing system call requests. |
|
||||||
* |
|
||||||
* @param <T> The type of request to be processed. |
|
||||||
*/ |
|
||||||
public abstract class AbstractSystemCallRequestProcessor<T extends Request> |
|
||||||
implements RequestProcessor { |
|
||||||
|
|
||||||
/** |
|
||||||
* Processes a system call and converts the result into requests of type T. |
|
||||||
* |
|
||||||
* @param context The request context being processed. |
|
||||||
* @return An {@link Optional} containing a list of {@link T} objects if any are found |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
public Optional<List<? extends Request>> process(final ProcessRequestContext context) { |
|
||||||
|
|
||||||
SystemCallProcessor systemCallProcessor = |
|
||||||
new SystemCallProcessor(context.protocolSpec().getTransactionProcessor()); |
|
||||||
|
|
||||||
Bytes systemCallOutput = |
|
||||||
systemCallProcessor.process( |
|
||||||
getCallAddress(), |
|
||||||
context.mutableWorldState().updater(), |
|
||||||
context.blockHeader(), |
|
||||||
context.operationTracer(), |
|
||||||
context.blockHashLookup()); |
|
||||||
|
|
||||||
List<T> requests = parseRequests(systemCallOutput); |
|
||||||
return Optional.ofNullable(requests); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Parses the provided bytes into a list of {@link T} objects. |
|
||||||
* |
|
||||||
* @param bytes The bytes representing requests. |
|
||||||
* @return A list of parsed {@link T} objects. |
|
||||||
*/ |
|
||||||
protected List<T> parseRequests(final Bytes bytes) { |
|
||||||
if (bytes == null) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
final List<T> requests = new ArrayList<>(); |
|
||||||
if (bytes.isEmpty()) { |
|
||||||
return requests; |
|
||||||
} |
|
||||||
int count = bytes.size() / getRequestBytesSize(); |
|
||||||
for (int i = 0; i < count; i++) { |
|
||||||
Bytes requestBytes = bytes.slice(i * getRequestBytesSize(), getRequestBytesSize()); |
|
||||||
requests.add(parseRequest(requestBytes)); |
|
||||||
} |
|
||||||
return requests; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Parses a single request from the provided bytes. |
|
||||||
* |
|
||||||
* @param requestBytes The bytes representing a single request. |
|
||||||
* @return A parsed {@link T} object. |
|
||||||
*/ |
|
||||||
protected abstract T parseRequest(final Bytes requestBytes); |
|
||||||
|
|
||||||
/** |
|
||||||
* Gets the call address for the specific request type. |
|
||||||
* |
|
||||||
* @return The call address. |
|
||||||
*/ |
|
||||||
protected abstract Address getCallAddress(); |
|
||||||
|
|
||||||
/** |
|
||||||
* Gets the size of the bytes representing a single request. |
|
||||||
* |
|
||||||
* @return The size of the bytes representing a single request. |
|
||||||
*/ |
|
||||||
protected abstract int getRequestBytesSize(); |
|
||||||
} |
|
@ -1,73 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.mainnet.requests; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.Address; |
|
||||||
import org.hyperledger.besu.datatypes.BLSPublicKey; |
|
||||||
import org.hyperledger.besu.ethereum.core.ConsolidationRequest; |
|
||||||
|
|
||||||
import org.apache.tuweni.bytes.Bytes; |
|
||||||
|
|
||||||
public class ConsolidationRequestProcessor |
|
||||||
extends AbstractSystemCallRequestProcessor<ConsolidationRequest> { |
|
||||||
public static final Address CONSOLIDATION_REQUEST_CONTRACT_ADDRESS = |
|
||||||
Address.fromHexString("0x00b42dbF2194e931E80326D950320f7d9Dbeac02"); |
|
||||||
|
|
||||||
private static final int ADDRESS_BYTES = 20; |
|
||||||
private static final int PUBLIC_KEY_BYTES = 48; |
|
||||||
private static final int CONSOLIDATION_REQUEST_BYTES_SIZE = |
|
||||||
ADDRESS_BYTES + PUBLIC_KEY_BYTES + PUBLIC_KEY_BYTES; |
|
||||||
private final Address consolidationRequestContractAddress; |
|
||||||
|
|
||||||
public ConsolidationRequestProcessor(final Address consolidationRequestContractAddress) { |
|
||||||
this.consolidationRequestContractAddress = consolidationRequestContractAddress; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Gets the call address for consolidation requests. |
|
||||||
* |
|
||||||
* @return The call address. |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
protected Address getCallAddress() { |
|
||||||
return consolidationRequestContractAddress; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Gets the size of the bytes representing a single consolidation request. |
|
||||||
* |
|
||||||
* @return The size of the bytes representing a single consolidation request. |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
protected int getRequestBytesSize() { |
|
||||||
return CONSOLIDATION_REQUEST_BYTES_SIZE; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Parses a single consolidation request from the provided bytes. |
|
||||||
* |
|
||||||
* @param requestBytes The bytes representing a single consolidation request. |
|
||||||
* @return A parsed {@link ConsolidationRequest} object. |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
protected ConsolidationRequest parseRequest(final Bytes requestBytes) { |
|
||||||
final Address sourceAddress = Address.wrap(requestBytes.slice(0, ADDRESS_BYTES)); |
|
||||||
final BLSPublicKey sourcePublicKey = |
|
||||||
BLSPublicKey.wrap(requestBytes.slice(ADDRESS_BYTES, PUBLIC_KEY_BYTES)); |
|
||||||
final BLSPublicKey targetPublicKey = |
|
||||||
BLSPublicKey.wrap(requestBytes.slice(ADDRESS_BYTES + PUBLIC_KEY_BYTES, PUBLIC_KEY_BYTES)); |
|
||||||
return new ConsolidationRequest(sourceAddress, sourcePublicKey, targetPublicKey); |
|
||||||
} |
|
||||||
} |
|
@ -1,92 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.mainnet.requests; |
|
||||||
|
|
||||||
import static org.hyperledger.besu.ethereum.mainnet.requests.RequestUtil.getConsolidationRequests; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.Hash; |
|
||||||
import org.hyperledger.besu.ethereum.core.Block; |
|
||||||
import org.hyperledger.besu.ethereum.core.ConsolidationRequest; |
|
||||||
import org.hyperledger.besu.ethereum.core.Request; |
|
||||||
import org.hyperledger.besu.ethereum.core.TransactionReceipt; |
|
||||||
|
|
||||||
import java.util.Collections; |
|
||||||
import java.util.List; |
|
||||||
import java.util.Optional; |
|
||||||
|
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
public class ConsolidationRequestValidator implements RequestValidator { |
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(ConsolidationRequestValidator.class); |
|
||||||
|
|
||||||
public static final int MAX_CONSOLIDATION_REQUESTS_PER_BLOCK = 1; |
|
||||||
|
|
||||||
private boolean validateConsolidationRequestParameter( |
|
||||||
final Optional<List<ConsolidationRequest>> consolidationRequests) { |
|
||||||
return consolidationRequests.isPresent(); |
|
||||||
} |
|
||||||
|
|
||||||
private boolean validateConsolidationRequestsInBlock( |
|
||||||
final Block block, final List<ConsolidationRequest> consolidationRequests) { |
|
||||||
final Hash blockHash = block.getHash(); |
|
||||||
|
|
||||||
final List<ConsolidationRequest> consolidationRequestsInBlock = |
|
||||||
block |
|
||||||
.getBody() |
|
||||||
.getRequests() |
|
||||||
.flatMap(requests -> getConsolidationRequests(Optional.of(requests))) |
|
||||||
.orElse(Collections.emptyList()); |
|
||||||
|
|
||||||
if (consolidationRequestsInBlock.size() > MAX_CONSOLIDATION_REQUESTS_PER_BLOCK) { |
|
||||||
LOG.warn( |
|
||||||
"Block {} has more than the allowed maximum number of consolidation requests", blockHash); |
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
// Validate ConsolidationRequests
|
|
||||||
final boolean expectedConsolidationRequestMatch = |
|
||||||
consolidationRequests.equals(consolidationRequestsInBlock); |
|
||||||
if (!expectedConsolidationRequestMatch) { |
|
||||||
LOG.warn( |
|
||||||
"Block {} has a mismatch between block consolidations and RPC consolidation requests (in_block = {}, " |
|
||||||
+ "expected = {})", |
|
||||||
blockHash, |
|
||||||
consolidationRequestsInBlock, |
|
||||||
consolidationRequests); |
|
||||||
return false; |
|
||||||
} |
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean validate( |
|
||||||
final Block block, final List<Request> requests, final List<TransactionReceipt> receipts) { |
|
||||||
var consolidationRequests = |
|
||||||
getConsolidationRequests(Optional.of(requests)).orElse(Collections.emptyList()); |
|
||||||
return validateConsolidationRequestsInBlock(block, consolidationRequests); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean validateParameter(final Optional<List<Request>> request) { |
|
||||||
if (request.isEmpty()) { |
|
||||||
return true; |
|
||||||
} |
|
||||||
var consolidationRequests = |
|
||||||
RequestUtil.filterRequestsOfType(request.get(), ConsolidationRequest.class); |
|
||||||
return validateConsolidationRequestParameter(Optional.of(consolidationRequests)); |
|
||||||
} |
|
||||||
} |
|
@ -1,86 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.mainnet.requests; |
|
||||||
|
|
||||||
import static org.hyperledger.besu.ethereum.mainnet.requests.RequestUtil.getDepositRequests; |
|
||||||
|
|
||||||
import org.hyperledger.besu.datatypes.Address; |
|
||||||
import org.hyperledger.besu.ethereum.core.Block; |
|
||||||
import org.hyperledger.besu.ethereum.core.DepositRequest; |
|
||||||
import org.hyperledger.besu.ethereum.core.Request; |
|
||||||
import org.hyperledger.besu.ethereum.core.TransactionReceipt; |
|
||||||
import org.hyperledger.besu.ethereum.core.encoding.DepositRequestDecoder; |
|
||||||
import org.hyperledger.besu.evm.log.Log; |
|
||||||
|
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.Collections; |
|
||||||
import java.util.List; |
|
||||||
import java.util.Optional; |
|
||||||
|
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
public class DepositRequestValidator implements RequestValidator { |
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(DepositRequestValidator.class); |
|
||||||
private final Address depositContractAddress; |
|
||||||
|
|
||||||
public DepositRequestValidator(final Address depositContractAddress) { |
|
||||||
this.depositContractAddress = depositContractAddress; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean validateParameter(final Optional<List<Request>> depositRequests) { |
|
||||||
return depositRequests.isPresent(); |
|
||||||
} |
|
||||||
|
|
||||||
public boolean validateDepositRequests( |
|
||||||
final Block block, |
|
||||||
final List<DepositRequest> actualDepositRequests, |
|
||||||
final List<TransactionReceipt> receipts) { |
|
||||||
|
|
||||||
List<DepositRequest> expectedDepositRequests = new ArrayList<>(); |
|
||||||
|
|
||||||
for (TransactionReceipt receipt : receipts) { |
|
||||||
for (Log log : receipt.getLogsList()) { |
|
||||||
if (depositContractAddress.equals(log.getLogger())) { |
|
||||||
DepositRequest depositRequest = DepositRequestDecoder.decodeFromLog(log); |
|
||||||
expectedDepositRequests.add(depositRequest); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
boolean isValid = actualDepositRequests.equals(expectedDepositRequests); |
|
||||||
|
|
||||||
if (!isValid) { |
|
||||||
LOG.warn( |
|
||||||
"Deposits validation failed. Deposits from block body do not match deposits from logs. Block hash: {}", |
|
||||||
block.getHash()); |
|
||||||
LOG.debug( |
|
||||||
"Deposits from logs: {}, deposits from block body: {}", |
|
||||||
expectedDepositRequests, |
|
||||||
actualDepositRequests); |
|
||||||
} |
|
||||||
|
|
||||||
return isValid; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean validate( |
|
||||||
final Block block, final List<Request> requests, final List<TransactionReceipt> receipts) { |
|
||||||
var depositRequests = getDepositRequests(Optional.of(requests)).orElse(Collections.emptyList()); |
|
||||||
return validateDepositRequests(block, depositRequests, receipts); |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,39 @@ |
|||||||
|
/* |
||||||
|
* Copyright contributors to Hyperledger Besu. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||||
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||||
|
* specific language governing permissions and limitations under the License. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.ethereum.mainnet.requests; |
||||||
|
|
||||||
|
import org.hyperledger.besu.datatypes.RequestType; |
||||||
|
|
||||||
|
public class MainnetRequestsProcessor { |
||||||
|
|
||||||
|
public static RequestProcessorCoordinator pragueRequestsProcessors( |
||||||
|
final RequestContractAddresses requestContractAddresses) { |
||||||
|
return new RequestProcessorCoordinator.Builder() |
||||||
|
.addProcessor( |
||||||
|
RequestType.WITHDRAWAL, |
||||||
|
new SystemCallRequestProcessor( |
||||||
|
requestContractAddresses.getWithdrawalRequestContractAddress(), |
||||||
|
RequestType.WITHDRAWAL)) |
||||||
|
.addProcessor( |
||||||
|
RequestType.CONSOLIDATION, |
||||||
|
new SystemCallRequestProcessor( |
||||||
|
requestContractAddresses.getConsolidationRequestContractAddress(), |
||||||
|
RequestType.CONSOLIDATION)) |
||||||
|
.addProcessor( |
||||||
|
RequestType.DEPOSIT, |
||||||
|
new DepositRequestProcessor(requestContractAddresses.getDepositContractAddress())) |
||||||
|
.build(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,38 @@ |
|||||||
|
/* |
||||||
|
* Copyright contributors to Besu. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||||
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||||
|
* specific language governing permissions and limitations under the License. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.ethereum.mainnet.requests; |
||||||
|
|
||||||
|
import org.hyperledger.besu.ethereum.core.Request; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
public class ProhibitedRequestValidator implements RequestsValidator { |
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(MainnetRequestsValidator.class); |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean validate(final Optional<List<Request>> maybeRequests) { |
||||||
|
boolean hasRequests = maybeRequests.isPresent(); |
||||||
|
|
||||||
|
if (hasRequests) { |
||||||
|
LOG.warn("There are requests but requests are prohibited"); |
||||||
|
} |
||||||
|
|
||||||
|
return !hasRequests; |
||||||
|
} |
||||||
|
} |
@ -1,53 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.mainnet.requests; |
|
||||||
|
|
||||||
import org.hyperledger.besu.ethereum.core.Block; |
|
||||||
import org.hyperledger.besu.ethereum.core.Request; |
|
||||||
import org.hyperledger.besu.ethereum.core.TransactionReceipt; |
|
||||||
|
|
||||||
import java.util.List; |
|
||||||
import java.util.Optional; |
|
||||||
|
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
/** Validates that a block does not contain any prohibited requests. */ |
|
||||||
public class ProhibitedRequestsValidator implements RequestValidator { |
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(ProhibitedRequestsValidator.class); |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean validate( |
|
||||||
final Block block, final List<Request> request, final List<TransactionReceipt> receipts) { |
|
||||||
boolean hasRequests = block.getBody().getRequests().isPresent(); |
|
||||||
boolean hasRequestsRoot = block.getHeader().getRequestsRoot().isPresent(); |
|
||||||
|
|
||||||
if (hasRequests) { |
|
||||||
LOG.warn("Block {} contains requests but requests are prohibited", block.getHash()); |
|
||||||
} |
|
||||||
|
|
||||||
if (hasRequestsRoot) { |
|
||||||
LOG.warn( |
|
||||||
"Block {} header contains requests_root but requests are prohibited", block.getHash()); |
|
||||||
} |
|
||||||
|
|
||||||
return !(hasRequests || hasRequestsRoot); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean validateParameter(final Optional<List<Request>> request) { |
|
||||||
return request.isEmpty(); |
|
||||||
} |
|
||||||
} |
|
@ -1,85 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright contributors to Hyperledger Besu. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
||||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
||||||
* specific language governing permissions and limitations under the License. |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: Apache-2.0 |
|
||||||
*/ |
|
||||||
package org.hyperledger.besu.ethereum.mainnet.requests; |
|
||||||
|
|
||||||
import org.hyperledger.besu.ethereum.core.ConsolidationRequest; |
|
||||||
import org.hyperledger.besu.ethereum.core.DepositRequest; |
|
||||||
import org.hyperledger.besu.ethereum.core.Request; |
|
||||||
import org.hyperledger.besu.ethereum.core.WithdrawalRequest; |
|
||||||
|
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.Collections; |
|
||||||
import java.util.List; |
|
||||||
import java.util.Optional; |
|
||||||
|
|
||||||
public class RequestUtil { |
|
||||||
|
|
||||||
/** |
|
||||||
* Filters and returns a list of requests of a specific type from a given list of requests. |
|
||||||
* |
|
||||||
* @param <T> The type of the request to filter by, extending Request. |
|
||||||
* @param requests The list of requests to filter. |
|
||||||
* @param requestType The class of the request type to filter for. |
|
||||||
* @return A List containing only requests of the specified type, or an empty list if the input |
|
||||||
* list is null or contains no requests of the specified type. |
|
||||||
*/ |
|
||||||
public static <T extends Request> List<T> filterRequestsOfType( |
|
||||||
final List<Request> requests, final Class<T> requestType) { |
|
||||||
if (requests == null) { |
|
||||||
return Collections.emptyList(); |
|
||||||
} |
|
||||||
return requests.stream().filter(requestType::isInstance).map(requestType::cast).toList(); |
|
||||||
} |
|
||||||
|
|
||||||
public static Optional<List<DepositRequest>> getDepositRequests( |
|
||||||
final Optional<List<Request>> requests) { |
|
||||||
return requests.map(r -> filterRequestsOfType(r, DepositRequest.class)); |
|
||||||
} |
|
||||||
|
|
||||||
public static Optional<List<WithdrawalRequest>> getWithdrawalRequests( |
|
||||||
final Optional<List<Request>> requests) { |
|
||||||
return requests.map(r -> filterRequestsOfType(r, WithdrawalRequest.class)); |
|
||||||
} |
|
||||||
|
|
||||||
public static Optional<List<ConsolidationRequest>> getConsolidationRequests( |
|
||||||
final Optional<List<Request>> requests) { |
|
||||||
return requests.map(r -> filterRequestsOfType(r, ConsolidationRequest.class)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Combines multiple optional lists of requests into a single optional list. |
|
||||||
* |
|
||||||
* @param maybeDepositRequests Optional list of deposit requests. |
|
||||||
* @param maybeWithdrawalRequest Optional list of withdrawal requests. |
|
||||||
* @param maybeConsolidationRequest Optional list of withdrawal requests. |
|
||||||
* @return An Optional containing the combined list of requests, or an empty Optional if all input |
|
||||||
* lists are empty. |
|
||||||
*/ |
|
||||||
public static Optional<List<Request>> combine( |
|
||||||
final Optional<List<Request>> maybeDepositRequests, |
|
||||||
final Optional<List<Request>> maybeWithdrawalRequest, |
|
||||||
final Optional<List<Request>> maybeConsolidationRequest) { |
|
||||||
if (maybeDepositRequests.isEmpty() |
|
||||||
&& maybeWithdrawalRequest.isEmpty() |
|
||||||
&& maybeConsolidationRequest.isEmpty()) { |
|
||||||
return Optional.empty(); |
|
||||||
} |
|
||||||
List<Request> requests = new ArrayList<>(); |
|
||||||
maybeDepositRequests.ifPresent(requests::addAll); |
|
||||||
maybeWithdrawalRequest.ifPresent(requests::addAll); |
|
||||||
maybeConsolidationRequest.ifPresent(requests::addAll); |
|
||||||
return Optional.of(requests); |
|
||||||
} |
|
||||||
} |
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue