2020-07-06 22:27:42 +00:00
|
|
|
package blocks_test
|
|
|
|
|
|
|
|
import (
|
2020-09-01 01:29:27 +00:00
|
|
|
"context"
|
2020-07-06 22:27:42 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-16 07:45:34 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-09-03 20:10:31 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core"
|
2020-07-06 22:27:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2021-09-27 16:19:20 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/signing"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
2020-07-06 22:27:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProcessRandao_IncorrectProposerFailsVerification(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
beaconState, privKeys := util.DeterministicGenesisState(t, 100)
|
2020-07-06 22:27:42 +00:00
|
|
|
// We fetch the proposer's index as that is whom the RANDAO will be verified against.
|
2021-09-26 15:27:57 +00:00
|
|
|
proposerIdx, err := helpers.BeaconProposerIndex(context.Background(), beaconState)
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-09 10:05:22 +00:00
|
|
|
epoch := types.Epoch(0)
|
2020-07-06 22:27:42 +00:00
|
|
|
buf := make([]byte, 32)
|
2021-02-09 10:05:22 +00:00
|
|
|
binary.LittleEndian.PutUint64(buf, uint64(epoch))
|
2021-09-27 16:19:20 +00:00
|
|
|
domain, err := signing.Domain(beaconState.Fork(), epoch, params.BeaconConfig().DomainRandao, beaconState.GenesisValidatorRoot())
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-29 21:45:17 +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[:])
|
2021-09-23 18:53:46 +00:00
|
|
|
b := util.NewBeaconBlock()
|
2020-09-01 01:29:27 +00:00
|
|
|
b.Block = ðpb.BeaconBlock{
|
2020-07-06 22:27:42 +00:00
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
RandaoReveal: epochSignature.Marshal(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
want := "block randao: signature did not verify"
|
2021-07-06 15:34:05 +00:00
|
|
|
_, err = blocks.ProcessRandao(context.Background(), beaconState, wrapper.WrappedPhase0SignedBeaconBlock(b))
|
2020-08-08 19:06:04 +00:00
|
|
|
assert.ErrorContains(t, want, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessRandao_SignatureVerifiesAndUpdatesLatestStateMixes(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
beaconState, privKeys := util.DeterministicGenesisState(t, 100)
|
2020-07-06 22:27:42 +00:00
|
|
|
|
2021-09-03 20:10:31 +00:00
|
|
|
epoch := core.CurrentEpoch(beaconState)
|
2021-09-23 18:53:46 +00:00
|
|
|
epochSignature, err := util.RandaoReveal(beaconState, epoch, privKeys)
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
b := util.NewBeaconBlock()
|
2020-09-01 01:29:27 +00:00
|
|
|
b.Block = ðpb.BeaconBlock{
|
2020-07-06 22:27:42 +00:00
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
RandaoReveal: epochSignature,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
newState, err := blocks.ProcessRandao(
|
2020-09-01 01:29:27 +00:00
|
|
|
context.Background(),
|
2020-07-06 22:27:42 +00:00
|
|
|
beaconState,
|
2021-07-06 15:34:05 +00:00
|
|
|
wrapper.WrappedPhase0SignedBeaconBlock(b),
|
2020-07-06 22:27:42 +00:00
|
|
|
)
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err, "Unexpected error processing block randao")
|
2021-09-03 20:10:31 +00:00
|
|
|
currentEpoch := core.CurrentEpoch(beaconState)
|
2020-07-06 22:27:42 +00:00
|
|
|
mix := newState.RandaoMixes()[currentEpoch%params.BeaconConfig().EpochsPerHistoricalVector]
|
2021-01-22 15:15:40 +00:00
|
|
|
assert.DeepNotEqual(t, params.BeaconConfig().ZeroHash[:], mix, "Expected empty signature to be overwritten by randao reveal")
|
2020-07-06 22:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRandaoSignatureSet_OK(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
beaconState, privKeys := util.DeterministicGenesisState(t, 100)
|
2020-07-06 22:27:42 +00:00
|
|
|
|
2021-09-03 20:10:31 +00:00
|
|
|
epoch := core.CurrentEpoch(beaconState)
|
2021-09-23 18:53:46 +00:00
|
|
|
epochSignature, err := util.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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-26 15:27:57 +00:00
|
|
|
set, err := blocks.RandaoSignatureSet(context.Background(), beaconState, block.Body.RandaoReveal)
|
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
|
|
|
}
|