diff --git a/Makefile b/Makefile index f741b65..ad04361 100644 --- a/Makefile +++ b/Makefile @@ -61,6 +61,9 @@ gen: print-%: @echo $*=$($*) +lint: + golangci-lint run + # docker .PHONY: docker diff --git a/go.mod b/go.mod index 7bf5d66..db1368a 100644 --- a/go.mod +++ b/go.mod @@ -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.9.0 + github.com/pkg/errors v0.9.1 github.com/raulk/clock v1.1.0 github.com/stretchr/testify v1.8.4 github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 @@ -168,7 +169,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.89.0 // indirect github.com/prometheus/client_golang v1.14.0 // indirect diff --git a/miner/miner_test.go b/miner/miner_test.go index 16c6103..97e3f23 100644 --- a/miner/miner_test.go +++ b/miner/miner_test.go @@ -221,7 +221,8 @@ func TestParentGridFail(t *testing.T) { } assert.True(t, len(chain.dropBlks) > 0) - chain.logMatcher.match("SLASH FILTER ERROR: produced block would trigger 'parent-grinding fault'") + chain.logMatcher.match("SLASH FILTER ERROR: produced block would trigger") + chain.logMatcher.match("parent-grinding fault") } func TestSameHeight(t *testing.T) { diff --git a/miner/multiminer.go b/miner/multiminer.go index ce6a0f8..5f8964a 100644 --- a/miner/multiminer.go +++ b/miner/multiminer.go @@ -5,7 +5,9 @@ import ( "context" "crypto/rand" "encoding/binary" + "errors" "fmt" + "os" "sync" "time" @@ -545,16 +547,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{}, types.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{}, types.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 { diff --git a/node/modules/slashfilter/api.go b/node/modules/slashfilter/api.go index a47c852..69341cd 100644 --- a/node/modules/slashfilter/api.go +++ b/node/modules/slashfilter/api.go @@ -2,6 +2,7 @@ package slashfilter import ( "context" + "errors" "time" "github.com/filecoin-project/go-state-types/abi" @@ -10,6 +11,9 @@ import ( 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 ( diff --git a/node/modules/slashfilter/local.go b/node/modules/slashfilter/local.go index 4af7391..e6c29b9 100644 --- a/node/modules/slashfilter/local.go +++ b/node/modules/slashfilter/local.go @@ -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" @@ -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 } } @@ -94,7 +95,8 @@ 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) } } } @@ -106,7 +108,7 @@ func (f *localSlashFilter) ListBlock(ctx context.Context, params *types.BlocksQu return nil, fmt.Errorf("you are using levelDB, List Block is not supported") } -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 @@ -127,7 +129,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 diff --git a/node/modules/slashfilter/slashfilter.go b/node/modules/slashfilter/slashfilter.go index c3be0f2..9767697 100644 --- a/node/modules/slashfilter/slashfilter.go +++ b/node/modules/slashfilter/slashfilter.go @@ -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" @@ -202,7 +203,7 @@ func (f *mysqlSlashFilter) MinedBlock(ctx context.Context, bh *venusTypes.BlockH } 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 {