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

EIP-7549: Allow multiple committee bits #14203

Merged
merged 1 commit into from
Jul 12, 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
1 change: 1 addition & 0 deletions proto/prysm/v1alpha1/attestation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
"//config/params:go_default_library",
"//consensus-types/primitives:go_default_library",
"//crypto/bls:go_default_library",
"//crypto/hash:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//runtime/version:go_default_library",
"@com_github_pkg_errors//:go_default_library",
Expand Down
27 changes: 15 additions & 12 deletions proto/prysm/v1alpha1/attestation/id.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package attestation

import (
"fmt"
"strconv"
"strings"

"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v5/crypto/hash"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
)
Expand Down Expand Up @@ -44,19 +45,21 @@ func NewId(att ethpb.Att, source IdSource) (Id, error) {
copy(id[1:], h[:])
return id, nil
case Data:
data := att.GetData()
dataHash, err := att.GetData().HashTreeRoot()
if err != nil {
return Id{}, err
}
h := dataHash
if att.Version() >= version.Electra {
committeeIndices := att.CommitteeBitsVal().BitIndices()
if len(committeeIndices) != 1 {
return Id{}, fmt.Errorf("%d committee bits are set instead of 1", len(committeeIndices))
if len(committeeIndices) == 0 {
return Id{}, errors.New("no committee bits are set")
}
dataCopy := ethpb.CopyAttestationData(att.GetData())
dataCopy.CommitteeIndex = primitives.CommitteeIndex(committeeIndices[0])
data = dataCopy
}
h, err := data.HashTreeRoot()
if err != nil {
return Id{}, err
stringCommitteeIndices := make([]string, len(committeeIndices))
for i, ix := range committeeIndices {
stringCommitteeIndices[i] = strconv.Itoa(ix)
}
h = hash.Hash(append(dataHash[:], []byte(strings.Join(stringCommitteeIndices, ","))...))
}
copy(id[1:], h[:])
return id, nil
Expand Down
26 changes: 17 additions & 9 deletions proto/prysm/v1alpha1/attestation/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ func TestNewId(t *testing.T) {

assert.NotEqual(t, phase0Id, electraId)
})
t.Run("ID is different for different committee bits", func(t *testing.T) {
cb := primitives.NewAttestationCommitteeBits()
cb.SetBitAt(0, true)
cb.SetBitAt(1, true)
att := util.HydrateAttestationElectra(&ethpb.AttestationElectra{CommitteeBits: cb})
id1, err := attestation.NewId(att, attestation.Data)
assert.NoError(t, err)
cb = primitives.NewAttestationCommitteeBits()
cb.SetBitAt(0, true)
cb.SetBitAt(2, true)
att = util.HydrateAttestationElectra(&ethpb.AttestationElectra{CommitteeBits: cb})
id2, err := attestation.NewId(att, attestation.Data)
assert.NoError(t, err)

assert.NotEqual(t, id1, id2)
})
t.Run("invalid source", func(t *testing.T) {
att := util.HydrateAttestation(&ethpb.Attestation{})
_, err := attestation.NewId(att, 123)
Expand All @@ -50,14 +66,6 @@ func TestNewId(t *testing.T) {
cb := primitives.NewAttestationCommitteeBits()
att := util.HydrateAttestationElectra(&ethpb.AttestationElectra{CommitteeBits: cb})
_, err := attestation.NewId(att, attestation.Data)
assert.ErrorContains(t, "0 committee bits are set", err)
})
t.Run("data source Electra - multiple bits set", func(t *testing.T) {
cb := primitives.NewAttestationCommitteeBits()
cb.SetBitAt(0, true)
cb.SetBitAt(1, true)
att := util.HydrateAttestationElectra(&ethpb.AttestationElectra{CommitteeBits: cb})
_, err := attestation.NewId(att, attestation.Data)
assert.ErrorContains(t, "2 committee bits are set", err)
Comment on lines -55 to -61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not keep this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no because this asserts that setting 2 committee bits is wrong, while this PR allows it

assert.ErrorContains(t, "no committee bits are set", err)
})
}
Loading