Search in sources :

Example 1 with SszDeserializeException

use of tech.pegasys.teku.infrastructure.ssz.sos.SszDeserializeException in project teku by ConsenSys.

the class AbstractSszCollectionSchema method sszDeserializeVariable.

private DeserializedData sszDeserializeVariable(SszReader reader) {
    final int endOffset = reader.getAvailableBytes();
    final List<TreeNode> childNodes = new ArrayList<>();
    if (endOffset > 0) {
        int firstElementOffset = SszType.sszBytesToLength(reader.read(SSZ_LENGTH_SIZE));
        checkSsz(firstElementOffset % SSZ_LENGTH_SIZE == 0, "Invalid first element offset");
        int elementsCount = firstElementOffset / SSZ_LENGTH_SIZE;
        checkSsz(elementsCount <= getMaxLength(), "SSZ sequence length exceeds max type length");
        List<Integer> elementOffsets = new ArrayList<>(elementsCount + 1);
        elementOffsets.add(firstElementOffset);
        for (int i = 1; i < elementsCount; i++) {
            int offset = SszType.sszBytesToLength(reader.read(SSZ_LENGTH_SIZE));
            elementOffsets.add(offset);
        }
        elementOffsets.add(endOffset);
        List<Integer> elementSizes = IntStream.range(0, elementOffsets.size() - 1).map(i -> elementOffsets.get(i + 1) - elementOffsets.get(i)).boxed().collect(Collectors.toList());
        if (elementSizes.stream().anyMatch(s -> s < 0)) {
            throw new SszDeserializeException("Invalid SSZ: wrong child offsets");
        }
        for (int elementSize : elementSizes) {
            try (SszReader sszReader = reader.slice(elementSize)) {
                childNodes.add(getElementSchema().sszDeserializeTree(sszReader));
            }
        }
    }
    return new DeserializedData(TreeUtil.createTree(childNodes, treeDepth()), childNodes.size());
}
Also used : SszDeserializeException(tech.pegasys.teku.infrastructure.ssz.sos.SszDeserializeException) SszReader(tech.pegasys.teku.infrastructure.ssz.sos.SszReader) TreeNode(tech.pegasys.teku.infrastructure.ssz.tree.TreeNode) ArrayList(java.util.ArrayList) SszSuperNodeHint(tech.pegasys.teku.infrastructure.ssz.schema.SszSchemaHints.SszSuperNodeHint)

Example 2 with SszDeserializeException

use of tech.pegasys.teku.infrastructure.ssz.sos.SszDeserializeException in project teku by ConsenSys.

the class AbstractSszContainerSchema method sszDeserializeTree.

@Override
public TreeNode sszDeserializeTree(SszReader reader) {
    int endOffset = reader.getAvailableBytes();
    int childCount = getFieldsCount();
    Queue<TreeNode> fixedChildrenSubtrees = new ArrayDeque<>(childCount);
    List<Integer> variableChildrenOffsets = new ArrayList<>(childCount);
    for (int i = 0; i < childCount; i++) {
        SszSchema<?> childType = getChildSchema(i);
        if (childType.isFixedSize()) {
            try (SszReader sszReader = reader.slice(childType.getSszFixedPartSize())) {
                TreeNode childNode = childType.sszDeserializeTree(sszReader);
                fixedChildrenSubtrees.add(childNode);
            }
        } else {
            int childOffset = SszType.sszBytesToLength(reader.read(SSZ_LENGTH_SIZE));
            variableChildrenOffsets.add(childOffset);
        }
    }
    if (variableChildrenOffsets.isEmpty()) {
        if (reader.getAvailableBytes() > 0) {
            throw new SszDeserializeException("Invalid SSZ: unread bytes for fixed size container");
        }
    } else {
        if (variableChildrenOffsets.get(0) != endOffset - reader.getAvailableBytes()) {
            throw new SszDeserializeException("First variable element offset doesn't match the end of fixed part");
        }
    }
    variableChildrenOffsets.add(endOffset);
    ArrayDeque<Integer> variableChildrenSizes = new ArrayDeque<>(variableChildrenOffsets.size() - 1);
    for (int i = 0; i < variableChildrenOffsets.size() - 1; i++) {
        variableChildrenSizes.add(variableChildrenOffsets.get(i + 1) - variableChildrenOffsets.get(i));
    }
    if (variableChildrenSizes.stream().anyMatch(s -> s < 0)) {
        throw new SszDeserializeException("Invalid SSZ: wrong child offsets");
    }
    List<TreeNode> childrenSubtrees = new ArrayList<>(childCount);
    for (int i = 0; i < childCount; i++) {
        SszSchema<?> childType = getChildSchema(i);
        if (childType.isFixedSize()) {
            childrenSubtrees.add(fixedChildrenSubtrees.remove());
        } else {
            try (SszReader sszReader = reader.slice(variableChildrenSizes.remove())) {
                TreeNode childNode = childType.sszDeserializeTree(sszReader);
                childrenSubtrees.add(childNode);
            }
        }
    }
    return TreeUtil.createTree(childrenSubtrees);
}
Also used : SszDeserializeException(tech.pegasys.teku.infrastructure.ssz.sos.SszDeserializeException) SszReader(tech.pegasys.teku.infrastructure.ssz.sos.SszReader) TreeNode(tech.pegasys.teku.infrastructure.ssz.tree.TreeNode) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque)

