prysm-pulse/beacon-chain/rpc/beacon/validators_stream_test.go
Victor Farazdagi f84e5ad6ef
Fix logged errs (#5645)
* fuzz tests, validators stream, and attestation fixed
* hastst
* signinig root fuzz test
* Merge refs/heads/master into fix-logged-errs
* Merge branch 'master' into fix-logged-errs
* Merge branch 'fix-logged-errs' of github.com:prysmaticlabs/prysm into fix-logged-errs
* add head fetchers to inforstream tests
2020-04-28 00:22:37 +00:00

187 lines
3.9 KiB
Go

package beacon
import (
"sync"
"testing"
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
)
func TestInfostream_EpochToTimestamp(t *testing.T) {
params.UseMainnetConfig()
tests := []struct {
name string
epoch uint64
timestamp uint64
}{
{
name: "Genesis",
epoch: 0,
timestamp: 0,
},
{
name: "One",
epoch: 1,
timestamp: 384,
},
{
name: "Two",
epoch: 2,
timestamp: 768,
},
{
name: "OneHundred",
epoch: 100,
timestamp: 38400,
},
}
is := &infostream{}
for _, test := range tests {
timestamp := is.epochToTimestamp(test.epoch)
if timestamp != test.timestamp {
t.Errorf("Incorrect timestamp: expected %v, received %v", test.timestamp, timestamp)
}
}
}
func TestInfostream_HandleSetValidatorKeys(t *testing.T) {
params.UseMainnetConfig()
tests := []struct {
name string
reqPubKeys [][]byte
}{
{
name: "None",
},
{
name: "One",
reqPubKeys: [][]byte{{0x01}},
},
{
name: "Two",
reqPubKeys: [][]byte{{0x01}, {0x02}},
},
}
is := &infostream{
pubKeysMutex: &sync.RWMutex{},
pubKeys: make([][]byte, 0),
headFetcher: &mock.ChainService{
State: testutil.NewBeaconState(),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if err := is.handleSetValidatorKeys(test.reqPubKeys); err != nil {
t.Error(err)
}
if len(is.pubKeys) != len(test.reqPubKeys) {
t.Errorf("Incorrect number of keys: expected %v, received %v", len(test.reqPubKeys), len(is.pubKeys))
}
})
}
}
func TestInfostream_HandleAddValidatorKeys(t *testing.T) {
params.UseMainnetConfig()
tests := []struct {
name string
initialPubKeys [][]byte
reqPubKeys [][]byte
finalLen int
}{
{
name: "None",
finalLen: 0,
},
{
name: "NoneAddOne",
reqPubKeys: [][]byte{{0x01}},
finalLen: 1,
},
{
name: "OneAddOne",
initialPubKeys: [][]byte{{0x01}},
reqPubKeys: [][]byte{{0x02}},
finalLen: 2,
},
{
name: "Duplicate",
initialPubKeys: [][]byte{{0x01}},
reqPubKeys: [][]byte{{0x01}},
finalLen: 1,
},
}
is := &infostream{
pubKeysMutex: &sync.RWMutex{},
pubKeys: make([][]byte, 0),
headFetcher: &mock.ChainService{
State: testutil.NewBeaconState(),
},
}
for _, test := range tests {
if err := is.handleSetValidatorKeys(test.initialPubKeys); err != nil {
t.Error(err)
}
if err := is.handleAddValidatorKeys(test.reqPubKeys); err != nil {
t.Error(err)
}
if len(is.pubKeys) != test.finalLen {
t.Errorf("Incorrect number of keys: expected %v, received %v", len(is.pubKeys), test.finalLen)
}
}
}
func TestInfostream_HandleRemoveValidatorKeys(t *testing.T) {
params.UseMainnetConfig()
tests := []struct {
name string
initialPubKeys [][]byte
reqPubKeys [][]byte
finalLen int
}{
{
name: "None",
finalLen: 0,
},
{
name: "OneRemoveNone",
initialPubKeys: [][]byte{{0x01}},
finalLen: 1,
},
{
name: "NoneRemoveOne",
initialPubKeys: [][]byte{},
reqPubKeys: [][]byte{{0x01}},
finalLen: 0,
},
{
name: "TwoRemoveOne",
initialPubKeys: [][]byte{{0x01, 0x02}},
reqPubKeys: [][]byte{{0x01}},
finalLen: 1,
},
}
is := &infostream{
pubKeysMutex: &sync.RWMutex{},
pubKeys: make([][]byte, 0),
headFetcher: &mock.ChainService{
State: testutil.NewBeaconState(),
},
}
for _, test := range tests {
if err := is.handleSetValidatorKeys(test.initialPubKeys); err != nil {
t.Error(err)
}
is.handleRemoveValidatorKeys(test.reqPubKeys)
if len(is.pubKeys) != test.finalLen {
t.Errorf("Incorrect number of keys: expected %v, received %v", len(is.pubKeys), test.finalLen)
}
}
}