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-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-07-28 21:23:44 +00:00
|
|
|
pbrpc "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-07-21 21:34:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
|
2020-08-06 03:55:39 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-07-20 02:15:51 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
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
|
|
|
|
|
|
|
b := testutil.NewBeaconBlock()
|
|
|
|
b.Block.Slot = 100
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, db.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(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-07-27 16:54:49 +00:00
|
|
|
res, err := bs.GetBlock(ctx, &pbrpc.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-07-27 16:54:49 +00:00
|
|
|
res, err = bs.GetBlock(ctx, &pbrpc.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
|
|
|
}
|
|
|
|
|
|
|
|
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 := ð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},
|
2020-08-27 18:13:32 +00:00
|
|
|
Signature: make([]byte, 96),
|
2020-08-06 03:55:39 +00:00
|
|
|
}
|
2020-08-27 18:13:32 +00:00
|
|
|
b := testutil.NewBeaconBlock()
|
|
|
|
b.Block.Slot = 2
|
|
|
|
b.Block.Body.Attestations = []*ethpb.Attestation{a}
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, bs.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b)))
|
2021-02-23 00:14:50 +00:00
|
|
|
res, err := bs.GetInclusionSlot(ctx, &pbrpc.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)
|
|
|
|
res, err = bs.GetInclusionSlot(ctx, &pbrpc.InclusionSlotRequest{Slot: 1, Id: 9999999})
|
|
|
|
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
|
|
|
}
|