2020-07-01 16:29:09 +00:00
|
|
|
package testkit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-07-07 12:58:09 +00:00
|
|
|
"time"
|
2020-07-01 16:29:09 +00:00
|
|
|
|
2020-07-07 12:58:09 +00:00
|
|
|
"contrib.go.opencensus.io/exporter/prometheus"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
|
|
|
"github.com/filecoin-project/go-jsonrpc/auth"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-10-13 16:42:23 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2022-06-14 17:21:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/wallet/key"
|
2020-07-01 16:29:09 +00:00
|
|
|
"github.com/filecoin-project/lotus/node"
|
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LotusClient struct {
|
|
|
|
*LotusNode
|
|
|
|
|
|
|
|
t *TestEnvironment
|
|
|
|
MinerAddrs []MinerAddressesMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrepareClient(t *TestEnvironment) (*LotusClient, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), PrepareNodeTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-07-27 13:58:27 +00:00
|
|
|
ApplyNetworkParameters(t)
|
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
pubsubTracer, err := GetPubsubTracerMaddr(ctx, t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
drandOpt, err := GetRandomBeaconOpts(ctx, t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// first create a wallet
|
2022-06-14 17:21:34 +00:00
|
|
|
walletKey, err := key.GenerateKey(types.KTBLS)
|
2020-07-01 16:29:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// publish the account ID/balance
|
2020-07-07 21:02:29 +00:00
|
|
|
balance := t.FloatParam("balance")
|
2020-07-01 16:29:09 +00:00
|
|
|
balanceMsg := &InitialBalanceMsg{Addr: walletKey.Address, Balance: balance}
|
|
|
|
t.SyncClient.Publish(ctx, BalanceTopic, balanceMsg)
|
|
|
|
|
|
|
|
// then collect the genesis block and bootstrapper address
|
|
|
|
genesisMsg, err := WaitForGenesis(t, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
clientIP := t.NetClient.MustGetDataNetworkIP().String()
|
|
|
|
|
|
|
|
nodeRepo := repo.NewMemory(nil)
|
|
|
|
|
|
|
|
// create the node
|
|
|
|
n := &LotusNode{}
|
|
|
|
stop, err := node.New(context.Background(),
|
|
|
|
node.FullAPI(&n.FullApi),
|
2021-07-07 11:56:37 +00:00
|
|
|
node.Base(),
|
2020-07-01 16:29:09 +00:00
|
|
|
node.Repo(nodeRepo),
|
2020-07-06 14:47:17 +00:00
|
|
|
withApiEndpoint(fmt.Sprintf("/ip4/0.0.0.0/tcp/%s", t.PortNumber("node_rpc", "0"))),
|
2020-07-01 16:29:09 +00:00
|
|
|
withGenesis(genesisMsg.Genesis),
|
|
|
|
withListenAddress(clientIP),
|
|
|
|
withBootstrapper(genesisMsg.Bootstrapper),
|
|
|
|
withPubsubConfig(false, pubsubTracer),
|
|
|
|
drandOpt,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the wallet
|
|
|
|
err = n.setWallet(ctx, walletKey)
|
|
|
|
if err != nil {
|
|
|
|
_ = stop(context.TODO())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-27 11:57:01 +00:00
|
|
|
fullSrv, err := startFullNodeAPIServer(t, nodeRepo, n.FullApi)
|
2020-07-01 16:29:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-27 11:57:01 +00:00
|
|
|
n.StopFn = func(ctx context.Context) error {
|
|
|
|
var err *multierror.Error
|
|
|
|
err = multierror.Append(fullSrv.Shutdown(ctx))
|
|
|
|
err = multierror.Append(stop(ctx))
|
|
|
|
return err.ErrorOrNil()
|
|
|
|
}
|
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
registerAndExportMetrics(fmt.Sprintf("client_%d", t.GroupSeq))
|
|
|
|
|
|
|
|
t.RecordMessage("publish our address to the clients addr topic")
|
|
|
|
addrinfo, err := n.FullApi.NetAddrsListen(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-07 12:58:09 +00:00
|
|
|
t.SyncClient.MustPublish(ctx, ClientsAddrsTopic, &ClientAddressesMsg{
|
2020-07-07 16:38:15 +00:00
|
|
|
PeerNetAddr: addrinfo,
|
|
|
|
WalletAddr: walletKey.Address,
|
2020-08-28 12:36:20 +00:00
|
|
|
GroupSeq: t.GroupSeq,
|
2020-07-07 12:58:09 +00:00
|
|
|
})
|
2020-07-01 16:29:09 +00:00
|
|
|
|
|
|
|
t.RecordMessage("waiting for all nodes to be ready")
|
|
|
|
t.SyncClient.MustSignalAndWait(ctx, StateReady, t.TestInstanceCount)
|
|
|
|
|
|
|
|
// collect miner addresses.
|
|
|
|
addrs, err := CollectMinerAddrs(t, ctx, t.IntParam("miners"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
t.RecordMessage("got %v miner addrs", len(addrs))
|
|
|
|
|
2020-07-07 12:58:09 +00:00
|
|
|
// densely connect the client to the full node and the miners themselves.
|
|
|
|
for _, miner := range addrs {
|
|
|
|
if err := n.FullApi.NetConnect(ctx, miner.FullNetAddrs); err != nil {
|
|
|
|
return nil, fmt.Errorf("client failed to connect to full node of miner: %w", err)
|
|
|
|
}
|
|
|
|
if err := n.FullApi.NetConnect(ctx, miner.MinerNetAddrs); err != nil {
|
|
|
|
return nil, fmt.Errorf("client failed to connect to storage miner node node of miner: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for all clients to have completed identify, pubsub negotiation with miners.
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
peers, err := n.FullApi.NetPeers(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to query connected peers: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.RecordMessage("connected peers: %d", len(peers))
|
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
cl := &LotusClient{
|
|
|
|
t: t,
|
|
|
|
LotusNode: n,
|
|
|
|
MinerAddrs: addrs,
|
|
|
|
}
|
|
|
|
return cl, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LotusClient) RunDefault() error {
|
|
|
|
// run forever
|
|
|
|
c.t.RecordMessage("running default client forever")
|
|
|
|
c.t.WaitUntilAllDone()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:26:30 +00:00
|
|
|
func startFullNodeAPIServer(t *TestEnvironment, repo repo.Repo, napi api.FullNode) (*http.Server, error) {
|
2020-07-27 11:57:01 +00:00
|
|
|
mux := mux.NewRouter()
|
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
rpcServer := jsonrpc.NewServer()
|
2021-03-25 14:26:30 +00:00
|
|
|
rpcServer.Register("Filecoin", napi)
|
2020-07-01 16:29:09 +00:00
|
|
|
|
2020-07-27 11:57:01 +00:00
|
|
|
mux.Handle("/rpc/v0", rpcServer)
|
2020-07-01 16:29:09 +00:00
|
|
|
|
2020-07-07 12:58:09 +00:00
|
|
|
exporter, err := prometheus.NewExporter(prometheus.Options{
|
|
|
|
Namespace: "lotus",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-07-27 11:57:01 +00:00
|
|
|
return nil, err
|
2020-07-07 12:58:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 11:57:01 +00:00
|
|
|
mux.Handle("/debug/metrics", exporter)
|
|
|
|
|
|
|
|
ah := &auth.Handler{
|
|
|
|
Verify: func(ctx context.Context, token string) ([]auth.Permission, error) {
|
2021-03-25 14:26:30 +00:00
|
|
|
return api.AllPermissions, nil
|
2020-07-27 11:57:01 +00:00
|
|
|
},
|
|
|
|
Next: mux.ServeHTTP,
|
|
|
|
}
|
2020-07-07 12:58:09 +00:00
|
|
|
|
2022-11-25 21:24:09 +00:00
|
|
|
srv := &http.Server{Handler: ah, ReadHeaderTimeout: 3 * time.Second}
|
2020-07-01 16:29:09 +00:00
|
|
|
|
2020-07-06 14:47:17 +00:00
|
|
|
endpoint, err := repo.APIEndpoint()
|
|
|
|
if err != nil {
|
2020-07-27 11:57:01 +00:00
|
|
|
return nil, fmt.Errorf("no API endpoint in repo: %w", err)
|
2020-07-06 14:47:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listenAddr, err := startServer(endpoint, srv)
|
|
|
|
if err != nil {
|
2020-07-27 11:57:01 +00:00
|
|
|
return nil, fmt.Errorf("failed to start client API endpoint: %w", err)
|
2020-07-06 14:47:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.RecordMessage("started node API server at %s", listenAddr)
|
2020-07-27 11:57:01 +00:00
|
|
|
return srv, nil
|
2020-07-01 16:29:09 +00:00
|
|
|
}
|