Add tests

,
Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-01-07 19:23:49 +01:00
parent a63c34a902
commit 65724133ea
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
3 changed files with 59 additions and 10 deletions

View File

@ -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") 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 { if nerr != nil {
return nil, aerrors.Absorb(err, 5, "RLE+ invalid") 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+") return nil, aerrors.Absorb(nerr, 2, "could not decode RLE+")
} }
if cf > ss.Count { if cf > 2*ss.Count {
return nil, aerrors.New(3, "too many declared faults") return nil, aerrors.Newf(3, "too many declared faults: %d > %d", cf, 2*ss.Count)
} }
self.FaultSet = nfaults 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") 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 { if nerr != nil {
return nil, aerrors.Absorb(nerr, 1, "invalid bitfield (fatal?)") 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") 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 { if nerr != nil {
return aerrors.Absorb(nerr, 1, "invalid bitfield (fatal?)") return aerrors.Absorb(nerr, 1, "invalid bitfield (fatal?)")
} }

View File

@ -3,6 +3,7 @@ package actors_test
import ( import (
"bytes" "bytes"
"context" "context"
"math"
"math/rand" "math/rand"
"testing" "testing"
@ -13,8 +14,10 @@ import (
"github.com/filecoin-project/lotus/chain/actors/aerrors" "github.com/filecoin-project/lotus/chain/actors/aerrors"
"github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/lib/rlepluslazy"
hamt "github.com/ipfs/go-hamt-ipld" hamt "github.com/ipfs/go-hamt-ipld"
blockstore "github.com/ipfs/go-ipfs-blockstore" blockstore "github.com/ipfs/go-ipfs-blockstore"
"github.com/stretchr/testify/assert"
cbg "github.com/whyrusleeping/cbor-gen" cbg "github.com/whyrusleeping/cbor-gen"
) )
@ -50,8 +53,24 @@ func TestMinerCommitSectors(t *testing.T) {
addSectorToMiner(h, t, minerAddr, worker, client, 1) addSectorToMiner(h, t, minerAddr, worker, client, 1)
assertSectorIDs(h, t, minerAddr, []uint64{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) { func TestMinerSubmitBadFault(t *testing.T) {
var worker, client address.Address var worker, client address.Address
var minerAddr address.Address var minerAddr address.Address
@ -99,11 +118,32 @@ func TestMinerSubmitBadFault(t *testing.T) {
badnum-- badnum--
bf = types.NewBitField() bf = types.NewBitField()
bf.Set(badnum) bf.Set(badnum)
bf.Set(badnum - 1)
ret, _ = h.Invoke(t, worker, minerAddr, actors.MAMethods.DeclareFaults, &actors.DeclareFaultsParams{bf}) ret, _ = h.Invoke(t, worker, minerAddr, actors.MAMethods.DeclareFaults, &actors.DeclareFaultsParams{bf})
ApplyOK(t, ret) ApplyOK(t, ret)
ret, _ = h.Invoke(t, actors.NetworkAddress, minerAddr, actors.MAMethods.SubmitElectionPoSt, nil) ret, _ = h.Invoke(t, actors.NetworkAddress, minerAddr, actors.MAMethods.SubmitElectionPoSt, nil)
ApplyOK(t, ret) 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 = types.NewBitField()
bf.Set(1) bf.Set(1)

View File

@ -19,14 +19,23 @@ type BitField struct {
} }
func NewBitField() BitField { func NewBitField() BitField {
rle, err := rlepluslazy.FromBuf([]byte{}) bf, err := NewBitFieldFromBytes([]byte{})
if err != nil { if err != nil {
panic(err) panic(fmt.Sprintf("creating empty rle: %+v", err))
} }
return BitField{ return bf
rle: rle,
bits: make(map[uint64]struct{}),
} }
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 { func BitFieldFromSet(setBits []uint64) BitField {