Skip to content

Commit

Permalink
Introduce caching of PowerTable CIDs
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Sztandera <[email protected]>
  • Loading branch information
Kubuxu committed Dec 10, 2024
1 parent 4bb99f0 commit 8417b74
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
60 changes: 43 additions & 17 deletions consensus_inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/filecoin-project/go-f3/gpbft"
"github.com/filecoin-project/go-f3/internal/clock"
"github.com/filecoin-project/go-f3/manifest"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/ipfs/go-cid"
"go.opentelemetry.io/otel/metric"
)

Expand All @@ -22,13 +24,47 @@ type gpbftInputs struct {
ec ec.Backend
verifier gpbft.Verifier
clock clock.Clock

ptCache *lru.Cache[string, cid.Cid]
}

func newInputs(manifest *manifest.Manifest, certStore *certstore.Store, ec ec.Backend,
verifier gpbft.Verifier, clk clock.Clock) gpbftInputs {
cache, err := lru.New[string, cid.Cid](256) // keep a bit more than 2x max ECChain size
if err != nil {
// panic as it only depends on the size
panic(fmt.Errorf("could not create cache: %w", err))
}

return gpbftInputs{
manifest: manifest,
certStore: certStore,
ec: ec,
verifier: verifier,
clock: clk,
ptCache: cache,
}
}

//ptCache lru.Cache[string, cid.Cid]
//}
//func (h *gpbftInputs) getPowerTableCIDForTipset(ctx context.Context, tsk gpbft.TipSetKey) {
func (h *gpbftInputs) getPowerTableCIDForTipset(ctx context.Context, tsk gpbft.TipSetKey) (cid.Cid, error) {
sTSK := string(tsk)
ptCid, ok := h.ptCache.Get(sTSK)
if ok {
return ptCid, nil
}

pt, err := h.ec.GetPowerTable(ctx, tsk)
if err != nil {
return cid.Undef, fmt.Errorf("getting power table to compute CID: %w", err)
}
ptCid, err = certs.MakePowerTableCID(pt)
if err != nil {
return cid.Undef, fmt.Errorf("computing power table CID: %w", err)
}

//}
h.ptCache.Add(sTSK, ptCid)
return ptCid, nil
}

func (h *gpbftInputs) collectChain(ctx context.Context, base ec.TipSet, head ec.TipSet) ([]ec.TipSet, error) {
// TODO: optimize when head is way beyond base
Expand Down Expand Up @@ -110,27 +146,17 @@ func (h *gpbftInputs) GetProposal(ctx context.Context, instance uint64) (_ *gpbf
Epoch: baseTs.Epoch(),
Key: baseTs.Key(),
}
pte, err := h.ec.GetPowerTable(ctx, baseTs.Key())
if err != nil {
return nil, nil, fmt.Errorf("getting power table for base: %w", err)
}
base.PowerTable, err = certs.MakePowerTableCID(pte)
if err != nil {
return nil, nil, fmt.Errorf("computing powertable CID for base: %w", err)
}
base.PowerTable, err = h.getPowerTableCIDForTipset(ctx, baseTs.Key())

suffix := make([]gpbft.TipSet, min(gpbft.ChainMaxLen-1, len(collectedChain))) // -1 because of base
for i := range suffix {
suffix[i].Key = collectedChain[i].Key()
suffix[i].Epoch = collectedChain[i].Epoch()

suffix[i].PowerTable, err = h.getPowerTableCIDForTipset(ctx, suffix[i].Key)
pte, err = h.ec.GetPowerTable(ctx, suffix[i].Key)

Check failure on line 157 in consensus_inputs.go

View workflow job for this annotation

GitHub Actions / go-fuzz

undefined: pte

Check failure on line 157 in consensus_inputs.go

View workflow job for this annotation

GitHub Actions / go-fuzz

undefined: pte

Check failure on line 157 in consensus_inputs.go

View workflow job for this annotation

GitHub Actions / go-check / All

undefined: pte

Check failure on line 157 in consensus_inputs.go

View workflow job for this annotation

GitHub Actions / go-check / All

undefined: pte

Check failure on line 157 in consensus_inputs.go

View workflow job for this annotation

GitHub Actions / go-check / All

undefined: pte (compile)

Check failure on line 157 in consensus_inputs.go

View workflow job for this annotation

GitHub Actions / go-check / All

undefined: pte (compile)

Check failure on line 157 in consensus_inputs.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

undefined: pte

Check failure on line 157 in consensus_inputs.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

undefined: pte
if err != nil {
return nil, nil, fmt.Errorf("getting power table for suffix %d: %w", i, err)
}
suffix[i].PowerTable, err = certs.MakePowerTableCID(pte)
if err != nil {
return nil, nil, fmt.Errorf("computing powertable CID for base: %w", err)
return nil, nil, fmt.Errorf("computing powertable CID for suffix %d: %w", i, err)
}
}
chain, err := gpbft.NewChain(base, suffix...)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/filecoin-project/go-bitfield v0.2.4
github.com/filecoin-project/go-clock v0.1.0
github.com/filecoin-project/go-state-types v0.14.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-datastore v0.6.0
github.com/ipfs/go-ds-leveldb v0.5.0
Expand Down Expand Up @@ -62,7 +63,6 @@ require (
github.com/google/pprof v0.0.0-20241017200806-017d972448fc // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
Expand Down
8 changes: 1 addition & 7 deletions host.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,7 @@ func newRunner(
ctxCancel: ctxCancel,
equivFilter: newEquivocationFilter(pID),
selfMessages: make(map[uint64]map[roundPhase][]*gpbft.GMessage),
inputs: gpbftInputs{
manifest: m,
certStore: cs,
ec: ec,
verifier: verifier,
clock: clock.GetClock(ctx),
},
inputs: newInputs(m, cs, ec, verifier, clock.GetClock(ctx)),
}

// create a stopped timer to facilitate alerts requested from gpbft
Expand Down

0 comments on commit 8417b74

Please sign in to comment.