lotus/node/impl/full.go

132 lines
3.1 KiB
Go
Raw Normal View History

package impl
2019-07-08 19:07:16 +00:00
import (
2020-10-01 15:14:08 +00:00
"context"
"time"
logging "github.com/ipfs/go-log/v2"
2022-08-25 18:20:41 +00:00
"github.com/libp2p/go-libp2p/core/peer"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/node/impl/client"
"github.com/filecoin-project/lotus/node/impl/common"
"github.com/filecoin-project/lotus/node/impl/full"
2019-11-08 20:11:56 +00:00
"github.com/filecoin-project/lotus/node/impl/market"
"github.com/filecoin-project/lotus/node/impl/net"
"github.com/filecoin-project/lotus/node/impl/paych"
2020-10-01 15:14:08 +00:00
"github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/modules/lp2p"
2019-07-08 19:07:16 +00:00
)
2019-07-23 17:27:45 +00:00
var log = logging.Logger("node")
type FullNodeAPI struct {
common.CommonAPI
net.NetAPI
2019-08-20 16:48:33 +00:00
full.ChainAPI
2019-09-16 13:46:05 +00:00
client.API
2019-08-20 16:48:33 +00:00
full.MpoolAPI
full.GasAPI
2019-11-08 17:15:38 +00:00
market.MarketAPI
2019-09-16 13:46:05 +00:00
paych.PaychAPI
2019-08-20 16:48:33 +00:00
full.StateAPI
full.MsigAPI
2019-08-20 16:48:33 +00:00
full.WalletAPI
full.SyncAPI
2022-09-27 16:08:04 +00:00
full.RaftAPI
full.EthAPI
2023-12-19 11:07:08 +00:00
full.ActorEventsAPI
2020-10-01 15:14:08 +00:00
DS dtypes.MetadataDS
NetworkName dtypes.NetworkName
2020-10-01 15:14:08 +00:00
}
func (n *FullNodeAPI) CreateBackup(ctx context.Context, fpath string) error {
2021-12-11 21:03:00 +00:00
return backup(ctx, n.DS, fpath)
2019-08-20 18:05:17 +00:00
}
func (n *FullNodeAPI) NodeStatus(ctx context.Context, inclChainStatus bool) (status api.NodeStatus, err error) {
curTs, err := n.ChainHead(ctx)
if err != nil {
return status, err
}
status.SyncStatus.Epoch = uint64(curTs.Height())
timestamp := time.Unix(int64(curTs.MinTimestamp()), 0)
delta := time.Since(timestamp).Seconds()
status.SyncStatus.Behind = uint64(delta / 30)
// get peers in the messages and blocks topics
peersMsgs := make(map[peer.ID]struct{})
peersBlocks := make(map[peer.ID]struct{})
for _, p := range n.PubSub.ListPeers(build.MessagesTopic(n.NetworkName)) {
peersMsgs[p] = struct{}{}
}
for _, p := range n.PubSub.ListPeers(build.BlocksTopic(n.NetworkName)) {
peersBlocks[p] = struct{}{}
}
// get scores for all connected and recent peers
scores, err := n.NetPubsubScores(ctx)
if err != nil {
return status, err
}
for _, score := range scores {
if score.Score.Score > lp2p.PublishScoreThreshold {
_, inMsgs := peersMsgs[score.ID]
if inMsgs {
status.PeerStatus.PeersToPublishMsgs++
}
_, inBlocks := peersBlocks[score.ID]
if inBlocks {
status.PeerStatus.PeersToPublishBlocks++
}
}
}
if inclChainStatus && status.SyncStatus.Epoch > uint64(build.Finality) {
blockCnt := 0
ts := curTs
for i := 0; i < 100; i++ {
blockCnt += len(ts.Blocks())
tsk := ts.Parents()
ts, err = n.ChainGetTipSet(ctx, tsk)
if err != nil {
return status, err
}
}
status.ChainStatus.BlocksPerTipsetLast100 = float64(blockCnt) / 100
for i := 100; i < int(build.Finality); i++ {
blockCnt += len(ts.Blocks())
tsk := ts.Parents()
ts, err = n.ChainGetTipSet(ctx, tsk)
if err != nil {
return status, err
}
}
status.ChainStatus.BlocksPerTipsetLastFinality = float64(blockCnt) / float64(build.Finality)
}
return status, nil
}
func (n *FullNodeAPI) RaftState(ctx context.Context) (*api.RaftStateData, error) {
2022-09-27 16:08:04 +00:00
return n.RaftAPI.GetRaftState(ctx)
}
func (n *FullNodeAPI) RaftLeader(ctx context.Context) (peer.ID, error) {
return n.RaftAPI.Leader(ctx)
}
var _ api.FullNode = &FullNodeAPI{}