From 753563897077795a683c8f149dccf8895f9be918 Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Tue, 10 Nov 2020 15:49:40 +0100 Subject: [PATCH 1/4] feat(lens): Optimize StateGetActor calls. Add a utility function called "OptimizedStateGetActorWithFallback()" which gets the current tipset's state by trying to load the child's state and using ParentState() on that, so that we avoid recomputing the state for the current tipset by applying messages. If anything goes wrong, the function falls back to the original, more expensive StateGetActor, and logs a warning. --- go.sum | 3 ++ lens/carrepo/carrepo.go | 5 ++++ lens/lotus/api.go | 3 +- lens/lotusrepo/repo.go | 6 ++++ lens/sqlrepo/repo.go | 6 ++++ lens/util.go | 65 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 lens/util.go diff --git a/go.sum b/go.sum index 11bf74c5a..c8afdd02a 100644 --- a/go.sum +++ b/go.sum @@ -1311,6 +1311,7 @@ github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoT github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.10.2 h1:aY/nuoWlKJud2J6U0E3NWsjlg+0GtwXxgEqthRdzlcs= github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= github.com/opentracing-contrib/go-grpc v0.0.0-20180928155321-4b5a12d3ff02/go.mod h1:JNdpVEzCpXBgIiv4ds+TzhN1hrtxq6ClLrTlT9OQRSc= github.com/opentracing-contrib/go-grpc v0.0.0-20191001143057-db30781987df h1:vdYtBU6zvL7v+Tr+0xFM/qhahw/EvY8DMMunZHKH6eE= @@ -1513,7 +1514,9 @@ github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtX github.com/urfave/cli/v2 v2.0.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4= github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8= github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU= github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= diff --git a/lens/carrepo/carrepo.go b/lens/carrepo/carrepo.go index 775bf19d9..f4d1cc778 100644 --- a/lens/carrepo/carrepo.go +++ b/lens/carrepo/carrepo.go @@ -117,6 +117,11 @@ func (ra *CarAPI) Store() adt.Store { return adtStore } +// TODO: Remove. See https://github.com/filecoin-project/sentinel-visor/issues/196 +func (ra *CarAPI) StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { + return lens.OptimizedStateGetActorWithFallback(ctx, ra, ra.FullNodeAPI, actor, tsk) +} + func (ra *CarAPI) ClientStartDeal(ctx context.Context, params *api.StartDealParams) (*cid.Cid, error) { return nil, fmt.Errorf("unsupported") } diff --git a/lens/lotus/api.go b/lens/lotus/api.go index 223587b17..ce1e903ea 100644 --- a/lens/lotus/api.go +++ b/lens/lotus/api.go @@ -131,7 +131,8 @@ func (aw *APIWrapper) StateGetActor(ctx context.Context, actor address.Address, ctx, _ = tag.New(ctx, tag.Upsert(metrics.API, "StateGetActor")) stop := metrics.Timer(ctx, metrics.LensRequestDuration) defer stop() - return aw.FullNode.StateGetActor(ctx, actor, tsk) + + return lens.OptimizedStateGetActorWithFallback(ctx, aw, aw.FullNode, actor, tsk) } func (aw *APIWrapper) StateListActors(ctx context.Context, tsk types.TipSetKey) ([]address.Address, error) { diff --git a/lens/lotusrepo/repo.go b/lens/lotusrepo/repo.go index e8eba1561..b48067a90 100644 --- a/lens/lotusrepo/repo.go +++ b/lens/lotusrepo/repo.go @@ -17,6 +17,7 @@ import ( "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/store" + "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/vm" "github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper" "github.com/filecoin-project/lotus/lib/cachebs" @@ -116,6 +117,11 @@ func (ra *RepoAPI) Store() adt.Store { return adtStore } +// TODO: Remove. See https://github.com/filecoin-project/sentinel-visor/issues/196 +func (ra *RepoAPI) StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { + return lens.OptimizedStateGetActorWithFallback(ctx, ra, ra.FullNodeAPI, actor, tsk) +} + func (ra *RepoAPI) ClientStartDeal(ctx context.Context, params *api.StartDealParams) (*cid.Cid, error) { return nil, fmt.Errorf("unsupported") } diff --git a/lens/sqlrepo/repo.go b/lens/sqlrepo/repo.go index 375371477..0680321f0 100644 --- a/lens/sqlrepo/repo.go +++ b/lens/sqlrepo/repo.go @@ -12,6 +12,7 @@ import ( "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/store" + "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/vm" "github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper" "github.com/filecoin-project/lotus/journal" @@ -101,6 +102,11 @@ type SQLAPI struct { cacheSize int } +// TODO: Remove. See https://github.com/filecoin-project/sentinel-visor/issues/196 +func (ra *SQLAPI) StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { + return lens.OptimizedStateGetActorWithFallback(ctx, ra, ra.FullNodeAPI, actor, tsk) +} + func (ra *SQLAPI) ComputeGasOutputs(gasUsed, gasLimit int64, baseFee, feeCap, gasPremium abi.TokenAmount) vm.GasOutputs { return vm.ComputeGasOutputs(gasUsed, gasLimit, baseFee, feeCap, gasPremium) } diff --git a/lens/util.go b/lens/util.go new file mode 100644 index 000000000..198d97463 --- /dev/null +++ b/lens/util.go @@ -0,0 +1,65 @@ +package lens + +import ( + "context" + "errors" + + "github.com/filecoin-project/go-address" + "github.com/filecoin-project/lotus/chain/state" + "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/lotus/node/impl/full" + "github.com/ipfs/go-cid" + logging "github.com/ipfs/go-log/v2" + "golang.org/x/xerrors" +) + +var logger = logging.Logger("visor/lens/lotus") + +// OptimizedStateGetActorWithFallback is a helper to obtain an actor in the +// state of the current tipset without recomputing the full tipset. It does +// this by obtaining the child tipset (current height+1) and using the +// pre-computed ParentState(). +// +// TODO: Remove. See: https://github.com/filecoin-project/sentinel-visor/issues/196 +func OptimizedStateGetActorWithFallback(ctx context.Context, api API, fallback full.StateModuleAPI, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { + act, err := efficientStateGetActor(ctx, api, actor, tsk) + if err != nil { + logger.Warnf("Optimized StateGetActorError: %s. Falling back to default StateGetActor().") + return fallback.StateGetActor(ctx, actor, tsk) + } + return act, nil +} + +func efficientStateGetActor(ctx context.Context, api API, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { + ts, err := api.ChainGetTipSet(ctx, tsk) + if err != nil { + return nil, xerrors.Errorf("Failed to load tipset: %w", err) + } + + child, err := api.ChainGetTipSetByHeight(ctx, ts.Height()+1, types.NewTipSetKey()) + if err != nil { + return nil, xerrors.Errorf("load child tipset: %w", err) + } + + if !cidsEqual(child.Parents().Cids(), ts.Cids()) { + return nil, errors.New("child is not on the same chain") + } + + st, err := state.LoadStateTree(api.Store(), child.ParentState()) + if err != nil { + return nil, xerrors.Errorf("load state tree: %w", err) + } + return st.GetActor(actor) +} + +func cidsEqual(c1, c2 []cid.Cid) bool { + if len(c1) != len(c2) { + return false + } + for i, c := range c1 { + if !c2[i].Equals(c) { + return false + } + } + return true +} From e0f626efa30045dbcac627dca1e1f8e17472a9ae Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Tue, 10 Nov 2020 15:49:40 +0100 Subject: [PATCH 2/4] feat(lens): Optimize StateGetActor calls. Cherry pick from #238. --- lens/carrepo/carrepo.go | 2 +- lens/lotus/api.go | 4 +++- lens/lotusrepo/repo.go | 2 +- lens/sqlrepo/repo.go | 2 +- lens/util.go | 14 ++++++++------ 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lens/carrepo/carrepo.go b/lens/carrepo/carrepo.go index f4d1cc778..07c8a098d 100644 --- a/lens/carrepo/carrepo.go +++ b/lens/carrepo/carrepo.go @@ -119,7 +119,7 @@ func (ra *CarAPI) Store() adt.Store { // TODO: Remove. See https://github.com/filecoin-project/sentinel-visor/issues/196 func (ra *CarAPI) StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { - return lens.OptimizedStateGetActorWithFallback(ctx, ra, ra.FullNodeAPI, actor, tsk) + return lens.OptimizedStateGetActorWithFallback(ctx, ra.ChainAPI.Chain, ra.FullNodeAPI, actor, tsk) } func (ra *CarAPI) ClientStartDeal(ctx context.Context, params *api.StartDealParams) (*cid.Cid, error) { diff --git a/lens/lotus/api.go b/lens/lotus/api.go index ce1e903ea..14dd0cb69 100644 --- a/lens/lotus/api.go +++ b/lens/lotus/api.go @@ -132,7 +132,9 @@ func (aw *APIWrapper) StateGetActor(ctx context.Context, actor address.Address, stop := metrics.Timer(ctx, metrics.LensRequestDuration) defer stop() - return lens.OptimizedStateGetActorWithFallback(ctx, aw, aw.FullNode, actor, tsk) + return aw.StateGetActor(ctx, actor, tsk) + // TODO idk how to get a chain store here, probably just need to change the lotus api? + //return lens.OptimizedStateGetActorWithFallback(ctx, aw, aw.FullNode, actor, tsk) } func (aw *APIWrapper) StateListActors(ctx context.Context, tsk types.TipSetKey) ([]address.Address, error) { diff --git a/lens/lotusrepo/repo.go b/lens/lotusrepo/repo.go index b48067a90..8b623a716 100644 --- a/lens/lotusrepo/repo.go +++ b/lens/lotusrepo/repo.go @@ -119,7 +119,7 @@ func (ra *RepoAPI) Store() adt.Store { // TODO: Remove. See https://github.com/filecoin-project/sentinel-visor/issues/196 func (ra *RepoAPI) StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { - return lens.OptimizedStateGetActorWithFallback(ctx, ra, ra.FullNodeAPI, actor, tsk) + return lens.OptimizedStateGetActorWithFallback(ctx, ra.ChainAPI.Chain, ra.FullNodeAPI, actor, tsk) } func (ra *RepoAPI) ClientStartDeal(ctx context.Context, params *api.StartDealParams) (*cid.Cid, error) { diff --git a/lens/sqlrepo/repo.go b/lens/sqlrepo/repo.go index 0680321f0..436325e3b 100644 --- a/lens/sqlrepo/repo.go +++ b/lens/sqlrepo/repo.go @@ -104,7 +104,7 @@ type SQLAPI struct { // TODO: Remove. See https://github.com/filecoin-project/sentinel-visor/issues/196 func (ra *SQLAPI) StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { - return lens.OptimizedStateGetActorWithFallback(ctx, ra, ra.FullNodeAPI, actor, tsk) + return lens.OptimizedStateGetActorWithFallback(ctx, ra.ChainAPI.Chain, ra.FullNodeAPI, actor, tsk) } func (ra *SQLAPI) ComputeGasOutputs(gasUsed, gasLimit int64, baseFee, feeCap, gasPremium abi.TokenAmount) vm.GasOutputs { diff --git a/lens/util.go b/lens/util.go index 198d97463..9d8795447 100644 --- a/lens/util.go +++ b/lens/util.go @@ -6,6 +6,7 @@ import ( "github.com/filecoin-project/go-address" "github.com/filecoin-project/lotus/chain/state" + "github.com/filecoin-project/lotus/chain/store" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/node/impl/full" "github.com/ipfs/go-cid" @@ -21,8 +22,8 @@ var logger = logging.Logger("visor/lens/lotus") // pre-computed ParentState(). // // TODO: Remove. See: https://github.com/filecoin-project/sentinel-visor/issues/196 -func OptimizedStateGetActorWithFallback(ctx context.Context, api API, fallback full.StateModuleAPI, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { - act, err := efficientStateGetActor(ctx, api, actor, tsk) +func OptimizedStateGetActorWithFallback(ctx context.Context, store *store.ChainStore, fallback full.StateModuleAPI, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { + act, err := efficientStateGetActorFromChainStore(ctx, store, actor, tsk) if err != nil { logger.Warnf("Optimized StateGetActorError: %s. Falling back to default StateGetActor().") return fallback.StateGetActor(ctx, actor, tsk) @@ -30,13 +31,14 @@ func OptimizedStateGetActorWithFallback(ctx context.Context, api API, fallback f return act, nil } -func efficientStateGetActor(ctx context.Context, api API, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { - ts, err := api.ChainGetTipSet(ctx, tsk) +func efficientStateGetActorFromChainStore(ctx context.Context, store *store.ChainStore, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { + ts, err := store.GetTipSetFromKey(tsk) if err != nil { return nil, xerrors.Errorf("Failed to load tipset: %w", err) } - child, err := api.ChainGetTipSetByHeight(ctx, ts.Height()+1, types.NewTipSetKey()) + // heaviest tipset means look on the main chain and false means return tipset following null round. + child, err := store.GetTipsetByHeight(ctx, ts.Height()+1, store.GetHeaviestTipSet(), false) if err != nil { return nil, xerrors.Errorf("load child tipset: %w", err) } @@ -45,7 +47,7 @@ func efficientStateGetActor(ctx context.Context, api API, actor address.Address, return nil, errors.New("child is not on the same chain") } - st, err := state.LoadStateTree(api.Store(), child.ParentState()) + st, err := state.LoadStateTree(store.Store(ctx), child.ParentState()) if err != nil { return nil, xerrors.Errorf("load state tree: %w", err) } From 74f722b21572034197d38aae4feafae6a863f381 Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Wed, 18 Nov 2020 17:39:49 +0100 Subject: [PATCH 3/4] PR #214: Address reviews --- lens/util.go | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/lens/util.go b/lens/util.go index 9d8795447..a6a4300c2 100644 --- a/lens/util.go +++ b/lens/util.go @@ -9,7 +9,6 @@ import ( "github.com/filecoin-project/lotus/chain/store" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/node/impl/full" - "github.com/ipfs/go-cid" logging "github.com/ipfs/go-log/v2" "golang.org/x/xerrors" ) @@ -25,7 +24,7 @@ var logger = logging.Logger("visor/lens/lotus") func OptimizedStateGetActorWithFallback(ctx context.Context, store *store.ChainStore, fallback full.StateModuleAPI, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { act, err := efficientStateGetActorFromChainStore(ctx, store, actor, tsk) if err != nil { - logger.Warnf("Optimized StateGetActorError: %s. Falling back to default StateGetActor().") + logger.Warnf("Optimized StateGetActorError: %s. Falling back to default StateGetActor().", err) return fallback.StateGetActor(ctx, actor, tsk) } return act, nil @@ -43,7 +42,7 @@ func efficientStateGetActorFromChainStore(ctx context.Context, store *store.Chai return nil, xerrors.Errorf("load child tipset: %w", err) } - if !cidsEqual(child.Parents().Cids(), ts.Cids()) { + if !types.CidArrsEqual(child.Parents().Cids(), ts.Cids()) { return nil, errors.New("child is not on the same chain") } @@ -53,15 +52,3 @@ func efficientStateGetActorFromChainStore(ctx context.Context, store *store.Chai } return st.GetActor(actor) } - -func cidsEqual(c1, c2 []cid.Cid) bool { - if len(c1) != len(c2) { - return false - } - for i, c := range c1 { - if !c2[i].Equals(c) { - return false - } - } - return true -} From 1b286c116946b168a9547e39cd5ce7184b41dd4b Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Tue, 12 Jan 2021 18:02:51 +0100 Subject: [PATCH 4/4] Make changes so that optimized path can be used by lotus API --- go.sum | 1 - lens/lotus/api.go | 6 +++--- lens/lotusrepo/repo.go | 2 +- lens/util.go | 18 +++++++++++------- lens/util/repo.go | 2 +- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/go.sum b/go.sum index 320b666b0..3e052b212 100644 --- a/go.sum +++ b/go.sum @@ -1551,7 +1551,6 @@ github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2 github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M= github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8= github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU= github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= diff --git a/lens/lotus/api.go b/lens/lotus/api.go index c4a6d23bd..cf2d52969 100644 --- a/lens/lotus/api.go +++ b/lens/lotus/api.go @@ -133,9 +133,9 @@ func (aw *APIWrapper) StateGetActor(ctx context.Context, actor address.Address, stop := metrics.Timer(ctx, metrics.LensRequestDuration) defer stop() - return aw.StateGetActor(ctx, actor, tsk) - // TODO idk how to get a chain store here, probably just need to change the lotus api? - //return lens.OptimizedStateGetActorWithFallback(ctx, aw, aw.FullNode, actor, tsk) + //return aw.FullNode.StateGetActor(ctx, actor, tsk) + // TODO idk how to get a store.ChainStore here + return lens.OptimizedStateGetActorWithFallback(ctx, aw.Store(), aw.FullNode, aw.FullNode, actor, tsk) } func (aw *APIWrapper) StateListActors(ctx context.Context, tsk types.TipSetKey) ([]address.Address, error) { diff --git a/lens/lotusrepo/repo.go b/lens/lotusrepo/repo.go index 2a08322f8..7a959ec73 100644 --- a/lens/lotusrepo/repo.go +++ b/lens/lotusrepo/repo.go @@ -128,7 +128,7 @@ func (ra *RepoAPI) Store() adt.Store { // TODO: Remove. See https://github.com/filecoin-project/sentinel-visor/issues/196 func (ra *RepoAPI) StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { - return lens.OptimizedStateGetActorWithFallback(ctx, ra.ChainAPI.Chain, ra.FullNodeAPI, actor, tsk) + return lens.OptimizedStateGetActorWithFallback(ctx, ra.ChainAPI.Chain.Store(ctx), ra.ChainAPI, ra.StateAPI, actor, tsk) } func (ra *RepoAPI) ClientStartDeal(ctx context.Context, params *api.StartDealParams) (*cid.Cid, error) { diff --git a/lens/util.go b/lens/util.go index a6a4300c2..4667b585a 100644 --- a/lens/util.go +++ b/lens/util.go @@ -6,9 +6,9 @@ import ( "github.com/filecoin-project/go-address" "github.com/filecoin-project/lotus/chain/state" - "github.com/filecoin-project/lotus/chain/store" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/node/impl/full" + cbor "github.com/ipfs/go-ipld-cbor" logging "github.com/ipfs/go-log/v2" "golang.org/x/xerrors" ) @@ -21,8 +21,8 @@ var logger = logging.Logger("visor/lens/lotus") // pre-computed ParentState(). // // TODO: Remove. See: https://github.com/filecoin-project/sentinel-visor/issues/196 -func OptimizedStateGetActorWithFallback(ctx context.Context, store *store.ChainStore, fallback full.StateModuleAPI, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { - act, err := efficientStateGetActorFromChainStore(ctx, store, actor, tsk) +func OptimizedStateGetActorWithFallback(ctx context.Context, store cbor.IpldStore, chainAPI full.ChainModuleAPI, fallback full.StateModuleAPI, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { + act, err := efficientStateGetActorFromChainStore(ctx, store, chainAPI, actor, tsk) if err != nil { logger.Warnf("Optimized StateGetActorError: %s. Falling back to default StateGetActor().", err) return fallback.StateGetActor(ctx, actor, tsk) @@ -30,14 +30,18 @@ func OptimizedStateGetActorWithFallback(ctx context.Context, store *store.ChainS return act, nil } -func efficientStateGetActorFromChainStore(ctx context.Context, store *store.ChainStore, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { - ts, err := store.GetTipSetFromKey(tsk) +func efficientStateGetActorFromChainStore(ctx context.Context, store cbor.IpldStore, chainAPI full.ChainModuleAPI, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { + ts, err := chainAPI.ChainGetTipSet(ctx, tsk) if err != nil { return nil, xerrors.Errorf("Failed to load tipset: %w", err) } // heaviest tipset means look on the main chain and false means return tipset following null round. - child, err := store.GetTipsetByHeight(ctx, ts.Height()+1, store.GetHeaviestTipSet(), false) + head, err := chainAPI.ChainHead(ctx) + if err != nil { + return nil, xerrors.Errorf("Failed to get chain head: %w", err) + } + child, err := chainAPI.ChainGetTipSetByHeight(ctx, ts.Height()+1, head.Key()) if err != nil { return nil, xerrors.Errorf("load child tipset: %w", err) } @@ -46,7 +50,7 @@ func efficientStateGetActorFromChainStore(ctx context.Context, store *store.Chai return nil, errors.New("child is not on the same chain") } - st, err := state.LoadStateTree(store.Store(ctx), child.ParentState()) + st, err := state.LoadStateTree(store, child.ParentState()) if err != nil { return nil, xerrors.Errorf("load state tree: %w", err) } diff --git a/lens/util/repo.go b/lens/util/repo.go index 196dca280..292f47551 100644 --- a/lens/util/repo.go +++ b/lens/util/repo.go @@ -106,7 +106,7 @@ type LensAPI struct { // TODO: Remove. See https://github.com/filecoin-project/sentinel-visor/issues/196 func (ra *LensAPI) StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) { - return lens.OptimizedStateGetActorWithFallback(ctx, ra.ChainAPI.Chain, ra.FullNodeAPI, actor, tsk) + return lens.OptimizedStateGetActorWithFallback(ctx, ra.cs.Store(ctx), ra.FullNodeAPI.ChainAPI, ra.FullNodeAPI.StateAPI, actor, tsk) } func (ra *LensAPI) ComputeGasOutputs(gasUsed, gasLimit int64, baseFee, feeCap, gasPremium abi.TokenAmount) vm.GasOutputs {