Skip to content

Commit

Permalink
Add DAS Ssz data structures and config constants (#12)
Browse files Browse the repository at this point in the history
* Add KZG prefixes to all struct in the 'kzg' package to avoid further ambiguity
* Add DAS Ssz data structures and config constants
  • Loading branch information
Nashatyrev authored Apr 12, 2024
1 parent 008ab85 commit 220d422
Show file tree
Hide file tree
Showing 23 changed files with 525 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ static SpecConfigElectra required(final SpecConfig specConfig) {

int getMaxExecutionLayerExits();

UInt64 getFieldElementsPerCell();

@Override
Optional<SpecConfigElectra> toVersionElectra();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ public class SpecConfigElectraImpl extends DelegatingSpecConfigDeneb implements
private final int maxDepositReceiptsPerPayload;
private final int maxExecutionLayerExits;

private final UInt64 fieldElementsPerCell;

public SpecConfigElectraImpl(
final SpecConfigDeneb specConfig,
final Bytes4 electraForkVersion,
final UInt64 electraForkEpoch,
final int maxDepositReceiptsPerPayload,
final int maxExecutionLayerExits) {
final int maxExecutionLayerExits,
final UInt64 fieldElementsPerCell) {
super(specConfig);
this.electraForkVersion = electraForkVersion;
this.electraForkEpoch = electraForkEpoch;
this.maxDepositReceiptsPerPayload = maxDepositReceiptsPerPayload;
this.maxExecutionLayerExits = maxExecutionLayerExits;
this.fieldElementsPerCell = fieldElementsPerCell;
}

@Override
Expand All @@ -59,6 +63,11 @@ public int getMaxExecutionLayerExits() {
return maxExecutionLayerExits;
}

@Override
public UInt64 getFieldElementsPerCell() {
return fieldElementsPerCell;
}

@Override
public Optional<SpecConfigElectra> toVersionElectra() {
return Optional.of(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ElectraBuilder implements ForkConfigBuilder<SpecConfigDeneb, SpecCo
private UInt64 electraForkEpoch;
private Integer maxDepositReceiptsPerPayload;
private Integer maxExecutionLayerExits;
private UInt64 fieldElementsPerCell;

ElectraBuilder() {}

Expand All @@ -42,7 +43,8 @@ public SpecConfigElectra build(final SpecConfigDeneb specConfig) {
electraForkVersion,
electraForkEpoch,
maxDepositReceiptsPerPayload,
maxExecutionLayerExits);
maxExecutionLayerExits,
fieldElementsPerCell);
}

public ElectraBuilder electraForkEpoch(final UInt64 electraForkEpoch) {
Expand All @@ -69,6 +71,12 @@ public ElectraBuilder maxExecutionLayerExits(final Integer maxExecutionLayerExit
return this;
}

public ElectraBuilder fieldElementsPerCell(final UInt64 fieldElementsPerCell) {
checkNotNull(fieldElementsPerCell);
this.fieldElementsPerCell = fieldElementsPerCell;
return this;
}

@Override
public void validate() {
if (electraForkEpoch == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Consensys Software Inc., 2022
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.spec.datastructures.blobs.versions.electra;

import org.apache.tuweni.bytes.Bytes;
import tech.pegasys.teku.infrastructure.ssz.collections.impl.SszByteVectorImpl;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;

public class Cell extends SszByteVectorImpl {

Cell(final CellSchema schema, final TreeNode backingNode) {
super(schema, backingNode);
}

Cell(final CellSchema cellSchema, final Bytes bytes) {
super(cellSchema, bytes);
}

public String toBriefString() {
return getBytes().slice(0, 7).toUnprefixedHexString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tech.pegasys.teku.spec.datastructures.blobs.versions.electra;

import org.apache.tuweni.bytes.Bytes;
import tech.pegasys.teku.infrastructure.ssz.schema.collections.impl.SszByteVectorSchemaImpl;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.spec.config.SpecConfigDeneb;
import tech.pegasys.teku.spec.config.SpecConfigElectra;

public class CellSchema extends SszByteVectorSchemaImpl<Cell> {

public CellSchema(final SpecConfigElectra specConfig) {
super(SpecConfigDeneb.BYTES_PER_FIELD_ELEMENT.longValue() * specConfig.getFieldElementsPerCell().longValue());
}

public Cell create(final Bytes bytes) {
return new Cell(this, bytes);
}

@Override
public Cell createFromBackingNode(TreeNode node) {
return new Cell(this, node);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Consensys Software Inc., 2022
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.spec.datastructures.blobs.versions.electra;

import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.ssz.impl.SszListImpl;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;

public class DataColumn extends SszListImpl<Cell> implements SszList<Cell> {

DataColumn(DataColumnSchema schema, TreeNode node) {
super(schema, node);
}

public String toBriefString() {
return isEmpty() ? "" : get(0).toBriefString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tech.pegasys.teku.spec.datastructures.blobs.versions.electra;

import tech.pegasys.teku.infrastructure.ssz.schema.impl.AbstractSszListSchema;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.spec.config.SpecConfigElectra;

public class DataColumnSchema
extends AbstractSszListSchema<Cell, DataColumn> {

public DataColumnSchema(final SpecConfigElectra specConfig) {
super(new CellSchema(specConfig), specConfig.getMaxBlobCommitmentsPerBlock());
}

@Override
public DataColumn createFromBackingNode(TreeNode node) {
return new DataColumn(this, node);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright Consensys Software Inc., 2023
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.spec.datastructures.blobs.versions.electra;

import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.infrastructure.logging.LogFormatter;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector;
import tech.pegasys.teku.infrastructure.ssz.containers.Container6;
import tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.kzg.KZGProof;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockHeader;
import tech.pegasys.teku.spec.datastructures.blocks.SlotAndBlockRoot;
import tech.pegasys.teku.spec.datastructures.type.SszKZGCommitment;
import tech.pegasys.teku.spec.datastructures.type.SszKZGProof;

public class DataColumnSidecar
extends Container6<
DataColumnSidecar,
SszUInt64,
DataColumn,
SszList<SszKZGCommitment>,
SszList<SszKZGProof>,
SignedBeaconBlockHeader,
SszBytes32Vector> {

DataColumnSidecar(final DataColumnSidecarSchema dataColumnSidecarSchema, final TreeNode backingTreeNode) {
super(dataColumnSidecarSchema, backingTreeNode);
}

// public DataColumnSidecar(
// final DataColumnSidecarSchema schema,
// final UInt64 index,
// final Blob blob,
// final SszKZGCommitment sszKzgCommitment,
// final SszKZGProof sszKzgProof,
// final SignedBeaconBlockHeader signedBeaconBlockHeader,
// final List<Bytes32> kzgCommitmentInclusionProof) {
// super(
// schema,
// SszUInt64.of(index),
// schema.getDataColumnSszSchema().create(blob.getBytes()),
// sszKzgCommitment,
// sszKzgProof,
// signedBeaconBlockHeader,
// schema
// .getKzgCommitmentInclusionProofSchema()
// .createFromElements(kzgCommitmentInclusionProof.stream().map(SszBytes32::of).toList()));
// }
//
// public DataColumnSidecar(
// final DataColumnSidecarSchema schema,
// final UInt64 index,
// final Blob blob,
// final KZGCommitment kzgCommitment,
// final KZGProof kzgProof,
// final SignedBeaconBlockHeader signedBeaconBlockHeader,
// final List<Bytes32> kzgCommitmentInclusionProof) {
// this(
// schema,
// index,
// blob,
// new SszKZGCommitment(kzgCommitment),
// new SszKZGProof(kzgProof),
// signedBeaconBlockHeader,
// kzgCommitmentInclusionProof);
// }

public UInt64 getIndex() {
return getField0().get();
}

public DataColumn getDataColumn() {
return getField1();
}

public SszList<SszKZGCommitment> getSszKZGCommitments() {
return getField2();
}

public SszList<SszKZGProof> getSszKZGProofs() {
return getField3();
}

public SignedBeaconBlockHeader getSignedBeaconBlockHeader() {
return getField4();
}

public SszBytes32Vector getKzgCommitmentInclusionProof() {
return getField5();
}

public UInt64 getSlot() {
return getSignedBeaconBlockHeader().getMessage().getSlot();
}

public Bytes32 getBlockBodyRoot() {
return getSignedBeaconBlockHeader().getMessage().getBodyRoot();
}

public Bytes32 getBlockRoot() {
return getSignedBeaconBlockHeader().getMessage().getRoot();
}

public SlotAndBlockRoot getSlotAndBlockRoot() {
return new SlotAndBlockRoot(getSlot(), getBlockRoot());
}

public String toLogString() {
return LogFormatter.formatBlobSidecar(
getSlot(),
getBlockRoot(),
getIndex(),
getDataColumn().toBriefString(),
"" + getSszKZGCommitments().size(),
"" + getSszKZGProofs().size());
}
}
Loading

0 comments on commit 220d422

Please sign in to comment.