2019-10-09 08:50:57 +00:00
|
|
|
package chain
|
|
|
|
|
|
|
|
import (
|
2020-07-06 17:23:29 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-10-09 12:23:45 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2019-10-09 08:50:57 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BadBlockCache struct {
|
2019-10-09 12:23:45 +00:00
|
|
|
badBlocks *lru.ARCCache
|
2019-10-09 08:50:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 17:23:29 +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: reason, OriginalReason: or}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bbr BadBlockReason) String() string {
|
|
|
|
res := bbr.Reason
|
|
|
|
if bbr.OriginalReason != nil {
|
2020-07-06 17:46:25 +00:00
|
|
|
res += " caused by: " + fmt.Sprintf("%s %s", bbr.OriginalReason.TipSet, bbr.OriginalReason.String())
|
2020-07-06 17:23:29 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-10-09 08:50:57 +00:00
|
|
|
func NewBadBlockCache() *BadBlockCache {
|
2019-10-09 12:55:27 +00:00
|
|
|
cache, err := lru.NewARC(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:23:29 +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-07-06 17:23:29 +00:00
|
|
|
func (bts *BadBlockCache) Has(c cid.Cid) (BadBlockReason, bool) {
|
2020-02-12 07:21:11 +00:00
|
|
|
rval, ok := bts.badBlocks.Get(c)
|
|
|
|
if !ok {
|
2020-07-06 17:23:29 +00:00
|
|
|
return BadBlockReason{}, false
|
2020-02-12 07:21:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 17:23:29 +00:00
|
|
|
return rval.(BadBlockReason), true
|
2019-10-09 08:50:57 +00:00
|
|
|
}
|