2020-05-21 01:36:05 +00:00
|
|
|
package debug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2020-08-06 03:55:39 +00:00
|
|
|
"time"
|
2020-05-21 01:36:05 +00:00
|
|
|
|
2020-08-06 03:55:39 +00:00
|
|
|
"github.com/prysmaticlabs/go-bitfield"
|
|
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2020-05-21 01:36:05 +00:00
|
|
|
dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
2020-08-06 03:55:39 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
|
2021-12-15 20:14:30 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
2020-05-21 01:36:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServer_GetBlock(t *testing.T) {
|
2020-12-16 16:56:21 +00:00
|
|
|
db := dbTest.SetupDB(t)
|
2020-05-21 01:36:05 +00:00
|
|
|
ctx := context.Background()
|
2020-08-27 18:13:32 +00:00
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
b := util.NewBeaconBlock()
|
2020-08-27 18:13:32 +00:00
|
|
|
b.Block.Slot = 100
|
2022-06-24 17:22:39 +00:00
|
|
|
util.SaveBlock(t, ctx, db, b)
|
2020-08-27 18:13:32 +00:00
|
|
|
blockRoot, err := b.Block.HashTreeRoot()
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
2020-05-21 01:36:05 +00:00
|
|
|
bs := &Server{
|
|
|
|
BeaconDB: db,
|
|
|
|
}
|
2021-12-07 17:52:39 +00:00
|
|
|
res, err := bs.GetBlock(ctx, ðpb.BlockRequestByRoot{
|
2020-05-21 01:36:05 +00:00
|
|
|
BlockRoot: blockRoot[:],
|
|
|
|
})
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-05 13:48:40 +00:00
|
|
|
wanted, err := b.MarshalSSZ()
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, wanted, res.Encoded)
|
2020-05-21 01:36:05 +00:00
|
|
|
|
|
|
|
// Checking for nil block.
|
|
|
|
blockRoot = [32]byte{}
|
2021-12-07 17:52:39 +00:00
|
|
|
res, err = bs.GetBlock(ctx, ðpb.BlockRequestByRoot{
|
2020-05-21 01:36:05 +00:00
|
|
|
BlockRoot: blockRoot[:],
|
|
|
|
})
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, []byte{}, res.Encoded)
|
2020-05-21 01:36:05 +00:00
|
|
|
}
|
2020-08-06 03:55:39 +00:00
|
|
|
|
|
|
|
func TestServer_GetAttestationInclusionSlot(t *testing.T) {
|
2020-12-16 16:56:21 +00:00
|
|
|
db := dbTest.SetupDB(t)
|
2020-08-06 03:55:39 +00:00
|
|
|
ctx := context.Background()
|
2021-02-16 07:45:34 +00:00
|
|
|
offset := int64(2 * params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().SecondsPerSlot))
|
2020-08-06 03:55:39 +00:00
|
|
|
bs := &Server{
|
2021-02-16 07:45:34 +00:00
|
|
|
BeaconDB: db,
|
|
|
|
StateGen: stategen.New(db),
|
|
|
|
GenesisTimeFetcher: &mock.ChainService{Genesis: time.Now().Add(time.Duration(-1*offset) * time.Second)},
|
2020-08-06 03:55:39 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
s, _ := util.DeterministicGenesisState(t, 2048)
|
2020-08-06 03:55:39 +00:00
|
|
|
tr := [32]byte{'a'}
|
|
|
|
require.NoError(t, bs.StateGen.SaveState(ctx, tr, s))
|
2021-09-26 15:27:57 +00:00
|
|
|
c, err := helpers.BeaconCommitteeFromState(context.Background(), s, 1, 0)
|
2020-08-06 03:55:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
a := ðpb.Attestation{
|
|
|
|
Data: ðpb.AttestationData{
|
2020-08-27 18:13:32 +00:00
|
|
|
Target: ðpb.Checkpoint{Root: tr[:]},
|
|
|
|
Source: ðpb.Checkpoint{Root: make([]byte, 32)},
|
|
|
|
BeaconBlockRoot: make([]byte, 32),
|
|
|
|
Slot: 1,
|
2020-08-06 03:55:39 +00:00
|
|
|
},
|
2021-01-20 03:00:52 +00:00
|
|
|
AggregationBits: bitfield.Bitlist{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01},
|
2021-12-15 20:14:30 +00:00
|
|
|
Signature: make([]byte, fieldparams.BLSSignatureLength),
|
2020-08-06 03:55:39 +00:00
|
|
|
}
|
2021-09-23 18:53:46 +00:00
|
|
|
b := util.NewBeaconBlock()
|
2020-08-27 18:13:32 +00:00
|
|
|
b.Block.Slot = 2
|
|
|
|
b.Block.Body.Attestations = []*ethpb.Attestation{a}
|
2022-06-24 17:22:39 +00:00
|
|
|
util.SaveBlock(t, ctx, bs.BeaconDB, b)
|
2021-12-07 17:52:39 +00:00
|
|
|
res, err := bs.GetInclusionSlot(ctx, ðpb.InclusionSlotRequest{Slot: 1, Id: uint64(c[0])})
|
2020-08-06 03:55:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, b.Block.Slot, res.Slot)
|
2021-12-07 17:52:39 +00:00
|
|
|
res, err = bs.GetInclusionSlot(ctx, ðpb.InclusionSlotRequest{Slot: 1, Id: 9999999})
|
2020-08-06 03:55:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-16 07:45:34 +00:00
|
|
|
require.Equal(t, params.BeaconConfig().FarFutureSlot, res.Slot)
|
2020-08-06 03:55:39 +00:00
|
|
|
}
|