lotus/chain/badtscache.go

71 lines
1.5 KiB
Go
Raw Permalink Normal View History

2019-10-09 08:50:57 +00:00
package chain
import (
"fmt"
lru "github.com/hashicorp/golang-lru/v2"
2019-10-09 08:50:57 +00:00
"github.com/ipfs/go-cid"
2022-06-14 15:00:51 +00:00
"github.com/filecoin-project/lotus/build"
2019-10-09 08:50:57 +00:00
)
type BadBlockCache struct {
badBlocks *lru.ARCCache[cid.Cid, BadBlockReason]
2019-10-09 08:50:57 +00:00
}
type BadBlockReason struct {
Reason string
TipSet []cid.Cid
OriginalReason *BadBlockReason
}
func NewBadBlockReason(cid []cid.Cid, format string, i ...interface{}) BadBlockReason {
return BadBlockReason{
TipSet: cid,
Reason: fmt.Sprintf(format, i...),
}
}
func (bbr BadBlockReason) Linked(reason string, i ...interface{}) BadBlockReason {
or := &bbr
if bbr.OriginalReason != nil {
or = bbr.OriginalReason
}
return BadBlockReason{Reason: fmt.Sprintf(reason, i...), OriginalReason: or}
}
func (bbr BadBlockReason) String() string {
res := bbr.Reason
if bbr.OriginalReason != nil {
res += " caused by: " + fmt.Sprintf("%s %s", bbr.OriginalReason.TipSet, bbr.OriginalReason.String())
}
return res
}
2019-10-09 08:50:57 +00:00
func NewBadBlockCache() *BadBlockCache {
cache, err := lru.NewARC[cid.Cid, BadBlockReason](build.BadBlockCacheSize)
2019-10-09 12:23:45 +00:00
if err != nil {
2019-11-16 23:41:14 +00:00
panic(err) // ok
2019-10-09 12:23:45 +00:00
}
2019-10-09 08:50:57 +00:00
return &BadBlockCache{
2019-10-09 12:23:45 +00:00
badBlocks: cache,
2019-10-09 08:50:57 +00:00
}
}
func (bts *BadBlockCache) Add(c cid.Cid, bbr BadBlockReason) {
bts.badBlocks.Add(c, bbr)
2019-10-09 08:50:57 +00:00
}
2020-09-09 07:24:09 +00:00
func (bts *BadBlockCache) Remove(c cid.Cid) {
bts.badBlocks.Remove(c)
}
2020-10-10 08:26:42 +00:00
func (bts *BadBlockCache) Purge() {
bts.badBlocks.Purge()
}
func (bts *BadBlockCache) Has(c cid.Cid) (BadBlockReason, bool) {
return bts.badBlocks.Get(c)
2019-10-09 08:50:57 +00:00
}