prysm-pulse/beacon-chain/rpc/debug/block_test.go
Victor Farazdagi a8e501b3cf
ETH2 Types: Epoch (#8373)
* update deps

* update deps

* update protos/*

* update deps

* reset protos

* update protos

* update shared/params/config

* update protos

* update /shared

* update shared/slotutil and shared/testutil

* update beacon-chain/core/helpers

* updates beacon-chain/state

* update beacon-chain/forkchoice

* update beacon-chain/blockchain

* update beacon-chain/cache

* update beacon-chain/core

* update beacon-chain/db

* update beacon-chain/node

* update beacon-chain/p2p

* update beacon-chain/rpc

* update beacon-chain/sync

* go mod tidy

* make sure that beacon-chain build suceeds

* go fmt

* update e2e tests

* update slasher

* remove redundant alias

* update validator

* gazelle

* fix build errors in unit tests

* go fmt

* update deps

* update fuzz/BUILD.bazel

* fix unit tests

* more unit test fixes

* fix blockchain UTs

* more unit test fixes
2021-02-09 10:05:22 +00:00

87 lines
2.8 KiB
Go

package debug
import (
"context"
"testing"
"time"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-bitfield"
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func TestServer_GetBlock(t *testing.T) {
db := dbTest.SetupDB(t)
ctx := context.Background()
b := testutil.NewBeaconBlock()
b.Block.Slot = 100
require.NoError(t, db.SaveBlock(ctx, b))
blockRoot, err := b.Block.HashTreeRoot()
require.NoError(t, err)
bs := &Server{
BeaconDB: db,
}
res, err := bs.GetBlock(ctx, &pbrpc.BlockRequest{
BlockRoot: blockRoot[:],
})
require.NoError(t, err)
wanted, err := b.MarshalSSZ()
require.NoError(t, err)
assert.DeepEqual(t, wanted, res.Encoded)
// Checking for nil block.
blockRoot = [32]byte{}
res, err = bs.GetBlock(ctx, &pbrpc.BlockRequest{
BlockRoot: blockRoot[:],
})
require.NoError(t, err)
assert.DeepEqual(t, []byte{}, res.Encoded)
}
func TestServer_GetAttestationInclusionSlot(t *testing.T) {
db := dbTest.SetupDB(t)
ctx := context.Background()
bs := &Server{
BeaconDB: db,
StateGen: stategen.New(db),
GenesisTimeFetcher: &mock.ChainService{Genesis: time.Now().Add(time.Duration(-1*int64(
2*params.BeaconConfig().SlotsPerEpoch*params.BeaconConfig().SecondsPerSlot)) * time.Second)},
}
s, _ := testutil.DeterministicGenesisState(t, 2048)
tr := [32]byte{'a'}
require.NoError(t, bs.StateGen.SaveState(ctx, tr, s))
c, err := helpers.BeaconCommitteeFromState(s, 1, 0)
require.NoError(t, err)
a := &ethpb.Attestation{
Data: &ethpb.AttestationData{
Target: &ethpb.Checkpoint{Root: tr[:]},
Source: &ethpb.Checkpoint{Root: make([]byte, 32)},
BeaconBlockRoot: make([]byte, 32),
Slot: 1,
},
AggregationBits: bitfield.Bitlist{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01},
Signature: make([]byte, 96),
}
b := testutil.NewBeaconBlock()
b.Block.Slot = 2
b.Block.Body.Attestations = []*ethpb.Attestation{a}
require.NoError(t, bs.BeaconDB.SaveBlock(ctx, b))
res, err := bs.GetInclusionSlot(ctx, &pbrpc.InclusionSlotRequest{Slot: 1, Id: c[0]})
require.NoError(t, err)
require.Equal(t, b.Block.Slot, res.Slot)
res, err = bs.GetInclusionSlot(ctx, &pbrpc.InclusionSlotRequest{Slot: 1, Id: 9999999})
require.NoError(t, err)
require.Equal(t, uint64(params.BeaconConfig().FarFutureEpoch), res.Slot)
}