From 05f10f0601f998bcf889c2de8fd22ee0ceeab0dc Mon Sep 17 00:00:00 2001 From: Tiance <1033935631@qq.com> Date: Thu, 23 Mar 2023 11:05:18 +0800 Subject: [PATCH] feat: update cache to the new generic version --- cmd/state.go | 3 +-- go.mod | 3 ++- go.sum | 2 ++ pkg/beacon/drand.go | 17 ++++++------- pkg/chain/store.go | 41 +++++++++++--------------------- pkg/consensus/block_validator.go | 9 ++++--- pkg/events/message_cache.go | 18 +++++++------- pkg/messagepool/gas.go | 11 +++++---- pkg/messagepool/messagepool.go | 19 ++++++--------- venus-devtool/go.mod | 1 + venus-devtool/go.sum | 2 ++ 11 files changed, 55 insertions(+), 71 deletions(-) diff --git a/cmd/state.go b/cmd/state.go index c0019fd1f1..277a28e738 100644 --- a/cmd/state.go +++ b/cmd/state.go @@ -8,8 +8,6 @@ import ( "os" "strconv" - "github.com/filecoin-project/venus/cmd/tablewriter" - "github.com/filecoin-project/go-address" "github.com/filecoin-project/go-state-types/abi" actorstypes "github.com/filecoin-project/go-state-types/actors" @@ -20,6 +18,7 @@ import ( "github.com/multiformats/go-multiaddr" "github.com/filecoin-project/venus/app/node" + "github.com/filecoin-project/venus/cmd/tablewriter" "github.com/filecoin-project/venus/pkg/constants" "github.com/filecoin-project/venus/venus-shared/actors/builtin" "github.com/filecoin-project/venus/venus-shared/types" diff --git a/go.mod b/go.mod index 76ab0e59ea..d3bbdc4217 100644 --- a/go.mod +++ b/go.mod @@ -55,7 +55,7 @@ require ( github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026 github.com/hannahhoward/go-pubsub v0.0.0-20200423002714-8d62886cc36e github.com/hashicorp/go-multierror v1.1.1 - github.com/hashicorp/golang-lru v0.5.4 + github.com/hashicorp/golang-lru/v2 v2.0.2 github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c github.com/ipfs-force-community/metrics v1.0.1-0.20211022060227-11142a08b729 github.com/ipfs/go-bitswap v0.10.2 @@ -121,6 +121,7 @@ require ( github.com/bytedance/sonic v1.8.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/goccy/go-json v0.10.0 // indirect + github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/ipfs/go-block-format v0.1.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect diff --git a/go.sum b/go.sum index 0543a2d403..353875b82b 100644 --- a/go.sum +++ b/go.sum @@ -657,6 +657,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/golang-lru/v2 v2.0.2 h1:Dwmkdr5Nc/oBiXgJS3CDHNhJtIHkuZ3DZF5twqnfBdU= +github.com/hashicorp/golang-lru/v2 v2.0.2/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= diff --git a/pkg/beacon/drand.go b/pkg/beacon/drand.go index 5ff4902ab2..cf8d871c7f 100644 --- a/pkg/beacon/drand.go +++ b/pkg/beacon/drand.go @@ -13,11 +13,12 @@ import ( dlog "github.com/drand/drand/log" "github.com/drand/kyber" kzap "github.com/go-kit/kit/log/zap" - lru "github.com/hashicorp/golang-lru" + lru "github.com/hashicorp/golang-lru/v2" "go.uber.org/zap/zapcore" "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/network" + cfg "github.com/filecoin-project/venus/pkg/config" "github.com/filecoin-project/venus/pkg/constants" "github.com/filecoin-project/venus/venus-shared/types" @@ -42,7 +43,7 @@ type DrandBeacon struct { filGenTime uint64 filRoundTime uint64 - localCache *lru.Cache + localCache *lru.Cache[uint64, *types.BeaconEntry] } // DrandHTTPClient interface overrides the user agent used by drand @@ -84,7 +85,7 @@ func NewDrandBeacon(genTimeStamp, interval uint64, config cfg.DrandConf) (*Drand return nil, fmt.Errorf("creating drand client: %v", err) } - lc, err := lru.New(1024) + lc, err := lru.New[uint64, *types.BeaconEntry](1024) if err != nil { return nil, err } @@ -136,16 +137,12 @@ func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan Response } func (db *DrandBeacon) cacheValue(e types.BeaconEntry) { - db.localCache.Add(e.Round, e) + db.localCache.Add(e.Round, &e) } func (db *DrandBeacon) getCachedValue(round uint64) *types.BeaconEntry { - v, ok := db.localCache.Get(round) - if !ok { - return nil - } - e, _ := v.(types.BeaconEntry) - return &e + v, _ := db.localCache.Get(round) + return v } func (db *DrandBeacon) VerifyEntry(curr types.BeaconEntry, prev types.BeaconEntry) error { diff --git a/pkg/chain/store.go b/pkg/chain/store.go index 6be3a1698a..372d6a399f 100644 --- a/pkg/chain/store.go +++ b/pkg/chain/store.go @@ -9,15 +9,7 @@ import ( "runtime/debug" "sync" - "github.com/filecoin-project/pubsub" - blockstoreutil "github.com/filecoin-project/venus/venus-shared/blockstore" - lru "github.com/hashicorp/golang-lru" - "github.com/ipfs/go-cid" - "github.com/ipfs/go-datastore" - blockstore "github.com/ipfs/go-ipfs-blockstore" - cbor "github.com/ipfs/go-ipld-cbor" - blocks "github.com/ipfs/go-libipfs/blocks" - logging "github.com/ipfs/go-log/v2" + lru "github.com/hashicorp/golang-lru/v2" "github.com/ipld/go-car" carutil "github.com/ipld/go-car/util" carv2 "github.com/ipld/go-car/v2" @@ -26,10 +18,18 @@ import ( cbg "github.com/whyrusleeping/cbor-gen" "go.opencensus.io/trace" + "github.com/ipfs/go-cid" + "github.com/ipfs/go-datastore" + blockstore "github.com/ipfs/go-ipfs-blockstore" + cbor "github.com/ipfs/go-ipld-cbor" + blocks "github.com/ipfs/go-libipfs/blocks" + logging "github.com/ipfs/go-log/v2" + "github.com/filecoin-project/go-address" "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/go-state-types/network" + "github.com/filecoin-project/pubsub" blockadt "github.com/filecoin-project/specs-actors/actors/util/adt" "github.com/filecoin-project/venus/pkg/constants" @@ -49,18 +49,10 @@ import ( "github.com/filecoin-project/venus/venus-shared/actors/builtin/reward" "github.com/filecoin-project/venus/venus-shared/actors/builtin/verifreg" "github.com/filecoin-project/venus/venus-shared/actors/policy" + blockstoreutil "github.com/filecoin-project/venus/venus-shared/blockstore" "github.com/filecoin-project/venus/venus-shared/types" ) -// ErrNoMethod is returned by Get when there is no method signature (eg, transfer). -var ErrNoMethod = errors.New("no method") - -// ErrNoActorImpl is returned by Get when the actor implementation doesn't exist, eg -// the actor address is an empty actor, an address that has received a transfer of FIL -// but hasn't yet been upgraded to an account actor. (The actor implementation might -// also genuinely be missing, which is not expected.) -var ErrNoActorImpl = errors.New("no actor implementation") - // GenesisKey is the key at which the genesis Cid is written in the datastore. var GenesisKey = datastore.NewKey("/consensus/genesisCid") @@ -92,10 +84,6 @@ type TSState struct { Receipts cid.Cid } -func ActorStore(ctx context.Context, bs blockstore.Blockstore) adt.Store { - return adt.WrapStore(ctx, cbor.NewCborStore(bs)) -} - // Store is a generic implementation of the Store interface. // It works(tm) for now. type Store struct { @@ -139,7 +127,7 @@ type Store struct { reorgCh chan reorg reorgNotifeeCh chan ReorgNotifee - tsCache *lru.ARCCache + tsCache *lru.ARCCache[types.TipSetKey, *types.TipSet] } // NewStore constructs a new default store. @@ -148,7 +136,7 @@ func NewStore(chainDs repo.Datastore, genesisCid cid.Cid, circulatiingSupplyCalculator ICirculatingSupplyCalcualtor, ) *Store { - tsCache, _ := lru.NewARC(DefaultTipsetLruCacheSize) + tsCache, _ := lru.NewARC[types.TipSetKey, *types.TipSet](DefaultTipsetLruCacheSize) store := &Store{ stateAndBlockSource: cbor.NewCborStore(bsstore), ds: chainDs, @@ -330,9 +318,8 @@ func (store *Store) GetTipSet(ctx context.Context, key types.TipSetKey) (*types. return store.GetHead(), nil } - val, has := store.tsCache.Get(key) - if has { - return val.(*types.TipSet), nil + if val, has := store.tsCache.Get(key); has { + return val, nil } cids := key.Cids() diff --git a/pkg/consensus/block_validator.go b/pkg/consensus/block_validator.go index 5f1c3a761f..5f0fd33607 100644 --- a/pkg/consensus/block_validator.go +++ b/pkg/consensus/block_validator.go @@ -10,10 +10,11 @@ import ( "github.com/Gurpartap/async" "github.com/hashicorp/go-multierror" - lru "github.com/hashicorp/golang-lru" + lru "github.com/hashicorp/golang-lru/v2" "github.com/ipfs/go-cid" cbor "github.com/ipfs/go-ipld-cbor" pubsub "github.com/libp2p/go-libp2p-pubsub" + "github.com/multiformats/go-varint" "github.com/pkg/errors" cbg "github.com/whyrusleeping/cbor-gen" "go.opencensus.io/trace" @@ -24,8 +25,6 @@ import ( builtintypes "github.com/filecoin-project/go-state-types/builtin" acrypto "github.com/filecoin-project/go-state-types/crypto" "github.com/filecoin-project/go-state-types/network" - "github.com/multiformats/go-varint" - blockadt "github.com/filecoin-project/specs-actors/actors/util/adt" "github.com/filecoin-project/specs-actors/v7/actors/runtime/proof" @@ -79,7 +78,7 @@ type BlockValidator struct { // gasprice for vm gasPirceSchedule *gas.PricesSchedule // cache for validate block - validateBlockCache *lru.ARCCache + validateBlockCache *lru.ARCCache[cid.Cid, struct{}] Stmgr StateTransformer } @@ -98,7 +97,7 @@ func NewBlockValidator(tv TicketValidator, config *config.NetworkParamsConfig, gasPirceSchedule *gas.PricesSchedule, ) *BlockValidator { - validateBlockCache, _ := lru.NewARC(2048) + validateBlockCache, _ := lru.NewARC[cid.Cid, struct{}](2048) return &BlockValidator{ tv: tv, bstore: bstore, diff --git a/pkg/events/message_cache.go b/pkg/events/message_cache.go index fb8b33a0a2..e2abf2acbb 100644 --- a/pkg/events/message_cache.go +++ b/pkg/events/message_cache.go @@ -4,21 +4,21 @@ import ( "context" "sync" - "github.com/filecoin-project/venus/venus-shared/types" - - lru "github.com/hashicorp/golang-lru" + lru "github.com/hashicorp/golang-lru/v2" "github.com/ipfs/go-cid" + + "github.com/filecoin-project/venus/venus-shared/types" ) type messageCache struct { api IEvent blockMsgLk sync.Mutex - blockMsgCache *lru.ARCCache + blockMsgCache *lru.ARCCache[cid.Cid, *types.BlockMessages] } func newMessageCache(api IEvent) *messageCache { - blsMsgCache, _ := lru.NewARC(500) + blsMsgCache, _ := lru.NewARC[cid.Cid, *types.BlockMessages](500) return &messageCache{ api: api, @@ -30,14 +30,14 @@ func (c *messageCache) ChainGetBlockMessages(ctx context.Context, blkCid cid.Cid c.blockMsgLk.Lock() defer c.blockMsgLk.Unlock() - msgsI, ok := c.blockMsgCache.Get(blkCid) + msgs, ok := c.blockMsgCache.Get(blkCid) var err error if !ok { - msgsI, err = c.api.ChainGetBlockMessages(ctx, blkCid) + msgs, err = c.api.ChainGetBlockMessages(ctx, blkCid) if err != nil { return nil, err } - c.blockMsgCache.Add(blkCid, msgsI) + c.blockMsgCache.Add(blkCid, msgs) } - return msgsI.(*types.BlockMessages), nil + return msgs, nil } diff --git a/pkg/messagepool/gas.go b/pkg/messagepool/gas.go index ee58a79c96..67714fb668 100644 --- a/pkg/messagepool/gas.go +++ b/pkg/messagepool/gas.go @@ -9,13 +9,14 @@ import ( "math/rand" "sort" + lru "github.com/hashicorp/golang-lru/v2" + "github.com/filecoin-project/go-address" "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" + builtin2 "github.com/filecoin-project/go-state-types/builtin" "github.com/filecoin-project/go-state-types/exitcode" - lru "github.com/hashicorp/golang-lru" - builtin2 "github.com/filecoin-project/go-state-types/builtin" "github.com/filecoin-project/venus/pkg/constants" "github.com/filecoin-project/venus/pkg/fork" "github.com/filecoin-project/venus/venus-shared/actors/builtin" @@ -27,7 +28,7 @@ const MinGasPremium = 100e3 // const MaxSpendOnFeeDenom = 100 type GasPriceCache struct { - c *lru.TwoQueueCache + c *lru.TwoQueueCache[types.TipSetKey, []GasMeta] } type GasMeta struct { @@ -37,7 +38,7 @@ type GasMeta struct { func NewGasPriceCache() *GasPriceCache { // 50 because we usually won't access more than 40 - c, err := lru.New2Q(50) + c, err := lru.New2Q[types.TipSetKey, []GasMeta](50) if err != nil { // err only if parameter is bad panic(err) @@ -51,7 +52,7 @@ func NewGasPriceCache() *GasPriceCache { func (g *GasPriceCache) GetTSGasStats(ctx context.Context, provider Provider, ts *types.TipSet) ([]GasMeta, error) { i, has := g.c.Get(ts.Key()) if has { - return i.([]GasMeta), nil + return i, nil } var prices []GasMeta diff --git a/pkg/messagepool/messagepool.go b/pkg/messagepool/messagepool.go index 6ae7594c73..d1810130f3 100644 --- a/pkg/messagepool/messagepool.go +++ b/pkg/messagepool/messagepool.go @@ -12,9 +12,8 @@ import ( "sync" "time" - lps "github.com/filecoin-project/pubsub" "github.com/hashicorp/go-multierror" - lru "github.com/hashicorp/golang-lru" + lru "github.com/hashicorp/golang-lru/v2" "github.com/ipfs/go-cid" "github.com/ipfs/go-datastore" "github.com/ipfs/go-datastore/namespace" @@ -28,6 +27,7 @@ import ( "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/go-state-types/crypto" + lps "github.com/filecoin-project/pubsub" "github.com/filecoin-project/venus/pkg/chain" "github.com/filecoin-project/venus/pkg/config" @@ -162,7 +162,7 @@ type MessagePool struct { // pruneCooldown is a channel used to allow a cooldown time between prunes pruneCooldown chan struct{} - blsSigCache *lru.TwoQueueCache + blsSigCache *lru.TwoQueueCache[cid.Cid, crypto.Signature] changes *lps.PubSub @@ -170,7 +170,7 @@ type MessagePool struct { netName string - sigValCache *lru.TwoQueueCache + sigValCache *lru.TwoQueueCache[string, struct{}] evtTypes [3]journal.EventType journal journal.Journal @@ -388,8 +388,8 @@ func New(ctx context.Context, netName string, j journal.Journal, ) (*MessagePool, error) { - cache, _ := lru.New2Q(constants.BlsSignatureCacheSize) - verifcache, _ := lru.New2Q(constants.VerifSigCacheSize) + cache, _ := lru.New2Q[cid.Cid, crypto.Signature](constants.BlsSignatureCacheSize) + verifcache, _ := lru.New2Q[string, struct{}](constants.VerifSigCacheSize) cfg, err := loadConfig(ctx, ds) if err != nil { @@ -1515,13 +1515,8 @@ func (mp *MessagePool) MessagesForBlocks(ctx context.Context, blks []*types.Bloc } func (mp *MessagePool) RecoverSig(msg *types.Message) *types.SignedMessage { - val, ok := mp.blsSigCache.Get(msg.Cid()) - if !ok { - return nil - } - sig, ok := val.(crypto.Signature) + sig, ok := mp.blsSigCache.Get(msg.Cid()) if !ok { - log.Errorf("value in signature cache was not a signature (got %T)", val) return nil } diff --git a/venus-devtool/go.mod b/venus-devtool/go.mod index 0ce11d988a..556e21bb20 100644 --- a/venus-devtool/go.mod +++ b/venus-devtool/go.mod @@ -99,6 +99,7 @@ require ( github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/golang-lru v0.5.4 // indirect + github.com/hashicorp/golang-lru/v2 v2.0.2 // indirect github.com/icza/backscanner v0.0.0-20210726202459-ac2ffc679f94 // indirect github.com/ipfs/bbloom v0.0.4 // indirect github.com/ipfs/go-block-format v0.1.1 // indirect diff --git a/venus-devtool/go.sum b/venus-devtool/go.sum index 575d3461cb..e6ca5e983a 100644 --- a/venus-devtool/go.sum +++ b/venus-devtool/go.sum @@ -596,6 +596,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/golang-lru/v2 v2.0.2 h1:Dwmkdr5Nc/oBiXgJS3CDHNhJtIHkuZ3DZF5twqnfBdU= +github.com/hashicorp/golang-lru/v2 v2.0.2/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=