Refactor signature function's argument (#8773)

This commit is contained in:
terence tsao
2021-04-15 11:35:53 -07:00
committed by GitHub
parent 3d3b9d1217
commit a4ff97d24b
8 changed files with 26 additions and 20 deletions

View File

@@ -55,7 +55,7 @@ func ProcessBlockHeader(
} }
// Verify proposer signature. // Verify proposer signature.
if err := VerifyBlockSignature(beaconState, block); err != nil { if err := VerifyBlockSignature(beaconState, block.Block.ProposerIndex, block.Signature, block.Block.HashTreeRoot); err != nil {
return nil, err return nil, err
} }

View File

@@ -318,7 +318,7 @@ func TestBlockSignatureSet_OK(t *testing.T) {
validators[proposerIdx].PublicKey = priv.PublicKey().Marshal() validators[proposerIdx].PublicKey = priv.PublicKey().Marshal()
err = state.UpdateValidatorAtIndex(proposerIdx, validators[proposerIdx]) err = state.UpdateValidatorAtIndex(proposerIdx, validators[proposerIdx])
require.NoError(t, err) require.NoError(t, err)
set, err := blocks.BlockSignatureSet(state, block) set, err := blocks.BlockSignatureSet(state, block.Block.ProposerIndex, block.Signature, block.Block.HashTreeRoot)
require.NoError(t, err) require.NoError(t, err)
verified, err := set.Verify() verified, err := set.Verify()

View File

@@ -80,7 +80,7 @@ func TestRandaoSignatureSet_OK(t *testing.T) {
}, },
} }
set, err := blocks.RandaoSignatureSet(beaconState, block.Body) set, err := blocks.RandaoSignatureSet(beaconState, block.Body.RandaoReveal)
require.NoError(t, err) require.NoError(t, err)
verified, err := set.Verify() verified, err := set.Verify()
require.NoError(t, err) require.NoError(t, err)

View File

