Skip to content

Commit

Permalink
Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvanloon committed Jul 29, 2020
2 parents 16ceea5 + cbd7311 commit 9168900
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 380 deletions.
1 change: 1 addition & 0 deletions beacon-chain/core/blocks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ go_library(
"//shared/attestationutil:go_default_library",
"//shared/bls:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/depositutil:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/mathutil:go_default_library",
"//shared/params:go_default_library",
Expand Down
26 changes: 2 additions & 24 deletions beacon-chain/core/blocks/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/depositutil"
"github.com/prysmaticlabs/prysm/shared/mathutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/trieutil"
Expand Down Expand Up @@ -218,30 +219,7 @@ func verifyDeposit(beaconState *stateTrie.BeaconState, deposit *ethpb.Deposit) e

// Deprecated: This method uses deprecated ssz.SigningRoot.
func verifyDepositDataSigningRoot(obj *ethpb.Deposit_Data, pub []byte, signature []byte, domain []byte) error {
publicKey, err := bls.PublicKeyFromBytes(pub)
if err != nil {
return errors.Wrap(err, "could not convert bytes to public key")
}
sig, err := bls.SignatureFromBytes(signature)
if err != nil {
return errors.Wrap(err, "could not convert bytes to signature")
}
root, err := ssz.SigningRoot(obj)
if err != nil {
return errors.Wrap(err, "could not get signing root")
}
signingData := &pb.SigningData{
ObjectRoot: root[:],
Domain: domain,
}
ctrRoot, err := signingData.HashTreeRoot()
if err != nil {
return errors.Wrap(err, "could not get container root")
}
if !sig.Verify(publicKey, ctrRoot[:]) {
return helpers.ErrSigFailedToVerify
}
return nil
return depositutil.VerifyDepositSignature(obj, domain)
}

func verifyDepositDataWithDomain(ctx context.Context, deps []*ethpb.Deposit, domain []byte) error {
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/rpc/validator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ go_library(
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bls:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/depositutil:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
Expand Down
18 changes: 16 additions & 2 deletions beacon-chain/rpc/validator/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/depositutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/traceutil"
"go.opencensus.io/trace"
Expand Down Expand Up @@ -159,11 +160,24 @@ func (vs *Server) validatorStatus(
log.Warn("Not connected to ETH1. Cannot determine validator ETH1 deposit block number")
return resp, nonExistentIndex
}
_, eth1BlockNumBigInt := vs.DepositFetcher.DepositByPubkey(ctx, pubKey)
deposit, eth1BlockNumBigInt := vs.DepositFetcher.DepositByPubkey(ctx, pubKey)
if eth1BlockNumBigInt == nil { // No deposit found in ETH1.
return resp, nonExistentIndex
}

domain, err := helpers.ComputeDomain(
params.BeaconConfig().DomainDeposit,
nil, /*forkVersion*/
nil, /*genesisValidatorsRoot*/
)
if err != nil {
log.Warn("Could not compute domain")
return resp, nonExistentIndex
}
if err := depositutil.VerifyDepositSignature(deposit.Data, domain); err != nil {
resp.Status = ethpb.ValidatorStatus_INVALID
log.Warn("Invalid Eth1 deposit")
return resp, nonExistentIndex
}
// Mark a validator as DEPOSITED if their deposit is visible.
resp.Status = ethpb.ValidatorStatus_DEPOSITED

Expand Down
104 changes: 54 additions & 50 deletions beacon-chain/rpc/validator/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,12 @@ import (
func TestValidatorStatus_DepositedEth1(t *testing.T) {
db, _ := dbutil.SetupDB(t)
ctx := context.Background()

pubKey1 := pubKey(1)
depData := &ethpb.Deposit_Data{
PublicKey: pubKey1,
Signature: []byte("hi"),
WithdrawalCredentials: []byte("hey"),
}
deposit := &ethpb.Deposit{
Data: depData,
deposits, _, err := testutil.DeterministicDepositsAndKeys(1)
if err != nil {
t.Fatalf("Could not generate deposits and keys: %v", err)
}
deposit := deposits[0]
pubKey1 := deposit.Data.PublicKey
depositTrie, err := trieutil.NewTrie(int(params.BeaconConfig().DepositContractTreeDepth))
require.NoError(t, err, "Could not setup deposit trie")
depositCache, err := depositcache.NewDepositCache()
Expand Down Expand Up @@ -471,7 +467,8 @@ func TestActivationStatus_OK(t *testing.T) {
db, _ := dbutil.SetupDB(t)
ctx := context.Background()

pubKeys := [][]byte{pubKey(1), pubKey(2), pubKey(3), pubKey(4)}
deposits, _, err := testutil.DeterministicDepositsAndKeys(4)
pubKeys := [][]byte{deposits[0].Data.PublicKey, deposits[1].Data.PublicKey, deposits[2].Data.PublicKey, deposits[3].Data.PublicKey}
stateObj, err := stateTrie.InitializeFromProtoUnsafe(&pbp2p.BeaconState{
Slot: 4000,
Validators: []*ethpb.Validator{
Expand All @@ -495,32 +492,15 @@ func TestActivationStatus_OK(t *testing.T) {
block := testutil.NewBeaconBlock()
genesisRoot, err := stateutil.BlockRoot(block.Block)
require.NoError(t, err, "Could not get signing root")
depData := &ethpb.Deposit_Data{
PublicKey: pubKey(1),
Signature: []byte("hi"),
WithdrawalCredentials: []byte("hey"),
Amount: 10,
}

dep := &ethpb.Deposit{
Data: depData,
}
dep := deposits[0]
depositTrie, err := trieutil.NewTrie(int(params.BeaconConfig().DepositContractTreeDepth))
require.NoError(t, err, "Could not setup deposit trie")
depositCache, err := depositcache.NewDepositCache()
require.NoError(t, err)

depositCache.InsertDeposit(ctx, dep, 10 /*blockNum*/, 0, depositTrie.Root())
depData = &ethpb.Deposit_Data{
PublicKey: pubKey(3),
Signature: []byte("hi"),
WithdrawalCredentials: []byte("hey"),
Amount: 10,
}

dep = &ethpb.Deposit{
Data: depData,
}
dep = deposits[2]
depositTrie.Insert(dep.Data.Signature, 15)
depositCache.InsertDeposit(context.Background(), dep, 0, 0, depositTrie.Root())

Expand Down Expand Up @@ -796,7 +776,8 @@ func TestMultipleValidatorStatus_Pubkeys(t *testing.T) {
db, _ := dbutil.SetupDB(t)
ctx := context.Background()

pubKeys := [][]byte{pubKey(1), pubKey(2), pubKey(3), pubKey(4)}
deposits, _, err := testutil.DeterministicDepositsAndKeys(4)
pubKeys := [][]byte{deposits[0].Data.PublicKey, deposits[1].Data.PublicKey, deposits[2].Data.PublicKey, deposits[3].Data.PublicKey}
stateObj, err := stateTrie.InitializeFromProtoUnsafe(&pbp2p.BeaconState{
Slot: 4000,
Validators: []*ethpb.Validator{
Expand All @@ -820,32 +801,14 @@ func TestMultipleValidatorStatus_Pubkeys(t *testing.T) {
block := testutil.NewBeaconBlock()
genesisRoot, err := stateutil.BlockRoot(block.Block)
require.NoError(t, err, "Could not get signing root")
depData := &ethpb.Deposit_Data{
PublicKey: pubKey(1),
Signature: []byte("hi"),
WithdrawalCredentials: []byte("hey"),
Amount: 10,
}

dep := &ethpb.Deposit{
Data: depData,
}
dep := deposits[0]
depositTrie, err := trieutil.NewTrie(int(params.BeaconConfig().DepositContractTreeDepth))
require.NoError(t, err, "Could not setup deposit trie")
depositCache, err := depositcache.NewDepositCache()
require.NoError(t, err)

depositCache.InsertDeposit(ctx, dep, 10 /*blockNum*/, 0, depositTrie.Root())
depData = &ethpb.Deposit_Data{
PublicKey: pubKey(3),
Signature: []byte("hi"),
WithdrawalCredentials: []byte("hey"),
Amount: 10,
}

dep = &ethpb.Deposit{
Data: depData,
}
dep = deposits[2]
depositTrie.Insert(dep.Data.Signature, 15)
depositCache.InsertDeposit(context.Background(), dep, 0, 0, depositTrie.Root())

Expand Down Expand Up @@ -972,3 +935,44 @@ func TestMultipleValidatorStatus_Indices(t *testing.T) {
}
}
}

func TestValidatorStatus_Invalid(t *testing.T) {
db, _ := dbutil.SetupDB(t)
ctx := context.Background()
deposits, _, err := testutil.DeterministicDepositsAndKeys(1)
if err != nil {
t.Fatalf("Could not generate deposits and keys: %v", err)
}
deposit := deposits[0]
pubKey1 := deposit.Data.PublicKey
deposit.Data.Signature = deposit.Data.Signature[1:]
depositTrie, err := trieutil.NewTrie(int(params.BeaconConfig().DepositContractTreeDepth))
require.NoError(t, err, "Could not setup deposit trie")
depositCache, err := depositcache.NewDepositCache()
require.NoError(t, err)

depositCache.InsertDeposit(ctx, deposit, 0 /*blockNum*/, 0, depositTrie.Root())
height := time.Unix(int64(params.BeaconConfig().Eth1FollowDistance), 0).Unix()
p := &mockPOW.POWChain{
TimesByHeight: map[int]uint64{
0: uint64(height),
},
}
stateObj, err := stateTrie.InitializeFromProtoUnsafe(&pbp2p.BeaconState{})
require.NoError(t, err)
vs := &Server{
BeaconDB: db,
DepositFetcher: depositCache,
BlockFetcher: p,
HeadFetcher: &mockChain.ChainService{
State: stateObj,
},
Eth1InfoFetcher: p,
}
req := &ethpb.ValidatorStatusRequest{
PublicKey: pubKey1,
}
resp, err := vs.ValidatorStatus(context.Background(), req)
require.NoError(t, err, "Could not get validator status")
assert.Equal(t, ethpb.ValidatorStatus_INVALID, resp.Status)
}
Loading

0 comments on commit 9168900

Please sign in to comment.