-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from avalonche/add-blinded-block-contents
Add blinded block contents
- Loading branch information
Showing
19 changed files
with
1,426 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.