Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugs leaky abstraction #1

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.hyperledger.besu.crypto.SECPSignature;

import java.math.BigInteger;
import java.util.List;
import java.util.Optional;

/**
Expand Down Expand Up @@ -60,13 +59,6 @@ public interface SetCodeAuthorization {
*/
Optional<Long> nonce();

/**
* Return all nonces in the list
*
* @return all the nonces
*/
List<Long> nonceList();

/**
* Return the recovery id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class SetCodeAuthorization implements org.hyperledger.besu.datatypes.SetC

private final BigInteger chainId;
private final Address address;
private final List<Long> nonces;
private final Optional<Long> nonce;
private final SECPSignature signature;
private Optional<Address> authorizer = Optional.empty();
private boolean isAuthorityComputed = false;
Expand All @@ -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<Long> nonces,
final Optional<Long> nonce,
final SECPSignature signature) {
this.chainId = chainId;
this.address = address;
this.nonces = nonces;
this.nonce = nonce;
this.signature = signature;
}

Expand All @@ -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")
Expand Down Expand Up @@ -118,13 +121,7 @@ public Optional<Address> authorizer() {

@Override
public Optional<Long> nonce() {
return nonces.size() == 1 ? Optional.of(nonces.getFirst()) : Optional.empty();
}

@JsonProperty("nonce")
@Override
public List<Long> nonceList() {
return nonces;
return nonce;
}

@JsonProperty("v")
Expand Down Expand Up @@ -170,7 +167,7 @@ public static Builder builder() {
public static class Builder {
private BigInteger chainId = BigInteger.ZERO;
private Address address;
private List<Long> nonces = List.of();
private Optional<Long> nonce = Optional.empty();
private SECPSignature signature;

/** Create a new builder. */
Expand Down Expand Up @@ -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<Long> nonces) {
this.nonces = nonces;
public Builder nonces(final Optional<Long> nonce) {
this.nonce = nonce;
return this;
}

Expand All @@ -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();

Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Long> nonces = new ArrayList<>();
Optional<Long> nonce = Optional.empty();

if (!input.nextIsList()) {
throw new IllegalArgumentException("Optional nonce must be an list, but isn't");
Expand All @@ -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();

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -78,29 +78,20 @@ void shouldDecodeInnerPayloadWithoutNonce() {
}

@Test
void shouldDecodeInnerPayloadWithMultipleNonces() {
void shouldThrowInnerPayloadWithMultipleNonces() {
// "d90194633688abc3ccf8b0c03088d2d1c6ae4958c2fa56c20107"

final BytesValueRLPInput input =
new BytesValueRLPInput(
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,7 +50,7 @@ void shouldEncodeSingleSetCodeWithNonce() {
new SetCodeAuthorization(
BigInteger.ONE,
Address.fromHexString("0x633688abc3cCf8B0C03088D2d1C6ae4958c2fA56"),
List.of(0L),
Optional.of(0L),
SIGNATURE_ALGORITHM
.get()
.createSignature(
Expand All @@ -77,7 +76,7 @@ void shouldEncodeSingleSetCodeWithoutNonce() {
new SetCodeAuthorization(
BigInteger.ONE,
Address.fromHexString("0x633688abc3cCf8B0C03088D2d1C6ae4958c2fA56"),
new ArrayList<>(),
Optional.empty(),
SIGNATURE_ALGORITHM
.get()
.createSignature(
Expand All @@ -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"
Expand All @@ -129,7 +102,7 @@ void shouldEncodeSingleSetCodeWithoutNonceAndChainIdZero() {
new SetCodeAuthorization(
BigInteger.ZERO,
Address.fromHexString("0x633688abc3cCf8B0C03088D2d1C6ae4958c2fA56"),
new ArrayList<>(),
Optional.empty(),
SIGNATURE_ALGORITHM
.get()
.createSignature(
Expand Down
Loading