Skip to content

Commit

Permalink
Merge pull request #60 from avalonche/add-blinded-block-contents
Browse files Browse the repository at this point in the history
Add blinded block contents
  • Loading branch information
mcdee authored Jul 20, 2023
2 parents 0c8305f + ea55b29 commit 2295a4c
Show file tree
Hide file tree
Showing 19 changed files with 1,426 additions and 8 deletions.
2 changes: 1 addition & 1 deletion api/v1/deneb/blindedbeaconblockbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type BlindedBeaconBlockBody struct {
SyncAggregate *altair.SyncAggregate
ExecutionPayloadHeader *deneb.ExecutionPayloadHeader
BLSToExecutionChanges []*capella.SignedBLSToExecutionChange `ssz-max:"16"`
BlobKzgCommitments []deneb.KzgCommitment `ssz-max:"4096" ssz-size:"?,48"`
BlobKzgCommitments []deneb.KzgCommitment `ssz-max:"6" ssz-size:"?,48"`
}

// String returns a string version of the structure.
Expand Down
14 changes: 7 additions & 7 deletions api/v1/deneb/blindedbeaconblockbody_ssz.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 129 additions & 0 deletions api/v1/deneb/blindedblobsidecar_ssz.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions api/v1/deneb/blindedblobsidecar_yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright © 2023 Attestant Limited.
// 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 deneb

import (
"bytes"
"encoding/json"

"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/goccy/go-yaml"
"github.com/pkg/errors"
)

// blindedBlobSidecarYAML is the spec representation of the struct.
type blindedBlobSidecarYAML struct {
BlockRoot phase0.Root `json:"block_root"`
Index uint64 `json:"index"`
Slot uint64 `json:"slot"`
BlockParentRoot phase0.Root `json:"block_parent_root"`
ProposerIndex uint64 `json:"proposer_index"`
BlobRoot phase0.Root `json:"blob_root"`
KzgCommitment deneb.KzgCommitment `json:"kzg_commitment"`
KzgProof deneb.KzgProof `json:"kzg_proof"`
}

// MarshalYAML implements json.Marshaler.
func (b *BlindedBlobSidecar) MarshalYAML() ([]byte, error) {
yamlBytes, err := yaml.MarshalWithOptions(&blindedBlobSidecarYAML{
BlockRoot: b.BlockRoot,
Index: uint64(b.Index),
Slot: uint64(b.Slot),
BlockParentRoot: b.BlockParentRoot,
ProposerIndex: uint64(b.ProposerIndex),
BlobRoot: b.BlobRoot,
KzgCommitment: b.KzgCommitment,
KzgProof: b.KzgProof,
}, yaml.Flow(true))
if err != nil {
return nil, err
}
return bytes.ReplaceAll(yamlBytes, []byte(`"`), []byte(`'`)), nil
}

// UnmarshalYAML implements json.Unmarshaler.
func (b *BlindedBlobSidecar) UnmarshalYAML(input []byte) error {
var data blindedBlobSidecarJSON
if err := yaml.Unmarshal(input, &data); err != nil {
return errors.Wrap(err, "failed to unmarshal YAML")
}
bytes, err := json.Marshal(data)
if err != nil {
return errors.Wrap(err, "failed to marshal JSON")
}

return b.UnmarshalJSON(bytes)
}
35 changes: 35 additions & 0 deletions api/v1/deneb/blindedblockcontents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright © 2023 Attestant Limited.
// 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 deneb

import (
"fmt"

"github.com/goccy/go-yaml"
)

// BlindedBlockContents represents the contents of a block, both blinded block and blob.
type BlindedBlockContents struct {
BlindedBlock *BlindedBeaconBlock
BlindedBlobSidecars []*BlindedBlobSidecar `ssz-max:"6"`
}

// String returns a string version of the structure.
func (b *BlindedBlockContents) String() string {
data, err := yaml.Marshal(b)
if err != nil {
return fmt.Sprintf("ERR: %v", err)
}
return string(data)
}
57 changes: 57 additions & 0 deletions api/v1/deneb/blindedblockcontents_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright © 2023 Attestant Limited.
// 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 deneb

import (
"encoding/json"

"github.com/pkg/errors"
)

// blindedBlockContentsJSON is the spec representation of the struct.
type blindedBlockContentsJSON struct {
BlindedBlock *BlindedBeaconBlock `json:"blinded_block"`
BlindedBlobSidecars []*BlindedBlobSidecar `json:"blinded_blob_sidecars"`
}

// MarshalJSON implements json.Marshaler.
func (b *BlindedBlockContents) MarshalJSON() ([]byte, error) {
return json.Marshal(&blindedBlockContentsJSON{
BlindedBlock: b.BlindedBlock,
BlindedBlobSidecars: b.BlindedBlobSidecars,
})
}

// UnmarshalJSON implements json.Unmarshaler.
func (b *BlindedBlockContents) UnmarshalJSON(input []byte) error {
var data blindedBlockContentsJSON
if err := json.Unmarshal(input, &data); err != nil {
return errors.Wrap(err, "invalid JSON")
}
return b.unpack(&data)
}

func (b *BlindedBlockContents) unpack(data *blindedBlockContentsJSON) error {
if data.BlindedBlock == nil {
return errors.New("blinded block missing")
}
b.BlindedBlock = data.BlindedBlock

if data.BlindedBlobSidecars == nil {
return errors.New("blinded blob sidecars missing")
}
b.BlindedBlobSidecars = data.BlindedBlobSidecars

return nil
}
Loading

0 comments on commit 2295a4c

Please sign in to comment.