Search in sources :

Example 1 with CompressedBranchInfo

use of tech.pegasys.teku.infrastructure.ssz.tree.TreeNodeSource.CompressedBranchInfo 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)

Example 2 with CompressedBranchInfo

use of tech.pegasys.teku.infrastructure.ssz.tree.TreeNodeSource.CompressedBranchInfo in project teku by ConsenSys.

the class KvStoreTreeNodeStore method storeBranchNode.

@Override
public void storeBranchNode(final Bytes32 root, final long gIndex, final int depth, final Bytes32[] children) {
    if (knownStoredBranchesCache.contains(root) || !newlyStoredBranches.add(root)) {
        return;
    }
    storedBranchNodes++;
    transaction.put(schema.getColumnFinalizedStateMerkleTreeBranches(), root, new CompressedBranchInfo(depth, children));
}
Also used : CompressedBranchInfo(tech.pegasys.teku.infrastructure.ssz.tree.TreeNodeSource.CompressedBranchInfo)

Example 3 with CompressedBranchInfo

use of tech.pegasys.teku.infrastructure.ssz.tree.TreeNodeSource.CompressedBranchInfo in project teku by ConsenSys.

the class CompressedBranchInfoSerializerTest method assertRoundTrip.

private void assertRoundTrip(final int depth, final Bytes32... children) {
    final CompressedBranchInfo input = new CompressedBranchInfo(depth, children);
    final byte[] serialized = serializer.serialize(input);
    final CompressedBranchInfo output = serializer.deserialize(serialized);
    assertThat(output).isEqualToComparingFieldByField(input);
}
Also used : CompressedBranchInfo(tech.pegasys.teku.infrastructure.ssz.tree.TreeNodeSource.CompressedBranchInfo)

Example 4 with CompressedBranchInfo

use of tech.pegasys.teku.infrastructure.ssz.tree.TreeNodeSource.CompressedBranchInfo in project teku by ConsenSys.

the class KvStoreTreeNodeSourceTest method storeBranch.

private CompressedBranchInfo storeBranch(final Bytes32 root, final int depth, final Bytes32... children) {
    try (final KvStoreTransaction transaction = accessor.startTransaction()) {
        final CompressedBranchInfo value = new CompressedBranchInfo(depth, children);
        transaction.put(schema.getColumnFinalizedStateMerkleTreeBranches(), root, value);
        transaction.commit();
        return value;
    }
}
Also used : KvStoreTransaction(tech.pegasys.teku.storage.server.kvstore.KvStoreAccessor.KvStoreTransaction) CompressedBranchInfo(tech.pegasys.teku.infrastructure.ssz.tree.TreeNodeSource.CompressedBranchInfo)

Example 5 with CompressedBranchInfo

use of tech.pegasys.teku.infrastructure.ssz.tree.TreeNodeSource.CompressedBranchInfo in project teku by ConsenSys.

the class KvStoreTreeNodeStoreTest method storeBranchNode_shouldNotStoreBranchWhenAlreadyStored.

@Test
void storeBranchNode_shouldNotStoreBranchWhenAlreadyStored() {
    final Bytes32 root = dataStructureUtil.randomBytes32();
    final int depth = 2;
    final Bytes32[] children = { dataStructureUtil.randomBytes32(), dataStructureUtil.randomBytes32() };
    store.storeBranchNode(root, 5, depth, children);
    store.storeBranchNode(root, 5, depth, children);
    verify(transaction, times(1)).put(schema.getColumnFinalizedStateMerkleTreeBranches(), root, new CompressedBranchInfo(depth, children));
    assertThat(store.getStoredBranchNodeCount()).isEqualTo(1);
}
Also used : CompressedBranchInfo(tech.pegasys.teku.infrastructure.ssz.tree.TreeNodeSource.CompressedBranchInfo) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Aggregations

CompressedBranchInfo (tech.pegasys.teku.infrastructure.ssz.tree.TreeNodeSource.CompressedBranchInfo)10 Bytes32 (org.apache.tuweni.bytes.Bytes32)7 Test (org.junit.jupiter.api.Test)3 TreeNode (tech.pegasys.teku.infrastructure.ssz.tree.TreeNode)3 Bytes (org.apache.tuweni.bytes.Bytes)2 ArrayList (java.util.ArrayList)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 TreeUtil.bitsCeilToBytes (tech.pegasys.teku.infrastructure.ssz.tree.TreeUtil.bitsCeilToBytes)1 KvStoreTransaction (tech.pegasys.teku.storage.server.kvstore.KvStoreAccessor.KvStoreTransaction)1