2019-09-23 19:24:42 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
testDB "github.com/prysmaticlabs/prysm/v5/beacon-chain/db/testing"
|
|
|
|
doublylinkedtree "github.com/prysmaticlabs/prysm/v5/beacon-chain/forkchoice/doubly-linked-tree"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state/stategen"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/consensus-types/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/util"
|
2019-09-23 19:24:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHeadSlot_DataRace(t *testing.T) {
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2019-09-23 19:24:42 +00:00
|
|
|
s := &Service{
|
2021-10-18 17:48:05 +00:00
|
|
|
cfg: &config{BeaconDB: beaconDB},
|
2019-09-23 19:24:42 +00:00
|
|
|
}
|
2022-08-02 15:30:46 +00:00
|
|
|
b, err := blocks.NewSignedBeaconBlock(util.NewBeaconBlock())
|
2022-04-06 11:05:53 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-06 23:36:52 +00:00
|
|
|
st, _ := util.DeterministicGenesisState(t, 1)
|
2022-03-14 20:58:13 +00:00
|
|
|
wait := make(chan struct{})
|
2019-09-23 19:24:42 +00:00
|
|
|
go func() {
|
2022-03-14 20:58:13 +00:00
|
|
|
defer close(wait)
|
2022-04-06 23:36:52 +00:00
|
|
|
require.NoError(t, s.saveHead(context.Background(), [32]byte{}, b, st))
|
2019-09-23 19:24:42 +00:00
|
|
|
}()
|
|
|
|
s.HeadSlot()
|
2022-03-14 20:58:13 +00:00
|
|
|
<-wait
|
2019-09-23 19:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeadRoot_DataRace(t *testing.T) {
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2019-09-23 19:24:42 +00:00
|
|
|
s := &Service{
|
2022-09-28 20:10:27 +00:00
|
|
|
cfg: &config{BeaconDB: beaconDB, StateGen: stategen.New(beaconDB, doublylinkedtree.New())},
|
2021-03-17 18:36:56 +00:00
|
|
|
head: &head{root: [32]byte{'A'}},
|
2019-09-23 19:24:42 +00:00
|
|
|
}
|
2022-08-02 15:30:46 +00:00
|
|
|
b, err := blocks.NewSignedBeaconBlock(util.NewBeaconBlock())
|
2022-04-06 11:05:53 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-14 20:58:13 +00:00
|
|
|
wait := make(chan struct{})
|
2022-04-06 23:36:52 +00:00
|
|
|
st, _ := util.DeterministicGenesisState(t, 1)
|
2019-09-23 19:24:42 +00:00
|
|
|
go func() {
|
2022-03-14 20:58:13 +00:00
|
|
|
defer close(wait)
|
2022-04-06 23:36:52 +00:00
|
|
|
require.NoError(t, s.saveHead(context.Background(), [32]byte{}, b, st))
|
|
|
|
|
2019-09-23 19:24:42 +00:00
|
|
|
}()
|
2022-04-06 11:05:53 +00:00
|
|
|
_, err = s.HeadRoot(context.Background())
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-14 20:58:13 +00:00
|
|
|
<-wait
|
2019-09-23 19:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeadBlock_DataRace(t *testing.T) {
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2022-08-02 15:30:46 +00:00
|
|
|
wsb, err := blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Body: ðpb.BeaconBlockBody{}}})
|
2022-03-25 23:00:44 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-23 19:24:42 +00:00
|
|
|
s := &Service{
|
2022-09-28 20:10:27 +00:00
|
|
|
cfg: &config{BeaconDB: beaconDB, StateGen: stategen.New(beaconDB, doublylinkedtree.New())},
|
2022-03-25 23:00:44 +00:00
|
|
|
head: &head{block: wsb},
|
2019-09-23 19:24:42 +00:00
|
|
|
}
|
2022-08-02 15:30:46 +00:00
|
|
|
b, err := blocks.NewSignedBeaconBlock(util.NewBeaconBlock())
|
2022-04-06 11:05:53 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-14 20:58:13 +00:00
|
|
|
wait := make(chan struct{})
|
2022-04-06 23:36:52 +00:00
|
|
|
st, _ := util.DeterministicGenesisState(t, 1)
|
2019-09-23 19:24:42 +00:00
|
|
|
go func() {
|
2022-03-14 20:58:13 +00:00
|
|
|
defer close(wait)
|
2022-04-06 23:36:52 +00:00
|
|
|
require.NoError(t, s.saveHead(context.Background(), [32]byte{}, b, st))
|
|
|
|
|
2019-09-23 19:24:42 +00:00
|
|
|
}()
|
2022-03-25 23:00:44 +00:00
|
|
|
_, err = s.HeadBlock(context.Background())
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-14 20:58:13 +00:00
|
|
|
<-wait
|
2019-09-23 19:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeadState_DataRace(t *testing.T) {
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2019-09-23 19:24:42 +00:00
|
|
|
s := &Service{
|
2022-09-28 20:10:27 +00:00
|
|
|
cfg: &config{BeaconDB: beaconDB, StateGen: stategen.New(beaconDB, doublylinkedtree.New())},
|
2019-09-23 19:24:42 +00:00
|
|
|
}
|
2022-08-02 15:30:46 +00:00
|
|
|
b, err := blocks.NewSignedBeaconBlock(util.NewBeaconBlock())
|
2022-04-06 11:05:53 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-14 20:58:13 +00:00
|
|
|
wait := make(chan struct{})
|
2022-04-06 23:36:52 +00:00
|
|
|
st, _ := util.DeterministicGenesisState(t, 1)
|
2023-01-24 22:14:50 +00:00
|
|
|
root := bytesutil.ToBytes32(bytesutil.PadTo([]byte{'s'}, 32))
|
|
|
|
require.NoError(t, beaconDB.SaveGenesisBlockRoot(context.Background(), root))
|
|
|
|
require.NoError(t, beaconDB.SaveState(context.Background(), st, root))
|
2019-09-23 19:24:42 +00:00
|
|
|
go func() {
|
2022-03-14 20:58:13 +00:00
|
|
|
defer close(wait)
|
2022-04-06 23:36:52 +00:00
|
|
|
require.NoError(t, s.saveHead(context.Background(), [32]byte{}, b, st))
|
|
|
|
|
2019-09-23 19:24:42 +00:00
|
|
|
}()
|
2022-04-06 11:05:53 +00:00
|
|
|
_, err = s.HeadState(context.Background())
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-14 20:58:13 +00:00
|
|
|
<-wait
|
2019-09-23 19:24:42 +00:00
|
|
|
}
|