@@ -60,45 +60,51 @@ func verifySignature(signedData, pub, signature, domain []byte) error {
} }
// VerifyBlockSignature verifies the proposer signature of a beacon block. // VerifyBlockSignature verifies the proposer signature of a beacon block.
func VerifyBlockSignature(beaconState iface.ReadOnlyBeaconState, block *ethpb.SignedBeaconBlock) error { func VerifyBlockSignature(beaconState iface.ReadOnlyBeaconState,
proposerIndex types.ValidatorIndex,
sig []byte,
rootFunc func() ([32]byte, error)) error {
currentEpoch := helpers.SlotToEpoch(beaconState.Slot()) currentEpoch := helpers.SlotToEpoch(beaconState.Slot())
domain, err := helpers.Domain(beaconState.Fork(), currentEpoch, params.BeaconConfig().DomainBeaconProposer, beaconState.GenesisValidatorRoot()) domain, err := helpers.Domain(beaconState.Fork(), currentEpoch, params.BeaconConfig().DomainBeaconProposer, beaconState.GenesisValidatorRoot())
if err != nil { if err != nil {
return err return err
} }
proposer, err := beaconState.ValidatorAtIndex(block.Block.ProposerIndex) proposer, err := beaconState.ValidatorAtIndex(proposerIndex)
if err != nil { if err != nil {
return err return err
} }
proposerPubKey := proposer.PublicKey proposerPubKey := proposer.PublicKey
return helpers.VerifyBlockSigningRoot(block.Block, proposerPubKey, block.Signature, domain) return helpers.VerifyBlockSigningRoot(proposerPubKey, sig, domain, rootFunc)
} }
// BlockSignatureSet retrieves the block signature set from the provided block and its corresponding state. // BlockSignatureSet retrieves the block signature set from the provided block and its corresponding state.
func BlockSignatureSet(beaconState iface.ReadOnlyBeaconState, block *ethpb.SignedBeaconBlock) (*bls.SignatureSet, error) { func BlockSignatureSet(beaconState iface.ReadOnlyBeaconState,
proposerIndex types.ValidatorIndex,
sig []byte,
rootFunc func() ([32]byte, error)) (*bls.SignatureSet, error) {
currentEpoch := helpers.SlotToEpoch(beaconState.Slot()) currentEpoch := helpers.SlotToEpoch(beaconState.Slot())
domain, err := helpers.Domain(beaconState.Fork(), currentEpoch, params.BeaconConfig().DomainBeaconProposer, beaconState.GenesisValidatorRoot()) domain, err := helpers.Domain(beaconState.Fork(), currentEpoch, params.BeaconConfig().DomainBeaconProposer, beaconState.GenesisValidatorRoot())
if err != nil { if err != nil {
return nil, err return nil, err
} }
proposer, err := beaconState.ValidatorAtIndex(block.Block.ProposerIndex) proposer, err := beaconState.ValidatorAtIndex(proposerIndex)
if err != nil { if err != nil {
return nil, err return nil, err
} }
proposerPubKey := proposer.PublicKey proposerPubKey := proposer.PublicKey
return helpers.BlockSignatureSet(block.Block, proposerPubKey, block.Signature, domain) return helpers.BlockSignatureSet(proposerPubKey, sig, domain, rootFunc)
} }
// RandaoSignatureSet retrieves the relevant randao specific signature set object // RandaoSignatureSet retrieves the relevant randao specific signature set object
// from a block and its corresponding state. // from a block and its corresponding state.
func RandaoSignatureSet(beaconState iface.ReadOnlyBeaconState, func RandaoSignatureSet(beaconState iface.ReadOnlyBeaconState,
body *ethpb.BeaconBlockBody, reveal []byte,
) (*bls.SignatureSet, error) { ) (*bls.SignatureSet, error) {
buf, proposerPub, domain, err := randaoSigningData(beaconState) buf, proposerPub, domain, err := randaoSigningData(beaconState)
if err != nil { if err != nil {
return nil, err return nil, err
} }
set, err := signatureSet(buf, proposerPub, body.RandaoReveal, domain) set, err := signatureSet(buf, proposerPub, reveal, domain)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -98,8 +98,8 @@ func VerifySigningRoot(obj fssz.HashRoot, pub, signature, domain []byte) error {
} }
// VerifyBlockSigningRoot verifies the signing root of a block given it's public key, signature and domain. // VerifyBlockSigningRoot verifies the signing root of a block given it's public key, signature and domain.
func VerifyBlockSigningRoot(blk *ethpb.BeaconBlock, pub, signature, domain []byte) error { func VerifyBlockSigningRoot(pub, signature, domain []byte, rootFunc func() ([32]byte, error)) error {
set, err := BlockSignatureSet(blk, pub, signature, domain) set, err := BlockSignatureSet(pub, signature, domain, rootFunc)
if err != nil { if err != nil {
return err return err
} }
@@ -120,13 +120,13 @@ func VerifyBlockSigningRoot(blk *ethpb.BeaconBlock, pub, signature, domain []byt
// BlockSignatureSet retrieves the relevant signature, message and pubkey data from a block and collating it // BlockSignatureSet retrieves the relevant signature, message and pubkey data from a block and collating it
// into a signature set object. // into a signature set object.
func BlockSignatureSet(blk *ethpb.BeaconBlock, pub, signature, domain []byte) (*bls.SignatureSet, error) { func BlockSignatureSet(pub, signature, domain []byte, rootFunc func() ([32]byte, error)) (*bls.SignatureSet, error) {
publicKey, err := bls.PublicKeyFromBytes(pub) publicKey, err := bls.PublicKeyFromBytes(pub)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not convert bytes to public key") return nil, errors.Wrap(err, "could not convert bytes to public key")
} }
// utilize custom block hashing function // utilize custom block hashing function
root, err := signingData(blk.HashTreeRoot, domain) root, err := signingData(rootFunc, domain)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not compute signing root") return nil, errors.Wrap(err, "could not compute signing root")
} }

View File

@@ -184,12 +184,12 @@ func ProcessBlockNoVerifyAnySig(
traceutil.AnnotateError(span, err) traceutil.AnnotateError(span, err)
return nil, nil, errors.Wrap(err, "could not process block header") return nil, nil, errors.Wrap(err, "could not process block header")
} }
bSet, err := b.BlockSignatureSet(state, signed) bSet, err := b.BlockSignatureSet(state, blk.ProposerIndex, signed.Signature, blk.HashTreeRoot)
if err != nil { if err != nil {
traceutil.AnnotateError(span, err) traceutil.AnnotateError(span, err)
return nil, nil, errors.Wrap(err, "could not retrieve block signature set") return nil, nil, errors.Wrap(err, "could not retrieve block signature set")
} }
rSet, err := b.RandaoSignatureSet(state, signed.Block.Body) rSet, err := b.RandaoSignatureSet(state, signed.Block.Body.RandaoReveal)
if err != nil { if err != nil {
traceutil.AnnotateError(span, err) traceutil.AnnotateError(span, err)
return nil, nil, errors.Wrap(err, "could not retrieve randao signature set") return nil, nil, errors.Wrap(err, "could not retrieve randao signature set")

View File

@@ -231,8 +231,8 @@ func (bs *Server) StreamBlocks(req *ethpb.StreamBlocksRequest, stream ethpb.Beac
log.WithError(err).WithField("blockSlot", data.SignedBlock.Block.Slot).Error("Could not get head state") log.WithError(err).WithField("blockSlot", data.SignedBlock.Block.Slot).Error("Could not get head state")
continue continue
} }
signed := data.SignedBlock
if err := blocks.VerifyBlockSignature(headState, data.SignedBlock); err != nil { if err := blocks.VerifyBlockSignature(headState, signed.Block.ProposerIndex, signed.Signature, signed.Block.HashTreeRoot); err != nil {
log.WithError(err).WithField("blockSlot", data.SignedBlock.Block.Slot).Error("Could not verify block signature") log.WithError(err).WithField("blockSlot", data.SignedBlock.Block.Slot).Error("Could not verify block signature")
continue continue
} }

View File

@@ -178,7 +178,7 @@ func (s *Service) validateBeaconBlock(ctx context.Context, blk *ethpb.SignedBeac
return err return err
} }
if err := blocks.VerifyBlockSignature(parentState, blk); err != nil { if err := blocks.VerifyBlockSignature(parentState, blk.Block.ProposerIndex, blk.Signature, blk.Block.HashTreeRoot); err != nil {
s.setBadBlock(ctx, blockRoot) s.setBadBlock(ctx, blockRoot)
return err return err
} }