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));
}
Aggregations