Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch the Correct Chain Head During AttestHead #1596

Merged
merged 17 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions beacon-chain/rpc/attester_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,10 @@ func (as *AttesterServer) AttestHead(ctx context.Context, att *pbp2p.Attestation
func (as *AttesterServer) AttestationInfoAtSlot(ctx context.Context, req *pb.AttestationInfoRequest) (*pb.AttestationInfoResponse, error) {
// Set the attestation data's beacon block root = hash_tree_root(head) where head
// is the validator's view of the head block of the beacon chain during the slot.
head, err := as.beaconDB.BlockBySlot(req.Slot)
head, err := as.beaconDB.ChainHead()
if err != nil {
return nil, fmt.Errorf("failed to retrieve chain head: %v", err)
}
if head == nil {
return nil, fmt.Errorf("no block found at slot %d", req.Slot)
}
blockRoot, err := ssz.TreeHash(head)
if err != nil {
return nil, fmt.Errorf("could not tree hash beacon block: %v", err)
Expand Down
41 changes: 11 additions & 30 deletions beacon-chain/rpc/attester_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,16 @@ func TestAttestHead(t *testing.T) {
}
}

func TestAttestationInfoAtSlot_NilHeadAtSlot(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
attesterServer := &AttesterServer{
beaconDB: db,
}
want := "no block found at slot 5"
req := &pb.AttestationInfoRequest{
Slot: 5,
}
if _, err := attesterServer.AttestationInfoAtSlot(context.Background(), req); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %v, received %v", want, err)
}
}

func TestAttestationInfoAtSlot_EpochBoundaryFailure(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
beaconState := &pbp2p.BeaconState{
Slot: 5,
Slot: params.BeaconConfig().GenesisSlot + 3*params.BeaconConfig().GenesisEpoch,
LatestBlockRootHash32S: make([][]byte, 20),
JustifiedEpoch: params.BeaconConfig().GenesisEpoch,
}
block := blocks.NewGenesisBlock([]byte("stateroot"))
block.Slot = 100
block.Slot = params.BeaconConfig().GenesisSlot
attesterServer := &AttesterServer{
beaconDB: db,
}
Expand All @@ -68,9 +54,7 @@ func TestAttestationInfoAtSlot_EpochBoundaryFailure(t *testing.T) {
t.Fatalf("Could not update chain head in test db: %v", err)
}
want := "could not get epoch boundary block"
req := &pb.AttestationInfoRequest{
Slot: 100,
}
req := &pb.AttestationInfoRequest{}
if _, err := attesterServer.AttestationInfoAtSlot(context.Background(), req); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %v, received %v", want, err)
}
Expand Down Expand Up @@ -105,9 +89,7 @@ func TestAttestationInfoAtSlot_JustifiedBlockFailure(t *testing.T) {
t.Fatalf("Could not update chain head in test db: %v", err)
}
want := "could not get justified block"
req := &pb.AttestationInfoRequest{
Slot: params.BeaconConfig().GenesisSlot + 1,
}
req := &pb.AttestationInfoRequest{}
if _, err := attesterServer.AttestationInfoAtSlot(context.Background(), req); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %v, received %v", want, err)
}
Expand Down Expand Up @@ -153,12 +135,6 @@ func TestAttestationInfoAtSlot_Ok(t *testing.T) {
attesterServer := &AttesterServer{
beaconDB: db,
}
if err := attesterServer.beaconDB.SaveBlock(block); err != nil {
t.Fatalf("Could not save block in test db: %v", err)
}
if err := attesterServer.beaconDB.UpdateChainHead(block, beaconState); err != nil {
t.Fatalf("Could not update chain head in test db: %v", err)
}
if err := attesterServer.beaconDB.SaveBlock(epochBoundaryBlock); err != nil {
t.Fatalf("Could not save block in test db: %v", err)
}
Expand All @@ -171,8 +147,13 @@ func TestAttestationInfoAtSlot_Ok(t *testing.T) {
if err := attesterServer.beaconDB.UpdateChainHead(justifiedBlock, beaconState); err != nil {
t.Fatalf("Could not update chain head in test db: %v", err)
}
if err := attesterServer.beaconDB.SaveBlock(block); err != nil {
t.Fatalf("Could not save block in test db: %v", err)
}
if err := attesterServer.beaconDB.UpdateChainHead(block, beaconState); err != nil {
t.Fatalf("Could not update chain head in test db: %v", err)
}
req := &pb.AttestationInfoRequest{
Slot: 1 + params.BeaconConfig().GenesisSlot,
Shard: 0,
}
res, err := attesterServer.AttestationInfoAtSlot(context.Background(), req)
Expand Down
Loading