From 65724133ea898b5b55fe0c23001c1d64177360ff Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 7 Jan 2020 19:23:49 +0100 Subject: [PATCH] Add tests , Signed-off-by: Jakub Sztandera --- chain/actors/actor_miner.go | 10 ++++---- chain/actors/actor_miner_test.go | 40 ++++++++++++++++++++++++++++++++ chain/types/bitfield.go | 19 +++++++++++---- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/chain/actors/actor_miner.go b/chain/actors/actor_miner.go index 2c67ba74a..a4a11b178 100644 --- a/chain/actors/actor_miner.go +++ b/chain/actors/actor_miner.go @@ -472,7 +472,7 @@ func (sma StorageMinerActor) SubmitFallbackPoSt(act *types.Actor, vmctx types.VM return nil, aerrors.HandleExternalError(lerr, "could not load proving set node") } - faults, nerr := self.FaultSet.AllMap(ss.Count) + faults, nerr := self.FaultSet.AllMap(2 * ss.Count) if nerr != nil { return nil, aerrors.Absorb(err, 5, "RLE+ invalid") } @@ -851,8 +851,8 @@ func (sma StorageMinerActor) DeclareFaults(act *types.Actor, vmctx types.VMConte return nil, aerrors.Absorb(nerr, 2, "could not decode RLE+") } - if cf > ss.Count { - return nil, aerrors.New(3, "too many declared faults") + if cf > 2*ss.Count { + return nil, aerrors.Newf(3, "too many declared faults: %d > %d", cf, 2*ss.Count) } self.FaultSet = nfaults @@ -940,7 +940,7 @@ func (sma StorageMinerActor) SubmitElectionPoSt(act *types.Actor, vmctx types.VM return nil, aerrors.HandleExternalError(nerr, "failed to load proving set") } - faults, nerr := self.FaultSet.AllMap(ss.Count) + faults, nerr := self.FaultSet.AllMap(2 * ss.Count) if nerr != nil { return nil, aerrors.Absorb(nerr, 1, "invalid bitfield (fatal?)") } @@ -988,7 +988,7 @@ func onSuccessfulPoSt(self *StorageMinerActorState, vmctx types.VMContext, activ return aerrors.HandleExternalError(nerr, "failed to load sector set") } - faults, nerr := self.FaultSet.All(ss.Count) + faults, nerr := self.FaultSet.All(2 * ss.Count) if nerr != nil { return aerrors.Absorb(nerr, 1, "invalid bitfield (fatal?)") } diff --git a/chain/actors/actor_miner_test.go b/chain/actors/actor_miner_test.go index a522fe349..7aa4d73e8 100644 --- a/chain/actors/actor_miner_test.go +++ b/chain/actors/actor_miner_test.go @@ -3,6 +3,7 @@ package actors_test import ( "bytes" "context" + "math" "math/rand" "testing" @@ -13,8 +14,10 @@ import ( "github.com/filecoin-project/lotus/chain/actors/aerrors" "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/lotus/lib/rlepluslazy" hamt "github.com/ipfs/go-hamt-ipld" blockstore "github.com/ipfs/go-ipfs-blockstore" + "github.com/stretchr/testify/assert" cbg "github.com/whyrusleeping/cbor-gen" ) @@ -50,8 +53,24 @@ func TestMinerCommitSectors(t *testing.T) { addSectorToMiner(h, t, minerAddr, worker, client, 1) assertSectorIDs(h, t, minerAddr, []uint64{1}) + } +type badRuns struct { + done bool +} + +func (br *badRuns) HasNext() bool { + return !br.done +} + +func (br *badRuns) NextRun() (rlepluslazy.Run, error) { + br.done = true + return rlepluslazy.Run{true, math.MaxInt64}, nil +} + +var _ rlepluslazy.RunIterator = (*badRuns)(nil) + func TestMinerSubmitBadFault(t *testing.T) { var worker, client address.Address var minerAddr address.Address @@ -99,11 +118,32 @@ func TestMinerSubmitBadFault(t *testing.T) { badnum-- bf = types.NewBitField() bf.Set(badnum) + bf.Set(badnum - 1) ret, _ = h.Invoke(t, worker, minerAddr, actors.MAMethods.DeclareFaults, &actors.DeclareFaultsParams{bf}) ApplyOK(t, ret) ret, _ = h.Invoke(t, actors.NetworkAddress, minerAddr, actors.MAMethods.SubmitElectionPoSt, nil) + ApplyOK(t, ret) + assertSectorIDs(h, t, minerAddr, []uint64{1}) + + bf.Set(badnum - 2) + ret, _ = h.Invoke(t, worker, minerAddr, actors.MAMethods.DeclareFaults, &actors.DeclareFaultsParams{bf}) + if ret.ExitCode != 3 { + t.Errorf("expected exit code 3, got %d: %+v", ret.ExitCode, ret.ActorErr) + } + assertSectorIDs(h, t, minerAddr, []uint64{1}) + + rle, err := rlepluslazy.EncodeRuns(&badRuns{}, []byte{}) + assert.NoError(t, err) + + bf, err = types.NewBitFieldFromBytes(rle) + assert.NoError(t, err) + ret, _ = h.Invoke(t, worker, minerAddr, actors.MAMethods.DeclareFaults, &actors.DeclareFaultsParams{bf}) + if ret.ExitCode != 3 { + t.Errorf("expected exit code 3, got %d: %+v", ret.ExitCode, ret.ActorErr) + } + assertSectorIDs(h, t, minerAddr, []uint64{1}) bf = types.NewBitField() bf.Set(1) diff --git a/chain/types/bitfield.go b/chain/types/bitfield.go index a9498d3b9..81031085b 100644 --- a/chain/types/bitfield.go +++ b/chain/types/bitfield.go @@ -19,14 +19,23 @@ type BitField struct { } func NewBitField() BitField { - rle, err := rlepluslazy.FromBuf([]byte{}) + bf, err := NewBitFieldFromBytes([]byte{}) if err != nil { - panic(err) + panic(fmt.Sprintf("creating empty rle: %+v", err)) } - return BitField{ - rle: rle, - bits: make(map[uint64]struct{}), + return bf +} + +func NewBitFieldFromBytes(rle []byte) (BitField, error) { + bf := BitField{} + rlep, err := rlepluslazy.FromBuf(rle) + if err != nil { + return BitField{}, xerrors.Errorf("could not decode rle+: %w", err) } + bf.rle = rlep + bf.bits = make(map[uint64]struct{}) + return bf, nil + } func BitFieldFromSet(setBits []uint64) BitField {