Skip to content

Commit

Permalink
Merge pull request #1649 from livepeer/nv/fix-activate-O
Browse files Browse the repository at this point in the history
server: fix current round locked check in activateOrchestrator
  • Loading branch information
kyriediculous authored Oct 30, 2020
2 parents ce3872b + 2fd9600 commit 2d64faf
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 3 deletions.
4 changes: 3 additions & 1 deletion eth/stubclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ type StubClient struct {
CheckTxErr error
TotalStake *big.Int
TranscoderPoolError error
RoundLocked bool
RoundLockedErr error
}

type stubTranscoder struct {
Expand All @@ -200,7 +202,7 @@ func (e *StubClient) BlockHashForRound(round *big.Int) ([32]byte, error) {
return e.BlockHashToReturn, nil
}
func (e *StubClient) CurrentRoundInitialized() (bool, error) { return false, nil }
func (e *StubClient) CurrentRoundLocked() (bool, error) { return false, nil }
func (e *StubClient) CurrentRoundLocked() (bool, error) { return e.RoundLocked, e.RoundLockedErr }
func (e *StubClient) CurrentRoundStartBlock() (*big.Int, error) { return nil, nil }
func (e *StubClient) Paused() (bool, error) { return false, nil }

Expand Down
156 changes: 156 additions & 0 deletions server/cliserver_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package server

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"net/http/httptest"
"net/url"
"runtime"
"strconv"
"strings"
"testing"
"time"

Expand All @@ -35,6 +39,158 @@ func newMockServer() *httptest.Server {
return srv
}

func TestActivateOrchestrator(t *testing.T) {
require := require.New(t)
assert := assert.New(t)
eth := &eth.StubClient{
Orch: &lpTypes.Transcoder{},
}
n, _ := core.NewLivepeerNode(eth, "./tmp", nil)
n.NodeType = core.TranscoderNode
n.TranscoderManager = core.NewRemoteTranscoderManager()
strm := &common.StubServerStream{}
go func() { n.TranscoderManager.Manage(strm, 5) }()
time.Sleep(1 * time.Millisecond)
n.Transcoder = n.TranscoderManager
s, _ := NewLivepeerServer("127.0.0.1:1938", n, true, "")
mux := s.cliWebServerHandlers("addr")
srv := httptest.NewServer(mux)
defer srv.Close()

var (
blockRewardCut int = 5
feeShare int = 10
pricePerUnit int = 1
pixelsPerUnit int = 1
serviceURI string = "http://foo.bar:1337"
)

form := url.Values{
"blockRewardCut": {fmt.Sprintf("%v", blockRewardCut)},
"feeShare": {fmt.Sprintf("%v", feeShare)},
"pricePerUnit": {fmt.Sprintf("%v", strconv.Itoa(pricePerUnit))},
"pixelsPerUnit": {fmt.Sprintf("%v", strconv.Itoa(pixelsPerUnit))},
"serviceURI": {fmt.Sprintf("%v", serviceURI)},
}

// Test GetTranscoderError
eth.Err = errors.New("GetTranscoder error")
req := bytes.NewBufferString(form.Encode())
res, err := http.Post(fmt.Sprintf("%s/activateOrchestrator", srv.URL), "application/x-www-form-urlencoded", req)
require.Equal(http.StatusInternalServerError, res.StatusCode)
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
require.Nil(err)
assert.Equal(strings.TrimSpace(string(body)), eth.Err.Error())
eth.Err = nil

// Test Transcoder Registered
eth.Orch.Status = "Registered"
req = bytes.NewBufferString(form.Encode())
res, err = http.Post(fmt.Sprintf("%s/activateOrchestrator", srv.URL), "application/x-www-form-urlencoded", req)
require.Equal(http.StatusBadRequest, res.StatusCode)
body, err = ioutil.ReadAll(res.Body)
res.Body.Close()
require.Nil(err)
assert.Equal(strings.TrimSpace(string(body)), "orchestrator already registered")
eth.Orch.Status = ""

// Test CurrentRoundLocked error
eth.RoundLockedErr = errors.New("CurrentRoundLocked error")
req = bytes.NewBufferString(form.Encode())
res, err = http.Post(fmt.Sprintf("%s/activateOrchestrator", srv.URL), "application/x-www-form-urlencoded", req)
require.Equal(http.StatusInternalServerError, res.StatusCode)
body, err = ioutil.ReadAll(res.Body)
res.Body.Close()
require.Nil(err)
assert.Equal(strings.TrimSpace(string(body)), eth.RoundLockedErr.Error())
eth.RoundLockedErr = nil

// Test Round Locked
eth.RoundLocked = true
req = bytes.NewBufferString(form.Encode())
res, err = http.Post(fmt.Sprintf("%s/activateOrchestrator", srv.URL), "application/x-www-form-urlencoded", req)
require.Equal(http.StatusInternalServerError, res.StatusCode)
body, err = ioutil.ReadAll(res.Body)
res.Body.Close()
require.Nil(err)
assert.Equal(strings.TrimSpace(string(body)), "current round is locked")
eth.RoundLocked = false

// Test no block reward cut
form["blockRewardCut"] = []string{""}
req = bytes.NewBufferString(form.Encode())
res, err = http.Post(fmt.Sprintf("%s/activateOrchestrator", srv.URL), "application/x-www-form-urlencoded", req)
require.Equal(http.StatusBadRequest, res.StatusCode)
body, err = ioutil.ReadAll(res.Body)
res.Body.Close()
require.Nil(err)
assert.Equal(strings.TrimSpace(string(body)), "Need to provide block reward cut")

// Test invalid block reward cut
form["blockRewardCut"] = []string{"foo"}
req = bytes.NewBufferString(form.Encode())
res, err = http.Post(fmt.Sprintf("%s/activateOrchestrator", srv.URL), "application/x-www-form-urlencoded", req)
require.Equal(http.StatusBadRequest, res.StatusCode)
body, err = ioutil.ReadAll(res.Body)
res.Body.Close()
require.Nil(err)
assert.Equal(strings.TrimSpace(string(body)), "strconv.ParseFloat: parsing \"foo\": invalid syntax")
form["blockRewardCut"] = []string{"5"}

// Test no feeshare
form["feeShare"] = []string{""}
req = bytes.NewBufferString(form.Encode())
res, err = http.Post(fmt.Sprintf("%s/activateOrchestrator", srv.URL), "application/x-www-form-urlencoded", req)
require.Equal(http.StatusBadRequest, res.StatusCode)
body, err = ioutil.ReadAll(res.Body)
res.Body.Close()
require.Nil(err)
assert.Equal(strings.TrimSpace(string(body)), "Need to provide fee share")

// Test invalid feeshare
form["feeShare"] = []string{"foo"}
req = bytes.NewBufferString(form.Encode())
res, err = http.Post(fmt.Sprintf("%s/activateOrchestrator", srv.URL), "application/x-www-form-urlencoded", req)
require.Equal(http.StatusBadRequest, res.StatusCode)
body, err = ioutil.ReadAll(res.Body)
res.Body.Close()
require.Nil(err)
assert.Equal(strings.TrimSpace(string(body)), "strconv.ParseFloat: parsing \"foo\": invalid syntax")
form["feeShare"] = []string{"10"}

// setOrchestratorPriceInfo is tested in webserver_test.go seperately

// Test no serviceURI
form["serviceURI"] = []string{""}
req = bytes.NewBufferString(form.Encode())
res, err = http.Post(fmt.Sprintf("%s/activateOrchestrator", srv.URL), "application/x-www-form-urlencoded", req)
require.Equal(http.StatusBadRequest, res.StatusCode)
body, err = ioutil.ReadAll(res.Body)
res.Body.Close()
require.Nil(err)
assert.Equal(strings.TrimSpace(string(body)), "Need to provide a service URI")

// Test invalid ServiceURI
form["serviceURI"] = []string{"hello world"}
req = bytes.NewBufferString(form.Encode())
res, err = http.Post(fmt.Sprintf("%s/activateOrchestrator", srv.URL), "application/x-www-form-urlencoded", req)
require.Equal(http.StatusBadRequest, res.StatusCode)
body, err = ioutil.ReadAll(res.Body)
res.Body.Close()
require.Nil(err)
assert.Equal(strings.TrimSpace(string(body)), "parse hello world: invalid URI for request")
form["serviceURI"] = []string{"http://foo.bar:1337"}

req = bytes.NewBufferString(form.Encode())
res, err = http.Post(fmt.Sprintf("%s/activateOrchestrator", srv.URL), "application/x-www-form-urlencoded", req)
require.Equal(http.StatusOK, res.StatusCode)
body, err = ioutil.ReadAll(res.Body)
res.Body.Close()
require.Nil(err)
assert.Equal(strings.TrimSpace(string(body)), "success")
}

func TestGetStatus(t *testing.T) {
srv := newMockServer()
defer srv.Close()
Expand Down
4 changes: 2 additions & 2 deletions server/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ func (s *LivepeerServer) cliWebServerHandlers(bindAddr string) *http.ServeMux {
return
}

ok, err := s.LivepeerNode.Eth.CurrentRoundLocked()
isLocked, err := s.LivepeerNode.Eth.CurrentRoundLocked()
if err != nil {
respondWith500(w, err.Error())
return
}
if !ok {
if isLocked {
respondWith500(w, "current round is locked")
return
}
Expand Down

0 comments on commit 2d64faf

Please sign in to comment.