Search in sources :

Example 1 with SszSuperNodeHint

use of tech.pegasys.teku.infrastructure.ssz.schema.SszSchemaHints.SszSuperNodeHint in project teku by ConsenSys.

the class AbstractSszVectorSchema method createDefaultTree.

@Override
protected TreeNode createDefaultTree() {
    if (isListBacking) {
        Optional<SszSuperNodeHint> sszSuperNodeHint = getHints().getHint(SszSuperNodeHint.class);
        if (sszSuperNodeHint.isPresent()) {
            int superNodeDepth = sszSuperNodeHint.get().getDepth();
            SszSuperNode defaultSuperSszNode = new SszSuperNode(superNodeDepth, elementSszSupernodeTemplate.get(), Bytes.EMPTY);
            int binaryDepth = treeDepth() - superNodeDepth;
            return TreeUtil.createTree(emptyList(), defaultSuperSszNode, binaryDepth);
        } else {
            return TreeUtil.createDefaultTree(maxChunks(), LeafNode.EMPTY_LEAF);
        }
    } else if (getElementsPerChunk() == 1) {
        return TreeUtil.createDefaultTree(maxChunks(), getElementSchema().getDefaultTree());
    } else {
        // packed vector
        int fullZeroNodesCount = getLength() / getElementsPerChunk();
        int lastNodeElementCount = getLength() % getElementsPerChunk();
        int lastNodeSizeBytes = bitsCeilToBytes(lastNodeElementCount * getSszElementBitSize());
        Stream<TreeNode> fullZeroNodes = Stream.<TreeNode>generate(() -> LeafNode.ZERO_LEAVES[32]).limit(fullZeroNodesCount);
        Stream<TreeNode> lastZeroNode = lastNodeSizeBytes > 0 ? Stream.of(LeafNode.ZERO_LEAVES[lastNodeSizeBytes]) : Stream.empty();
        return TreeUtil.createTree(Stream.concat(fullZeroNodes, lastZeroNode).collect(Collectors.toList()));
    }
}
Also used : TreeNode(tech.pegasys.teku.infrastructure.ssz.tree.TreeNode) SszSuperNodeHint(tech.pegasys.teku.infrastructure.ssz.schema.SszSchemaHints.SszSuperNodeHint) Stream(java.util.stream.Stream) SszSuperNodeHint(tech.pegasys.teku.infrastructure.ssz.schema.SszSchemaHints.SszSuperNodeHint) SszSuperNode(tech.pegasys.teku.infrastructure.ssz.tree.SszSuperNode)

Aggregations

Stream (java.util.stream.Stream)1 SszSuperNodeHint (tech.pegasys.teku.infrastructure.ssz.schema.SszSchemaHints.SszSuperNodeHint)1 SszSuperNode (tech.pegasys.teku.infrastructure.ssz.tree.SszSuperNode)1 TreeNode (tech.pegasys.teku.infrastructure.ssz.tree.TreeNode)1