diff --git a/datatypes/src/main/java/org/hyperledger/besu/datatypes/SetCodeAuthorization.java b/datatypes/src/main/java/org/hyperledger/besu/datatypes/SetCodeAuthorization.java index 71c65a3517a..b12e65de412 100644 --- a/datatypes/src/main/java/org/hyperledger/besu/datatypes/SetCodeAuthorization.java +++ b/datatypes/src/main/java/org/hyperledger/besu/datatypes/SetCodeAuthorization.java @@ -17,7 +17,6 @@ import org.hyperledger.besu.crypto.SECPSignature; import java.math.BigInteger; -import java.util.List; import java.util.Optional; /** @@ -60,13 +59,6 @@ public interface SetCodeAuthorization { */ Optional nonce(); - /** - * Return all nonces in the list - * - * @return all the nonces - */ - List nonceList(); - /** * Return the recovery id. * diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/SetCodeAuthorization.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/SetCodeAuthorization.java index 2c965a7474a..ea9cae2fcb2 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/SetCodeAuthorization.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/SetCodeAuthorization.java @@ -41,7 +41,7 @@ public class SetCodeAuthorization implements org.hyperledger.besu.datatypes.SetC private final BigInteger chainId; private final Address address; - private final List nonces; + private final Optional nonce; private final SECPSignature signature; private Optional
authorizer = Optional.empty(); private boolean isAuthorityComputed = false; @@ -51,17 +51,17 @@ public class SetCodeAuthorization implements org.hyperledger.besu.datatypes.SetC * * @param chainId can be either the current chain id or zero * @param address the address from which the code will be set into the EOA account - * @param nonces the list of nonces + * @param nonce an optional nonce after which this auth expires * @param signature the signature of the EOA account which will be used to set the code */ public SetCodeAuthorization( final BigInteger chainId, final Address address, - final List nonces, + final Optional nonce, final SECPSignature signature) { this.chainId = chainId; this.address = address; - this.nonces = nonces; + this.nonce = nonce; this.signature = signature; } @@ -85,7 +85,10 @@ public static org.hyperledger.besu.datatypes.SetCodeAuthorization createSetCodeA @JsonProperty("r") final BigInteger r, @JsonProperty("s") final BigInteger s) { return new SetCodeAuthorization( - chainId, address, nonces, SIGNATURE_ALGORITHM.get().createSignature(r, s, v)); + chainId, + address, + Optional.ofNullable(nonces.get(0)), + SIGNATURE_ALGORITHM.get().createSignature(r, s, v)); } @JsonProperty("chainId") @@ -118,13 +121,7 @@ public Optional
authorizer() { @Override public Optional nonce() { - return nonces.size() == 1 ? Optional.of(nonces.getFirst()) : Optional.empty(); - } - - @JsonProperty("nonce") - @Override - public List nonceList() { - return nonces; + return nonce; } @JsonProperty("v") @@ -170,7 +167,7 @@ public static Builder builder() { public static class Builder { private BigInteger chainId = BigInteger.ZERO; private Address address; - private List nonces = List.of(); + private Optional nonce = Optional.empty(); private SECPSignature signature; /** Create a new builder. */ @@ -199,13 +196,13 @@ public Builder address(final Address address) { } /** - * Set the list of optional nonces. + * Set the optional nonce. * - * @param nonces the list of nonces. Only the first nonce will be used. + * @param nonce the optional nonce. * @return this builder */ - public Builder nonces(final List nonces) { - this.nonces = nonces; + public Builder nonces(final Optional nonce) { + this.nonce = nonce; return this; } @@ -232,7 +229,7 @@ public org.hyperledger.besu.datatypes.SetCodeAuthorization signAndBuild(final Ke output.writeBigIntegerScalar(chainId); output.writeBytes(address); output.startList(); - nonces.forEach(output::writeLongScalar); + nonce.ifPresent(output::writeLongScalar); output.endList(); output.endList(); @@ -257,7 +254,7 @@ public org.hyperledger.besu.datatypes.SetCodeAuthorization build() { throw new IllegalStateException("Signature must be set"); } - return new SetCodeAuthorization(chainId, address, nonces, signature); + return new SetCodeAuthorization(chainId, address, nonce, signature); } } } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionDecoder.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionDecoder.java index b394b970489..80d59de83be 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionDecoder.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionDecoder.java @@ -26,8 +26,7 @@ import org.hyperledger.besu.ethereum.rlp.RLPInput; import java.math.BigInteger; -import java.util.ArrayList; -import java.util.List; +import java.util.Optional; import java.util.function.Supplier; import com.google.common.base.Suppliers; @@ -88,7 +87,7 @@ public static org.hyperledger.besu.datatypes.SetCodeAuthorization decodeInnerPay final BigInteger chainId = input.readBigIntegerScalar(); final Address address = Address.wrap(input.readBytes()); - final List nonces = new ArrayList<>(); + Optional nonce = Optional.empty(); if (!input.nextIsList()) { throw new IllegalArgumentException("Optional nonce must be an list, but isn't"); @@ -97,8 +96,10 @@ public static org.hyperledger.besu.datatypes.SetCodeAuthorization decodeInnerPay final long noncesSize = input.nextSize(); input.enterList(); - for (int i = 0; i < noncesSize; i++) { - nonces.add(input.readLongScalar()); + if (noncesSize == 1) { + nonce = Optional.ofNullable(input.readLongScalar()); + } else if (noncesSize > 1) { + throw new IllegalArgumentException("Nonce list may only have 1 member, if any"); } input.leaveList(); @@ -110,6 +111,6 @@ public static org.hyperledger.besu.datatypes.SetCodeAuthorization decodeInnerPay final SECPSignature signature = SIGNATURE_ALGORITHM.get().createSignature(r, s, yParity); - return new SetCodeAuthorization(chainId, address, nonces, signature); + return new SetCodeAuthorization(chainId, address, nonce, signature); } } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionEncoder.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionEncoder.java index 6f11c9f87d7..05808f3129f 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionEncoder.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionEncoder.java @@ -60,7 +60,7 @@ private static void encodeAuthorizationDetails( rlpOutput.writeBigIntegerScalar(payload.chainId()); rlpOutput.writeBytes(payload.address().copy()); rlpOutput.startList(); - payload.nonceList().forEach(rlpOutput::writeLongScalar); + payload.nonce().ifPresent(rlpOutput::writeLongScalar); rlpOutput.endList(); } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionDecoderTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionDecoderTest.java index 1064cfcbaba..610b5906f54 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionDecoderTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionDecoderTest.java @@ -15,6 +15,7 @@ package org.hyperledger.besu.ethereum.core.encoding; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.hyperledger.besu.crypto.SECPSignature; import org.hyperledger.besu.datatypes.Address; @@ -42,7 +43,6 @@ void shouldDecodeInnerPayloadWithNonce() { assertThat(authorization.chainId()).isEqualTo(BigInteger.ONE); assertThat(authorization.address()) .isEqualTo(Address.fromHexStringStrict("0x633688abc3cCf8B0C03088D2d1C6ae4958c2fA56")); - assertThat(authorization.nonceList().size()).isEqualTo(1); assertThat(authorization.nonce().get()).isEqualTo(0L); final SECPSignature signature = authorization.signature(); @@ -67,7 +67,7 @@ void shouldDecodeInnerPayloadWithoutNonce() { assertThat(authorization.chainId()).isEqualTo(BigInteger.ONE); assertThat(authorization.address()) .isEqualTo(Address.fromHexStringStrict("0x633688abc3cCf8B0C03088D2d1C6ae4958c2fA56")); - assertThat(authorization.nonceList().size()).isEqualTo(0); + assertThat(authorization.nonce()).isEmpty(); final SECPSignature signature = authorization.signature(); assertThat(signature.getRecId()).isEqualTo((byte) 1); @@ -78,7 +78,7 @@ void shouldDecodeInnerPayloadWithoutNonce() { } @Test - void shouldDecodeInnerPayloadWithMultipleNonces() { + void shouldThrowInnerPayloadWithMultipleNonces() { // "d90194633688abc3ccf8b0c03088d2d1c6ae4958c2fa56c20107" final BytesValueRLPInput input = @@ -86,21 +86,12 @@ void shouldDecodeInnerPayloadWithMultipleNonces() { Bytes.fromHexString( "0xf85c0194633688abc3ccf8b0c03088d2d1c6ae4958c2fa56c2010201a0401b5d4ebe88306448115d1a46a30e5ad1136f2818b4ebb0733d9c4efffd135aa0753ff1dbce6db504ecb9635a64d8c4506ff887e2d2a0d2b7175baf94c849eccc"), true); - final SetCodeAuthorization authorization = SetCodeTransactionDecoder.decodeInnerPayload(input); - - assertThat(authorization.chainId()).isEqualTo(BigInteger.ONE); - assertThat(authorization.address()) - .isEqualTo(Address.fromHexStringStrict("0x633688abc3cCf8B0C03088D2d1C6ae4958c2fA56")); - assertThat(authorization.nonceList().size()).isEqualTo(2); - assertThat(authorization.nonce().get()).isEqualTo(1L); - assertThat(authorization.nonceList().get(1)).isEqualTo(2L); - final SECPSignature signature = authorization.signature(); - assertThat(signature.getRecId()).isEqualTo((byte) 1); - assertThat(signature.getR().toString(16)) - .isEqualTo("401b5d4ebe88306448115d1a46a30e5ad1136f2818b4ebb0733d9c4efffd135a"); - assertThat(signature.getS().toString(16)) - .isEqualTo("753ff1dbce6db504ecb9635a64d8c4506ff887e2d2a0d2b7175baf94c849eccc"); + assertThrows( + IllegalArgumentException.class, + () -> { + SetCodeTransactionDecoder.decodeInnerPayload(input); + }); } @Test diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionEncoderTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionEncoderTest.java index bd63f7527b0..89211f4ba31 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionEncoderTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/SetCodeTransactionEncoderTest.java @@ -23,8 +23,7 @@ import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; import java.math.BigInteger; -import java.util.ArrayList; -import java.util.List; +import java.util.Optional; import java.util.function.Supplier; import com.google.common.base.Suppliers; @@ -51,7 +50,7 @@ void shouldEncodeSingleSetCodeWithNonce() { new SetCodeAuthorization( BigInteger.ONE, Address.fromHexString("0x633688abc3cCf8B0C03088D2d1C6ae4958c2fA56"), - List.of(0L), + Optional.of(0L), SIGNATURE_ALGORITHM .get() .createSignature( @@ -77,7 +76,7 @@ void shouldEncodeSingleSetCodeWithoutNonce() { new SetCodeAuthorization( BigInteger.ONE, Address.fromHexString("0x633688abc3cCf8B0C03088D2d1C6ae4958c2fA56"), - new ArrayList<>(), + Optional.empty(), SIGNATURE_ALGORITHM .get() .createSignature( @@ -95,32 +94,6 @@ void shouldEncodeSingleSetCodeWithoutNonce() { "0xf85a0194633688abc3ccf8b0c03088d2d1c6ae4958c2fa56c001a0dd6b24048be1b7d7fe5bbbb73ffc37eb2ce1997ecb4ae5b6096532ef19363148a025b58a1ff8ad00bddbbfa1d5c2411961cbb6d08dcdc8ae88303db3c6cf983031")); } - @Test - void shouldEncodeSingleSetCodeWithMultipleNonces() { - // "d90194633688abc3ccf8b0c03088d2d1c6ae4958c2fa56c20107" - - final SetCodeAuthorization authorization = - new SetCodeAuthorization( - BigInteger.ONE, - Address.fromHexString("0x633688abc3cCf8B0C03088D2d1C6ae4958c2fA56"), - List.of(1L, 2L), - SIGNATURE_ALGORITHM - .get() - .createSignature( - new BigInteger( - "401b5d4ebe88306448115d1a46a30e5ad1136f2818b4ebb0733d9c4efffd135a", 16), - new BigInteger( - "753ff1dbce6db504ecb9635a64d8c4506ff887e2d2a0d2b7175baf94c849eccc", 16), - (byte) 1)); - - SetCodeTransactionEncoder.encodeSingleSetCode(authorization, output); - - assertThat(output.encoded()) - .isEqualTo( - Bytes.fromHexString( - "0xf85c0194633688abc3ccf8b0c03088d2d1c6ae4958c2fa56c2010201a0401b5d4ebe88306448115d1a46a30e5ad1136f2818b4ebb0733d9c4efffd135aa0753ff1dbce6db504ecb9635a64d8c4506ff887e2d2a0d2b7175baf94c849eccc")); - } - @Test void shouldEncodeSingleSetCodeWithoutNonceAndChainIdZero() { // "d70094633688abc3ccf8b0c03088d2d1c6ae4958c2fa56c5" @@ -129,7 +102,7 @@ void shouldEncodeSingleSetCodeWithoutNonceAndChainIdZero() { new SetCodeAuthorization( BigInteger.ZERO, Address.fromHexString("0x633688abc3cCf8B0C03088D2d1C6ae4958c2fA56"), - new ArrayList<>(), + Optional.empty(), SIGNATURE_ALGORITHM .get() .createSignature(