2021-06-18 18:45:29 +00:00
|
|
|
package kit
|
2021-06-11 17:26:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-23 10:44:52 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-06-11 17:26:25 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api/client"
|
|
|
|
"github.com/filecoin-project/lotus/node"
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-06-23 10:44:52 +00:00
|
|
|
func CreateRPCServer(t *testing.T, handler http.Handler, listener net.Listener) (*httptest.Server, multiaddr.Multiaddr) {
|
2021-07-06 15:57:08 +00:00
|
|
|
testServ := &httptest.Server{
|
|
|
|
Listener: listener,
|
|
|
|
Config: &http.Server{Handler: handler},
|
2021-06-23 10:44:52 +00:00
|
|
|
}
|
2021-07-06 15:57:08 +00:00
|
|
|
testServ.Start()
|
|
|
|
|
2021-06-11 17:26:25 +00:00
|
|
|
t.Cleanup(testServ.Close)
|
|
|
|
t.Cleanup(testServ.CloseClientConnections)
|
|
|
|
|
|
|
|
addr := testServ.Listener.Addr()
|
|
|
|
maddr, err := manet.FromNetAddr(addr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return testServ, maddr
|
|
|
|
}
|
|
|
|
|
|
|
|
func fullRpc(t *testing.T, f *TestFullNode) *TestFullNode {
|
|
|
|
handler, err := node.FullNodeHandler(f.FullNode, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-06 15:57:08 +00:00
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
srv, maddr := CreateRPCServer(t, handler, l)
|
2021-06-11 17:26:25 +00:00
|
|
|
|
2021-06-21 22:24:59 +00:00
|
|
|
cl, stop, err := client.NewFullNodeRPCV1(context.Background(), "ws://"+srv.Listener.Addr().String()+"/rpc/v1", nil)
|
2021-06-11 17:26:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(stop)
|
|
|
|
f.ListenAddr, f.FullNode = maddr, cl
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func minerRpc(t *testing.T, m *TestMiner) *TestMiner {
|
|
|
|
handler, err := node.MinerHandler(m.StorageMiner, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-23 10:44:52 +00:00
|
|
|
srv, maddr := CreateRPCServer(t, handler, m.RemoteListener)
|
2021-06-11 17:26:25 +00:00
|
|
|
|
2021-06-23 10:44:52 +00:00
|
|
|
fmt.Println("creating RPC server for", m.ActorAddr, "at: ", srv.Listener.Addr().String())
|
|
|
|
url := "ws://" + srv.Listener.Addr().String() + "/rpc/v0"
|
|
|
|
cl, stop, err := client.NewStorageMinerRPCV0(context.Background(), url, nil)
|
2021-06-11 17:26:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(stop)
|
|
|
|
|
|
|
|
m.ListenAddr, m.StorageMiner = maddr, cl
|
|
|
|
return m
|
|
|
|
}
|