2020-07-01 16:29:09 +00:00
|
|
|
package testkit
|
2020-06-24 10:52:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-06-29 12:57:55 +00:00
|
|
|
"net/http"
|
2020-06-26 14:30:44 +00:00
|
|
|
"os"
|
|
|
|
"sort"
|
2020-06-24 10:52:23 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
2020-06-25 14:55:44 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/beacon"
|
2020-06-24 10:52:23 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
2020-06-30 16:02:02 +00:00
|
|
|
"github.com/filecoin-project/lotus/metrics"
|
2020-06-24 10:52:23 +00:00
|
|
|
"github.com/filecoin-project/lotus/node"
|
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
|
|
modtest "github.com/filecoin-project/lotus/node/modules/testing"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
|
|
|
saminer "github.com/filecoin-project/specs-actors/actors/builtin/miner"
|
2020-06-25 10:33:52 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/power"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/verifreg"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2020-06-30 16:02:02 +00:00
|
|
|
influxdb "github.com/kpacha/opencensus-influxdb"
|
2020-07-06 14:47:17 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2020-06-30 16:02:02 +00:00
|
|
|
|
2020-07-03 18:07:46 +00:00
|
|
|
tstats "github.com/filecoin-project/lotus/tools/stats"
|
2020-07-03 18:06:10 +00:00
|
|
|
|
2020-06-25 10:33:52 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2020-06-29 12:57:55 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr-net"
|
2020-06-30 16:02:02 +00:00
|
|
|
"go.opencensus.io/stats"
|
|
|
|
"go.opencensus.io/stats/view"
|
2020-06-24 10:52:23 +00:00
|
|
|
)
|
|
|
|
|
2020-06-25 10:33:52 +00:00
|
|
|
func init() {
|
2020-07-01 16:29:09 +00:00
|
|
|
_ = logging.SetLogLevel("*", "ERROR")
|
2020-06-25 10:33:52 +00:00
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
_ = os.Setenv("BELLMAN_NO_GPU", "1")
|
2020-06-25 10:33:52 +00:00
|
|
|
|
|
|
|
build.InsecurePoStValidation = true
|
|
|
|
build.DisableBuiltinAssets = true
|
|
|
|
|
|
|
|
power.ConsensusMinerMinPower = big.NewInt(2048)
|
|
|
|
saminer.SupportedProofTypes = map[abi.RegisteredSealProof]struct{}{
|
|
|
|
abi.RegisteredSealProof_StackedDrg2KiBV1: {},
|
|
|
|
}
|
|
|
|
verifreg.MinVerifiedDealSize = big.NewInt(256)
|
|
|
|
}
|
|
|
|
|
2020-06-30 22:02:01 +00:00
|
|
|
var PrepareNodeTimeout = time.Minute
|
2020-06-24 10:52:23 +00:00
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
type LotusNode struct {
|
|
|
|
FullApi api.FullNode
|
|
|
|
MinerApi api.StorageMiner
|
|
|
|
StopFn node.StopFunc
|
2020-07-06 11:45:26 +00:00
|
|
|
MineOne func(context.Context, func(bool, error)) error
|
2020-06-24 10:52:23 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
func (n *LotusNode) setWallet(ctx context.Context, walletKey *wallet.Key) error {
|
|
|
|
_, err := n.FullApi.WalletImport(ctx, &walletKey.KeyInfo)
|
2020-06-24 10:52:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
err = n.FullApi.WalletSetDefault(ctx, walletKey.Address)
|
2020-06-24 10:52:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
func WaitForBalances(t *TestEnvironment, ctx context.Context, nodes int) ([]*InitialBalanceMsg, error) {
|
2020-06-24 11:10:35 +00:00
|
|
|
ch := make(chan *InitialBalanceMsg)
|
2020-07-01 16:29:09 +00:00
|
|
|
sub := t.SyncClient.MustSubscribe(ctx, BalanceTopic, ch)
|
2020-06-24 11:10:35 +00:00
|
|
|
|
|
|
|
balances := make([]*InitialBalanceMsg, 0, nodes)
|
|
|
|
for i := 0; i < nodes; i++ {
|
|
|
|
select {
|
|
|
|
case m := <-ch:
|
|
|
|
balances = append(balances, m)
|
|
|
|
case err := <-sub.Done():
|
|
|
|
return nil, fmt.Errorf("got error while waiting for balances: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return balances, nil
|
|
|
|
}
|
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
func CollectPreseals(t *TestEnvironment, ctx context.Context, miners int) ([]*PresealMsg, error) {
|
2020-06-24 11:10:35 +00:00
|
|
|
ch := make(chan *PresealMsg)
|
2020-07-01 16:29:09 +00:00
|
|
|
sub := t.SyncClient.MustSubscribe(ctx, PresealTopic, ch)
|
2020-06-24 11:10:35 +00:00
|
|
|
|
|
|
|
preseals := make([]*PresealMsg, 0, miners)
|
|
|
|
for i := 0; i < miners; i++ {
|
|
|
|
select {
|
|
|
|
case m := <-ch:
|
|
|
|
preseals = append(preseals, m)
|
|
|
|
case err := <-sub.Done():
|
|
|
|
return nil, fmt.Errorf("got error while waiting for preseals: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 14:30:44 +00:00
|
|
|
sort.Slice(preseals, func(i, j int) bool {
|
|
|
|
return preseals[i].Seqno < preseals[j].Seqno
|
|
|
|
})
|
|
|
|
|
2020-06-24 11:10:35 +00:00
|
|
|
return preseals, nil
|
|
|
|
}
|
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
func WaitForGenesis(t *TestEnvironment, ctx context.Context) (*GenesisMsg, error) {
|
2020-06-24 11:10:35 +00:00
|
|
|
genesisCh := make(chan *GenesisMsg)
|
2020-07-01 16:29:09 +00:00
|
|
|
sub := t.SyncClient.MustSubscribe(ctx, GenesisTopic, genesisCh)
|
2020-06-24 11:10:35 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case genesisMsg := <-genesisCh:
|
|
|
|
return genesisMsg, nil
|
|
|
|
case err := <-sub.Done():
|
|
|
|
return nil, fmt.Errorf("error while waiting for genesis msg: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 11:10:00 +00:00
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
func CollectMinerAddrs(t *TestEnvironment, ctx context.Context, miners int) ([]MinerAddressesMsg, error) {
|
2020-06-30 22:02:01 +00:00
|
|
|
ch := make(chan MinerAddressesMsg)
|
2020-07-01 16:29:09 +00:00
|
|
|
sub := t.SyncClient.MustSubscribe(ctx, MinersAddrsTopic, ch)
|
2020-06-25 11:10:00 +00:00
|
|
|
|
2020-06-30 22:02:01 +00:00
|
|
|
addrs := make([]MinerAddressesMsg, 0, miners)
|
2020-06-25 11:10:00 +00:00
|
|
|
for i := 0; i < miners; i++ {
|
|
|
|
select {
|
|
|
|
case a := <-ch:
|
|
|
|
addrs = append(addrs, a)
|
|
|
|
case err := <-sub.Done():
|
|
|
|
return nil, fmt.Errorf("got error while waiting for miners addrs: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
func CollectClientAddrs(t *TestEnvironment, ctx context.Context, clients int) ([]peer.AddrInfo, error) {
|
2020-06-25 11:10:00 +00:00
|
|
|
ch := make(chan peer.AddrInfo)
|
2020-07-01 16:29:09 +00:00
|
|
|
sub := t.SyncClient.MustSubscribe(ctx, ClientsAddrsTopic, ch)
|
2020-06-25 11:10:00 +00:00
|
|
|
|
|
|
|
addrs := make([]peer.AddrInfo, 0, clients)
|
|
|
|
for i := 0; i < clients; i++ {
|
|
|
|
select {
|
|
|
|
case a := <-ch:
|
|
|
|
addrs = append(addrs, a)
|
|
|
|
case err := <-sub.Done():
|
|
|
|
return nil, fmt.Errorf("got error while waiting for clients addrs: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs, nil
|
|
|
|
}
|
2020-06-24 14:41:57 +00:00
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
func GetPubsubTracerMaddr(ctx context.Context, t *TestEnvironment) (string, error) {
|
2020-06-26 08:58:56 +00:00
|
|
|
if !t.BooleanParam("enable_pubsub_tracer") {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan *PubsubTracerMsg)
|
2020-07-01 16:29:09 +00:00
|
|
|
sub := t.SyncClient.MustSubscribe(ctx, PubsubTracerTopic, ch)
|
2020-06-26 08:58:56 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case m := <-ch:
|
2020-06-30 22:02:01 +00:00
|
|
|
return m.Multiaddr, nil
|
2020-06-26 08:58:56 +00:00
|
|
|
case err := <-sub.Done():
|
2020-06-26 11:06:45 +00:00
|
|
|
return "", fmt.Errorf("got error while waiting for pubsub tracer config: %w", err)
|
2020-06-26 08:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
func GetRandomBeaconOpts(ctx context.Context, t *TestEnvironment) (node.Option, error) {
|
2020-06-25 14:55:44 +00:00
|
|
|
beaconType := t.StringParam("random_beacon_type")
|
|
|
|
switch beaconType {
|
|
|
|
case "external-drand":
|
2020-06-24 14:41:57 +00:00
|
|
|
noop := func(settings *node.Settings) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return noop, nil
|
2020-06-25 14:55:44 +00:00
|
|
|
|
|
|
|
case "local-drand":
|
|
|
|
cfg, err := waitForDrandConfig(ctx, t.SyncClient)
|
|
|
|
if err != nil {
|
|
|
|
t.RecordMessage("error getting drand config: %w", err)
|
|
|
|
return nil, err
|
2020-06-26 08:58:56 +00:00
|
|
|
|
2020-06-25 14:55:44 +00:00
|
|
|
}
|
|
|
|
t.RecordMessage("setting drand config: %v", cfg)
|
|
|
|
return node.Options(
|
|
|
|
node.Override(new(dtypes.DrandConfig), cfg.Config),
|
|
|
|
node.Override(new(dtypes.DrandBootstrap), cfg.GossipBootstrap),
|
|
|
|
), nil
|
|
|
|
|
|
|
|
case "mock":
|
2020-06-25 15:56:12 +00:00
|
|
|
return node.Options(
|
|
|
|
node.Override(new(beacon.RandomBeacon), modtest.RandomBeacon),
|
|
|
|
node.Override(new(dtypes.DrandConfig), dtypes.DrandConfig{
|
|
|
|
ChainInfoJSON: "{\"Hash\":\"wtf\"}",
|
|
|
|
}),
|
|
|
|
node.Override(new(dtypes.DrandBootstrap), dtypes.DrandBootstrap{}),
|
|
|
|
), nil
|
2020-06-25 14:55:44 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown random_beacon_type: %s", beaconType)
|
2020-06-24 14:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-30 11:50:46 +00:00
|
|
|
|
2020-07-06 14:47:17 +00:00
|
|
|
func startServer(endpoint ma.Multiaddr, srv *http.Server) (listenAddr string, err error) {
|
2020-06-30 15:53:27 +00:00
|
|
|
lst, err := manet.Listen(endpoint)
|
|
|
|
if err != nil {
|
2020-07-06 14:47:17 +00:00
|
|
|
return "", fmt.Errorf("could not listen: %w", err)
|
2020-06-30 15:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_ = srv.Serve(manet.NetListener(lst))
|
|
|
|
}()
|
|
|
|
|
2020-07-06 14:47:17 +00:00
|
|
|
return lst.Addr().String(), nil
|
2020-06-30 15:53:27 +00:00
|
|
|
}
|
2020-06-30 16:02:02 +00:00
|
|
|
|
|
|
|
func registerAndExportMetrics(instanceName string) {
|
|
|
|
// Register all Lotus metric views
|
|
|
|
err := view.Register(metrics.DefaultViews...)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the metric to one so it is published to the exporter
|
|
|
|
stats.Record(context.Background(), metrics.LotusInfo.M(1))
|
|
|
|
|
|
|
|
// Register our custom exporter to opencensus
|
|
|
|
e, err := influxdb.NewExporter(context.Background(), influxdb.Options{
|
|
|
|
Database: "testground",
|
|
|
|
Address: os.Getenv("INFLUXDB_URL"),
|
|
|
|
Username: "",
|
|
|
|
Password: "",
|
|
|
|
InstanceName: instanceName,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
view.RegisterExporter(e)
|
|
|
|
view.SetReportingPeriod(5 * time.Second)
|
|
|
|
}
|
2020-07-03 18:06:10 +00:00
|
|
|
|
|
|
|
func collectStats(ctx context.Context, api api.FullNode) error {
|
|
|
|
var database string = "testground"
|
|
|
|
var headlag int = 3
|
|
|
|
|
|
|
|
influxAddr := os.Getenv("INFLUXDB_URL")
|
|
|
|
influxUser := ""
|
|
|
|
influxPass := ""
|
|
|
|
|
2020-07-03 18:07:46 +00:00
|
|
|
influx, err := tstats.InfluxClient(influxAddr, influxUser, influxPass)
|
2020-07-03 18:06:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:07:46 +00:00
|
|
|
height, err := tstats.GetLastRecordedHeight(influx, database)
|
2020-07-03 18:06:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:07:46 +00:00
|
|
|
go func() {
|
|
|
|
time.Sleep(15 * time.Second)
|
2020-07-03 18:45:33 +00:00
|
|
|
tstats.Collect(context.Background(), api, influx, database, height, headlag)
|
2020-07-03 18:07:46 +00:00
|
|
|
}()
|
2020-07-03 18:06:10 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|