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

Calculate min and max ssz lengths #2358

Merged
merged 8 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
@@ -0,0 +1,157 @@
/*
* Copyright 2020 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 tech.pegasys.teku.datastructures.util;

import static tech.pegasys.teku.datastructures.util.SimpleOffsetSerializer.BOOLEAN_SIZE;
import static tech.pegasys.teku.datastructures.util.SimpleOffsetSerializer.UNSIGNED_LONG_SIZE;
import static tech.pegasys.teku.datastructures.util.SimpleOffsetSerializer.getOptionalReflectionInfo;
import static tech.pegasys.teku.datastructures.util.SimpleOffsetSerializer.getRequiredReflectionInfo;
import static tech.pegasys.teku.datastructures.util.SimpleOffsetSerializer.isBitvector;
import static tech.pegasys.teku.datastructures.util.SimpleOffsetSerializer.isPrimitive;
import static tech.pegasys.teku.datastructures.util.SimpleOffsetSerializer.isVariable;
import static tech.pegasys.teku.datastructures.util.SimpleOffsetSerializer.isVector;
import static tech.pegasys.teku.util.config.Constants.BYTES_PER_LENGTH_OFFSET;

import java.lang.reflect.Field;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.bls.BLSPublicKey;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.ssz.SSZTypes.Bitlist;
import tech.pegasys.teku.ssz.SSZTypes.Bitvector;
import tech.pegasys.teku.ssz.SSZTypes.Bytes4;
import tech.pegasys.teku.ssz.SSZTypes.SSZList;
import tech.pegasys.teku.ssz.sos.ReflectionInformation;

public class LengthBoundCalculator {

static <T> LengthBounds calculateLengthBounds(final Class<T> type) {
final ReflectionInformation reflectionInfo = getRequiredReflectionInfo(type);
LengthBounds lengthBounds = LengthBounds.ZERO;
int variableFieldCount = 0;
int vectorCount = 0;
int bitvectorCount = 0;
for (Field field : reflectionInfo.getFields()) {
final Class<?> fieldType = field.getType();
final LengthBounds fieldLengthBounds;
if (getOptionalReflectionInfo(fieldType).isPresent()) {
fieldLengthBounds = calculateLengthBounds(fieldType);

} else if (fieldType == Bitlist.class) {
fieldLengthBounds = calculateBitlistLength(reflectionInfo, variableFieldCount);

} else if (fieldType == SSZList.class) {
fieldLengthBounds = calculateSszListLength(reflectionInfo, variableFieldCount);

} else if (isVector(fieldType)) {
fieldLengthBounds = calculateSszVectorLength(reflectionInfo, vectorCount);
vectorCount++;

} else if (isBitvector(fieldType)) {
fieldLengthBounds = calculateBitvectorLength(reflectionInfo, bitvectorCount);
bitvectorCount++;

} else if (isPrimitive(fieldType)) {
fieldLengthBounds = new LengthBounds(getPrimitiveLength(fieldType));

} else {
throw new IllegalArgumentException(
"Don't know how to calculate length for " + fieldType.getSimpleName());
}

if (isVariable(fieldType)) {
variableFieldCount++;
// The fixed parts includes an offset in place of the variable length value
lengthBounds = lengthBounds.add(new LengthBounds(BYTES_PER_LENGTH_OFFSET.longValue()));
}
lengthBounds = lengthBounds.add(fieldLengthBounds);
}
return lengthBounds;
}

private static LengthBounds calculateBitvectorLength(
final ReflectionInformation reflectionInfo, final int bitvectorCount) {
final LengthBounds fieldLengthBounds;
final Integer size = reflectionInfo.getBitvectorSizes().get(bitvectorCount);
final int serializationLength = Bitvector.sszSerializationLength(size);
fieldLengthBounds = new LengthBounds(serializationLength, serializationLength);
return fieldLengthBounds;
}

private static LengthBounds calculateSszVectorLength(
final ReflectionInformation reflectionInfo, final int vectorCount) {
final LengthBounds fieldLengthBounds;
final Class<?> elementType = reflectionInfo.getVectorElementTypes().get(vectorCount);
final int vectorLength = reflectionInfo.getVectorLengths().get(vectorCount);
final LengthBounds elementLengthBounds = getElementLengthBounds(elementType);
fieldLengthBounds =
new LengthBounds(
vectorLength * elementLengthBounds.getMin(),
vectorLength * elementLengthBounds.getMax());
return fieldLengthBounds;
}

private static LengthBounds calculateSszListLength(
final ReflectionInformation reflectionInfo, final int variableFieldCount) {
final LengthBounds fieldLengthBounds;
final Class<?> listElementType = reflectionInfo.getListElementTypes().get(variableFieldCount);
final long listElementMaxSize = reflectionInfo.getListElementMaxSizes().get(variableFieldCount);
Comment on lines +109 to +110
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same question as for calculateBitlistLength

final LengthBounds elementLengthBounds = getElementLengthBounds(listElementType);
final long variableFieldOffsetsLength =
isVariable(listElementType) ? BYTES_PER_LENGTH_OFFSET.intValue() * listElementMaxSize : 0;
fieldLengthBounds =
new LengthBounds(
0, elementLengthBounds.getMax() * listElementMaxSize + variableFieldOffsetsLength);
return fieldLengthBounds;
}

private static LengthBounds calculateBitlistLength(
final ReflectionInformation reflectionInfo, final int variableFieldCount) {
final LengthBounds fieldLengthBounds;
final long maxSize = reflectionInfo.getBitlistElementMaxSizes().get(variableFieldCount);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure here: getBitlistElementMaxSizes() seems to return only Bitlist instance sizes in the referred class. Why is it indexed by variableFieldCount which is a total counter of lists and bitfields?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is highly suspicious but exactly matches what SimpleOffsetSerializer does. It also works for every type we know how to SSZ serialise, but I strongly suspect that's a coincidence and there just aren't any objects that have both SSZList and Bitlist.

Created a test class with both and sure enough it fails with IndexOutOfBoundsException. So have fixed both LengthBoundsCalculator and SimpleOffsetSerializer.

fieldLengthBounds =
new LengthBounds(
Bitlist.sszSerializationLength(Math.toIntExact(0)),
Bitlist.sszSerializationLength(Math.toIntExact(maxSize)));
return fieldLengthBounds;
}

private static LengthBounds getElementLengthBounds(final Class<?> listElementType) {
if (isPrimitive(listElementType)) {
final int primitiveLength = getPrimitiveLength(listElementType);
return new LengthBounds(primitiveLength, primitiveLength);
}
return calculateLengthBounds(listElementType);
}

private static int getPrimitiveLength(final Class<?> classInfo) {
switch (classInfo.getSimpleName()) {
case "UnsignedLong":
return UNSIGNED_LONG_SIZE;
case "ArrayWrappingBytes32":
case "Bytes32":
return Bytes32.SIZE;
case "Bytes4":
return Bytes4.SIZE;
case "BLSSignature":
return BLSSignature.BLS_SIGNATURE_SIZE;
case "BLSPublicKey":
return BLSPublicKey.BLS_PUBKEY_SIZE;
case "Boolean":
case "boolean":
return BOOLEAN_SIZE;
default:
throw new IllegalArgumentException("Unable to deserialize " + classInfo.getSimpleName());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be change exception wording?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2020 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 tech.pegasys.teku.datastructures.util;

import com.google.common.base.MoreObjects;
import java.util.Objects;

public class LengthBounds {
public static final LengthBounds ZERO = new LengthBounds(0, 0);
private final long min;
private final long max;

public LengthBounds(final long fixedSize) {
this(fixedSize, fixedSize);
}

public LengthBounds(final long min, final long max) {
this.min = min;
this.max = max;
}

public long getMin() {
return min;
}

public long getMax() {
return max;
}

public LengthBounds add(final LengthBounds other) {
return new LengthBounds(this.min + other.min, this.max + other.max);
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final LengthBounds that = (LengthBounds) o;
return getMin() == that.getMin() && getMax() == that.getMax();
}

@Override
public int hashCode() {
return Objects.hash(getMin(), getMax());
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("min", min).add("max", max).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package tech.pegasys.teku.datastructures.util;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static tech.pegasys.teku.util.config.Constants.BYTES_PER_LENGTH_OFFSET;

import com.google.common.primitives.UnsignedLong;
Expand Down Expand Up @@ -79,7 +80,10 @@
@SuppressWarnings({"rawtypes", "unchecked"})
public class SimpleOffsetSerializer {

static final int UNSIGNED_LONG_SIZE = 8;
static final int BOOLEAN_SIZE = 1;
public static HashMap<Class, ReflectionInformation> classReflectionInfo = new HashMap<>();
public static HashMap<Class, LengthBounds> classLengthBounds = new HashMap<>();

public static void setConstants() {
List<Class> classes =
Expand Down Expand Up @@ -121,6 +125,10 @@ public static void setConstants() {
for (Class classItem : classes) {
classReflectionInfo.put(classItem, new ReflectionInformation(classItem));
}

for (Class classItem : classes) {
classLengthBounds.put(classItem, LengthBoundCalculator.calculateLengthBounds(classItem));
}
}

static {
Expand Down Expand Up @@ -220,6 +228,10 @@ public static <T> T deserialize(Bytes bytes, Class<T> classInfo) {
}
}

public static <T> LengthBounds getLengthBounds(final Class<T> type) {
return checkNotNull(classLengthBounds.get(type), "Length bounds unknown for type %s", type);
}

private static void assertAllDataRead(SSZReader reader) {
if (!reader.isComplete()) {
throw new IllegalStateException("Unread data detected.");
Expand Down Expand Up @@ -462,40 +474,40 @@ private static SSZVector deserializeFixedElementVector(
return SSZVector.createMutable(newList, classInfo);
}

private static ReflectionInformation getRequiredReflectionInfo(Class classInfo) {
static ReflectionInformation getRequiredReflectionInfo(Class classInfo) {
final ReflectionInformation reflectionInfo = classReflectionInfo.get(classInfo);
checkArgument(
reflectionInfo != null,
"Unable to find reflection information for class " + classInfo.getSimpleName());
return reflectionInfo;
}

private static Optional<ReflectionInformation> getOptionalReflectionInfo(Class classInfo) {
static Optional<ReflectionInformation> getOptionalReflectionInfo(Class classInfo) {
return Optional.ofNullable(classReflectionInfo.get(classInfo));
}

private static Object deserializePrimitive(
Class classInfo, SSZReader reader, MutableInt bytePointer) {
switch (classInfo.getSimpleName()) {
case "UnsignedLong":
bytePointer.add(8);
bytePointer.add(UNSIGNED_LONG_SIZE);
return UnsignedLong.fromLongBits(reader.readUInt64());
case "ArrayWrappingBytes32":
case "Bytes32":
bytePointer.add(32);
return Bytes32.wrap(reader.readFixedBytes(32));
bytePointer.add(Bytes32.SIZE);
return Bytes32.wrap(reader.readFixedBytes(Bytes32.SIZE));
case "Bytes4":
bytePointer.add(4);
return new Bytes4(reader.readFixedBytes(4));
bytePointer.add(Bytes4.SIZE);
return new Bytes4(reader.readFixedBytes(Bytes4.SIZE));
case "BLSSignature":
bytePointer.add(96);
return BLSSignature.fromBytes(reader.readFixedBytes(96));
bytePointer.add(BLSSignature.BLS_SIGNATURE_SIZE);
return BLSSignature.fromBytes(reader.readFixedBytes(BLSSignature.BLS_SIGNATURE_SIZE));
case "BLSPublicKey":
bytePointer.add(48);
return BLSPublicKey.fromBytes(reader.readFixedBytes(48));
bytePointer.add(BLSPublicKey.BLS_PUBKEY_SIZE);
return BLSPublicKey.fromBytes(reader.readFixedBytes(BLSPublicKey.BLS_PUBKEY_SIZE));
case "Boolean":
case "boolean":
bytePointer.add(1);
bytePointer.add(BOOLEAN_SIZE);
return reader.readBoolean();
default:
throw new IllegalArgumentException("Unable to deserialize " + classInfo.getSimpleName());
Expand All @@ -507,7 +519,7 @@ private static int readOffset(SSZReader reader, MutableInt bytesPointer) {
return reader.readInt32();
}

private static boolean isVariable(Class classInfo) {
static boolean isVariable(Class classInfo) {
if (classInfo == SSZList.class || classInfo == Bitlist.class) {
return true;
} else {
Expand All @@ -517,18 +529,18 @@ private static boolean isVariable(Class classInfo) {
}
}

private static boolean isPrimitive(Class classInfo) {
static boolean isPrimitive(Class classInfo) {
return !(SSZContainer.class.isAssignableFrom(classInfo)
|| classInfo == SSZVector.class
|| classInfo == Bitvector.class
|| classInfo == VoteTracker.class);
}

private static boolean isVector(Class classInfo) {
static boolean isVector(Class classInfo) {
return classInfo == SSZVector.class;
}

private static boolean isBitvector(Class classInfo) {
static boolean isBitvector(Class classInfo) {
return classInfo == Bitvector.class;
}

Expand Down
Loading