Search in sources :

Example 1 with ChildLoader

use of tech.pegasys.teku.infrastructure.ssz.schema.impl.LoadingUtil.ChildLoader in project teku by ConsenSys.

the class AbstractSszListSchema method loadBackingNodes.

@Override
public TreeNode loadBackingNodes(final TreeNodeSource nodeSource, final Bytes32 rootHash, final long rootGIndex) {
    if (TreeUtil.ZERO_TREES_BY_ROOT.containsKey(rootHash) || rootHash.equals(Bytes32.ZERO)) {
        return getDefaultTree();
    }
    final CompressedBranchInfo branchData = nodeSource.loadBranchNode(rootHash, rootGIndex);
    checkState(branchData.getChildren().length == 2, "List root node must have exactly two children");
    checkState(branchData.getDepth() == 1, "List root node must have depth of 1");
    final Bytes32 vectorHash = branchData.getChildren()[0];
    final Bytes32 lengthHash = branchData.getChildren()[1];
    final int length = nodeSource.loadLeafNode(lengthHash, GIndexUtil.gIdxRightGIndex(rootGIndex)).getInt(0, ByteOrder.LITTLE_ENDIAN);
    final int superNodeDepth = getSuperNodeDepth();
    final ChildLoader childLoader = superNodeDepth == 0 ? (childNodeSource, childHash, childGIndex) -> LoadingUtil.loadCollectionChild(childNodeSource, childHash, childGIndex, length, compatibleVectorSchema.getElementsPerChunk(), compatibleVectorSchema.treeDepth(), compatibleVectorSchema.getElementSchema()) : (childNodeSource, childHash, childGIndex) -> {
        final Bytes data;
        if (TreeUtil.ZERO_TREES_BY_ROOT.containsKey(childHash)) {
            data = Bytes.EMPTY;
        } else {
            data = nodeSource.loadLeafNode(childHash, childGIndex);
        }
        return new SszSuperNode(superNodeDepth, elementSszSupernodeTemplate.get(), data);
    };
    final long vectorRootGIndex = GIndexUtil.gIdxLeftGIndex(rootGIndex);
    final long lastUsefulGIndex = getVectorLastUsefulGIndex(vectorRootGIndex, length, superNodeDepth);
    final TreeNode vectorNode = LoadingUtil.loadNodesToDepth(nodeSource, vectorHash, vectorRootGIndex, compatibleVectorSchema.treeDepth() - superNodeDepth, compatibleVectorSchema.getDefault().getBackingNode(), lastUsefulGIndex, childLoader);
    return BranchNode.create(vectorNode, toLengthNode(length));
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) TreeUtil.bitsCeilToBytes(tech.pegasys.teku.infrastructure.ssz.tree.TreeUtil.bitsCeilToBytes) ChildLoader(tech.pegasys.teku.infrastructure.ssz.schema.impl.LoadingUtil.ChildLoader) TreeNode(tech.pegasys.teku.infrastructure.ssz.tree.TreeNode) CompressedBranchInfo(tech.pegasys.teku.infrastructure.ssz.tree.TreeNodeSource.CompressedBranchInfo) Bytes32(org.apache.tuweni.bytes.Bytes32) SszSuperNodeHint(tech.pegasys.teku.infrastructure.ssz.schema.SszSchemaHints.SszSuperNodeHint) SszSuperNode(tech.pegasys.teku.infrastructure.ssz.tree.SszSuperNode)

Aggregations

Bytes (org.apache.tuweni.bytes.Bytes)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 SszSuperNodeHint (tech.pegasys.teku.infrastructure.ssz.schema.SszSchemaHints.SszSuperNodeHint)1 ChildLoader (tech.pegasys.teku.infrastructure.ssz.schema.impl.LoadingUtil.ChildLoader)1 SszSuperNode (tech.pegasys.teku.infrastructure.ssz.tree.SszSuperNode)1 TreeNode (tech.pegasys.teku.infrastructure.ssz.tree.TreeNode)1 CompressedBranchInfo (tech.pegasys.teku.infrastructure.ssz.tree.TreeNodeSource.CompressedBranchInfo)1 TreeUtil.bitsCeilToBytes (tech.pegasys.teku.infrastructure.ssz.tree.TreeUtil.bitsCeilToBytes)1