Skip to content

Commit

Permalink
feat: no some slash-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
diwufeiwen committed Jun 29, 2023
1 parent 462e6e7 commit d35a67e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1
github.com/mitchellh/go-homedir v1.1.0
github.com/multiformats/go-multiaddr v0.8.0
github.com/pkg/errors v0.9.1
github.com/raulk/clock v1.1.0
github.com/stretchr/testify v1.8.3
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
Expand Down Expand Up @@ -156,7 +157,6 @@ require (
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/polydawn/refmt v0.0.0-20201211092308-30ac6d18308e // indirect
github.com/prometheus/client_golang v1.13.0 // indirect
Expand Down
26 changes: 16 additions & 10 deletions miner/multiminer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"context"
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"os"
"sync"
"time"

Expand Down Expand Up @@ -456,7 +458,7 @@ func (m *Miner) mine(ctx context.Context) {
// Wait until the next epoch, plus the propagation delay, so a new tipset
// has enough time to form.
m.untilNextEpoch(base)

if len(winPoSts) == 0 {
base.NullRounds++
}
Expand Down Expand Up @@ -485,16 +487,20 @@ func (m *Miner) broadCastBlock(ctx context.Context, base MiningBase, bm *sharedT

if err := m.sf.MinedBlock(ctx, bm.Header, base.TipSet.Height()+base.NullRounds); err != nil {
log.Errorf("<!!> SLASH FILTER ERROR: %s", err)
if err = m.sf.PutBlock(ctx, bm.Header, base.TipSet.Height()+base.NullRounds, time.Time{}, slashfilter.Error); err != nil {
log.Errorf("failed to put block: %s", err)
}

mtsMineBlockFailCtx, _ := tag.New(
ctx,
tag.Upsert(metrics.MinerID, bm.Header.Miner.String()),
)
stats.Record(mtsMineBlockFailCtx, metrics.NumberOfMiningError.M(1))
return
if !(errors.Is(err, slashfilter.ParentGrindingFaults) &&
os.Getenv("SOPHON_MINER_NO_SLASHFILTER") == "_yes_i_know_and_i_accept_that_may_loss_my_fil") {
if err = m.sf.PutBlock(ctx, bm.Header, base.TipSet.Height()+base.NullRounds, time.Time{}, slashfilter.Error); err != nil {
log.Errorf("failed to put block: %s", err)
}

mtsMineBlockFailCtx, _ := tag.New(
ctx,
tag.Upsert(metrics.MinerID, bm.Header.Miner.String()),
)
stats.Record(mtsMineBlockFailCtx, metrics.NumberOfMiningError.M(1))
return
}
}

if err := m.api.SyncSubmitBlock(ctx, bm); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions node/modules/slashfilter/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package slashfilter

import (
"context"
"errors"
"time"

"github.com/filecoin-project/go-state-types/abi"

vtypes "github.com/filecoin-project/venus/venus-shared/types"
)

var TimeOffsetMiningFaults = errors.New("time-offset mining faults")
var ParentGrindingFaults = errors.New("parent-grinding fault")

type BlockStoreType string

const (
Expand Down
10 changes: 6 additions & 4 deletions node/modules/slashfilter/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
"github.com/pkg/errors"

"github.com/filecoin-project/go-state-types/abi"

Expand Down Expand Up @@ -59,7 +60,7 @@ func (f *localSlashFilter) MinedBlock(ctx context.Context, bh *vtypes.BlockHeade
parentsKey := datastore.NewKey(fmt.Sprintf("/%s/%x", bh.Miner, vtypes.NewTipSetKey(bh.Parents...).Bytes()))
{
// time-offset mining faults (2 blocks with the same parents)
if err := checkFault(ctx, f.byParents, parentsKey, bh, "time-offset mining faults"); err != nil {
if err := checkFault(ctx, f.byParents, parentsKey, bh, TimeOffsetMiningFaults); err != nil {
return err
}
}
Expand Down Expand Up @@ -94,15 +95,16 @@ func (f *localSlashFilter) MinedBlock(ctx context.Context, bh *vtypes.BlockHeade
}

if !found {
return fmt.Errorf("produced block would trigger 'parent-grinding fault' consensus fault; miner: %s; bh: %s, expected parent: %s", bh.Miner, bh.Cid(), parent)
return errors.Wrapf(ParentGrindingFaults, "produced block would trigger consensus fault; miner: %s; bh: %s, expected parent: %s", bh.Miner, bh.Cid(), parent)
// return fmt.Errorf("produced block would trigger 'parent-grinding fault' consensus fault; miner: %s; bh: %s, expected parent: %s", bh.Miner, bh.Cid(), parent)
}
}
}

return nil
}

func checkFault(ctx context.Context, t datastore.Datastore, key datastore.Key, bh *vtypes.BlockHeader, faultType string) error {
func checkFault(ctx context.Context, t datastore.Datastore, key datastore.Key, bh *vtypes.BlockHeader, faultType error) error {
fault, err := t.Has(ctx, key)
if err != nil {
return err
Expand All @@ -123,7 +125,7 @@ func checkFault(ctx context.Context, t datastore.Datastore, key datastore.Key, b
return nil
}

return fmt.Errorf("produced block would trigger '%s' consensus fault; miner: %s; bh: %s, other: %s", faultType, bh.Miner, bh.Cid(), other)
return errors.Wrapf(faultType, "produced block would trigger consensus fault; miner: %s; bh: %s, other: %s", bh.Miner, bh.Cid(), other)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion node/modules/slashfilter/slashfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"github.com/pkg/errors"
"gorm.io/driver/mysql"
"gorm.io/gorm"

Expand Down Expand Up @@ -216,7 +217,7 @@ func (f *mysqlSlashFilter) MinedBlock(ctx context.Context, bh *types.BlockHeader
}

if !found {
return fmt.Errorf("produced block would trigger 'parent-grinding fault' consensus fault; miner: %s; bh: %s, expected parent: %s", bh.Miner, bh.Cid(), parent)
return errors.Wrapf(ParentGrindingFaults, "produced block would trigger consensus fault; miner: %s; bh: %s, expected parent: %s", bh.Miner, bh.Cid(), parent)
}
}
} else if err != gorm.ErrRecordNotFound {
Expand Down

0 comments on commit d35a67e

Please sign in to comment.