mirror of https://github.com/hyperledger/besu
Merge branch 'refs/heads/7311-add-cli-feature-toggle-for-peertask-system' into 7311-add-GetReceiptsFromPeerTask
commit
76724ed3b1
@ -0,0 +1,59 @@ |
|||||||
|
/* |
||||||
|
* 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.crypto; |
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull; |
||||||
|
|
||||||
|
import java.math.BigInteger; |
||||||
|
|
||||||
|
/** Secp signature with code delegation. */ |
||||||
|
public class CodeDelegationSignature extends SECPSignature { |
||||||
|
private static final BigInteger TWO_POW_256 = BigInteger.TWO.pow(256); |
||||||
|
|
||||||
|
/** |
||||||
|
* Instantiates a new SECPSignature. |
||||||
|
* |
||||||
|
* @param r the r part of the signature |
||||||
|
* @param s the s part of the signature |
||||||
|
* @param yParity the parity of the y coordinate of the public key |
||||||
|
*/ |
||||||
|
public CodeDelegationSignature(final BigInteger r, final BigInteger s, final byte yParity) { |
||||||
|
super(r, s, yParity); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new CodeDelegationSignature. |
||||||
|
* |
||||||
|
* @param r the r part of the signature |
||||||
|
* @param s the s part of the signature |
||||||
|
* @param yParity the parity of the y coordinate of the public key |
||||||
|
* @return the new CodeDelegationSignature |
||||||
|
*/ |
||||||
|
public static CodeDelegationSignature create( |
||||||
|
final BigInteger r, final BigInteger s, final long yParity) { |
||||||
|
checkNotNull(r); |
||||||
|
checkNotNull(s); |
||||||
|
|
||||||
|
if (r.compareTo(TWO_POW_256) >= 0) { |
||||||
|
throw new IllegalArgumentException("Invalid 'r' value, should be < 2^256 but got " + r); |
||||||
|
} |
||||||
|
|
||||||
|
if (s.compareTo(TWO_POW_256) >= 0) { |
||||||
|
throw new IllegalArgumentException("Invalid 's' value, should be < 2^256 but got " + s); |
||||||
|
} |
||||||
|
|
||||||
|
return new CodeDelegationSignature(r, s, (byte) yParity); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,93 @@ |
|||||||
|
/* |
||||||
|
* 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.crypto; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
||||||
|
|
||||||
|
import java.math.BigInteger; |
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
class CodeDelegationSignatureTest { |
||||||
|
|
||||||
|
private static final BigInteger TWO_POW_256 = BigInteger.valueOf(2).pow(256); |
||||||
|
|
||||||
|
@Test |
||||||
|
void testValidInputs() { |
||||||
|
BigInteger r = BigInteger.ONE; |
||||||
|
BigInteger s = BigInteger.TEN; |
||||||
|
long yParity = 1L; |
||||||
|
|
||||||
|
CodeDelegationSignature result = CodeDelegationSignature.create(r, s, yParity); |
||||||
|
|
||||||
|
assertThat(r).isEqualTo(result.getR()); |
||||||
|
assertThat(s).isEqualTo(result.getS()); |
||||||
|
assertThat((byte) yParity).isEqualTo(result.getRecId()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void testNullRValue() { |
||||||
|
BigInteger s = BigInteger.TEN; |
||||||
|
long yParity = 0L; |
||||||
|
|
||||||
|
assertThatExceptionOfType(NullPointerException.class) |
||||||
|
.isThrownBy(() -> CodeDelegationSignature.create(null, s, yParity)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void testNullSValue() { |
||||||
|
BigInteger r = BigInteger.ONE; |
||||||
|
long yParity = 0L; |
||||||
|
|
||||||
|
assertThatExceptionOfType(NullPointerException.class) |
||||||
|
.isThrownBy(() -> CodeDelegationSignature.create(r, null, yParity)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void testRValueExceedsTwoPow256() { |
||||||
|
BigInteger r = TWO_POW_256; |
||||||
|
BigInteger s = BigInteger.TEN; |
||||||
|
long yParity = 0L; |
||||||
|
|
||||||
|
assertThatExceptionOfType(IllegalArgumentException.class) |
||||||
|
.isThrownBy(() -> CodeDelegationSignature.create(r, s, yParity)) |
||||||
|
.withMessageContainingAll("Invalid 'r' value, should be < 2^256"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void testSValueExceedsTwoPow256() { |
||||||
|
BigInteger r = BigInteger.ONE; |
||||||
|
BigInteger s = TWO_POW_256; |
||||||
|
long yParity = 0L; |
||||||
|
|
||||||
|
assertThatExceptionOfType(IllegalArgumentException.class) |
||||||
|
.isThrownBy(() -> CodeDelegationSignature.create(r, s, yParity)) |
||||||
|
.withMessageContainingAll("Invalid 's' value, should be < 2^256"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void testValidYParityZero() { |
||||||
|
BigInteger r = BigInteger.ONE; |
||||||
|
BigInteger s = BigInteger.TEN; |
||||||
|
long yParity = 0L; |
||||||
|
|
||||||
|
CodeDelegationSignature result = CodeDelegationSignature.create(r, s, yParity); |
||||||
|
|
||||||
|
assertThat(r).isEqualTo(result.getR()); |
||||||
|
assertThat(s).isEqualTo(result.getS()); |
||||||
|
assertThat((byte) yParity).isEqualTo(result.getRecId()); |
||||||
|
} |
||||||
|
} |
@ -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.eth.manager.peertask; |
|
||||||
|
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
/** |
|
||||||
* Temporary class to allow easy access to the PeerTask feature toggle. This class can be removed |
|
||||||
* once we've properly tested the PeerTask system and want to enable it permanently |
|
||||||
*/ |
|
||||||
public class PeerTaskFeatureToggle { |
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(PeerTaskFeatureToggle.class); |
|
||||||
private static Boolean USE_PEER_TASK_SYSTEM = null; |
|
||||||
|
|
||||||
/** |
|
||||||
* Initialize the PeerTaskFeatureToggle with the supplied usePeerTaskSystem Boolean. |
|
||||||
* PeerTaskFeatureToggle may only be initialized once! |
|
||||||
* |
|
||||||
* @param usePeerTaskSystem Boolean indicating whether or not the PeerTask system should be used |
|
||||||
*/ |
|
||||||
public static void initialize(final Boolean usePeerTaskSystem) { |
|
||||||
if (USE_PEER_TASK_SYSTEM != null) { |
|
||||||
LOGGER.warn( |
|
||||||
"PeerTaskFeatureToggle has already been initialized, and cannot be initialized again"); |
|
||||||
} else { |
|
||||||
USE_PEER_TASK_SYSTEM = usePeerTaskSystem; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Indicates whether or not to use the PeerTask system. PeerTaskFeatureToggle must have been |
|
||||||
* initialized before this method can be called! |
|
||||||
* |
|
||||||
* @return whether or not to use the PeerTask system |
|
||||||
* @throws IllegalStateException if the PeerTaskFeatureToggle has not been initialized |
|
||||||
*/ |
|
||||||
public static Boolean usePeerTaskSystem() { |
|
||||||
if (USE_PEER_TASK_SYSTEM == null) { |
|
||||||
throw new IllegalStateException( |
|
||||||
"PeerTaskFeatureToggle has not been initialized, but getUsePeerTaskSystem() has been called"); |
|
||||||
} |
|
||||||
return USE_PEER_TASK_SYSTEM; |
|
||||||
} |
|
||||||
|
|
||||||
private PeerTaskFeatureToggle() {} |
|
||||||
} |
|
Loading…
Reference in new issue