forked from Consensys/teku
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.7.1 Helper function updates (Consensys#730)
* Implement Test Suites to Run Shuffling Reference Tests from Eth2 Testing Spec (Consensys#715) * Update BLS tests to use official eth2 spec vectors. * Test shuffling with different validator set sizes from eth2-test spec reference tests. * Test shuffling with default shuffling test vectors from eth2-test spec reference tests. * Fix get shuffling * changes made to give p2pNetwork access to the data needed for RPC calls (Consensys#726) * V0.7.1 integration (#1) * Constants update (v0.7.1) (Consensys#725) * Change mainnet constants * Change minimal constants in config.toml * Update data structures (Consensys#727) * add bls_domain * get_churn_limit added * is_slashable_attestation_data added * validate_indexed_attestation added * get_domain modified * convert_to_indexed * get_attesting_indices modified * get_shuffled_index added * compute_committee added * get_crosslink_committee added * get_epoch_start_shard added * get_shard_delta added * get_epoch_committee_count added * get_block_root_at_slot added * get_attestation_data_slot added * decrease_balance added * increase_balance added * is_slashable_validator added * get_previous_epoch modified * get_active_validator_indices modified * get_randao_mix modified * get_active_index_root modified * generate_seed modified * get_beacon_proposer_index modified * removed methods * replacce assert * bug fixes * bug fixes * couture * bug fixes * best dev evar * bug fixes * oops
- Loading branch information
1 parent
8dd4b86
commit 537cc61
Showing
27 changed files
with
975 additions
and
1,124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
eth-reference-tests/src/test/java/pegasys/artemis/reference/ShufflingSetSizeTestSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package pegasys.artemis.reference; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
import com.google.common.primitives.UnsignedLong; | ||
import com.google.errorprone.annotations.MustBeClosed; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.UncheckedIOException; | ||
import java.math.BigInteger; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import org.apache.tuweni.io.Resources; | ||
import org.apache.tuweni.junit.BouncyCastleExtension; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import tech.pegasys.artemis.datastructures.state.Validator; | ||
import tech.pegasys.artemis.datastructures.util.BeaconStateUtil; | ||
import tech.pegasys.artemis.util.bls.BLSPublicKey; | ||
|
||
/* | ||
* The "official" Shuffling reference test data is from https://github.com/ethereum/eth2.0-tests/ | ||
*/ | ||
|
||
@ExtendWith(BouncyCastleExtension.class) | ||
class ShufflingSetSizeTestSuite { | ||
|
||
private static String shufflingTestVectorsFile = "**/shuffling/test_vector_shuffling.yml"; | ||
private static String shufflingDifferentSetSizeTestFile = "**/shuffling/shuffling_set_size.yml"; | ||
|
||
@ParameterizedTest(name = "{index}. Shuffling validator set. Expecting: {2}") | ||
@MethodSource("readStandardShufflingTests") | ||
void testStandardShufflingVectors( | ||
LinkedHashMap<String, Object> input, String inputSeed, List<List<BigInteger>> output) { | ||
|
||
performShufflingTests(input, inputSeed, output); | ||
} | ||
|
||
@ParameterizedTest(name = "{index}. Shuffling validator sets with varied sizes. Expecting: {2}") | ||
@MethodSource("readShufflingWithDifferentSetSizeTests") | ||
void testShufflingWithDifferentSetSizes( | ||
LinkedHashMap<String, Object> input, String inputSeed, List<List<BigInteger>> output) { | ||
|
||
performShufflingTests(input, inputSeed, output); | ||
} | ||
|
||
@SuppressWarnings({"unchecked"}) | ||
private void performShufflingTests( | ||
LinkedHashMap<String, Object> input, String inputSeed, List<List<BigInteger>> output) { | ||
UnsignedLong epoch = UnsignedLong.valueOf((BigInteger) input.get("epoch")); | ||
Bytes32 seed = Bytes32.fromHexString(inputSeed); | ||
|
||
List<LinkedHashMap<String, BigInteger>> validatorSet = | ||
(List<LinkedHashMap<String, BigInteger>>) input.get("validators"); | ||
|
||
// TODO: While order is implicitly preserved here based on how it's read in, | ||
// we need to make sure we're explicitly adding validators to the list based on | ||
// their "original_index" field in the tests. | ||
List<Validator> validators = | ||
validatorSet.stream() | ||
.map( | ||
validatorMap -> | ||
new Validator( | ||
BLSPublicKey.empty(), | ||
Bytes32.random(), | ||
UnsignedLong.valueOf(validatorMap.get("activation_epoch")), | ||
UnsignedLong.valueOf(validatorMap.get("exit_epoch")), | ||
UnsignedLong.ZERO, | ||
false, | ||
false)) | ||
.collect(Collectors.toList()); | ||
|
||
List<List<Integer>> actual = BeaconStateUtil.get_shuffling(seed, validators, epoch); | ||
|
||
// Just in case something goes haywire, use BigInteger#intValueExact to be able | ||
// to gracefully handle an integer overrun when reading in expected indices. | ||
try { | ||
// This is a nasty hack, but there's not much we can do about it. In order | ||
// for Jackson to read in the epochs from the YAML without blowing up about | ||
// them being larger than ints/longs, we have to read them in as BigIntegers. | ||
// Unfortunately, this goes for index values read in from the output as well. | ||
// This Stream converts our BigIntegers indices to ints for the equality | ||
// assertion. | ||
List<List<Integer>> expected = | ||
output.stream() | ||
.map( | ||
committees -> | ||
committees.stream() | ||
.map(index -> index.intValueExact()) | ||
.collect(Collectors.toList())) | ||
.collect(Collectors.toList()); | ||
assertEquals(expected, actual); | ||
} catch (ArithmeticException ae) { | ||
ae.printStackTrace(); | ||
fail("An error occurred while parsing the test. 'original_index' didn't fit into an int."); | ||
} | ||
} | ||
|
||
@MustBeClosed | ||
private static Stream<Arguments> readStandardShufflingTests() throws IOException { | ||
return findTests(shufflingTestVectorsFile, "test_cases"); | ||
} | ||
|
||
@MustBeClosed | ||
private static Stream<Arguments> readShufflingWithDifferentSetSizeTests() throws IOException { | ||
return findTests(shufflingDifferentSetSizeTestFile, "test_cases"); | ||
} | ||
|
||
@MustBeClosed | ||
private static Stream<Arguments> findTests(String glob, String tcase) throws IOException { | ||
return Resources.find(glob) | ||
.flatMap( | ||
url -> { | ||
try (InputStream in = url.openConnection().getInputStream()) { | ||
return prepareTests(in, tcase); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
}); | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
private static Stream<Arguments> prepareTests(InputStream in, String tcase) throws IOException { | ||
ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); | ||
Map allTests = | ||
mapper | ||
.readerFor(Map.class) | ||
.with(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS) | ||
.readValue(in); | ||
|
||
return ((List<Map>) allTests.get(tcase)) | ||
.stream() | ||
.map( | ||
testCase -> | ||
Arguments.of( | ||
testCase.get("input"), testCase.get("seed"), testCase.get("output"))); | ||
} | ||
} |
Oops, something went wrong.