Skip to content

Commit

Permalink
Handle genesis case for blocks/states at slot index (#5224)
Browse files Browse the repository at this point in the history
* Handle highest slot = 0
* TestStore_GenesisState_CanGetHighestBelow
* TestStore_GenesisBlock_CanGetHighestAt
* Merge refs/heads/master into handle-genesis
  • Loading branch information
terencechain authored Mar 27, 2020
1 parent c4c9a84 commit 1a0a399
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 6 deletions.
2 changes: 1 addition & 1 deletion beacon-chain/blockchain/process_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"encoding/hex"
"fmt"

"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/flags"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/shared/attestationutil"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/blockchain/receive_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/hex"

"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
Expand All @@ -14,6 +13,7 @@ import (
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/traceutil"
"github.com/sirupsen/logrus"
Expand Down
9 changes: 9 additions & 0 deletions beacon-chain/db/kv/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,15 @@ func (k *Store) blocksAtSlotBitfieldIndex(ctx context.Context, tx *bolt.Tx, inde

highestSlot := index - 1
highestSlot = int(math.Max(0, float64(highestSlot)))

if highestSlot == 0 {
gBlock, err := k.GenesisBlock(ctx)
if err != nil {
return nil, err
}
return []*ethpb.SignedBeaconBlock{gBlock}, nil
}

f := filters.NewFilter().SetStartSlot(uint64(highestSlot)).SetEndSlot(uint64(highestSlot))

keys, err := getBlockRootsByFilter(ctx, tx, f)
Expand Down
35 changes: 35 additions & 0 deletions beacon-chain/db/kv/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,41 @@ func TestStore_SaveBlock_CanGetHighestAt(t *testing.T) {
}
}

func TestStore_GenesisBlock_CanGetHighestAt(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
ctx := context.Background()

genesisBlock := &ethpb.SignedBeaconBlock{Block: &ethpb.BeaconBlock{}}
genesisRoot, _ := ssz.HashTreeRoot(genesisBlock.Block)
db.SaveGenesisBlockRoot(ctx, genesisRoot)
db.SaveBlock(ctx, genesisBlock)
block1 := &ethpb.SignedBeaconBlock{Block: &ethpb.BeaconBlock{Slot: 1}}
db.SaveBlock(ctx, block1)

highestAt, err := db.HighestSlotBlocksBelow(ctx, 2)
if err != nil {
t.Fatal(err)
}
if !proto.Equal(block1, highestAt[0]) {
t.Errorf("Wanted %v, received %v", block1, highestAt)
}
highestAt, err = db.HighestSlotBlocksBelow(ctx, 1)
if err != nil {
t.Fatal(err)
}
if !proto.Equal(genesisBlock, highestAt[0]) {
t.Errorf("Wanted %v, received %v", genesisBlock, highestAt)
}
highestAt, err = db.HighestSlotBlocksBelow(ctx, 0)
if err != nil {
t.Fatal(err)
}
if !proto.Equal(genesisBlock, highestAt[0]) {
t.Errorf("Wanted %v, received %v", genesisBlock, highestAt)
}
}

func TestStore_SaveBlocks_CanGetHighest(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
Expand Down
9 changes: 9 additions & 0 deletions beacon-chain/db/kv/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,15 @@ func (k *Store) statesAtSlotBitfieldIndex(ctx context.Context, tx *bolt.Tx, inde

highestSlot := index - 1
highestSlot = int(math.Max(0, float64(highestSlot)))

if highestSlot == 0 {
gState, err := k.GenesisState(ctx)
if err != nil {
return nil, err
}
return []*state.BeaconState{gState}, nil
}

f := filters.NewFilter().SetStartSlot(uint64(highestSlot)).SetEndSlot(uint64(highestSlot))

keys, err := getBlockRootsByFilter(ctx, tx, f)
Expand Down
51 changes: 51 additions & 0 deletions beacon-chain/db/kv/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,54 @@ func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) {
t.Errorf("Did not retrieve saved state: %v != %v", highest, s2)
}
}

func TestStore_GenesisState_CanGetHighestBelow(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)

s := &pb.BeaconState{}
genesisState, err := state.InitializeFromProto(s)
if err != nil {
t.Fatal(err)
}
genesisRoot := [32]byte{'a'}
db.SaveGenesisBlockRoot(context.Background(), genesisRoot)
db.SaveState(context.Background(), genesisState, genesisRoot)

s0 := &pb.BeaconState{Slot: 1}
b := &ethpb.SignedBeaconBlock{Block: &ethpb.BeaconBlock{Slot: 1}}
r, _ := ssz.HashTreeRoot(b.Block)
if err := db.SaveBlock(context.Background(), b); err != nil {
t.Fatal(err)
}
st, err := state.InitializeFromProto(s0)
if err != nil {
t.Fatal(err)
}
if err := db.SaveState(context.Background(), st, r); err != nil {
t.Fatal(err)
}

highest, err := db.HighestSlotStatesBelow(context.Background(), 2)
if err != nil {
t.Fatal(err)
}
if !proto.Equal(highest[0].InnerStateUnsafe(), s0) {
t.Errorf("Did not retrieve saved state: %v != %v", highest, s0)
}

highest, err = db.HighestSlotStatesBelow(context.Background(), 1)
if err != nil {
t.Fatal(err)
}
if !proto.Equal(highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe()) {
t.Errorf("Did not retrieve saved state: %v != %v", highest, s0)
}
highest, err = db.HighestSlotStatesBelow(context.Background(), 0)
if err != nil {
t.Fatal(err)
}
if !proto.Equal(highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe()) {
t.Errorf("Did not retrieve saved state: %v != %v", highest, s0)
}
}
2 changes: 1 addition & 1 deletion beacon-chain/rpc/beacon/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"strconv"

"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
ptypes "github.com/gogo/protobuf/types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters"
"github.com/prysmaticlabs/prysm/beacon-chain/flags"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/pagination"
"google.golang.org/grpc/codes"
Expand Down
4 changes: 2 additions & 2 deletions slasher/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package rpc
import (
"context"

"github.com/prysmaticlabs/prysm/slasher/db"
log "github.com/sirupsen/logrus"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
slashpb "github.com/prysmaticlabs/prysm/proto/slashing"
"github.com/prysmaticlabs/prysm/slasher/db"
"github.com/prysmaticlabs/prysm/slasher/detection"
log "github.com/sirupsen/logrus"
"go.opencensus.io/trace"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down
2 changes: 1 addition & 1 deletion slasher/rpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"fmt"
"net"

"github.com/prysmaticlabs/prysm/slasher/db"
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
slashpb "github.com/prysmaticlabs/prysm/proto/slashing"
"github.com/prysmaticlabs/prysm/shared/traceutil"
"github.com/prysmaticlabs/prysm/slasher/db"
"github.com/prysmaticlabs/prysm/slasher/detection"
log "github.com/sirupsen/logrus"
"go.opencensus.io/plugin/ocgrpc"
Expand Down

0 comments on commit 1a0a399

Please sign in to comment.