lotus/chain/sub/bcast/consistent.go

179 lines
4.8 KiB
Go
Raw Normal View History

2022-09-12 17:03:06 +00:00
package bcast
import (
"context"
"sync"
"time"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
2023-03-16 16:03:25 +00:00
"golang.org/x/xerrors"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/chain/types"
2022-09-12 17:03:06 +00:00
)
var log = logging.Logger("sub-cb")
2022-09-12 17:03:06 +00:00
const (
// GcSanityCheck determines the number of epochs in the past
// that will be garbage collected from the current epoch.
GcSanityCheck = 5
// GcLookback determines the number of epochs kept in the consistent
// broadcast cache.
2023-03-16 16:03:25 +00:00
GcLookback = 1000
2022-09-12 17:03:06 +00:00
)
type blksInfo struct {
ctx context.Context
cancel context.CancelFunc
blks []cid.Cid
}
type bcastDict struct {
m map[string]*blksInfo
2022-09-13 10:33:28 +00:00
}
2023-03-16 16:03:25 +00:00
func (bd *bcastDict) load(key []byte) (*blksInfo, bool) {
v, ok := bd.m[string(key)]
2022-09-13 10:33:28 +00:00
if !ok {
return nil, ok
}
return v, ok
2022-09-13 10:33:28 +00:00
}
func (bd *bcastDict) blkLen(key []byte) int {
return len(bd.m[string(key)].blks)
2022-09-12 17:03:06 +00:00
}
func (bd *bcastDict) store(key []byte, d *blksInfo) {
bd.m[string(key)] = d
2022-12-13 11:45:01 +00:00
}
// ConsistentBCast tracks recent information about the
// blocks and tickets received at different epochs
2022-09-12 17:03:06 +00:00
type ConsistentBCast struct {
2022-12-13 11:45:01 +00:00
lk sync.RWMutex
2022-09-12 17:03:06 +00:00
delay time.Duration
2023-03-16 16:03:25 +00:00
m map[abi.ChainEpoch]*bcastDict
2022-09-12 17:03:06 +00:00
}
func newBcastDict() *bcastDict {
return &bcastDict{m: make(map[string]*blksInfo)}
2022-09-12 17:03:06 +00:00
}
2023-03-16 16:03:25 +00:00
func BCastKey(bh *types.BlockHeader) []byte {
return bh.Ticket.VRFProof
2022-09-12 17:03:06 +00:00
}
func NewConsistentBCast(delay time.Duration) *ConsistentBCast {
return &ConsistentBCast{
delay: delay,
m: make(map[abi.ChainEpoch]*bcastDict),
}
}
func cidExists(cids []cid.Cid, c cid.Cid) bool {
for _, v := range cids {
if v == c {
return true
}
}
return false
}
func (bInfo *blksInfo) eqErr() error {
bInfo.cancel()
2023-03-16 16:03:25 +00:00
return xerrors.Errorf("different blocks with the same ticket already seen")
2022-09-12 17:03:06 +00:00
}
func (cb *ConsistentBCast) Len() int {
2022-12-13 11:45:01 +00:00
cb.lk.RLock()
defer cb.lk.RUnlock()
return len(cb.m)
}
2023-03-16 16:03:25 +00:00
// RcvBlock is called every time a new block is received through the network.
//
// This function keeps track of all the blocks with a specific VRFProof received
// for the same height. Every time a new block with a VRFProof not seen at certain
// height is received, a new timer is triggered to wait for the delay time determined by
// the consistent broadcast before informing the syncer. During this time, if a new
// block with the same VRFProof for that height is received, it means a miner is
// trying to equivocate, and both blocks are discarded.
//
// The delay time should be set to a value high enough to allow any block sent for
// certain epoch to be propagated to a large amount of miners in the network.
func (cb *ConsistentBCast) RcvBlock(ctx context.Context, blk *types.BlockMsg) {
2022-09-12 17:03:06 +00:00
cb.lk.Lock()
defer cb.lk.Unlock()
2022-09-12 17:03:06 +00:00
bcastDict, ok := cb.m[blk.Header.Height]
if !ok {
bcastDict = newBcastDict()
2022-09-13 10:33:28 +00:00
cb.m[blk.Header.Height] = bcastDict
2022-09-12 17:03:06 +00:00
}
2023-03-16 16:03:25 +00:00
key := BCastKey(blk.Header)
2022-09-12 17:03:06 +00:00
blkCid := blk.Cid()
2022-09-13 10:33:28 +00:00
bInfo, ok := bcastDict.load(key)
2022-09-12 17:03:06 +00:00
if ok {
if len(bInfo.blks) > 1 {
log.Errorf("equivocation detected for height %d: %s", blk.Header.Height, bInfo.eqErr())
return
2022-09-12 17:03:06 +00:00
}
if !cidExists(bInfo.blks, blkCid) {
2022-12-13 11:45:01 +00:00
bcastDict.store(key, &blksInfo{bInfo.ctx, bInfo.cancel, append(bInfo.blks, blkCid)})
2023-03-28 08:33:26 +00:00
// By calling bInfo.eqErr() inside this log we cancel the context for all blocks waiting for
// the epoch-ticket combination making them to fail and not be sent to the syncer, as
// a potential equivocation is detected.
log.Errorf("equivocation detected for height %d: %s", blk.Header.Height, bInfo.eqErr())
return
2022-09-12 17:03:06 +00:00
}
return
2022-09-12 17:03:06 +00:00
}
ctx, cancel := context.WithTimeout(ctx, cb.delay)
2022-09-13 10:33:28 +00:00
bcastDict.store(key, &blksInfo{ctx, cancel, []cid.Cid{blkCid}})
2022-09-12 17:03:06 +00:00
}
2023-03-16 16:03:25 +00:00
// WaitForDelivery is called before informing the syncer about a new block
// to check if the consistent broadcast delay triggered or if the block should
// be held off for a bit more time.
2022-09-12 17:03:06 +00:00
func (cb *ConsistentBCast) WaitForDelivery(bh *types.BlockHeader) error {
2022-12-13 11:45:01 +00:00
cb.lk.RLock()
2022-09-12 17:03:06 +00:00
bcastDict := cb.m[bh.Height]
2023-03-16 16:03:25 +00:00
key := BCastKey(bh)
2022-09-13 10:33:28 +00:00
bInfo, ok := bcastDict.load(key)
cb.lk.RUnlock()
2022-09-12 17:03:06 +00:00
if !ok {
2023-03-16 16:03:25 +00:00
return xerrors.Errorf("something went wrong, unknown block with Epoch + VRFProof (cid=%s) in consistent broadcast storage", key)
2022-09-12 17:03:06 +00:00
}
2022-09-12 17:03:06 +00:00
// Wait for the timeout
<-bInfo.ctx.Done()
2022-12-13 11:45:01 +00:00
if bcastDict.blkLen(key) > 1 {
2023-03-16 16:03:25 +00:00
return xerrors.Errorf("equivocation detected for epoch %d. Two blocks being broadcast with same VRFProof", bh.Height)
2022-09-12 17:03:06 +00:00
}
return nil
}
func (cb *ConsistentBCast) GarbageCollect(currEpoch abi.ChainEpoch) {
cb.lk.Lock()
defer cb.lk.Unlock()
// keep currEpoch-2 and delete a few more in the past
// as a sanity-check
2022-09-13 10:33:28 +00:00
// Garbage collection is triggered before block delivery,
// and we use the sanity-check in case there were a few rounds
// without delivery, and the garbage collection wasn't triggered
// for a few epochs.
for i := 0; i < GcSanityCheck; i++ {
if currEpoch > GcLookback {
delete(cb.m, currEpoch-abi.ChainEpoch(GcLookback+i))
}
2022-09-12 17:03:06 +00:00
}
}