From 171734ea31d317c2ca73e9df607ec18109f201c1 Mon Sep 17 00:00:00 2001 From: vyzo Date: Sun, 12 Mar 2023 15:25:07 +0200 Subject: [PATCH] hook the index into the rest of lotus --- chain/gen/gen.go | 3 ++- chain/index/interface.go | 12 ++++++++++++ chain/stmgr/searchwait.go | 31 ++++++++++++++++++++++++++++++- chain/stmgr/stmgr.go | 10 +++++++--- cmd/lotus/daemon.go | 3 ++- node/builder_chain.go | 2 ++ node/modules/chain.go | 3 ++- node/modules/msgindex.go | 31 +++++++++++++++++++++++++++++++ node/modules/stmgr.go | 5 +++-- 9 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 node/modules/msgindex.go diff --git a/chain/gen/gen.go b/chain/gen/gen.go index de2df97c2..0da8e8318 100644 --- a/chain/gen/gen.go +++ b/chain/gen/gen.go @@ -34,6 +34,7 @@ import ( "github.com/filecoin-project/lotus/chain/consensus" "github.com/filecoin-project/lotus/chain/consensus/filcns" genesis2 "github.com/filecoin-project/lotus/chain/gen/genesis" + "github.com/filecoin-project/lotus/chain/index" "github.com/filecoin-project/lotus/chain/rand" "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/store" @@ -256,7 +257,7 @@ func NewGeneratorWithSectorsAndUpgradeSchedule(numSectors int, us stmgr.UpgradeS //return nil, xerrors.Errorf("creating drand beacon: %w", err) //} - sm, err := stmgr.NewStateManager(cs, consensus.NewTipSetExecutor(filcns.RewardFunc), sys, us, beac, ds) + sm, err := stmgr.NewStateManager(cs, consensus.NewTipSetExecutor(filcns.RewardFunc), sys, us, beac, ds, index.DummyMsgIndex) if err != nil { return nil, xerrors.Errorf("initing stmgr: %w", err) } diff --git a/chain/index/interface.go b/chain/index/interface.go index 8056cd662..c45a69361 100644 --- a/chain/index/interface.go +++ b/chain/index/interface.go @@ -30,3 +30,15 @@ type MsgIndex interface { // Close closes the index Close() error } + +type dummyMsgIndex struct{} + +func (_ dummyMsgIndex) GetMsgInfo(ctx context.Context, m cid.Cid) (MsgInfo, error) { + return MsgInfo{}, ErrNotFound +} + +func (_ dummyMsgIndex) Close() error { + return nil +} + +var DummyMsgIndex MsgIndex = dummyMsgIndex{} diff --git a/chain/stmgr/searchwait.go b/chain/stmgr/searchwait.go index 468f33db7..27a304611 100644 --- a/chain/stmgr/searchwait.go +++ b/chain/stmgr/searchwait.go @@ -10,6 +10,7 @@ import ( "github.com/filecoin-project/go-state-types/abi" + "github.com/filecoin-project/lotus/chain/index" "github.com/filecoin-project/lotus/chain/store" "github.com/filecoin-project/lotus/chain/types" ) @@ -145,7 +146,20 @@ func (sm *StateManager) SearchForMessage(ctx context.Context, head *types.TipSet return head, r, foundMsg, nil } - fts, r, foundMsg, err := sm.searchBackForMsg(ctx, head, msg, lookbackLimit, allowReplaced) + fts, r, foundMsg, err := sm.searchForIndexedMsg(ctx, mcid, msg) + + switch { + case err == nil: + return fts, r, foundMsg, nil + + case errors.Is(err, index.ErrNotFound): + // ok for the index to have incomplete data + + default: + log.Warnf("error searching message index: %s", err) + } + + fts, r, foundMsg, err = sm.searchBackForMsg(ctx, head, msg, lookbackLimit, allowReplaced) if err != nil { log.Warnf("failed to look back through chain for message %s", mcid) @@ -159,6 +173,21 @@ func (sm *StateManager) SearchForMessage(ctx context.Context, head *types.TipSet return fts, r, foundMsg, nil } +func (sm *StateManager) searchForIndexedMsg(ctx context.Context, mcid cid.Cid, m types.ChainMsg) (*types.TipSet, *types.MessageReceipt, cid.Cid, error) { + minfo, err := sm.msgIndex.GetMsgInfo(ctx, mcid) + if err != nil { + return nil, nil, cid.Undef, err + } + + ts, err := sm.cs.GetTipSetByCid(ctx, minfo.TipSet) + if err != nil { + return nil, nil, cid.Undef, err + } + + r, foundMsg, err := sm.tipsetExecutedMessage(ctx, ts, mcid, m.VMMessage(), false) + return ts, r, foundMsg, err +} + // searchBackForMsg searches up to limit tipsets backwards from the given // tipset for a message receipt. // If limit is diff --git a/chain/stmgr/stmgr.go b/chain/stmgr/stmgr.go index b9f8d81bf..827aeeee5 100644 --- a/chain/stmgr/stmgr.go +++ b/chain/stmgr/stmgr.go @@ -25,6 +25,7 @@ import ( "github.com/filecoin-project/lotus/chain/actors/builtin/paych" "github.com/filecoin-project/lotus/chain/actors/policy" "github.com/filecoin-project/lotus/chain/beacon" + "github.com/filecoin-project/lotus/chain/index" "github.com/filecoin-project/lotus/chain/rand" "github.com/filecoin-project/lotus/chain/state" "github.com/filecoin-project/lotus/chain/store" @@ -136,6 +137,8 @@ type StateManager struct { tsExec Executor tsExecMonitor ExecMonitor beacon beacon.Schedule + + msgIndex index.MsgIndex } // Caches a single state tree @@ -144,7 +147,7 @@ type treeCache struct { tree *state.StateTree } -func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder, us UpgradeSchedule, beacon beacon.Schedule, metadataDs dstore.Batching) (*StateManager, error) { +func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder, us UpgradeSchedule, beacon beacon.Schedule, metadataDs dstore.Batching, msgIndex index.MsgIndex) (*StateManager, error) { // If we have upgrades, make sure they're in-order and make sense. if err := us.Validate(); err != nil { return nil, err @@ -199,11 +202,12 @@ func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder, tree: nil, }, compWait: make(map[string]chan struct{}), + msgIndex: msgIndex, }, nil } -func NewStateManagerWithUpgradeScheduleAndMonitor(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder, us UpgradeSchedule, b beacon.Schedule, em ExecMonitor, metadataDs dstore.Batching) (*StateManager, error) { - sm, err := NewStateManager(cs, exec, sys, us, b, metadataDs) +func NewStateManagerWithUpgradeScheduleAndMonitor(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder, us UpgradeSchedule, b beacon.Schedule, em ExecMonitor, metadataDs dstore.Batching, msgIndex index.MsgIndex) (*StateManager, error) { + sm, err := NewStateManager(cs, exec, sys, us, b, metadataDs, msgIndex) if err != nil { return nil, err } diff --git a/cmd/lotus/daemon.go b/cmd/lotus/daemon.go index b3341bd79..585d4b2ce 100644 --- a/cmd/lotus/daemon.go +++ b/cmd/lotus/daemon.go @@ -35,6 +35,7 @@ import ( "github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/chain/consensus" "github.com/filecoin-project/lotus/chain/consensus/filcns" + "github.com/filecoin-project/lotus/chain/index" "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/store" "github.com/filecoin-project/lotus/chain/types" @@ -540,7 +541,7 @@ func ImportChain(ctx context.Context, r repo.Repo, fname string, snapshot bool) } // TODO: We need to supply the actual beacon after v14 - stm, err := stmgr.NewStateManager(cst, consensus.NewTipSetExecutor(filcns.RewardFunc), vm.Syscalls(ffiwrapper.ProofVerifier), filcns.DefaultUpgradeSchedule(), nil, mds) + stm, err := stmgr.NewStateManager(cst, consensus.NewTipSetExecutor(filcns.RewardFunc), vm.Syscalls(ffiwrapper.ProofVerifier), filcns.DefaultUpgradeSchedule(), nil, mds, index.DummyMsgIndex) if err != nil { return err } diff --git a/node/builder_chain.go b/node/builder_chain.go index d334d782e..20817acff 100644 --- a/node/builder_chain.go +++ b/node/builder_chain.go @@ -20,6 +20,7 @@ import ( "github.com/filecoin-project/lotus/chain/events" "github.com/filecoin-project/lotus/chain/exchange" "github.com/filecoin-project/lotus/chain/gen/slashfilter" + "github.com/filecoin-project/lotus/chain/index" "github.com/filecoin-project/lotus/chain/market" "github.com/filecoin-project/lotus/chain/messagepool" "github.com/filecoin-project/lotus/chain/messagesigner" @@ -79,6 +80,7 @@ var ChainNode = Options( Override(new(stmgr.Executor), consensus.NewTipSetExecutor(filcns.RewardFunc)), Override(new(consensus.Consensus), filcns.NewFilecoinExpectedConsensus), Override(new(*store.ChainStore), modules.ChainStore), + Override(new(index.MsgIndex), modules.MsgIndex), Override(new(*stmgr.StateManager), modules.StateManager), Override(new(dtypes.ChainBitswap), modules.ChainBitswap), Override(new(dtypes.ChainBlockService), modules.ChainBlockService), // todo: unused diff --git a/node/modules/chain.go b/node/modules/chain.go index 0c3bad2c7..762c77e4b 100644 --- a/node/modules/chain.go +++ b/node/modules/chain.go @@ -21,6 +21,7 @@ import ( "github.com/filecoin-project/lotus/chain/consensus/filcns" "github.com/filecoin-project/lotus/chain/exchange" "github.com/filecoin-project/lotus/chain/gen/slashfilter" + "github.com/filecoin-project/lotus/chain/index" "github.com/filecoin-project/lotus/chain/messagepool" "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/store" @@ -123,7 +124,7 @@ func NetworkName(mctx helpers.MetricsCtx, ctx := helpers.LifecycleCtx(mctx, lc) - sm, err := stmgr.NewStateManager(cs, tsexec, syscalls, us, nil, nil) + sm, err := stmgr.NewStateManager(cs, tsexec, syscalls, us, nil, nil, index.DummyMsgIndex) if err != nil { return "", err } diff --git a/node/modules/msgindex.go b/node/modules/msgindex.go new file mode 100644 index 000000000..296ecde49 --- /dev/null +++ b/node/modules/msgindex.go @@ -0,0 +1,31 @@ +package modules + +import ( + "context" + + "go.uber.org/fx" + + "github.com/filecoin-project/lotus/chain/index" + "github.com/filecoin-project/lotus/chain/store" + "github.com/filecoin-project/lotus/node/repo" +) + +func MsgIndex(lc fx.Lifecycle, cs *store.ChainStore, r repo.LockedRepo) (index.MsgIndex, error) { + basePath, err := r.SqlitePath() + if err != nil { + return nil, err + } + + msgIndex, err := index.NewMsgIndex(basePath, cs) + if err != nil { + return nil, err + } + + lc.Append(fx.Hook{ + OnStop: func(_ context.Context) error { + return msgIndex.Close() + }, + }) + + return msgIndex, nil +} diff --git a/node/modules/stmgr.go b/node/modules/stmgr.go index b8f6f4776..f3eaee219 100644 --- a/node/modules/stmgr.go +++ b/node/modules/stmgr.go @@ -4,14 +4,15 @@ import ( "go.uber.org/fx" "github.com/filecoin-project/lotus/chain/beacon" + "github.com/filecoin-project/lotus/chain/index" "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/store" "github.com/filecoin-project/lotus/chain/vm" "github.com/filecoin-project/lotus/node/modules/dtypes" ) -func StateManager(lc fx.Lifecycle, cs *store.ChainStore, exec stmgr.Executor, sys vm.SyscallBuilder, us stmgr.UpgradeSchedule, b beacon.Schedule, metadataDs dtypes.MetadataDS) (*stmgr.StateManager, error) { - sm, err := stmgr.NewStateManager(cs, exec, sys, us, b, metadataDs) +func StateManager(lc fx.Lifecycle, cs *store.ChainStore, exec stmgr.Executor, sys vm.SyscallBuilder, us stmgr.UpgradeSchedule, b beacon.Schedule, metadataDs dtypes.MetadataDS, msgIndex index.MsgIndex) (*stmgr.StateManager, error) { + sm, err := stmgr.NewStateManager(cs, exec, sys, us, b, metadataDs, msgIndex) if err != nil { return nil, err }