2021-09-23 18:53:46 +00:00
|
|
|
package util
|
2021-07-28 17:15:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prysmaticlabs/go-bitfield"
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/core/signing"
|
|
|
|
p2pType "github.com/prysmaticlabs/prysm/v3/beacon-chain/p2p/types"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/crypto/bls"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/time/slots"
|
2021-07-28 17:15:23 +00:00
|
|
|
)
|
|
|
|
|
2022-05-20 22:40:03 +00:00
|
|
|
func generateSyncAggregate(st state.BeaconState, privs []bls.SecretKey, parentRoot [32]byte) (*ethpb.SyncAggregate, error) {
|
2021-10-01 20:17:57 +00:00
|
|
|
nextSlotEpoch := slots.ToEpoch(st.Slot() + 1)
|
|
|
|
currEpoch := slots.ToEpoch(st.Slot())
|
2021-07-28 17:15:23 +00:00
|
|
|
|
2021-07-29 21:45:17 +00:00
|
|
|
var syncCommittee *ethpb.SyncCommittee
|
2021-07-28 17:15:23 +00:00
|
|
|
var err error
|
2021-10-01 20:17:57 +00:00
|
|
|
if slots.SyncCommitteePeriod(currEpoch) == slots.SyncCommitteePeriod(nextSlotEpoch) {
|
2021-07-28 17:15:23 +00:00
|
|
|
syncCommittee, err = st.CurrentSyncCommittee()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
syncCommittee, err = st.NextSyncCommittee()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sigs := make([]bls.Signature, 0, len(syncCommittee.Pubkeys))
|
|
|
|
var bVector []byte
|
2021-07-29 21:45:17 +00:00
|
|
|
currSize := new(ethpb.SyncAggregate).SyncCommitteeBits.Len()
|
2021-07-28 17:15:23 +00:00
|
|
|
switch currSize {
|
|
|
|
case 512:
|
|
|
|
bVector = bitfield.NewBitvector512()
|
|
|
|
case 32:
|
|
|
|
bVector = bitfield.NewBitvector32()
|
|
|
|
default:
|
|
|
|
return nil, errors.New("invalid bit vector size")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, p := range syncCommittee.Pubkeys {
|
|
|
|
idx, ok := st.ValidatorIndexByPubkey(bytesutil.ToBytes48(p))
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-02-14 13:34:38 +00:00
|
|
|
d, err := signing.Domain(st.Fork(), slots.ToEpoch(st.Slot()), params.BeaconConfig().DomainSyncCommittee, st.GenesisValidatorsRoot())
|
2021-07-28 17:15:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sszBytes := p2pType.SSZBytes(parentRoot[:])
|
2021-09-27 16:19:20 +00:00
|
|
|
r, err := signing.ComputeSigningRoot(&sszBytes, d)
|
2021-07-28 17:15:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sigs = append(sigs, privs[idx].Sign(r[:]))
|
|
|
|
if currSize == 512 {
|
|
|
|
bitfield.Bitvector512(bVector).SetBitAt(uint64(i), true)
|
|
|
|
}
|
|
|
|
if currSize == 32 {
|
|
|
|
bitfield.Bitvector32(bVector).SetBitAt(uint64(i), true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(sigs) == 0 {
|
|
|
|
fakeSig := [96]byte{0xC0}
|
2021-07-29 21:45:17 +00:00
|
|
|
return ðpb.SyncAggregate{SyncCommitteeSignature: fakeSig[:], SyncCommitteeBits: bVector}, nil
|
2021-07-28 17:15:23 +00:00
|
|
|
}
|
|
|
|
aggSig := bls.AggregateSignatures(sigs)
|
2021-07-29 21:45:17 +00:00
|
|
|
return ðpb.SyncAggregate{SyncCommitteeSignature: aggSig.Marshal(), SyncCommitteeBits: bVector}, nil
|
2021-07-28 17:15:23 +00:00
|
|
|
}
|