Example 3 with SszDeserializeException

use of tech.pegasys.teku.infrastructure.ssz.sos.SszDeserializeException in project teku by ConsenSys.

the class SszUnionSchemaImpl method sszDeserializeTree.

@Override
public TreeNode sszDeserializeTree(SszReader reader) throws SszDeserializeException {
    int selector = reader.read(1).get(0) & 0xFF;
    if (selector >= getTypesCount()) {
        throw new SszDeserializeException("Invalid selector " + selector + " for Union schema: " + this);
    }
    SszSchema<?> valueSchema = childrenSchemas.get(selector);
    TreeNode valueNode = valueSchema.sszDeserializeTree(reader);
    return createUnionNode(valueNode, selector);
}
Also used : SszDeserializeException(tech.pegasys.teku.infrastructure.ssz.sos.SszDeserializeException) TreeNode(tech.pegasys.teku.infrastructure.ssz.tree.TreeNode)

Example 4 with SszDeserializeException

use of tech.pegasys.teku.infrastructure.ssz.sos.SszDeserializeException in project teku by ConsenSys.

the class SszBitlistSchemaImpl method sszDeserializeTree.

@Override
public TreeNode sszDeserializeTree(SszReader reader) {
    int availableBytes = reader.getAvailableBytes();
    // preliminary rough check
    checkSsz((availableBytes - 1) * 8 <= getMaxLength(), "SSZ sequence length exceeds max type length");
    Bytes bytes = reader.read(availableBytes);
    int length = SszBitlistImpl.sszGetLengthAndValidate(bytes);
    if (length > getMaxLength()) {
        throw new SszDeserializeException("Too long bitlist");
    }
    Bytes treeBytes = SszBitlistImpl.sszTruncateLeadingBit(bytes, length);
    try (SszReader sszReader = SszReader.fromBytes(treeBytes)) {
        DeserializedData data = sszDeserializeVector(sszReader);
        return createTree(data.getDataTree(), length);
    }
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) SszDeserializeException(tech.pegasys.teku.infrastructure.ssz.sos.SszDeserializeException) SszReader(tech.pegasys.teku.infrastructure.ssz.sos.SszReader)

Example 5 with SszDeserializeException

use of tech.pegasys.teku.infrastructure.ssz.sos.SszDeserializeException in project teku by ConsenSys.

the class AbstractSszCollectionSchema method sszDeserializeSupernode.

private DeserializedData sszDeserializeSupernode(SszReader reader, int supernodeDepth) {
    SszNodeTemplate template = elementSszSupernodeTemplate.get();
    int sszSize = reader.getAvailableBytes();
    if (sszSize % template.getSszLength() != 0) {
        throw new SszDeserializeException("Ssz length is not multiple of element length");
    }
    int elementsCount = sszSize / template.getSszLength();
    int chunkSize = (1 << supernodeDepth) * template.getSszLength();
    int bytesRemain = sszSize;
    List<SszSuperNode> sszNodes = new ArrayList<>(bytesRemain / chunkSize + 1);
    while (bytesRemain > 0) {
        int toRead = min(bytesRemain, chunkSize);
        bytesRemain -= toRead;
        Bytes bytes = reader.read(toRead);
        SszSuperNode node = new SszSuperNode(supernodeDepth, template, bytes);
        sszNodes.add(node);
    }
    TreeNode tree = TreeUtil.createTree(sszNodes, new SszSuperNode(supernodeDepth, template, Bytes.EMPTY), treeDepth() - supernodeDepth);
    return new DeserializedData(tree, elementsCount);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) SszDeserializeException(tech.pegasys.teku.infrastructure.ssz.sos.SszDeserializeException) SszNodeTemplate(tech.pegasys.teku.infrastructure.ssz.tree.SszNodeTemplate) TreeNode(tech.pegasys.teku.infrastructure.ssz.tree.TreeNode) ArrayList(java.util.ArrayList) SszSuperNodeHint(tech.pegasys.teku.infrastructure.ssz.schema.SszSchemaHints.SszSuperNodeHint) SszSuperNode(tech.pegasys.teku.infrastructure.ssz.tree.SszSuperNode)

Aggregations

SszDeserializeException (tech.pegasys.teku.infrastructure.ssz.sos.SszDeserializeException)5 TreeNode (tech.pegasys.teku.infrastructure.ssz.tree.TreeNode)4 ArrayList (java.util.ArrayList)3 SszReader (tech.pegasys.teku.infrastructure.ssz.sos.SszReader)3 Bytes (org.apache.tuweni.bytes.Bytes)2 SszSuperNodeHint (tech.pegasys.teku.infrastructure.ssz.schema.SszSchemaHints.SszSuperNodeHint)2 ArrayDeque (java.util.ArrayDeque)1 SszNodeTemplate (tech.pegasys.teku.infrastructure.ssz.tree.SszNodeTemplate)1 SszSuperNode (tech.pegasys.teku.infrastructure.ssz.tree.SszSuperNode)1