2019-06-25 11:42:17 +00:00
|
|
|
package modules
|
2019-07-01 10:18:00 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-11 00:31:06 +00:00
|
|
|
"context"
|
2019-07-23 20:37:06 +00:00
|
|
|
"crypto/rand"
|
2020-02-12 20:25:29 +00:00
|
|
|
"errors"
|
2019-11-22 16:20:56 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2020-12-03 13:16:10 +00:00
|
|
|
"os"
|
2021-01-20 18:09:19 +00:00
|
|
|
"path/filepath"
|
2020-12-02 16:40:28 +00:00
|
|
|
"time"
|
2019-11-22 16:20:56 +00:00
|
|
|
|
2020-05-20 18:23:51 +00:00
|
|
|
"github.com/gbrlsnchs/jwt/v3"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2020-09-10 10:41:29 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2020-05-20 18:23:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peerstore"
|
|
|
|
record "github.com/libp2p/go-libp2p-record"
|
2021-01-20 18:09:19 +00:00
|
|
|
"github.com/raulk/go-watchdog"
|
2020-12-02 16:40:28 +00:00
|
|
|
"go.uber.org/fx"
|
2020-05-20 18:23:51 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-jsonrpc/auth"
|
2020-10-29 19:50:04 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-02-12 20:25:29 +00:00
|
|
|
|
2020-05-20 18:23:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/apistruct"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-10-23 11:11:18 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/addrutil"
|
2020-10-29 19:50:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2019-10-23 11:11:18 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2020-12-02 16:40:28 +00:00
|
|
|
"github.com/filecoin-project/lotus/system"
|
2019-07-01 10:18:00 +00:00
|
|
|
)
|
|
|
|
|
2020-12-03 13:16:10 +00:00
|
|
|
const (
|
|
|
|
// EnvWatchdogDisabled is an escape hatch to disable the watchdog explicitly
|
|
|
|
// in case an OS/kernel appears to report incorrect information. The
|
|
|
|
// watchdog will be disabled if the value of this env variable is 1.
|
|
|
|
EnvWatchdogDisabled = "LOTUS_DISABLE_WATCHDOG"
|
|
|
|
)
|
|
|
|
|
2020-12-02 16:40:28 +00:00
|
|
|
const (
|
|
|
|
JWTSecretName = "auth-jwt-private" //nolint:gosec
|
|
|
|
KTJwtHmacSecret = "jwt-hmac-secret" //nolint:gosec
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
log = logging.Logger("modules")
|
|
|
|
logWatchdog = logging.Logger("watchdog")
|
|
|
|
)
|
2019-07-03 17:39:07 +00:00
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
type Genesis func() (*types.BlockHeader, error)
|
2019-07-08 13:36:43 +00:00
|
|
|
|
2019-07-01 10:18:00 +00:00
|
|
|
// RecordValidator provides namesys compatible routing record validator
|
|
|
|
func RecordValidator(ps peerstore.Peerstore) record.Validator {
|
|
|
|
return record.NamespacedValidator{
|
2019-07-01 20:00:22 +00:00
|
|
|
"pk": record.PublicKeyValidator{},
|
2019-07-01 10:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-08 13:36:43 +00:00
|
|
|
|
2020-12-02 22:26:30 +00:00
|
|
|
// MemoryConstraints returns the memory constraints configured for this system.
|
|
|
|
func MemoryConstraints() system.MemoryConstraints {
|
|
|
|
constraints := system.GetMemoryConstraints()
|
|
|
|
log.Infow("memory limits initialized",
|
|
|
|
"max_mem_heap", constraints.MaxHeapMem,
|
|
|
|
"total_system_mem", constraints.TotalSystemMem,
|
|
|
|
"effective_mem_limit", constraints.EffectiveMemLimit)
|
|
|
|
return constraints
|
|
|
|
}
|
|
|
|
|
2020-12-02 16:40:28 +00:00
|
|
|
// MemoryWatchdog starts the memory watchdog, applying the computed resource
|
|
|
|
// constraints.
|
2021-01-20 18:09:19 +00:00
|
|
|
func MemoryWatchdog(lr repo.LockedRepo, lc fx.Lifecycle, constraints system.MemoryConstraints) {
|
2021-01-22 00:19:11 +00:00
|
|
|
if true || os.Getenv(EnvWatchdogDisabled) == "1" {
|
2020-12-03 13:16:10 +00:00
|
|
|
log.Infof("memory watchdog is disabled via %s", EnvWatchdogDisabled)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-20 18:09:19 +00:00
|
|
|
// configure heap profile capture so that one is captured per episode where
|
|
|
|
// utilization climbs over 90% of the limit. A maximum of 10 heapdumps
|
|
|
|
// will be captured during life of this process.
|
|
|
|
watchdog.HeapProfileDir = filepath.Join(lr.Path(), "heapprof")
|
|
|
|
watchdog.HeapProfileMaxCaptures = 10
|
2021-01-21 14:06:18 +00:00
|
|
|
watchdog.HeapProfileThreshold = 0.9
|
2021-01-20 18:09:19 +00:00
|
|
|
watchdog.Logger = logWatchdog
|
|
|
|
|
|
|
|
policy := watchdog.NewWatermarkPolicy(0.50, 0.60, 0.70, 0.85, 0.90, 0.925, 0.95)
|
|
|
|
|
|
|
|
// Try to initialize a watchdog in the following order of precedence:
|
|
|
|
// 1. If a max heap limit has been provided, initialize a heap-driven watchdog.
|
|
|
|
// 2. Else, try to initialize a cgroup-driven watchdog.
|
|
|
|
// 3. Else, try to initialize a system-driven watchdog.
|
|
|
|
// 4. Else, log a warning that the system is flying solo, and return.
|
|
|
|
|
|
|
|
addStopHook := func(stopFn func()) {
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStop: func(ctx context.Context) error {
|
|
|
|
stopFn()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
2020-12-02 16:40:28 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 18:09:19 +00:00
|
|
|
// 1. If user has set max heap limit, apply it.
|
2020-12-02 22:26:30 +00:00
|
|
|
if maxHeap := constraints.MaxHeapMem; maxHeap != 0 {
|
2021-01-20 18:09:19 +00:00
|
|
|
const minGOGC = 10
|
|
|
|
err, stopFn := watchdog.HeapDriven(maxHeap, minGOGC, policy)
|
|
|
|
if err == nil {
|
|
|
|
log.Infof("initialized heap-driven watchdog; max heap: %d bytes", maxHeap)
|
|
|
|
addStopHook(stopFn)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Warnf("failed to initialize heap-driven watchdog; err: %s", err)
|
|
|
|
log.Warnf("trying a cgroup-driven watchdog")
|
2020-12-02 16:40:28 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 18:09:19 +00:00
|
|
|
// 2. cgroup-driven watchdog.
|
|
|
|
err, stopFn := watchdog.CgroupDriven(5*time.Second, policy)
|
|
|
|
if err == nil {
|
|
|
|
log.Infof("initialized cgroup-driven watchdog")
|
|
|
|
addStopHook(stopFn)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Warnf("failed to initialize cgroup-driven watchdog; err: %s", err)
|
|
|
|
log.Warnf("trying a system-driven watchdog")
|
|
|
|
|
|
|
|
// 3. system-driven watchdog.
|
|
|
|
err, stopFn = watchdog.SystemDriven(0, 5*time.Second, policy) // 0 calculates the limit automatically.
|
|
|
|
if err == nil {
|
|
|
|
log.Infof("initialized system-driven watchdog")
|
|
|
|
addStopHook(stopFn)
|
2020-12-02 16:40:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-20 18:09:19 +00:00
|
|
|
// 4. log the failure
|
|
|
|
log.Warnf("failed to initialize system-driven watchdog; err: %s", err)
|
|
|
|
log.Warnf("system running without a memory watchdog")
|
2020-12-02 16:40:28 +00:00
|
|
|
}
|
2019-07-23 20:23:44 +00:00
|
|
|
|
2020-08-19 19:18:36 +00:00
|
|
|
type JwtPayload struct {
|
2020-05-20 18:23:51 +00:00
|
|
|
Allow []auth.Permission
|
2019-07-23 20:37:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 17:19:24 +00:00
|
|
|
func APISecret(keystore types.KeyStore, lr repo.LockedRepo) (*dtypes.APIAlg, error) {
|
2019-07-23 20:23:44 +00:00
|
|
|
key, err := keystore.Get(JWTSecretName)
|
2020-02-12 20:25:29 +00:00
|
|
|
|
|
|
|
if errors.Is(err, types.ErrKeyInfoNotFound) {
|
2019-07-23 20:37:06 +00:00
|
|
|
log.Warn("Generating new API secret")
|
|
|
|
|
|
|
|
sk, err := ioutil.ReadAll(io.LimitReader(rand.Reader, 32))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key = types.KeyInfo{
|
2020-08-19 19:18:36 +00:00
|
|
|
Type: KTJwtHmacSecret,
|
2019-07-23 20:37:06 +00:00
|
|
|
PrivateKey: sk,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := keystore.Put(JWTSecretName, key); err != nil {
|
|
|
|
return nil, xerrors.Errorf("writing API secret: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make this configurable
|
2020-08-19 19:18:36 +00:00
|
|
|
p := JwtPayload{
|
2019-12-09 17:08:32 +00:00
|
|
|
Allow: apistruct.AllPermissions,
|
2019-07-23 20:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cliToken, err := jwt.Sign(&p, jwt.NewHS256(key.PrivateKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := lr.SetAPIToken(cliToken); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-12 20:25:29 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return nil, xerrors.Errorf("could not get JWT Token: %w", err)
|
2019-07-23 20:23:44 +00:00
|
|
|
}
|
2019-07-23 20:37:06 +00:00
|
|
|
|
2019-08-20 17:19:24 +00:00
|
|
|
return (*dtypes.APIAlg)(jwt.NewHS256(key.PrivateKey)), nil
|
2019-07-23 20:23:44 +00:00
|
|
|
}
|
2019-10-11 00:31:06 +00:00
|
|
|
|
2019-10-11 03:16:12 +00:00
|
|
|
func ConfigBootstrap(peers []string) func() (dtypes.BootstrapPeers, error) {
|
|
|
|
return func() (dtypes.BootstrapPeers, error) {
|
|
|
|
return addrutil.ParseAddresses(context.TODO(), peers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuiltinBootstrap() (dtypes.BootstrapPeers, error) {
|
|
|
|
return build.BuiltinBootstrap()
|
|
|
|
}
|
2020-06-08 09:31:33 +00:00
|
|
|
|
2020-09-10 10:41:29 +00:00
|
|
|
func DrandBootstrap(ds dtypes.DrandSchedule) (dtypes.DrandBootstrap, error) {
|
2020-08-27 17:00:42 +00:00
|
|
|
// TODO: retry resolving, don't fail if at least one resolve succeeds
|
2021-01-20 18:09:19 +00:00
|
|
|
var res []peer.AddrInfo
|
2020-09-10 10:41:29 +00:00
|
|
|
for _, d := range ds {
|
|
|
|
addrs, err := addrutil.ParseAddresses(context.TODO(), d.Config.Relays)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("reoslving drand relays addresses: %+v", err)
|
2020-10-28 17:53:11 +00:00
|
|
|
continue
|
2020-09-10 10:41:29 +00:00
|
|
|
}
|
|
|
|
res = append(res, addrs...)
|
2020-08-27 15:50:16 +00:00
|
|
|
}
|
2020-09-10 10:41:29 +00:00
|
|
|
return res, nil
|
2020-06-08 09:31:33 +00:00
|
|
|
}
|
2020-10-29 19:50:04 +00:00
|
|
|
|
|
|
|
func NewDefaultMaxFeeFunc(r repo.LockedRepo) dtypes.DefaultMaxFeeFunc {
|
|
|
|
return func() (out abi.TokenAmount, err error) {
|
|
|
|
err = readNodeCfg(r, func(cfg *config.FullNode) {
|
|
|
|
out = abi.TokenAmount(cfg.Fees.DefaultMaxFee)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readNodeCfg(r repo.LockedRepo, accessor func(node *config.FullNode)) error {
|
|
|
|
raw, err := r.Config()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, ok := raw.(*config.FullNode)
|
|
|
|
if !ok {
|
|
|
|
return xerrors.New("expected config.FullNode")
|
|
|
|
}
|
|
|
|
|
|
|
|
accessor(cfg)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|