Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SingleAttestation container #172

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ coverage.html
# Vim
*.sw?

# Intellij and friends
.idea/

# Local TODO
TODO.md
6 changes: 3 additions & 3 deletions spec/electra/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ package electra

//nolint:revive
// Need to `go install github.com/ferranbt/fastssz/sszgen@latest` for this to work.
//go:generate rm -f aggregateandproof_ssz.go attestation_ssz.go attesterslashing_ssz.go beaconblockbody_ssz.go beaconblock_ssz.go beaconstate_ssz.go consolidation_ssz.go consolidationrequest_ssz.go depositrequest_ssz.go withdrawalrequest_ssz.go executionrequests_ssz.go pendingconsolidation_ssz.go pendingdeposit_ssz.go pendingpartialwithdrawal_ssz.go signedaggregateandproof_ssz.go signedbeaconblock_ssz.go
//go:generate sszgen --suffix=ssz --path . --include ../phase0,../altair,../bellatrix,../capella,../deneb --objs AggregateAndProof,Attestation,AttesterSlashing,BeaconBlockBody,BeaconBlock,BeaconState,Consolidation,ConsolidationRequest,DepositRequest,WithdrawalRequest,ExecutionRequests,PendingConsolidation,PendingDeposit,PendingPartialWithdrawal,SignedAggregateAndProof,SignedBeaconBlock
//go:generate goimports -w aggregateandproof_ssz.go attestation_ssz.go attesterslashing_ssz.go beaconblockbody_ssz.go beaconblock_ssz.go beaconstate_ssz.go consolidation_ssz.go consolidationrequest_ssz.go depositrequest_ssz.go withdrawalrequest_ssz.go executionrequests_ssz.go pendingconsolidation_ssz.go pendingdeposit_ssz.go pendingpartialwithdrawal_ssz.go signedaggregateandproof_ssz.go signedbeaconblock_ssz.go
//go:generate rm -f aggregateandproof_ssz.go attestation_ssz.go attesterslashing_ssz.go beaconblockbody_ssz.go beaconblock_ssz.go beaconstate_ssz.go consolidation_ssz.go consolidationrequest_ssz.go depositrequest_ssz.go withdrawalrequest_ssz.go executionrequests_ssz.go pendingconsolidation_ssz.go pendingdeposit_ssz.go pendingpartialwithdrawal_ssz.go signedaggregateandproof_ssz.go signedbeaconblock_ssz.go singleattestation_ssz.go
//go:generate sszgen --suffix=ssz --path . --include ../phase0,../altair,../bellatrix,../capella,../deneb --objs AggregateAndProof,Attestation,AttesterSlashing,BeaconBlockBody,BeaconBlock,BeaconState,Consolidation,ConsolidationRequest,DepositRequest,WithdrawalRequest,ExecutionRequests,PendingConsolidation,PendingDeposit,PendingPartialWithdrawal,SignedAggregateAndProof,SignedBeaconBlock,SingleAttestation
//go:generate goimports -w aggregateandproof_ssz.go attestation_ssz.go attesterslashing_ssz.go beaconblockbody_ssz.go beaconblock_ssz.go beaconstate_ssz.go consolidation_ssz.go consolidationrequest_ssz.go depositrequest_ssz.go withdrawalrequest_ssz.go executionrequests_ssz.go pendingconsolidation_ssz.go pendingdeposit_ssz.go pendingpartialwithdrawal_ssz.go signedaggregateandproof_ssz.go signedbeaconblock_ssz.go singleattestation_ssz.go
40 changes: 40 additions & 0 deletions spec/electra/singleattestation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright © 2024 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 electra

import (
"fmt"

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

// SingleAttestation is a new struct in electra for propagating network only Attestations. See:
// https://github.com/ethereum/consensus-specs/blob/dev/specs/electra/beacon-chain.md#singleattestation.
type SingleAttestation struct {
CommitteeIndex phase0.CommitteeIndex
AttesterIndex phase0.ValidatorIndex
Data *phase0.AttestationData
Signature phase0.BLSSignature `ssz-size:"96"`
}

// String returns a string version of the structure.
func (a *SingleAttestation) String() string {
data, err := yaml.Marshal(a)
if err != nil {
return fmt.Sprintf("ERR: %v", err)
}

return string(data)
}
91 changes: 91 additions & 0 deletions spec/electra/singleattestation_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright © 2024 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 electra

import (
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"strings"

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

// singleAttestationJSON is the spec representation of the struct.
type singleAttestationJSON struct {
CommitteeIndex string `json:"committee_index"`
AttesterIndex string `json:"attester_index"`
Data *phase0.AttestationData `json:"data"`
Signature string `json:"signature"`
}

// MarshalJSON implements json.Marshaler.
func (a *SingleAttestation) MarshalJSON() ([]byte, error) {
return json.Marshal(&singleAttestationJSON{
CommitteeIndex: fmt.Sprintf("%d", a.CommitteeIndex),
AttesterIndex: fmt.Sprintf("%d", a.AttesterIndex),
Data: a.Data,
Signature: fmt.Sprintf("%#x", a.Signature),
})
}

// UnmarshalJSON implements json.Unmarshaler.
func (a *SingleAttestation) UnmarshalJSON(input []byte) error {
var singleAttestationJSON singleAttestationJSON
err := json.Unmarshal(input, &singleAttestationJSON)
if err != nil {
return errors.Wrap(err, "invalid JSON")
}

return a.unpack(&singleAttestationJSON)
}

func (a *SingleAttestation) unpack(singleAttestationJSON *singleAttestationJSON) error {
var err error
if singleAttestationJSON.CommitteeIndex == "" {
mcdee marked this conversation as resolved.
Show resolved Hide resolved
return errors.New("committee index missing")
}
committeeIndex, err := strconv.ParseUint(singleAttestationJSON.CommitteeIndex, 10, 64)
if err != nil {
return errors.Wrap(err, "invalid value for committee index")
}
a.CommitteeIndex = phase0.CommitteeIndex(committeeIndex)
if singleAttestationJSON.AttesterIndex == "" {
return errors.New("attester index missing")
}
attesterIndex, err := strconv.ParseUint(singleAttestationJSON.AttesterIndex, 10, 64)
if err != nil {
return errors.Wrap(err, "invalid value for attester index")
}
a.AttesterIndex = phase0.ValidatorIndex(attesterIndex)
a.Data = singleAttestationJSON.Data
if a.Data == nil {
return errors.New("data missing")
}
if singleAttestationJSON.Signature == "" {
return errors.New("signature missing")
}
signature, err := hex.DecodeString(strings.TrimPrefix(singleAttestationJSON.Signature, "0x"))
if err != nil {
return errors.Wrap(err, "invalid value for signature")
}
if len(signature) != phase0.SignatureLength {
return errors.New("incorrect length for signature")
}
copy(a.Signature[:], signature)

return nil
}
107 changes: 107 additions & 0 deletions spec/electra/singleattestation_ssz.go

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

Loading
Loading