use of tech.pegasys.teku.infrastructure.ssz.SszData in project teku by ConsenSys.
the class SszSchemaTestBase method assertTreeRoundtrip.
protected void assertTreeRoundtrip(final SszSchema<?> schema, final int maxBranchLevelsSkipped, final SszData data) {
final InMemoryStoringTreeNodeStore nodeStore = new InMemoryStoringTreeNodeStore();
final TreeNode node = data.getBackingNode();
final long rootGIndex = 34;
schema.storeBackingNodes(nodeStore, maxBranchLevelsSkipped, rootGIndex, node);
final TreeNode result = schema.loadBackingNodes(nodeStore, data.hashTreeRoot(), rootGIndex);
assertThatTreeNode(result).isTreeEqual(node);
final SszData rebuiltData = schema.createFromBackingNode(result);
assertThat(rebuiltData).isEqualTo(data);
}
use of tech.pegasys.teku.infrastructure.ssz.SszData in project teku by ConsenSys.
the class SszSchemaTestBase method loadBackingNodes_shouldRestoreDefaultTree.
@MethodSource("testSchemaArguments")
@ParameterizedTest
void loadBackingNodes_shouldRestoreDefaultTree(SszSchema<?> schema) {
final InMemoryStoringTreeNodeStore nodeStore = new InMemoryStoringTreeNodeStore();
final TreeNode node = schema.getDefault().getBackingNode();
final long rootGIndex = 67;
schema.storeBackingNodes(nodeStore, 100, rootGIndex, node);
// LeafNode should be equal
final TreeNode result = schema.loadBackingNodes(nodeStore, node.hashTreeRoot(), rootGIndex);
assertThatTreeNode(result).isTreeEqual(node);
// And should be equal when accessing the actual value
final SszData rebuiltData = schema.createFromBackingNode(result);
assertThat(rebuiltData).isEqualTo(schema.createFromBackingNode(node));
}
use of tech.pegasys.teku.infrastructure.ssz.SszData in project teku by ConsenSys.
the class AbstractSszGenericTestExecutor method runTest.
@Override
public void runTest(final TestDefinition testDefinition) throws Throwable {
final Bytes inputData = TestDataUtils.readSszData(testDefinition, "serialized.ssz_snappy");
final Path path = testDefinition.getTestDirectory().resolve("value.yaml");
if (path.toFile().exists()) {
final SszSchema<?> schema = getSchema(testDefinition);
final Meta meta = TestDataUtils.loadYaml(testDefinition, "meta.yaml", Meta.class);
final SszData result = schema.sszDeserialize(inputData);
assertThat(result.hashTreeRoot()).isEqualTo(Bytes32.fromHexString(meta.root));
assertValueCorrect(testDefinition, result);
} else {
try {
final SszSchema<?> schema = getSchema(testDefinition);
assertThatThrownBy(() -> schema.sszDeserialize(inputData)).isInstanceOf(IllegalArgumentException.class);
} catch (final IllegalArgumentException e) {
// The schema itself was invalid so this counts as success
// Can't just put getSchema in the assertThatThrownBy because we need to let
// TestAbortedException actually throw so that the test is ignored if we don't support the
// type
}
}
}
use of tech.pegasys.teku.infrastructure.ssz.SszData in project teku by ConsenSys.
the class SszSchemaTestBase method sszDeserialize_tooLongSszShouldFailFastWithoutReadingWholeInput.
@MethodSource("testSchemaArguments")
@ParameterizedTest
void sszDeserialize_tooLongSszShouldFailFastWithoutReadingWholeInput(SszSchema<SszData> schema) {
long maxSszLength = schema.getSszLengthBounds().getMaxBytes();
// ignore too large and degenerative structs
assumeThat(maxSszLength).isLessThan(32 * 1024 * 1024).isGreaterThan(0);
// ignore lists using SszSuperNode as many validations are skipped
if (schema instanceof AbstractSszCollectionSchema) {
assumeThat(((AbstractSszCollectionSchema<?, ?>) schema).getHints().getHint(SszSuperNodeHint.class)).describedAs("uses SszSuperNode").isEmpty();
}
SszData data = randomSsz.randomData(schema);
Bytes ssz = data.sszSerialize();
Bytes sszWithExtraData = Bytes.wrap(ssz, Bytes.random((int) (maxSszLength - ssz.size() + 1)));
AtomicInteger bytesCounter = new AtomicInteger();
SimpleSszReader countingReader = new SimpleSszReader(sszWithExtraData) {
@Override
public Bytes read(int length) {
bytesCounter.addAndGet(length);
return super.read(length);
}
};
assertThatThrownBy(() -> schema.sszDeserialize(countingReader)).isInstanceOf(SszDeserializeException.class);
assertThat(bytesCounter.get()).isLessThanOrEqualTo(ssz.size());
}
use of tech.pegasys.teku.infrastructure.ssz.SszData in project teku by ConsenSys.
the class SszSchemaTestBase method getDefaultTree_shouldBeEqualToDefaultStructure.
@MethodSource("testSchemaArguments")
@ParameterizedTest
void getDefaultTree_shouldBeEqualToDefaultStructure(SszSchema<SszData> schema) {
SszData defaultTreeData = schema.createFromBackingNode(schema.getDefaultTree());
SszDataAssert.assertThatSszData(defaultTreeData).isEqualByAllMeansTo(schema.getDefault());
}
Aggregations