prysm-pulse/beacon-chain/rpc/proposer_server_test.go
Raul Jordan e19920aec1
Miscellaneous Runtime Fixes & Improvements - Raul (#1674)
* fatal if impossible to receive chainstart

* fix tests

* fix

* custom delay

* completed custom delay

* errors

* better logs, nothing at genesis

* use demo in val

* add gazelle

* log

* starting to log stuff

* pass in ops

* avoid printing the large #s for debug, still working on tests..

* all around better logging

* fixed build error in epoch process

* fixed state transiton tests

* fixed block tests

* lint

* verify sigs in randao

* ready for inclusion falg

* only print waiting when slot is not valid

* fix build

* mod config

* fixed last justified slot issue

* fix inclusion

* fixed attestation issue

* using zero hash from params instead

* fix tests

* update balance

* removed swp

* more `- genesis_slot` for logs

* rem unused log

* fix broken tests

* account for skip slots in state root computation

* fixes done

* validator guide bug fixes - 671

* epoch boundary at the last slot of the epoch

* fix epoch issue

* more balance cal logs for debugging

* greater balance

* attestaton fixes

* fixes

* addressed testrun

* fixed ejection balance

* fix tests with far future epoch

* revert sync change

* revert initial sync change

* fix changes

* off by one att fix

* revert the att fix

* address comments

* format

* fix build

* rem file
2019-02-24 20:09:45 -06:00

172 lines
4.7 KiB
Go

package rpc
import (
"context"
"strconv"
"testing"
"time"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/params"
)
func TestProposeBlock_OK(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
mockChain := &mockChainService{}
genesis := b.NewGenesisBlock([]byte{})
if err := db.SaveBlock(genesis); err != nil {
t.Fatalf("Could not save genesis block: %v", err)
}
deposits := make([]*pbp2p.Deposit, params.BeaconConfig().DepositsForChainStart)
for i := 0; i < len(deposits); i++ {
depositData, err := helpers.EncodeDepositData(
&pbp2p.DepositInput{
Pubkey: []byte(strconv.Itoa(i)),
},
params.BeaconConfig().MaxDepositAmount,
time.Now().Unix(),
)
if err != nil {
t.Fatalf("Could not encode deposit input: %v", err)
}
deposits[i] = &pbp2p.Deposit{
DepositData: depositData,
}
}
beaconState, err := state.GenesisBeaconState(deposits, 0, nil)
if err != nil {
t.Fatalf("Could not instantiate genesis state: %v", err)
}
if err := db.UpdateChainHead(genesis, beaconState); err != nil {
t.Fatalf("Could not save genesis state: %v", err)
}
proposerServer := &ProposerServer{
chainService: mockChain,
beaconDB: db,
powChainService: &mockPOWChainService{},
}
req := &pbp2p.BeaconBlock{
Slot: 5,
ParentRootHash32: []byte("parent-hash"),
}
if _, err := proposerServer.ProposeBlock(context.Background(), req); err != nil {
t.Errorf("Could not propose block correctly: %v", err)
}
}
func TestComputeStateRoot_OK(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
mockChain := &mockChainService{}
genesis := b.NewGenesisBlock([]byte{})
if err := db.SaveBlock(genesis); err != nil {
t.Fatalf("Could not save genesis block: %v", err)
}
deposits := make([]*pbp2p.Deposit, params.BeaconConfig().DepositsForChainStart)
for i := 0; i < len(deposits); i++ {
depositData, err := helpers.EncodeDepositData(
&pbp2p.DepositInput{
Pubkey: []byte(strconv.Itoa(i)),
},
params.BeaconConfig().MaxDepositAmount,
time.Now().Unix(),
)
if err != nil {
t.Fatalf("Could not encode deposit input: %v", err)
}
deposits[i] = &pbp2p.Deposit{
DepositData: depositData,
}
}
beaconState, err := state.GenesisBeaconState(deposits, 0, nil)
if err != nil {
t.Fatalf("Could not instantiate genesis state: %v", err)
}
beaconState.Slot = 10
if err := db.UpdateChainHead(genesis, beaconState); err != nil {
t.Fatalf("Could not save genesis state: %v", err)
}
proposerServer := &ProposerServer{
chainService: mockChain,
beaconDB: db,
powChainService: &mockPOWChainService{},
}
req := &pbp2p.BeaconBlock{
ParentRootHash32: nil,
Slot: 11,
RandaoReveal: nil,
Body: &pbp2p.BeaconBlockBody{
ProposerSlashings: nil,
AttesterSlashings: nil,
},
}
_, _ = proposerServer.ComputeStateRoot(context.Background(), req)
}
func TestPendingAttestations_FiltersWithinInclusionDelay(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
proposerServer := &ProposerServer{
operationService: &mockOperationService{},
beaconDB: db,
}
beaconState := &pbp2p.BeaconState{
Slot: params.BeaconConfig().GenesisSlot + params.BeaconConfig().MinAttestationInclusionDelay,
}
if err := db.SaveState(beaconState); err != nil {
t.Fatal(err)
}
res, err := proposerServer.PendingAttestations(context.Background(), &pb.PendingAttestationsRequest{
FilterReadyForInclusion: true,
})
if err != nil {
t.Fatalf("Unexpected error fetching pending attestations: %v", err)
}
if len(res.PendingAttestations) == 0 {
t.Error("Expected pending attestations list to be non-empty")
}
}
func TestPendingAttestations_OK(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
proposerServer := &ProposerServer{
operationService: &mockOperationService{},
beaconDB: db,
}
beaconState := &pbp2p.BeaconState{
Slot: params.BeaconConfig().GenesisSlot + params.BeaconConfig().MinAttestationInclusionDelay,
}
if err := db.SaveState(beaconState); err != nil {
t.Fatal(err)
}
res, err := proposerServer.PendingAttestations(context.Background(), &pb.PendingAttestationsRequest{})
if err != nil {
t.Fatalf("Unexpected error fetching pending attestations: %v", err)
}
if len(res.PendingAttestations) == 0 {
t.Error("Expected pending attestations list to be non-empty")
}
}