mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-05-02 03:02:54 -04:00
* Ran gopls modernize to fix everything go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... * Override rules_go provided dependency for golang.org/x/tools to v0.38.0. To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches. * Fix buildtag violations and ignore buildtag violations in external * Introduce modernize analyzer package. * Add modernize "any" analyzer. * Fix violations of any analyzer * Add modernize "appendclipped" analyzer. * Fix violations of appendclipped * Add modernize "bloop" analyzer. * Add modernize "fmtappendf" analyzer. * Add modernize "forvar" analyzer. * Add modernize "mapsloop" analyzer. * Add modernize "minmax" analyzer. * Fix violations of minmax analyzer * Add modernize "omitzero" analyzer. * Add modernize "rangeint" analyzer. * Fix violations of rangeint. * Add modernize "reflecttypefor" analyzer. * Fix violations of reflecttypefor analyzer. * Add modernize "slicescontains" analyzer. * Add modernize "slicessort" analyzer. * Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686. * Add modernize "stringscutprefix" analyzer. * Add modernize "stringsbuilder" analyzer. * Fix violations of stringsbuilder analyzer. * Add modernize "stringsseq" analyzer. * Add modernize "testingcontext" analyzer. * Add modernize "waitgroup" analyzer. * Changelog fragment * gofmt * gazelle * Add modernize "newexpr" analyzer. * Disable newexpr until go1.26 * Add more details in WORKSPACE on how to update the override * @nalepae feedback on min() * gofmt * Fix violations of forvar
143 lines
5.2 KiB
Go
143 lines
5.2 KiB
Go
package core
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"testing"
|
|
"time"
|
|
|
|
mockChain "github.com/OffchainLabs/prysm/v7/beacon-chain/blockchain/testing"
|
|
"github.com/OffchainLabs/prysm/v7/beacon-chain/cache"
|
|
p2pmock "github.com/OffchainLabs/prysm/v7/beacon-chain/p2p/testing"
|
|
"github.com/OffchainLabs/prysm/v7/config/params"
|
|
"github.com/OffchainLabs/prysm/v7/consensus-types/primitives"
|
|
"github.com/OffchainLabs/prysm/v7/consensus-types/validator"
|
|
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
|
|
"github.com/OffchainLabs/prysm/v7/testing/assert"
|
|
"github.com/OffchainLabs/prysm/v7/testing/require"
|
|
"github.com/OffchainLabs/prysm/v7/time/slots"
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
)
|
|
|
|
func TestRegisterSyncSubnetProto(t *testing.T) {
|
|
k := pubKey(3)
|
|
committee := make([][]byte, 0)
|
|
|
|
for i := range 100 {
|
|
committee = append(committee, pubKey(uint64(i)))
|
|
}
|
|
sCommittee := ðpb.SyncCommittee{
|
|
Pubkeys: committee,
|
|
}
|
|
registerSyncSubnetProto(0, 0, k, sCommittee, ethpb.ValidatorStatus_ACTIVE)
|
|
coms, _, ok, exp := cache.SyncSubnetIDs.GetSyncCommitteeSubnets(k, 0)
|
|
require.Equal(t, true, ok, "No cache entry found for validator")
|
|
assert.Equal(t, uint64(1), uint64(len(coms)))
|
|
epochDuration := time.Duration(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().SecondsPerSlot))
|
|
totalTime := time.Duration(params.BeaconConfig().EpochsPerSyncCommitteePeriod) * epochDuration * time.Second
|
|
receivedTime := time.Until(exp.Round(time.Second)).Round(time.Second)
|
|
if receivedTime < totalTime {
|
|
t.Fatalf("Expiration time of %f was less than expected duration of %f ", receivedTime.Seconds(), totalTime.Seconds())
|
|
}
|
|
}
|
|
|
|
func TestRegisterSyncSubnet(t *testing.T) {
|
|
k := pubKey(3)
|
|
committee := make([][]byte, 0)
|
|
|
|
for i := range 100 {
|
|
committee = append(committee, pubKey(uint64(i)))
|
|
}
|
|
sCommittee := ðpb.SyncCommittee{
|
|
Pubkeys: committee,
|
|
}
|
|
registerSyncSubnet(0, 0, k, sCommittee, validator.Active)
|
|
coms, _, ok, exp := cache.SyncSubnetIDs.GetSyncCommitteeSubnets(k, 0)
|
|
require.Equal(t, true, ok, "No cache entry found for validator")
|
|
assert.Equal(t, uint64(1), uint64(len(coms)))
|
|
epochDuration := time.Duration(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().SecondsPerSlot))
|
|
totalTime := time.Duration(params.BeaconConfig().EpochsPerSyncCommitteePeriod) * epochDuration * time.Second
|
|
receivedTime := time.Until(exp.Round(time.Second)).Round(time.Second)
|
|
if receivedTime < totalTime {
|
|
t.Fatalf("Expiration time of %f was less than expected duration of %f ", receivedTime.Seconds(), totalTime.Seconds())
|
|
}
|
|
}
|
|
|
|
// pubKey is a helper to generate a well-formed public key.
|
|
func pubKey(i uint64) []byte {
|
|
pubKey := make([]byte, params.BeaconConfig().BLSPubkeyLength)
|
|
binary.LittleEndian.PutUint64(pubKey, i)
|
|
return pubKey
|
|
}
|
|
|
|
func TestService_SubmitSignedAggregateSelectionProof(t *testing.T) {
|
|
slot := primitives.Slot(0)
|
|
mock := &mockChain.ChainService{Slot: &slot, Genesis: time.Now().Add(-75 * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second)}
|
|
s := &Service{GenesisTimeFetcher: mock}
|
|
var err error
|
|
t.Run("Happy path electra", func(t *testing.T) {
|
|
slot, err = slots.EpochEnd(params.BeaconConfig().ElectraForkEpoch)
|
|
require.NoError(t, err)
|
|
broadcaster := &p2pmock.MockBroadcaster{}
|
|
s.Broadcaster = broadcaster
|
|
fakeSig, err := hexutil.Decode("0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505")
|
|
require.NoError(t, err)
|
|
agg := ðpb.SignedAggregateAttestationAndProofElectra{
|
|
Message: ðpb.AggregateAttestationAndProofElectra{
|
|
AggregatorIndex: 72,
|
|
Aggregate: ðpb.AttestationElectra{
|
|
AggregationBits: make([]byte, 4),
|
|
Data: ðpb.AttestationData{
|
|
Slot: 75,
|
|
CommitteeIndex: 76,
|
|
BeaconBlockRoot: make([]byte, 32),
|
|
Source: ðpb.Checkpoint{
|
|
Epoch: 78,
|
|
Root: make([]byte, 32),
|
|
},
|
|
Target: ðpb.Checkpoint{
|
|
Epoch: 80,
|
|
Root: make([]byte, 32),
|
|
},
|
|
},
|
|
Signature: fakeSig,
|
|
CommitteeBits: make([]byte, 8),
|
|
},
|
|
SelectionProof: fakeSig,
|
|
},
|
|
Signature: fakeSig,
|
|
}
|
|
rpcError := s.SubmitSignedAggregateSelectionProof(t.Context(), agg)
|
|
t.Log(rpcError)
|
|
assert.Equal(t, true, rpcError == nil)
|
|
})
|
|
|
|
t.Run("Phase 0 post electra", func(t *testing.T) {
|
|
slot, err = slots.EpochEnd(params.BeaconConfig().ElectraForkEpoch)
|
|
require.NoError(t, err)
|
|
agg := ðpb.SignedAggregateAttestationAndProof{
|
|
Message: ðpb.AggregateAttestationAndProof{
|
|
Aggregate: ðpb.Attestation{
|
|
Data: ðpb.AttestationData{},
|
|
},
|
|
},
|
|
Signature: make([]byte, 96),
|
|
}
|
|
rpcError := s.SubmitSignedAggregateSelectionProof(t.Context(), agg)
|
|
assert.ErrorContains(t, "old aggregate and proof", rpcError.Err)
|
|
})
|
|
|
|
t.Run("electra agg pre electra", func(t *testing.T) {
|
|
slot = primitives.Slot(0)
|
|
agg := ðpb.SignedAggregateAttestationAndProofElectra{
|
|
Message: ðpb.AggregateAttestationAndProofElectra{
|
|
Aggregate: ðpb.AttestationElectra{
|
|
Data: ðpb.AttestationData{},
|
|
},
|
|
},
|
|
Signature: make([]byte, 96),
|
|
}
|
|
rpcError := s.SubmitSignedAggregateSelectionProof(t.Context(), agg)
|
|
assert.ErrorContains(t, "electra aggregate and proof not supported yet", rpcError.Err)
|
|
})
|
|
}
|