Remove VerifySignatures Flag From ProposerSlashings and Transfers (#3009)

* Remove verifySignatures from ProposerSlashings

* Remove flag from process transfers

* resolve all conflicts

* fix more references to old pbs
This commit is contained in:
Ivan Martinez 2019-07-22 15:25:32 -04:00 committed by Raul Jordan
parent 8cfbf0309d
commit 1d71398b7c
6 changed files with 168 additions and 94 deletions

View File

@ -267,7 +267,6 @@ func ProcessRandao(
func ProcessProposerSlashings(
beaconState *pb.BeaconState,
body *ethpb.BeaconBlockBody,
verifySignatures bool,
) (*pb.BeaconState, error) {
var err error
for idx, slashing := range body.ProposerSlashings {
@ -275,7 +274,7 @@ func ProcessProposerSlashings(
return nil, fmt.Errorf("invalid proposer index given in slashing %d", slashing.ProposerIndex)
}
proposer := beaconState.Validators[slashing.ProposerIndex]
if err = verifyProposerSlashing(beaconState, proposer, slashing, verifySignatures); err != nil {
if err = verifyProposerSlashing(beaconState, proposer, slashing); err != nil {
return nil, fmt.Errorf("could not verify proposer slashing %d: %v", idx, err)
}
beaconState, err = v.SlashValidator(
@ -293,7 +292,6 @@ func verifyProposerSlashing(
beaconState *pb.BeaconState,
proposer *ethpb.Validator,
slashing *ethpb.ProposerSlashing,
verifySignatures bool,
) error {
headerEpoch1 := helpers.SlotToEpoch(slashing.Header_1.Slot)
headerEpoch2 := helpers.SlotToEpoch(slashing.Header_2.Slot)
@ -306,17 +304,13 @@ func verifyProposerSlashing(
if !helpers.IsSlashableValidator(proposer, helpers.CurrentEpoch(beaconState)) {
return fmt.Errorf("validator with key %#x is not slashable", proposer.PublicKey)
}
if verifySignatures {
// Using headerEpoch1 here because both of the headers should have the same epoch.
domain := helpers.Domain(beaconState, headerEpoch1, params.BeaconConfig().DomainBeaconProposer)
headers := append([]*ethpb.BeaconBlockHeader{slashing.Header_1}, slashing.Header_2)
for _, header := range headers {
if err := verifySigningRoot(header, proposer.PublicKey, header.Signature, domain); err != nil {
return fmt.Errorf("could not verify beacon block header: %v", err)
}
// Using headerEpoch1 here because both of the headers should have the same epoch.
domain := helpers.Domain(beaconState, headerEpoch1, params.BeaconConfig().DomainBeaconProposer)
headers := append([]*ethpb.BeaconBlockHeader{slashing.Header_1}, slashing.Header_2)
for _, header := range headers {
if err := verifySigningRoot(header, proposer.PublicKey, header.Signature, domain); err != nil {
return fmt.Errorf("could not verify beacon block header: %v", err)
}
return nil
}
return nil
}
@ -1035,12 +1029,11 @@ func verifyExit(beaconState *pb.BeaconState, exit *ethpb.VoluntaryExit, verifySi
func ProcessTransfers(
beaconState *pb.BeaconState,
body *ethpb.BeaconBlockBody,
verifySignatures bool,
) (*pb.BeaconState, error) {
transfers := body.Transfers
for idx, transfer := range transfers {
if err := verifyTransfer(beaconState, transfer, verifySignatures); err != nil {
if err := verifyTransfer(beaconState, transfer); err != nil {
return nil, fmt.Errorf("could not verify transfer %d: %v", idx, err)
}
// Process the transfer between accounts.
@ -1071,7 +1064,7 @@ func ProcessTransfers(
return beaconState, nil
}
func verifyTransfer(beaconState *pb.BeaconState, transfer *ethpb.Transfer, verifySignatures bool) error {
func verifyTransfer(beaconState *pb.BeaconState, transfer *ethpb.Transfer) error {
if transfer.SenderIndex > uint64(len(beaconState.Validators)) {
return errors.New("transfer sender index out of bounds in validator registry")
}
@ -1114,11 +1107,9 @@ func verifyTransfer(beaconState *pb.BeaconState, transfer *ethpb.Transfer, verif
if !bytes.Equal(sender.WithdrawalCredentials, buf) {
return fmt.Errorf("invalid public key, expected %v, received %v", buf, sender.WithdrawalCredentials)
}
if verifySignatures {
domain := helpers.Domain(beaconState, helpers.CurrentEpoch(beaconState), params.BeaconConfig().DomainTransfer)
if err := verifySigningRoot(transfer, transfer.SenderWithdrawalPublicKey, transfer.Signature, domain); err != nil {
return fmt.Errorf("could not verify voluntary exit signature: %v", err)
}
domain := helpers.Domain(beaconState, helpers.CurrentEpoch(beaconState), params.BeaconConfig().DomainTransfer)
if err := verifySigningRoot(transfer, transfer.SenderWithdrawalPublicKey, transfer.Signature, domain); err != nil {
return fmt.Errorf("could not verify transfer signature: %v", err)
}
return nil
}

View File

@ -469,7 +469,6 @@ func TestProcessProposerSlashings_UnmatchedHeaderEpochs(t *testing.T) {
if _, err := blocks.ProcessProposerSlashings(
beaconState,
block.Body,
false,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
@ -501,10 +500,8 @@ func TestProcessProposerSlashings_SameHeaders(t *testing.T) {
}
want := "expected slashing headers to differ"
if _, err := blocks.ProcessProposerSlashings(
beaconState,
block.Body,
false,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
@ -551,7 +548,6 @@ func TestProcessProposerSlashings_ValidatorNotSlashable(t *testing.T) {
if _, err := blocks.ProcessProposerSlashings(
beaconState,
block.Body,
false,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
@ -576,39 +572,68 @@ func TestProcessProposerSlashings_AppliesCorrectStatus(t *testing.T) {
validatorBalances[i] = params.BeaconConfig().MaxEffectiveBalance
}
slashings := []*ethpb.ProposerSlashing{
{
ProposerIndex: 1,
Header_1: &ethpb.BeaconBlockHeader{
Slot: 0,
Signature: []byte("A"),
},
Header_2: &ethpb.BeaconBlockHeader{
Slot: 0,
Signature: []byte("B"),
},
},
}
currentSlot := uint64(0)
beaconState := &pb.BeaconState{
Validators: validators,
Slot: currentSlot,
Balances: validatorBalances,
Validators: validators,
Slot: currentSlot,
Balances: validatorBalances,
Fork: &pb.Fork{
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
Epoch: 0,
},
Slashings: make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector),
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
}
domain := helpers.Domain(
beaconState,
helpers.CurrentEpoch(beaconState),
params.BeaconConfig().DomainBeaconProposer,
)
privKey, err := bls.RandKey(rand.Reader)
if err != nil {
t.Errorf("Could not generate random private key: %v", err)
}
header1 := &ethpb.BeaconBlockHeader{
Slot: 0,
StateRoot: []byte("A"),
}
signingRoot, err := ssz.SigningRoot(header1)
if err != nil {
t.Errorf("Could not get signing root of beacon block header: %v", err)
}
header1.Signature = privKey.Sign(signingRoot[:], domain).Marshal()[:]
header2 := &ethpb.BeaconBlockHeader{
Slot: 0,
StateRoot: []byte("B"),
}
signingRoot, err = ssz.SigningRoot(header2)
if err != nil {
t.Errorf("Could not get signing root of beacon block header: %v", err)
}
header2.Signature = privKey.Sign(signingRoot[:], domain).Marshal()[:]
slashings := []*ethpb.ProposerSlashing{
{
ProposerIndex: 1,
Header_1: header1,
Header_2: header2,
},
}
beaconState.Validators[1].PublicKey = privKey.PublicKey().Marshal()[:]
block := &ethpb.BeaconBlock{
Body: &ethpb.BeaconBlockBody{
ProposerSlashings: slashings,
},
}
newState, err := blocks.ProcessProposerSlashings(
beaconState,
block.Body,
false,
)
newState, err := blocks.ProcessProposerSlashings(beaconState, block.Body)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
@ -1683,7 +1708,6 @@ func TestProcessBeaconTransfers_NotEnoughSenderIndexBalance(t *testing.T) {
if _, err := blocks.ProcessTransfers(
state,
block.Body,
false,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
@ -1725,7 +1749,6 @@ func TestProcessBeaconTransfers_FailsVerification(t *testing.T) {
if _, err := blocks.ProcessTransfers(
state,
block.Body,
false,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
@ -1744,7 +1767,6 @@ func TestProcessBeaconTransfers_FailsVerification(t *testing.T) {
if _, err := blocks.ProcessTransfers(
state,
block.Body,
false,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
@ -1763,7 +1785,6 @@ func TestProcessBeaconTransfers_FailsVerification(t *testing.T) {
if _, err := blocks.ProcessTransfers(
state,
block.Body,
false,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
@ -1787,7 +1808,6 @@ func TestProcessBeaconTransfers_FailsVerification(t *testing.T) {
if _, err := blocks.ProcessTransfers(
state,
block.Body,
false,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
@ -1813,40 +1833,53 @@ func TestProcessBeaconTransfers_OK(t *testing.T) {
}
state := &pb.BeaconState{
Validators: validators,
Slot: 0,
Balances: validatorBalances,
Validators: validators,
Slot: 0,
Balances: validatorBalances,
Fork: &pb.Fork{
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
},
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
Slashings: make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector),
ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
}
transfers := []*ethpb.Transfer{
{
SenderIndex: 0,
RecipientIndex: 1,
Fee: params.BeaconConfig().MinDepositAmount,
Amount: params.BeaconConfig().MinDepositAmount,
Slot: state.Slot,
SenderWithdrawalPublicKey: []byte("A"),
},
transfer := &ethpb.Transfer{
SenderIndex: 0,
RecipientIndex: 1,
Fee: params.BeaconConfig().MinDepositAmount,
Amount: params.BeaconConfig().MinDepositAmount,
Slot: state.Slot,
}
priv, err := bls.RandKey(rand.Reader)
if err != nil {
t.Fatal(err)
}
pubKey := priv.PublicKey().Marshal()[:]
transfer.SenderWithdrawalPublicKey = pubKey
state.Validators[transfer.SenderIndex].PublicKey = pubKey
signingRoot, err := ssz.SigningRoot(transfer)
if err != nil {
t.Fatalf("Failed to get signing root of block: %v", err)
}
epoch := helpers.CurrentEpoch(state)
dt := helpers.Domain(state, epoch, params.BeaconConfig().DomainTransfer)
transferSig := priv.Sign(signingRoot[:], dt)
transfer.Signature = transferSig.Marshal()[:]
block := &ethpb.BeaconBlock{
Body: &ethpb.BeaconBlockBody{
Transfers: transfers,
Transfers: []*ethpb.Transfer{transfer},
},
}
buf := []byte{params.BeaconConfig().BLSWithdrawalPrefixByte}
pubKey := []byte("A")
hashed := hashutil.Hash(pubKey)
buf = append(buf, hashed[:][1:]...)
state.Validators[0].WithdrawalCredentials = buf
state.Validators[0].ActivationEligibilityEpoch = params.BeaconConfig().FarFutureEpoch
newState, err := blocks.ProcessTransfers(
state,
block.Body,
false,
)
newState, err := blocks.ProcessTransfers(state, block.Body)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

View File

@ -39,7 +39,7 @@ func runProposerSlashingTest(t *testing.T, filename string) {
body := &ethpb.BeaconBlockBody{ProposerSlashings: []*ethpb.ProposerSlashing{tt.ProposerSlashing}}
postState, err := blocks.ProcessProposerSlashings(tt.Pre, body, true)
postState, err := blocks.ProcessProposerSlashings(tt.Pre, body)
// Note: This doesn't test anything worthwhile. It essentially tests
// that *any* error has occurred, not any specific error.
if tt.Post == nil {

View File

@ -39,7 +39,7 @@ func runTransferTest(t *testing.T, filename string) {
body := &ethpb.BeaconBlockBody{Transfers: []*ethpb.Transfer{tt.Transfer}}
postState, err := blocks.ProcessTransfers(tt.Pre, body, true)
postState, err := blocks.ProcessTransfers(tt.Pre, body)
// Note: This doesn't test anything worthwhile. It essentially tests
// that *any* error has occurred, not any specific error.
if tt.Post == nil {

View File

@ -253,7 +253,7 @@ func ProcessOperations(
transferSet[h] = true
}
state, err := b.ProcessProposerSlashings(state, body, config.VerifySignatures)
state, err := b.ProcessProposerSlashings(state, body)
if err != nil {
return nil, fmt.Errorf("could not process block proposer slashings: %v", err)
}
@ -273,7 +273,7 @@ func ProcessOperations(
if err != nil {
return nil, fmt.Errorf("could not process validator exits: %v", err)
}
state, err = b.ProcessTransfers(state, body, config.VerifySignatures)
state, err = b.ProcessTransfers(state, body)
if err != nil {
return nil, fmt.Errorf("could not process block transfers: %v", err)
}

View File

@ -144,19 +144,44 @@ func TestProcessBlock_IncorrectProcessBlockAttestations(t *testing.T) {
t.Fatal(err)
}
beaconState.Slashings = make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector)
proposerIdx := uint64(3)
domain := helpers.Domain(
beaconState,
helpers.CurrentEpoch(beaconState),
params.BeaconConfig().DomainBeaconProposer,
)
header1 := &ethpb.BeaconBlockHeader{
Slot: 1,
StateRoot: []byte("A"),
}
signingRoot, err := ssz.SigningRoot(header1)
if err != nil {
t.Errorf("Could not get signing root of beacon block header: %v", err)
}
header1.Signature = privKeys[proposerIdx].Sign(signingRoot[:], domain).Marshal()[:]
header2 := &ethpb.BeaconBlockHeader{
Slot: 1,
StateRoot: []byte("B"),
}
signingRoot, err = ssz.SigningRoot(header2)
if err != nil {
t.Errorf("Could not get signing root of beacon block header: %v", err)
}
header2.Signature = privKeys[proposerIdx].Sign(signingRoot[:], domain).Marshal()[:]
proposerSlashings := []*ethpb.ProposerSlashing{
{
ProposerIndex: 3,
Header_1: &ethpb.BeaconBlockHeader{
Slot: 1,
Signature: []byte("A"),
},
Header_2: &ethpb.BeaconBlockHeader{
Slot: 1,
Signature: []byte("B"),
},
ProposerIndex: proposerIdx,
Header_1: header1,
Header_2: header2,
},
}
beaconState.Validators[proposerIdx].PublicKey = privKeys[proposerIdx].PublicKey().Marshal()[:]
attesterSlashings := []*ethpb.AttesterSlashing{
{
Attestation_1: &ethpb.IndexedAttestation{
@ -352,7 +377,7 @@ func TestProcessBlock_IncorrectProcessExits(t *testing.T) {
}
func TestProcessBlock_PassesProcessingConditions(t *testing.T) {
deposits, _ := testutil.SetupInitialDeposits(t, params.BeaconConfig().MinGenesisActiveValidatorCount/8, false)
deposits, privKeys := testutil.SetupInitialDeposits(t, params.BeaconConfig().MinGenesisActiveValidatorCount/8, true)
beaconState, err := state.GenesisBeaconState(deposits, uint64(0), nil)
if err != nil {
t.Fatal(err)
@ -368,19 +393,44 @@ func TestProcessBlock_PassesProcessingConditions(t *testing.T) {
BodyRoot: bodyRoot[:],
}
beaconState.Slashings = make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector)
proposerIdx := uint64(3)
domain := helpers.Domain(
beaconState,
helpers.CurrentEpoch(beaconState),
params.BeaconConfig().DomainBeaconProposer,
)
header1 := &ethpb.BeaconBlockHeader{
Slot: 1,
StateRoot: []byte("A"),
}
signingRoot, err := ssz.SigningRoot(header1)
if err != nil {
t.Errorf("Could not get signing root of beacon block header: %v", err)
}
header1.Signature = privKeys[proposerIdx].Sign(signingRoot[:], domain).Marshal()[:]
header2 := &ethpb.BeaconBlockHeader{
Slot: 1,
StateRoot: []byte("B"),
}
signingRoot, err = ssz.SigningRoot(header2)
if err != nil {
t.Errorf("Could not get signing root of beacon block header: %v", err)
}
header2.Signature = privKeys[proposerIdx].Sign(signingRoot[:], domain).Marshal()[:]
proposerSlashings := []*ethpb.ProposerSlashing{
{
ProposerIndex: 3,
Header_1: &ethpb.BeaconBlockHeader{
Slot: 1,
Signature: []byte("A"),
},
Header_2: &ethpb.BeaconBlockHeader{
Slot: 1,
Signature: []byte("B"),
},
ProposerIndex: proposerIdx,
Header_1: header1,
Header_2: header2,
},
}
beaconState.Validators[proposerIdx].PublicKey = privKeys[proposerIdx].PublicKey().Marshal()[:]
attesterSlashings := []*ethpb.AttesterSlashing{
{
Attestation_1: &ethpb.IndexedAttestation{