Pass to validator the interfaces needed to get miner info

This commit is contained in:
gammazero 2022-02-08 04:55:59 -08:00
parent 1dc6a2fea6
commit b2805823ce
2 changed files with 15 additions and 13 deletions

View File

@ -6,7 +6,7 @@ import (
"time" "time"
address "github.com/filecoin-project/go-address" address "github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/api" //"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain" "github.com/filecoin-project/lotus/chain"
"github.com/filecoin-project/lotus/chain/consensus" "github.com/filecoin-project/lotus/chain/consensus"
@ -16,6 +16,7 @@ import (
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/metrics" "github.com/filecoin-project/lotus/metrics"
"github.com/filecoin-project/lotus/node/impl/client" "github.com/filecoin-project/lotus/node/impl/client"
"github.com/filecoin-project/lotus/node/impl/full"
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
blocks "github.com/ipfs/go-block-format" blocks "github.com/ipfs/go-block-format"
bserv "github.com/ipfs/go-blockservice" bserv "github.com/ipfs/go-blockservice"
@ -459,16 +460,18 @@ type IndexerMessageValidator struct {
self peer.ID self peer.ID
peerCache *lru.TwoQueueCache peerCache *lru.TwoQueueCache
fullNode api.FullNode chainApi full.ChainModuleAPI
stateApi full.StateModuleAPI
} }
func NewIndexerMessageValidator(self peer.ID, fullNode api.FullNode) *IndexerMessageValidator { func NewIndexerMessageValidator(self peer.ID, chainApi full.ChainModuleAPI, stateApi full.StateModuleAPI) *IndexerMessageValidator {
peerCache, _ := lru.New2Q(1024) peerCache, _ := lru.New2Q(1024)
return &IndexerMessageValidator{ return &IndexerMessageValidator{
self: self, self: self,
peerCache: peerCache, peerCache: peerCache,
fullNode: fullNode, chainApi: chainApi,
stateApi: stateApi,
} }
} }
@ -512,7 +515,7 @@ func (v *IndexerMessageValidator) Validate(ctx context.Context, pid peer.ID, msg
defer msgInfo.mutex.Unlock() defer msgInfo.mutex.Unlock()
if !ok || originPeer != msgInfo.peerID { if !ok || originPeer != msgInfo.peerID {
// Check that the message was signed by an authenticated peer. // Check that the miner ID maps to the peer that sent the message.
err = v.authenticateMessage(ctx, minerID, originPeer) err = v.authenticateMessage(ctx, minerID, originPeer)
if err != nil { if err != nil {
log.Warnw("cannot authenticate messsage", "err", err, "peer", originPeer, "minerID", minerID) log.Warnw("cannot authenticate messsage", "err", err, "peer", originPeer, "minerID", minerID)
@ -626,12 +629,12 @@ func (v *IndexerMessageValidator) authenticateMessage(ctx context.Context, miner
return xerrors.Errorf("invalid miner id: %w", err) return xerrors.Errorf("invalid miner id: %w", err)
} }
ts, err := v.fullNode.ChainHead(ctx) ts, err := v.chainApi.ChainHead(ctx)
if err != nil { if err != nil {
return err return err
} }
minerInfo, err := v.fullNode.StateMinerInfo(ctx, minerAddress, ts.Key()) minerInfo, err := v.stateApi.StateMinerInfo(ctx, minerAddress, ts.Key())
if err != nil { if err != nil {
return err return err
} }
@ -640,7 +643,8 @@ func (v *IndexerMessageValidator) authenticateMessage(ctx context.Context, miner
return xerrors.New("no peer id for miner") return xerrors.New("no peer id for miner")
} }
if *minerInfo.PeerId != peerID { if *minerInfo.PeerId != peerID {
return xerrors.New("message not signed by peer in miner info") return xerrors.New("miner id does not map to peer that sent message")
} }
return nil return nil
} }

View File

@ -20,7 +20,6 @@ import (
"github.com/filecoin-project/go-fil-markets/discovery" "github.com/filecoin-project/go-fil-markets/discovery"
discoveryimpl "github.com/filecoin-project/go-fil-markets/discovery/impl" discoveryimpl "github.com/filecoin-project/go-fil-markets/discovery/impl"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain" "github.com/filecoin-project/lotus/chain"
"github.com/filecoin-project/lotus/chain/beacon" "github.com/filecoin-project/lotus/chain/beacon"
@ -36,6 +35,7 @@ import (
"github.com/filecoin-project/lotus/lib/peermgr" "github.com/filecoin-project/lotus/lib/peermgr"
marketevents "github.com/filecoin-project/lotus/markets/loggers" marketevents "github.com/filecoin-project/lotus/markets/loggers"
"github.com/filecoin-project/lotus/node/hello" "github.com/filecoin-project/lotus/node/hello"
"github.com/filecoin-project/lotus/node/impl/full"
"github.com/filecoin-project/lotus/node/modules/dtypes" "github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/modules/helpers" "github.com/filecoin-project/lotus/node/modules/helpers"
"github.com/filecoin-project/lotus/node/repo" "github.com/filecoin-project/lotus/node/repo"
@ -199,12 +199,10 @@ func HandleIncomingMessages(mctx helpers.MetricsCtx, lc fx.Lifecycle, ps *pubsub
waitForSync(stmgr, pubsubMsgsSyncEpochs, subscribe) waitForSync(stmgr, pubsubMsgsSyncEpochs, subscribe)
} }
func RelayIndexerMessages(lc fx.Lifecycle, ps *pubsub.PubSub, nn dtypes.NetworkName, h host.Host) error { func RelayIndexerMessages(lc fx.Lifecycle, ps *pubsub.PubSub, nn dtypes.NetworkName, h host.Host, chainModule full.ChainModule, stateModule full.StateModule) error {
topicName := build.IndexerIngestTopic(nn) topicName := build.IndexerIngestTopic(nn)
// TODO: How do this get set? v := sub.NewIndexerMessageValidator(h.ID(), &chainModule, &stateModule)
var fullNode api.FullNode
v := sub.NewIndexerMessageValidator(h.ID(), fullNode)
if err := ps.RegisterTopicValidator(topicName, v.Validate); err != nil { if err := ps.RegisterTopicValidator(topicName, v.Validate); err != nil {
return xerrors.Errorf("failed to register validator for topic %s, err: %w", topicName, err) return xerrors.Errorf("failed to register validator for topic %s, err: %w", topicName, err)