use of tech.pegasys.teku.infrastructure.ssz.tree.TreeNode in project teku by ConsenSys.
the class SszBasicListTest method simpleUInt64ListTest.
@Test
public void simpleUInt64ListTest() {
SszListSchema<SszUInt64, ?> listType = SszListSchema.create(SszPrimitiveSchemas.UINT64_SCHEMA, 7);
SszMutableList<SszUInt64> listView = listType.getDefault().createWritableCopy();
TreeNode n0 = listView.commitChanges().getBackingNode();
listView.append(SszUInt64.of(UInt64.valueOf(0x111)));
TreeNode n1 = listView.commitChanges().getBackingNode();
listView.append(SszUInt64.of(UInt64.valueOf(0x222)));
listView.append(SszUInt64.of(UInt64.valueOf(0x333)));
listView.append(SszUInt64.of(UInt64.valueOf(0x444)));
TreeNode n2 = listView.commitChanges().getBackingNode();
listView.append(SszUInt64.of(UInt64.valueOf(0x555)));
TreeNode n3 = listView.commitChanges().getBackingNode();
listView.append(SszUInt64.of(UInt64.valueOf(0x666)));
listView.append(SszUInt64.of(UInt64.valueOf(0x777)));
TreeNode n4 = listView.commitChanges().getBackingNode();
listView.set(0, SszUInt64.of(UInt64.valueOf(0x800)));
TreeNode n5 = listView.commitChanges().getBackingNode();
listView.set(1, SszUInt64.of(UInt64.valueOf(0x801)));
listView.set(2, SszUInt64.of(UInt64.valueOf(0x802)));
listView.set(3, SszUInt64.of(UInt64.valueOf(0x803)));
TreeNode n6 = listView.commitChanges().getBackingNode();
Assertions.assertThat(listType.createFromBackingNode(n0).size()).isEqualTo(0);
Assertions.assertThat(listType.createFromBackingNode(n1).size()).isEqualTo(1);
Assertions.assertThat(listType.createFromBackingNode(n1).get(0).longValue()).isEqualTo(0x111);
Assertions.assertThat(listType.createFromBackingNode(n2).size()).isEqualTo(4);
Assertions.assertThat(listType.createFromBackingNode(n2).get(0).longValue()).isEqualTo(0x111);
Assertions.assertThat(listType.createFromBackingNode(n2).get(1).longValue()).isEqualTo(0x222);
Assertions.assertThat(listType.createFromBackingNode(n2).get(2).longValue()).isEqualTo(0x333);
Assertions.assertThat(listType.createFromBackingNode(n2).get(3).longValue()).isEqualTo(0x444);
Assertions.assertThat(listType.createFromBackingNode(n3).size()).isEqualTo(5);
Assertions.assertThat(listType.createFromBackingNode(n3).get(0).longValue()).isEqualTo(0x111);
Assertions.assertThat(listType.createFromBackingNode(n3).get(1).longValue()).isEqualTo(0x222);
Assertions.assertThat(listType.createFromBackingNode(n3).get(2).longValue()).isEqualTo(0x333);
Assertions.assertThat(listType.createFromBackingNode(n3).get(3).longValue()).isEqualTo(0x444);
Assertions.assertThat(listType.createFromBackingNode(n3).get(4).longValue()).isEqualTo(0x555);
Assertions.assertThat(listType.createFromBackingNode(n4).size()).isEqualTo(7);
Assertions.assertThat(listType.createFromBackingNode(n4).get(5).longValue()).isEqualTo(0x666);
Assertions.assertThat(listType.createFromBackingNode(n4).get(6).longValue()).isEqualTo(0x777);
Assertions.assertThat(listType.createFromBackingNode(n5).get(0).longValue()).isEqualTo(0x800);
Assertions.assertThat(listType.createFromBackingNode(n5).get(1).longValue()).isEqualTo(0x222);
Assertions.assertThat(listType.createFromBackingNode(n6).size()).isEqualTo(7);
Assertions.assertThat(listType.createFromBackingNode(n6).get(0).longValue()).isEqualTo(0x800);
Assertions.assertThat(listType.createFromBackingNode(n6).get(1).longValue()).isEqualTo(0x801);
Assertions.assertThat(listType.createFromBackingNode(n6).get(2).longValue()).isEqualTo(0x802);
Assertions.assertThat(listType.createFromBackingNode(n6).get(3).longValue()).isEqualTo(0x803);
Assertions.assertThat(listType.createFromBackingNode(n6).get(4).longValue()).isEqualTo(0x555);
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> listType.createFromBackingNode(n3).createWritableCopy().set(7, SszUInt64.of(UInt64.valueOf(0xaaa))));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> listType.createFromBackingNode(n3).get(7));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> listType.createFromBackingNode(n3).get(8));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> listView.set(7, SszUInt64.of(UInt64.valueOf(0xaaa))));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> listView.append(SszUInt64.of(UInt64.valueOf(0xaaa))));
listView.clear();
assertThat(listView.commitChanges().hashTreeRoot()).isEqualTo(n0.hashTreeRoot());
}
use of tech.pegasys.teku.infrastructure.ssz.tree.TreeNode in project teku by ConsenSys.
the class SszCompositeListTest method simpleTest1.
@Test
public void simpleTest1() {
SszListSchema<TestView, ?> listType = SszListSchema.create(testType, 3);
SszMutableList<TestView> list = listType.getDefault().createWritableCopy();
TreeNode n0 = list.commitChanges().getBackingNode();
list.set(0, new TestView(0x111));
TreeNode n1 = list.commitChanges().getBackingNode();
list.set(1, new TestView(0x222));
TreeNode n2 = list.commitChanges().getBackingNode();
list.set(2, new TestView(0x333));
TreeNode n3 = list.commitChanges().getBackingNode();
list.set(0, new TestView(0x444));
TreeNode n4 = list.commitChanges().getBackingNode();
Assertions.assertThat(listType.createFromBackingNode(n0).size()).isEqualTo(0);
Assertions.assertThat(listType.createFromBackingNode(n1).size()).isEqualTo(1);
Assertions.assertThat(listType.createFromBackingNode(n1).get(0).v).isEqualTo(0x111);
Assertions.assertThat(listType.createFromBackingNode(n2).size()).isEqualTo(2);
Assertions.assertThat(listType.createFromBackingNode(n2).get(0).v).isEqualTo(0x111);
Assertions.assertThat(listType.createFromBackingNode(n2).get(1).v).isEqualTo(0x222);
Assertions.assertThat(listType.createFromBackingNode(n3).size()).isEqualTo(3);
Assertions.assertThat(listType.createFromBackingNode(n3).get(0).v).isEqualTo(0x111);
Assertions.assertThat(listType.createFromBackingNode(n3).get(1).v).isEqualTo(0x222);
Assertions.assertThat(listType.createFromBackingNode(n3).get(2).v).isEqualTo(0x333);
Assertions.assertThat(listType.createFromBackingNode(n4).size()).isEqualTo(3);
Assertions.assertThat(listType.createFromBackingNode(n4).get(0).v).isEqualTo(0x444);
Assertions.assertThat(listType.createFromBackingNode(n4).get(1).v).isEqualTo(0x222);
Assertions.assertThat(listType.createFromBackingNode(n4).get(2).v).isEqualTo(0x333);
assertThat(n0.hashTreeRoot()).isEqualTo(sha256(concatenate(sha256(concatenate(sha256(concatenate(ZERO, ZERO)), sha256(concatenate(ZERO, ZERO)))), ZERO)));
assertThat(n1.hashTreeRoot()).isEqualTo(sha256(concatenate(sha256(concatenate(sha256(concatenate(Bytes32.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000111"), ZERO)), sha256(concatenate(ZERO, ZERO)))), Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"))));
assertThat(n2.hashTreeRoot()).isEqualTo(sha256(concatenate(sha256(concatenate(sha256(concatenate(Bytes32.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000111"), Bytes32.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000222"))), sha256(concatenate(ZERO, ZERO)))), Bytes32.fromHexString("0x0200000000000000000000000000000000000000000000000000000000000000"))));
assertThat(n3.hashTreeRoot()).isEqualTo(sha256(concatenate(sha256(concatenate(sha256(concatenate(Bytes32.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000111"), Bytes32.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000222"))), sha256(concatenate(Bytes32.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000333"), ZERO)))), Bytes32.fromHexString("0x0300000000000000000000000000000000000000000000000000000000000000"))));
}
use of tech.pegasys.teku.infrastructure.ssz.tree.TreeNode 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());
}
use of tech.pegasys.teku.infrastructure.ssz.tree.TreeNode 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));
}
use of tech.pegasys.teku.infrastructure.ssz.tree.TreeNode 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);
}
Aggregations