Upgrade geth and cleanup

This commit is contained in:
Aleksandr Bezobchuk
2018-09-28 17:40:58 -04:00
parent 6e1a73bcad
commit 85ca203972
11 changed files with 114 additions and 80 deletions
+2 -1
View File
@@ -9,7 +9,8 @@ import (
"github.com/ethereum/go-ethereum/rpc"
)
// returns the master list of public APIs for use with StartHTTPEndpoint
// GetRPCAPIs returns the master list of public APIs for use with
// StartHTTPEndpoint.
func GetRPCAPIs() []rpc.API {
return []rpc.API{
{
+13 -2
View File
@@ -2,10 +2,13 @@ package rpc
import (
"context"
"testing"
"time"
"github.com/cosmos/ethermint/version"
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"testing"
)
type apisTestSuite struct {
@@ -56,10 +59,18 @@ func startAPIServer() (context.CancelFunc, int, error) {
RPCAddr: "127.0.0.1",
RPCPort: randomPort(),
}
timeouts := rpc.HTTPTimeouts{
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
IdleTimeout: 5 * time.Second,
}
ctx, cancel := context.WithCancel(context.Background())
_, err := StartHTTPEndpoint(ctx, config, GetRPCAPIs())
_, err := StartHTTPEndpoint(ctx, config, GetRPCAPIs(), timeouts)
if err != nil {
return cancel, 0, err
}
return cancel, config.RPCPort, nil
}
+9 -4
View File
@@ -3,16 +3,19 @@ package rpc
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/rpc"
)
// StartHTTPEndpoint starts the Tendermint Web3-compatible RPC layer. Consumes a Context for cancellation, a config
// struct, and a list of rpc.API interfaces that will be automatically wired into a JSON-RPC webserver.
func StartHTTPEndpoint(ctx context.Context, config *Config, apis []rpc.API) (*rpc.Server, error) {
// StartHTTPEndpoint starts the Tendermint Web3-compatible RPC layer. Consumes
// a Context for cancellation, a config struct, and a list of rpc.API interfaces
// that will be automatically wired into a JSON-RPC webserver.
func StartHTTPEndpoint(ctx context.Context, config *Config, apis []rpc.API, timeouts rpc.HTTPTimeouts) (*rpc.Server, error) {
uniqModules := make(map[string]string)
for _, api := range apis {
uniqModules[api.Namespace] = api.Namespace
}
modules := make([]string, len(uniqModules))
i := 0
for k := range uniqModules {
@@ -21,7 +24,9 @@ func StartHTTPEndpoint(ctx context.Context, config *Config, apis []rpc.API) (*rp
}
endpoint := fmt.Sprintf("%s:%d", config.RPCAddr, config.RPCPort)
_, server, err := rpc.StartHTTPEndpoint(endpoint, apis, modules, config.RPCCORSDomains, config.RPCVHosts)
_, server, err := rpc.StartHTTPEndpoint(
endpoint, apis, modules, config.RPCCORSDomains, config.RPCVHosts, timeouts,
)
go func() {
<-ctx.Done()
+31 -11
View File
@@ -4,13 +4,15 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/require"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"testing"
"time"
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/require"
)
type TestService struct{}
@@ -24,21 +26,34 @@ func TestStartHTTPEndpointStartStop(t *testing.T) {
RPCAddr: "127.0.0.1",
RPCPort: randomPort(),
}
ctx, cancel := context.WithCancel(context.Background())
_, err := StartHTTPEndpoint(ctx, config, []rpc.API{
{
Namespace: "test",
Version: "1.0",
Service: &TestService{},
Public: true,
_, err := StartHTTPEndpoint(
ctx, config, []rpc.API{
{
Namespace: "test",
Version: "1.0",
Service: &TestService{},
Public: true,
},
},
})
rpc.HTTPTimeouts{
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
IdleTimeout: 5 * time.Second,
},
)
require.Nil(t, err, "unexpected error")
res, err := rpcCall(config.RPCPort, "test_foo", []string{"baz"})
require.Nil(t, err, "unexpected error")
resStr := res.(string)
require.Equal(t, "baz", resStr)
cancel()
_, err = rpcCall(config.RPCPort, "test_foo", []string{"baz"})
require.NotNil(t, err)
}
@@ -48,12 +63,17 @@ func rpcCall(port int, method string, params []string) (interface{}, error) {
if err != nil {
return nil, err
}
fullBody := fmt.Sprintf(`{ "id": 1, "jsonrpc": "2.0", "method": "%s", "params": %s }`,
method, string(parsedParams))
fullBody := fmt.Sprintf(
`{ "id": 1, "jsonrpc": "2.0", "method": "%s", "params": %s }`,
method, string(parsedParams),
)
res, err := http.Post(fmt.Sprintf("http://127.0.0.1:%d", port), "application/json", strings.NewReader(fullBody))
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err