2020-07-06 22:27:42 +00:00
|
|
|
package blocks_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-08-08 19:06:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-07-06 22:27:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProcessRandao_IncorrectProposerFailsVerification(t *testing.T) {
|
|
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, 100)
|
|
|
|
// We fetch the proposer's index as that is whom the RANDAO will be verified against.
|
|
|
|
proposerIdx, err := helpers.BeaconProposerIndex(beaconState)
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
epoch := uint64(0)
|
|
|
|
buf := make([]byte, 32)
|
|
|
|
binary.LittleEndian.PutUint64(buf, epoch)
|
|
|
|
domain, err := helpers.Domain(beaconState.Fork(), epoch, params.BeaconConfig().DomainRandao, beaconState.GenesisValidatorRoot())
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-27 18:13:32 +00:00
|
|
|
root, err := (&pb.SigningData{ObjectRoot: buf, Domain: domain}).HashTreeRoot()
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
// We make the previous validator's index sign the message instead of the proposer.
|
|
|
|
epochSignature := privKeys[proposerIdx-1].Sign(root[:])
|
|
|
|
block := ðpb.BeaconBlock{
|
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
RandaoReveal: epochSignature.Marshal(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
want := "block randao: signature did not verify"
|
2020-08-08 19:06:04 +00:00
|
|
|
_, err = blocks.ProcessRandao(beaconState, block.Body)
|
|
|
|
assert.ErrorContains(t, want, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessRandao_SignatureVerifiesAndUpdatesLatestStateMixes(t *testing.T) {
|
|
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, 100)
|
|
|
|
|
|
|
|
epoch := helpers.CurrentEpoch(beaconState)
|
|
|
|
epochSignature, err := testutil.RandaoReveal(beaconState, epoch, privKeys)
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
|
|
|
|
block := ðpb.BeaconBlock{
|
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
RandaoReveal: epochSignature,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
newState, err := blocks.ProcessRandao(
|
|
|
|
beaconState,
|
|
|
|
block.Body,
|
|
|
|
)
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err, "Unexpected error processing block randao")
|
2020-07-06 22:27:42 +00:00
|
|
|
currentEpoch := helpers.CurrentEpoch(beaconState)
|
|
|
|
mix := newState.RandaoMixes()[currentEpoch%params.BeaconConfig().EpochsPerHistoricalVector]
|
|
|
|
|
|
|
|
if bytes.Equal(mix, params.BeaconConfig().ZeroHash[:]) {
|
|
|
|
t.Errorf(
|
|
|
|
"Expected empty signature to be overwritten by randao reveal, received %v",
|
|
|
|
params.BeaconConfig().EmptySignature,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRandaoSignatureSet_OK(t *testing.T) {
|
|
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, 100)
|
|
|
|
|
|
|
|
epoch := helpers.CurrentEpoch(beaconState)
|
|
|
|
epochSignature, err := testutil.RandaoReveal(beaconState, epoch, privKeys)
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
|
|
|
|
block := ðpb.BeaconBlock{
|
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
RandaoReveal: epochSignature,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
set, _, err := blocks.RandaoSignatureSet(beaconState, block.Body)
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
verified, err := set.Verify()
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, verified, "Unable to verify randao signature set")
|
2020-07-06 22:27:42 +00:00
|
|
|
}
|