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"
|
2022-11-25 21:36:33 +00:00
|
|
|
"time"
|
2022-01-18 10:37:15 +00:00
|
|
|
|
2021-06-11 17:26:25 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-18 10:37:15 +00:00
|
|
|
|
2023-01-26 14:20:49 +00:00
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
|
|
|
|
2022-01-18 10:37:15 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/client"
|
2022-03-24 22:37:01 +00:00
|
|
|
"github.com/filecoin-project/lotus/cmd/lotus-worker/sealworker"
|
2022-01-18 10:37:15 +00:00
|
|
|
"github.com/filecoin-project/lotus/node"
|
2021-06-11 17:26:25 +00:00
|
|
|
)
|
|
|
|
|
2022-09-27 16:08:04 +00:00
|
|
|
type Closer func()
|
|
|
|
|
|
|
|
func CreateRPCServer(t *testing.T, handler http.Handler, listener net.Listener) (*httptest.Server, multiaddr.Multiaddr, Closer) {
|
2021-07-06 15:57:08 +00:00
|
|
|
testServ := &httptest.Server{
|
|
|
|
Listener: listener,
|
2022-11-25 21:36:33 +00:00
|
|
|
Config: &http.Server{
|
|
|
|
Handler: handler,
|
2022-11-29 15:31:45 +00:00
|
|
|
ReadHeaderTimeout: 30 * time.Second,
|
2022-11-25 21:36:33 +00:00
|
|
|
},
|
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
|
|
|
addr := testServ.Listener.Addr()
|
|
|
|
maddr, err := manet.FromNetAddr(addr)
|
|
|
|
require.NoError(t, err)
|
2022-09-27 16:08:04 +00:00
|
|
|
closer := func() {
|
|
|
|
testServ.CloseClientConnections()
|
|
|
|
testServ.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return testServ, maddr, closer
|
2021-06-11 17:26:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 16:08:04 +00:00
|
|
|
func fullRpc(t *testing.T, f *TestFullNode) (*TestFullNode, Closer) {
|
2021-06-11 17:26:25 +00:00
|
|
|
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)
|
|
|
|
|
2022-09-27 16:08:04 +00:00
|
|
|
srv, maddr, rpcCloser := CreateRPCServer(t, handler, l)
|
2022-02-08 13:28:13 +00:00
|
|
|
fmt.Printf("FULLNODE RPC ENV FOR CLI DEBUGGING `export FULLNODE_API_INFO=%s`\n", "ws://"+srv.Listener.Addr().String())
|
2022-03-10 18:31:36 +00:00
|
|
|
sendItestdNotif("FULLNODE_API_INFO", t.Name(), "ws://"+srv.Listener.Addr().String())
|
2021-06-11 17:26:25 +00:00
|
|
|
|
2023-01-26 14:20:49 +00:00
|
|
|
rpcOpts := []jsonrpc.Option{
|
|
|
|
jsonrpc.WithClientHandler("Filecoin", f.EthSubRouter),
|
|
|
|
jsonrpc.WithClientHandlerAlias("eth_subscription", "Filecoin.EthSubscription"),
|
|
|
|
}
|
|
|
|
|
|
|
|
cl, stop, err := client.NewFullNodeRPCV1(context.Background(), "ws://"+srv.Listener.Addr().String()+"/rpc/v1", nil, rpcOpts...)
|
2021-06-11 17:26:25 +00:00
|
|
|
require.NoError(t, err)
|
2022-11-07 15:14:56 +00:00
|
|
|
f.ListenAddr, f.ListenURL, f.FullNode = maddr, srv.URL, cl
|
2021-06-11 17:26:25 +00:00
|
|
|
|
2022-09-27 16:08:04 +00:00
|
|
|
return f, func() { stop(); rpcCloser() }
|
2021-06-11 17:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func minerRpc(t *testing.T, m *TestMiner) *TestMiner {
|
|
|
|
handler, err := node.MinerHandler(m.StorageMiner, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-27 16:08:04 +00:00
|
|
|
srv, maddr, _ := CreateRPCServer(t, handler, m.RemoteListener)
|
2021-06-11 17:26:25 +00:00
|
|
|
|
2022-02-08 13:28:13 +00:00
|
|
|
fmt.Printf("creating RPC server for %s at %s\n", m.ActorAddr, srv.Listener.Addr().String())
|
2022-02-08 14:06:42 +00:00
|
|
|
fmt.Printf("SP RPC ENV FOR CLI DEBUGGING `export MINER_API_INFO=%s`\n", "ws://"+srv.Listener.Addr().String())
|
2022-03-10 18:31:36 +00:00
|
|
|
sendItestdNotif("MINER_API_INFO", t.Name(), "ws://"+srv.Listener.Addr().String())
|
2022-02-08 13:28:13 +00:00
|
|
|
|
2021-06-23 10:44:52 +00:00
|
|
|
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
|
|
|
|
}
|
2022-01-14 13:11:04 +00:00
|
|
|
|
|
|
|
func workerRpc(t *testing.T, m *TestWorker) *TestWorker {
|
|
|
|
handler := sealworker.WorkerHandler(m.MinerNode.AuthVerify, m.FetchHandler, m.Worker, false)
|
|
|
|
|
2022-09-27 16:08:04 +00:00
|
|
|
srv, maddr, _ := CreateRPCServer(t, handler, m.RemoteListener)
|
2022-01-14 13:11:04 +00:00
|
|
|
|
|
|
|
fmt.Println("creating RPC server for a worker at: ", srv.Listener.Addr().String())
|
|
|
|
url := "ws://" + srv.Listener.Addr().String() + "/rpc/v0"
|
|
|
|
cl, stop, err := client.NewWorkerRPCV0(context.Background(), url, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(stop)
|
|
|
|
|
2023-03-06 13:30:22 +00:00
|
|
|
m.Stop = func(ctx context.Context) error {
|
|
|
|
srv.Close()
|
|
|
|
srv.CloseClientConnections()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-14 13:11:04 +00:00
|
|
|
m.ListenAddr, m.Worker = maddr, cl
|
|
|
|
return m
|
|
|
|
}
|