mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-26 05:17:22 +00:00
7cc32c4dda
* remove unused code * remove defer use in loop * Remove unused methods and constants * gofmt and gaz * nilness check * remove unused args * Add TODO for refactoring subscribeWithBase to remove unused arg. It seems too involved to include in this sweeping PR. https://github.com/prysmaticlabs/prysm/issues/7437 * replace empty slice declaration * Remove unnecessary type conversions * remove redundant type declaration * rename receivers to be consistent * Remove bootnode query tool. It is now obsolete by discv5 * Remove relay node. It is no longer used or supported * Revert "Remove relay node. It is no longer used or supported" This reverts commit 4bd7717334dad85ef4766ed9bc4da711fb5fa810. * Delete unused test directory * Delete unsupported gcp startup script * Delete old k8s script * build fixes * fix build * go mod tidy * revert slasher/db/kv/block_header.go * fix build * remove redundant nil check * combine func args Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
198 lines
6.3 KiB
Go
198 lines
6.3 KiB
Go
package sync
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"reflect"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/kevinms/leakybucket-go"
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
"github.com/libp2p/go-libp2p-core/protocol"
|
|
"github.com/protolambda/zssz"
|
|
"github.com/protolambda/zssz/types"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
|
db "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
|
|
"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 TestRecentBeaconBlocksRPCHandler_ReturnsBlocks(t *testing.T) {
|
|
p1 := p2ptest.NewTestP2P(t)
|
|
p2 := p2ptest.NewTestP2P(t)
|
|
p1.Connect(p2)
|
|
assert.Equal(t, 1, len(p1.BHost.Network().Peers()), "Expected peers to be connected")
|
|
d, _ := db.SetupDB(t)
|
|
|
|
var blkRoots [][32]byte
|
|
// Populate the database with blocks that would match the request.
|
|
for i := 1; i < 11; i++ {
|
|
blk := testutil.NewBeaconBlock()
|
|
blk.Block.Slot = uint64(i)
|
|
root, err := blk.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
require.NoError(t, d.SaveBlock(context.Background(), blk))
|
|
blkRoots = append(blkRoots, root)
|
|
}
|
|
|
|
r := &Service{p2p: p1, db: d, rateLimiter: newRateLimiter(p1)}
|
|
pcl := protocol.ID("/testing")
|
|
topic := string(pcl)
|
|
r.rateLimiter.limiterMap[topic] = leakybucket.NewCollector(10000, 10000, false)
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
p2.BHost.SetStreamHandler(pcl, func(stream network.Stream) {
|
|
defer wg.Done()
|
|
for i := range blkRoots {
|
|
expectSuccess(t, stream)
|
|
res := testutil.NewBeaconBlock()
|
|
assert.NoError(t, r.p2p.Encoding().DecodeWithMaxLength(stream, &res))
|
|
if res.Block.Slot != uint64(i+1) {
|
|
t.Errorf("Received unexpected block slot %d but wanted %d", res.Block.Slot, i+1)
|
|
}
|
|
}
|
|
})
|
|
|
|
stream1, err := p1.BHost.NewStream(context.Background(), p2.BHost.ID(), pcl)
|
|
require.NoError(t, err)
|
|
err = r.beaconBlocksRootRPCHandler(context.Background(), blkRoots, stream1)
|
|
assert.NoError(t, err)
|
|
|
|
if testutil.WaitTimeout(&wg, 1*time.Second) {
|
|
t.Fatal("Did not receive stream within 1 sec")
|
|
}
|
|
}
|
|
|
|
func TestRecentBeaconBlocks_RPCRequestSent(t *testing.T) {
|
|
p1 := p2ptest.NewTestP2P(t)
|
|
p2 := p2ptest.NewTestP2P(t)
|
|
p1.DelaySend = true
|
|
|
|
blockA := testutil.NewBeaconBlock()
|
|
blockA.Block.Slot = 111
|
|
blockB := testutil.NewBeaconBlock()
|
|
blockB.Block.Slot = 40
|
|
// Set up a head state with data we expect.
|
|
blockARoot, err := blockA.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
blockBRoot, err := blockB.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
genesisState, err := state.GenesisBeaconState(nil, 0, ðpb.Eth1Data{})
|
|
require.NoError(t, err)
|
|
require.NoError(t, genesisState.SetSlot(111))
|
|
require.NoError(t, genesisState.UpdateBlockRootAtIndex(111%params.BeaconConfig().SlotsPerHistoricalRoot, blockARoot))
|
|
finalizedCheckpt := ðpb.Checkpoint{
|
|
Epoch: 5,
|
|
Root: blockBRoot[:],
|
|
}
|
|
|
|
expectedRoots := [][32]byte{blockBRoot, blockARoot}
|
|
|
|
r := &Service{
|
|
p2p: p1,
|
|
chain: &mock.ChainService{
|
|
State: genesisState,
|
|
FinalizedCheckPoint: finalizedCheckpt,
|
|
Root: blockARoot[:],
|
|
},
|
|
slotToPendingBlocks: make(map[uint64][]*ethpb.SignedBeaconBlock),
|
|
seenPendingBlocks: make(map[[32]byte]bool),
|
|
ctx: context.Background(),
|
|
rateLimiter: newRateLimiter(p1),
|
|
}
|
|
|
|
// Setup streams
|
|
pcl := protocol.ID("/eth2/beacon_chain/req/beacon_blocks_by_root/1/ssz_snappy")
|
|
topic := string(pcl)
|
|
r.rateLimiter.limiterMap[topic] = leakybucket.NewCollector(10000, 10000, false)
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
p2.BHost.SetStreamHandler(pcl, func(stream network.Stream) {
|
|
defer wg.Done()
|
|
var out [][32]byte
|
|
assert.NoError(t, p2.Encoding().DecodeWithMaxLength(stream, &out))
|
|
assert.DeepEqual(t, expectedRoots, out, "Did not receive expected message")
|
|
response := []*ethpb.SignedBeaconBlock{blockB, blockA}
|
|
for _, blk := range response {
|
|
_, err := stream.Write([]byte{responseCodeSuccess})
|
|
assert.NoError(t, err, "Failed to write to stream")
|
|
_, err = p2.Encoding().EncodeWithMaxLength(stream, blk)
|
|
assert.NoError(t, err, "Could not send response back")
|
|
}
|
|
assert.NoError(t, stream.Close())
|
|
})
|
|
|
|
p1.Connect(p2)
|
|
require.NoError(t, r.sendRecentBeaconBlocksRequest(context.Background(), expectedRoots, p2.PeerID()))
|
|
|
|
if testutil.WaitTimeout(&wg, 1*time.Second) {
|
|
t.Fatal("Did not receive stream within 1 sec")
|
|
}
|
|
}
|
|
|
|
func TestRecentBeaconBlocksRPCHandler_HandleZeroBlocks(t *testing.T) {
|
|
p1 := p2ptest.NewTestP2P(t)
|
|
p2 := p2ptest.NewTestP2P(t)
|
|
p1.Connect(p2)
|
|
assert.Equal(t, 1, len(p1.BHost.Network().Peers()), "Expected peers to be connected")
|
|
d, _ := db.SetupDB(t)
|
|
|
|
r := &Service{p2p: p1, db: d, rateLimiter: newRateLimiter(p1)}
|
|
pcl := protocol.ID("/testing")
|
|
topic := string(pcl)
|
|
r.rateLimiter.limiterMap[topic] = leakybucket.NewCollector(1, 1, false)
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
p2.BHost.SetStreamHandler(pcl, func(stream network.Stream) {
|
|
defer wg.Done()
|
|
expectFailure(t, 1, "no block roots provided in request", stream)
|
|
})
|
|
|
|
stream1, err := p1.BHost.NewStream(context.Background(), p2.BHost.ID(), pcl)
|
|
require.NoError(t, err)
|
|
err = r.beaconBlocksRootRPCHandler(context.Background(), [][32]byte{}, stream1)
|
|
assert.ErrorContains(t, "no block roots provided", err)
|
|
if testutil.WaitTimeout(&wg, 1*time.Second) {
|
|
t.Fatal("Did not receive stream within 1 sec")
|
|
}
|
|
|
|
lter, err := r.rateLimiter.retrieveCollector(topic)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 1, int(lter.Count(stream1.Conn().RemotePeer().String())))
|
|
}
|
|
|
|
type testList [][32]byte
|
|
|
|
func (*testList) Limit() uint64 {
|
|
return 2 << 10
|
|
}
|
|
|
|
func TestSSZCompatibility(t *testing.T) {
|
|
rootA := [32]byte{'a'}
|
|
rootB := [32]byte{'B'}
|
|
rootC := [32]byte{'C'}
|
|
list := testList{rootA, rootB, rootC}
|
|
writer := bytes.NewBuffer([]byte{})
|
|
sszType, err := types.SSZFactory(reflect.TypeOf(list))
|
|
assert.NoError(t, err)
|
|
n, err := zssz.Encode(writer, list, sszType)
|
|
assert.NoError(t, err)
|
|
encodedPart := writer.Bytes()[:n]
|
|
fastSSZ, err := ssz.Marshal(list)
|
|
assert.NoError(t, err)
|
|
if !bytes.Equal(fastSSZ, encodedPart) {
|
|
t.Errorf("Wanted the same result as ZSSZ of %#x but got %#X", encodedPart, fastSSZ)
|
|
}
|
|
}
|