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

Refactor signature function's argument #8773

Merged
merged 8 commits into from
Apr 15, 2021
2 changes: 1 addition & 1 deletion beacon-chain/core/blocks/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ProcessBlockHeader(
}

// 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
}

Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/blocks/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func TestBlockSignatureSet_OK(t *testing.T) {
validators[proposerIdx].PublicKey = priv.PublicKey().Marshal()
err = state.UpdateValidatorAtIndex(proposerIdx, validators[proposerIdx])
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)

verified, err := set.Verify()
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/blocks/randao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
verified, err := set.Verify()
require.NoError(t, err)
Expand Down
22 changes: 14 additions & 8 deletions beacon-chain/core/blocks/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,45 +60,51 @@ func verifySignature(signedData, pub, signature, domain []byte) error {
}

// 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())
domain, err := helpers.Domain(beaconState.Fork(), currentEpoch, params.BeaconConfig().DomainBeaconProposer, beaconState.GenesisValidatorRoot())
if err != nil {
return err
}
proposer, err := beaconState.ValidatorAtIndex(block.Block.ProposerIndex)
proposer, err := beaconState.ValidatorAtIndex(proposerIndex)
if err != nil {
return err
}
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.
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())
domain, err := helpers.Domain(beaconState.Fork(), currentEpoch, params.BeaconConfig().DomainBeaconProposer, beaconState.GenesisValidatorRoot())
if err != nil {
return nil, err
}
proposer, err := beaconState.ValidatorAtIndex(block.Block.ProposerIndex)
proposer, err := beaconState.ValidatorAtIndex(proposerIndex)
if err != nil {
return nil, err
}
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
// from a block and its corresponding state.
func RandaoSignatureSet(beaconState iface.ReadOnlyBeaconState,
body *ethpb.BeaconBlockBody,
reveal []byte,
) (*bls.SignatureSet, error) {
buf, proposerPub, domain, err := randaoSigningData(beaconState)
if err != nil {
return nil, err
}
set, err := signatureSet(buf, proposerPub, body.RandaoReveal, domain)
set, err := signatureSet(buf, proposerPub, reveal, domain)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/core/helpers/signing_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
func VerifyBlockSigningRoot(blk *ethpb.BeaconBlock, pub, signature, domain []byte) error {
set, err := BlockSignatureSet(blk, pub, signature, domain)
func VerifyBlockSigningRoot(pub, signature, domain []byte, rootFunc func() ([32]byte, error)) error {
set, err := BlockSignatureSet(pub, signature, domain, rootFunc)
if err != nil {
return err
}
Expand All @@ -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
// 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)
if err != nil {
return nil, errors.Wrap(err, "could not convert bytes to public key")
}
// utilize custom block hashing function
root, err := signingData(blk.HashTreeRoot, domain)
root, err := signingData(rootFunc, domain)
if err != nil {
return nil, errors.Wrap(err, "could not compute signing root")
}
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/core/state/transition_no_verify_sig.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ func ProcessBlockNoVerifyAnySig(
traceutil.AnnotateError(span, err)
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 {
traceutil.AnnotateError(span, err)
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 {
traceutil.AnnotateError(span, err)
return nil, nil, errors.Wrap(err, "could not retrieve randao signature set")
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/rpc/beacon/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
continue
}

if err := blocks.VerifyBlockSignature(headState, data.SignedBlock); err != nil {
signed := data.SignedBlock
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")
continue
}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/sync/validate_beacon_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (s *Service) validateBeaconBlock(ctx context.Context, blk *ethpb.SignedBeac
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)
return err
}
Expand Down