mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-10 03:31:20 +00:00
68eba02cc2
* Remove most of the remaining geth code and set up bazel for this * chmod +x * Add flake check * better flake detection Former-commit-id: 5c332ecbf2923943f646f1fe40befa95be883329 [formerly 99590fc354514584700e5ce8d7d30a8a7d541f29] Former-commit-id: e5f919b553fe698e98090965d34eb721990b5693
179 lines
5.1 KiB
Go
179 lines
5.1 KiB
Go
package syncer
|
|
|
|
import (
|
|
"fmt"
|
|
"math/big"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/mainchain"
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/params"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/p2p/messages"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding"
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/database"
|
|
internal "github.com/prysmaticlabs/geth-sharding/sharding/internal"
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/p2p"
|
|
)
|
|
|
|
var _ = sharding.Service(&Syncer{})
|
|
|
|
func TestStop(t *testing.T) {
|
|
h := internal.NewLogHandler(t)
|
|
log.Root().SetHandler(h)
|
|
|
|
shardChainDB, err := database.NewShardDB("", "", true)
|
|
if err != nil {
|
|
t.Fatalf("unable to setup db: %v", err)
|
|
}
|
|
shardID := 0
|
|
server, err := p2p.NewServer()
|
|
if err != nil {
|
|
t.Fatalf("Unable to setup p2p server: %v", err)
|
|
}
|
|
|
|
syncer, err := NewSyncer(params.DefaultConfig, &mainchain.SMCClient{}, server, shardChainDB, shardID)
|
|
if err != nil {
|
|
t.Fatalf("Unable to setup sync service: %v", err)
|
|
}
|
|
|
|
feed := server.Feed(messages.CollationBodyRequest{})
|
|
syncer.msgChan = make(chan p2p.Message)
|
|
syncer.errChan = make(chan error)
|
|
syncer.bodyRequests = feed.Subscribe(syncer.msgChan)
|
|
|
|
if err := syncer.Stop(); err != nil {
|
|
t.Fatalf("Unable to stop sync service: %v", err)
|
|
}
|
|
|
|
h.VerifyLogMsg("Stopping sync service")
|
|
|
|
// The context should have been canceled.
|
|
if syncer.ctx.Err() == nil {
|
|
t.Error("Context was not canceled")
|
|
}
|
|
}
|
|
|
|
// This test uses a faulty Signer interface in order to trigger an error
|
|
// in the simulateNotaryRequests goroutine when attempting to sign
|
|
// a collation header within the goroutine's internals.
|
|
func TestHandleCollationBodyRequests_FaultySigner(t *testing.T) {
|
|
h := internal.NewLogHandler(t)
|
|
log.Root().SetHandler(h)
|
|
|
|
shardChainDB, err := database.NewShardDB("", "", true)
|
|
if err != nil {
|
|
t.Fatalf("unable to setup db: %v", err)
|
|
}
|
|
shardID := 0
|
|
server, err := p2p.NewServer()
|
|
if err != nil {
|
|
t.Fatalf("Unable to setup p2p server: %v", err)
|
|
}
|
|
|
|
syncer, err := NewSyncer(params.DefaultConfig, &mainchain.SMCClient{}, server, shardChainDB, shardID)
|
|
if err != nil {
|
|
t.Fatalf("Unable to setup syncer service: %v", err)
|
|
}
|
|
|
|
feed := server.Feed(messages.CollationBodyRequest{})
|
|
shard := sharding.NewShard(big.NewInt(int64(shardID)), shardChainDB.DB())
|
|
|
|
syncer.msgChan = make(chan p2p.Message)
|
|
syncer.errChan = make(chan error)
|
|
syncer.bodyRequests = feed.Subscribe(syncer.msgChan)
|
|
|
|
go syncer.HandleCollationBodyRequests(shard)
|
|
|
|
msg := p2p.Message{
|
|
Peer: p2p.Peer{},
|
|
Data: messages.CollationBodyRequest{},
|
|
}
|
|
syncer.msgChan <- msg
|
|
receivedErr := <-syncer.errChan
|
|
expectedErr := "could not construct response"
|
|
if !strings.Contains(receivedErr.Error(), expectedErr) {
|
|
t.Errorf("Expected error did not match. want: %v, got: %v", expectedErr, receivedErr)
|
|
}
|
|
|
|
syncer.cancel()
|
|
|
|
// The context should have been canceled.
|
|
if syncer.ctx.Err() == nil {
|
|
t.Error("Context was not canceled")
|
|
}
|
|
}
|
|
|
|
// This test checks the proper functioning of the handleCollationBodyRequests goroutine
|
|
// by listening to the responseSent channel which occurs after successful
|
|
// construction and sending of a response via p2p.
|
|
func TestHandleCollationBodyRequests(t *testing.T) {
|
|
h := internal.NewLogHandler(t)
|
|
log.Root().SetHandler(h)
|
|
|
|
shardChainDB, err := database.NewShardDB("", "", true)
|
|
if err != nil {
|
|
t.Fatalf("unable to setup db: %v", err)
|
|
}
|
|
server, err := p2p.NewServer()
|
|
if err != nil {
|
|
t.Fatalf("Unable to setup p2p server: %v", err)
|
|
}
|
|
|
|
body := []byte{1, 2, 3, 4, 5}
|
|
shardID := big.NewInt(0)
|
|
chunkRoot := types.DeriveSha(sharding.Chunks(body))
|
|
period := big.NewInt(0)
|
|
proposerAddress := common.BytesToAddress([]byte{})
|
|
|
|
header := sharding.NewCollationHeader(shardID, &chunkRoot, period, &proposerAddress, [32]byte{})
|
|
|
|
// Stores the collation into the inmemory kv store shardChainDB.
|
|
collation := sharding.NewCollation(header, body, nil)
|
|
|
|
shard := sharding.NewShard(shardID, shardChainDB.DB())
|
|
|
|
if err := shard.SaveCollation(collation); err != nil {
|
|
t.Fatalf("Could not store collation in shardChainDB: %v", err)
|
|
}
|
|
|
|
syncer, err := NewSyncer(params.DefaultConfig, &mainchain.SMCClient{}, server, shardChainDB, 0)
|
|
if err != nil {
|
|
t.Fatalf("Unable to setup syncer service: %v", err)
|
|
}
|
|
|
|
feed := server.Feed(messages.CollationBodyRequest{})
|
|
|
|
syncer.msgChan = make(chan p2p.Message)
|
|
syncer.errChan = make(chan error)
|
|
syncer.bodyRequests = feed.Subscribe(syncer.msgChan)
|
|
|
|
go syncer.HandleCollationBodyRequests(shard)
|
|
|
|
msg := p2p.Message{
|
|
Peer: p2p.Peer{},
|
|
Data: messages.CollationBodyRequest{
|
|
ChunkRoot: &chunkRoot,
|
|
ShardID: shardID,
|
|
Period: period,
|
|
Proposer: &proposerAddress,
|
|
},
|
|
}
|
|
syncer.msgChan <- msg
|
|
|
|
h.VerifyLogMsg(fmt.Sprintf("Received p2p request of type: %T", p2p.Message{}))
|
|
h.VerifyLogMsg(fmt.Sprintf("Responding to p2p request with collation with headerHash: %v", header.Hash().Hex()))
|
|
|
|
syncer.cancel()
|
|
// The context should have been canceled.
|
|
if syncer.ctx.Err() == nil {
|
|
t.Error("Context was not canceled")
|
|
}
|
|
}
|