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"
|
2022-08-16 12:20:13 +00:00
|
|
|
mock "github.com/prysmaticlabs/prysm/v3/beacon-chain/blockchain/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/core/helpers"
|
|
|
|
dbTest "github.com/prysmaticlabs/prysm/v3/beacon-chain/db/testing"
|
2022-09-28 20:10:27 +00:00
|
|
|
doublylinkedtree "github.com/prysmaticlabs/prysm/v3/beacon-chain/forkchoice/doubly-linked-tree"
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state/stategen"
|
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/config/params"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/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,
|
2022-09-28 20:10:27 +00:00
|
|
|
StateGen: stategen.New(db, doublylinkedtree.New()),
|
2021-02-16 07:45:34 +00:00
|
|
|
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
|
|
|
}
|