2018-07-14 19:48:42 +00:00
|
|
|
package powchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-07-14 19:48:42 +00:00
|
|
|
gethTypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
type badReader struct{}
|
|
|
|
|
|
|
|
func (b *badReader) SubscribeNewHead(ctx context.Context, ch chan<- *gethTypes.Header) (ethereum.Subscription, error) {
|
|
|
|
return nil, errors.New("subscription has failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
type goodReader struct{}
|
|
|
|
|
|
|
|
func (g *goodReader) SubscribeNewHead(ctx context.Context, ch chan<- *gethTypes.Header) (ethereum.Subscription, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
type badLogger struct{}
|
|
|
|
|
|
|
|
func (b *badLogger) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- gethTypes.Log) (ethereum.Subscription, error) {
|
|
|
|
return nil, errors.New("subscription has failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
type goodLogger struct{}
|
|
|
|
|
|
|
|
func (g *goodLogger) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- gethTypes.Log) (ethereum.Subscription, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-07-14 19:48:42 +00:00
|
|
|
func TestNewWeb3Service(t *testing.T) {
|
|
|
|
endpoint := "http://127.0.0.1"
|
|
|
|
ctx := context.Background()
|
2018-07-19 16:31:50 +00:00
|
|
|
if _, err := NewWeb3Service(ctx, &Web3ServiceConfig{endpoint, "", common.Address{}}); err == nil {
|
2018-07-14 19:48:42 +00:00
|
|
|
t.Errorf("passing in an HTTP endpoint should throw an error, received nil")
|
|
|
|
}
|
|
|
|
endpoint = "ftp://127.0.0.1"
|
2018-07-19 16:31:50 +00:00
|
|
|
if _, err := NewWeb3Service(ctx, &Web3ServiceConfig{endpoint, "", common.Address{}}); err == nil {
|
2018-07-14 19:48:42 +00:00
|
|
|
t.Errorf("passing in a non-ws, wss, or ipc endpoint should throw an error, received nil")
|
|
|
|
}
|
|
|
|
endpoint = "ws://127.0.0.1"
|
2018-07-19 16:31:50 +00:00
|
|
|
if _, err := NewWeb3Service(ctx, &Web3ServiceConfig{endpoint, "", common.Address{}}); err != nil {
|
2018-07-14 19:48:42 +00:00
|
|
|
t.Errorf("passing in as ws endpoint should not throw error, received %v", err)
|
|
|
|
}
|
|
|
|
endpoint = "ipc://geth.ipc"
|
2018-07-19 16:31:50 +00:00
|
|
|
if _, err := NewWeb3Service(ctx, &Web3ServiceConfig{endpoint, "", common.Address{}}); err != nil {
|
2018-07-14 19:48:42 +00:00
|
|
|
t.Errorf("passing in an ipc endpoint should not throw error, received %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStart(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
|
|
|
endpoint := "ws://127.0.0.1"
|
2018-07-19 16:31:50 +00:00
|
|
|
web3Service, err := NewWeb3Service(context.Background(), &Web3ServiceConfig{endpoint, "", common.Address{}})
|
2018-07-14 19:48:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to setup web3 PoW chain service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
web3Service.Start()
|
|
|
|
|
|
|
|
msg := hook.LastEntry().Message
|
|
|
|
want := "Cannot connect to PoW chain RPC client"
|
|
|
|
if strings.Contains(want, msg) {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
|
|
}
|
|
|
|
hook.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStop(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
|
|
|
endpoint := "ws://127.0.0.1"
|
2018-07-19 16:31:50 +00:00
|
|
|
web3Service, err := NewWeb3Service(context.Background(), &Web3ServiceConfig{endpoint, "", common.Address{}})
|
2018-07-14 19:48:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to setup web3 PoW chain service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := web3Service.Stop(); err != nil {
|
|
|
|
t.Fatalf("Unable to stop web3 PoW chain service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := hook.LastEntry().Message
|
2018-07-19 16:31:50 +00:00
|
|
|
want := "Stopping web3 proof-of-work chain service"
|
2018-07-14 19:48:42 +00:00
|
|
|
if msg != want {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The context should have been canceled.
|
|
|
|
if web3Service.ctx.Err() == nil {
|
|
|
|
t.Error("context was not canceled")
|
|
|
|
}
|
|
|
|
hook.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadReader(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
endpoint := "ws://127.0.0.1"
|
2018-07-19 16:31:50 +00:00
|
|
|
web3Service, err := NewWeb3Service(context.Background(), &Web3ServiceConfig{endpoint, "", common.Address{}})
|
2018-07-14 19:48:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to setup web3 PoW chain service: %v", err)
|
|
|
|
}
|
|
|
|
web3Service.latestPOWChainInfo(&badReader{}, web3Service.ctx.Done())
|
|
|
|
msg := hook.LastEntry().Message
|
|
|
|
want := "Unable to subscribe to incoming PoW chain headers: subscription has failed"
|
|
|
|
if msg != want {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
|
|
}
|
|
|
|
hook.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLatestMainchainInfo(t *testing.T) {
|
|
|
|
endpoint := "ws://127.0.0.1"
|
2018-07-19 16:31:50 +00:00
|
|
|
web3Service, err := NewWeb3Service(context.Background(), &Web3ServiceConfig{endpoint, "", common.Address{}})
|
2018-07-14 19:48:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to setup web3 PoW chain service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doneChan := make(chan struct{})
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
web3Service.latestPOWChainInfo(&goodReader{}, doneChan)
|
|
|
|
<-exitRoutine
|
|
|
|
}()
|
|
|
|
|
|
|
|
header := &gethTypes.Header{Number: big.NewInt(42)}
|
|
|
|
|
|
|
|
web3Service.headerChan <- header
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
exitRoutine <- true
|
|
|
|
|
|
|
|
if web3Service.blockNumber.Cmp(header.Number) != 0 {
|
|
|
|
t.Errorf("block number not set, expected %v, got %v", header.Number, web3Service.blockNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
if web3Service.blockHash.Hex() != header.Hash().Hex() {
|
|
|
|
t.Errorf("block hash not set, expected %v, got %v", header.Hash().Hex(), web3Service.blockHash.Hex())
|
|
|
|
}
|
|
|
|
}
|
2018-07-19 16:31:50 +00:00
|
|
|
|
|
|
|
func TestBadLogger(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
endpoint := "ws://127.0.0.1"
|
|
|
|
web3Service, err := NewWeb3Service(context.Background(), &Web3ServiceConfig{endpoint, "", common.Address{}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to setup web3 PoW chain service: %v", err)
|
|
|
|
}
|
|
|
|
web3Service.queryValidatorStatus(&badLogger{}, web3Service.ctx.Done())
|
|
|
|
msg := hook.LastEntry().Message
|
|
|
|
want := "Unable to query logs from VRC: subscription has failed"
|
|
|
|
if msg != want {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
|
|
}
|
|
|
|
hook.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGoodLogger(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
endpoint := "ws://127.0.0.1"
|
|
|
|
web3Service, err := NewWeb3Service(context.Background(), &Web3ServiceConfig{endpoint, "", common.Address{}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to setup web3 PoW chain service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
web3Service.pubKey = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
|
|
|
pubkey := common.HexToHash(web3Service.pubKey)
|
|
|
|
|
|
|
|
doneChan := make(chan struct{})
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
web3Service.queryValidatorStatus(&goodLogger{}, doneChan)
|
|
|
|
<-exitRoutine
|
|
|
|
}()
|
|
|
|
|
|
|
|
log := gethTypes.Log{Topics: []common.Hash{[32]byte{}, pubkey}}
|
|
|
|
web3Service.logChan <- log
|
|
|
|
exitRoutine <- true
|
|
|
|
|
2018-07-21 17:51:18 +00:00
|
|
|
lastEntry := hook.LastEntry()
|
|
|
|
want := "Validator registered in VRC with public key"
|
|
|
|
if lastEntry.Message != want {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, lastEntry.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastEntry.Data["publicKey"] != web3Service.pubKey {
|
|
|
|
t.Errorf("incorrect pubKey, expected %s, got %s", lastEntry.Data["publicKey"], web3Service.pubKey)
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !web3Service.validatorRegistered {
|
2018-07-21 17:51:18 +00:00
|
|
|
t.Errorf("validatorRegistered status expected true, got %v", web3Service.validatorRegistered)
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hook.Reset()
|
|
|
|
}
|