Skip to content

Commit

Permalink
Fix omissions for spec-actor miner (#4572)
Browse files Browse the repository at this point in the history
Co-authored-by: 一页素书 <[email protected]>
  • Loading branch information
diwufeiwen and ta0li authored Oct 19, 2021
1 parent 9668dfe commit 795d911
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 36 deletions.
24 changes: 12 additions & 12 deletions pkg/types/specactors/aerrors/error_test.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
package aerrors_test

import (
"github.com/filecoin-project/venus/pkg/types/specactors/aerrors"
"testing"

"github.com/filecoin-project/go-state-types/exitcode"
"github.com/stretchr/testify/assert"
"golang.org/x/xerrors"

tf "github.com/filecoin-project/venus/pkg/testhelpers/testflags"
. "github.com/filecoin-project/venus/pkg/types/specactors/aerrors"
)

func TestFatalError(t *testing.T) {
tf.UnitTest(t)
e1 := xerrors.New("out of disk space")
e2 := xerrors.Errorf("could not put node: %w", e1)
e3 := xerrors.Errorf("could not save head: %w", e2)
ae := aerrors.Escalate(e3, "failed to save the head")
aw1 := aerrors.Wrap(ae, "saving head of new miner actor")
aw2 := aerrors.Absorb(aw1, 1, "try to absorb fatal error")
aw3 := aerrors.Wrap(aw2, "initializing actor")
aw4 := aerrors.Wrap(aw3, "creating miner in storage market")
ae := Escalate(e3, "failed to save the head")
aw1 := Wrap(ae, "saving head of new miner actor")
aw2 := Absorb(aw1, 1, "try to absorb fatal error")
aw3 := Wrap(aw2, "initializing actor")
aw4 := Wrap(aw3, "creating miner in storage market")
t.Logf("Verbose error: %+v", aw4)
t.Logf("Normal error: %v", aw4)
assert.True(t, aerrors.IsFatal(aw4), "should be fatal")
assert.True(t, IsFatal(aw4), "should be fatal")
}
func TestAbsorbeError(t *testing.T) {
tf.UnitTest(t)
e1 := xerrors.New("EOF")
e2 := xerrors.Errorf("could not decode: %w", e1)
ae := aerrors.Absorb(e2, 35, "failed to decode CBOR")
aw1 := aerrors.Wrap(ae, "saving head of new miner actor")
aw2 := aerrors.Wrap(aw1, "initializing actor")
aw3 := aerrors.Wrap(aw2, "creating miner in storage market")
ae := Absorb(e2, 35, "failed to decode CBOR")
aw1 := Wrap(ae, "saving head of new miner actor")
aw2 := Wrap(aw1, "initializing actor")
aw3 := Wrap(aw2, "creating miner in storage market")
t.Logf("Verbose error: %+v", aw3)
t.Logf("Normal error: %v", aw3)
assert.Equal(t, exitcode.ExitCode(35), aerrors.RetCode(aw3))
assert.Equal(t, exitcode.ExitCode(35), RetCode(aw3))
}
20 changes: 19 additions & 1 deletion pkg/types/specactors/builtin/miner/actor.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func init() {

var Methods = builtin{{.latestVersion}}.MethodsMiner

// Unchanged between v0, v2, v3, and v4 actors
// Unchanged between v0, v2, v3, v4, and v5 actors
var WPoStProvingPeriod = miner0.WPoStProvingPeriod
var WPoStPeriodDeadlines = miner0.WPoStPeriodDeadlines
var WPoStChallengeWindow = miner0.WPoStChallengeWindow
Expand Down Expand Up @@ -105,6 +105,7 @@ type State interface {
// UnallocatedSectorNumbers returns up to count unallocated sector numbers (or less than
// count if there aren't enough).
UnallocatedSectorNumbers(count int) ([]abi.SectorNumber, error)
GetAllocatedSectors() (*bitfield.BitField, error)

// Note that ProvingPeriodStart is deprecated and will be renamed / removed in a future version of actors
GetProvingPeriodStart() (abi.ChainEpoch, error)
Expand Down Expand Up @@ -140,11 +141,28 @@ type Deadline interface {
}

type Partition interface {
// AllSectors returns all sector numbers in this partition, including faulty, unproven, and terminated sectors
AllSectors() (bitfield.BitField, error)

// Subset of sectors detected/declared faulty and not yet recovered (excl. from PoSt).
// Faults ∩ Terminated = ∅
FaultySectors() (bitfield.BitField, error)

// Subset of faulty sectors expected to recover on next PoSt
// Recoveries ∩ Terminated = ∅
RecoveringSectors() (bitfield.BitField, error)

// Live sectors are those that are not terminated (but may be faulty).
LiveSectors() (bitfield.BitField, error)

// Active sectors are those that are neither terminated nor faulty nor unproven, i.e. actively contributing power.
ActiveSectors() (bitfield.BitField, error)

// Unproven sectors in this partition. This bitfield will be cleared on
// a successful window post (or at the end of the partition's next
// deadline). At that time, any still unproven sectors will be added to
// the faulty sector bitfield.
UnprovenSectors() (bitfield.BitField, error)
}

type SectorOnChainInfo struct {
Expand Down
20 changes: 19 additions & 1 deletion pkg/types/specactors/builtin/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func init() {

var Methods = builtin6.MethodsMiner

// Unchanged between v0, v2, v3, and v4 actors
// Unchanged between v0, v2, v3, v4, and v5 actors
var WPoStProvingPeriod = miner0.WPoStProvingPeriod
var WPoStPeriodDeadlines = miner0.WPoStPeriodDeadlines
var WPoStChallengeWindow = miner0.WPoStChallengeWindow
Expand Down Expand Up @@ -179,6 +179,7 @@ type State interface {
// UnallocatedSectorNumbers returns up to count unallocated sector numbers (or less than
// count if there aren't enough).
UnallocatedSectorNumbers(count int) ([]abi.SectorNumber, error)
GetAllocatedSectors() (*bitfield.BitField, error)

// Note that ProvingPeriodStart is deprecated and will be renamed / removed in a future version of actors
GetProvingPeriodStart() (abi.ChainEpoch, error)
Expand Down Expand Up @@ -214,11 +215,28 @@ type Deadline interface {
}

type Partition interface {
// AllSectors returns all sector numbers in this partition, including faulty, unproven, and terminated sectors
AllSectors() (bitfield.BitField, error)

// Subset of sectors detected/declared faulty and not yet recovered (excl. from PoSt).
// Faults ∩ Terminated = ∅
FaultySectors() (bitfield.BitField, error)

// Subset of faulty sectors expected to recover on next PoSt
// Recoveries ∩ Terminated = ∅
RecoveringSectors() (bitfield.BitField, error)

// Live sectors are those that are not terminated (but may be faulty).
LiveSectors() (bitfield.BitField, error)

// Active sectors are those that are neither terminated nor faulty nor unproven, i.e. actively contributing power.
ActiveSectors() (bitfield.BitField, error)

// Unproven sectors in this partition. This bitfield will be cleared on
// a successful window post (or at the end of the partition's next
// deadline). At that time, any still unproven sectors will be added to
// the faulty sector bitfield.
UnprovenSectors() (bitfield.BitField, error)
}

type SectorOnChainInfo struct {
Expand Down
22 changes: 19 additions & 3 deletions pkg/types/specactors/builtin/miner/state.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ func (s *state{{.v}}) loadAllocatedSectorNumbers() (bitfield.BitField, error) {
}

func (s *state{{.v}}) IsAllocated(num abi.SectorNumber) (bool, error) {
var allocatedSectors bitfield.BitField
if err := s.store.Get(s.store.Context(), s.State.AllocatedSectors, &allocatedSectors); err != nil {
allocatedSectors, err := s.loadAllocatedSectorNumbers()
if err != nil {
return false, err
}

Expand Down Expand Up @@ -318,6 +318,15 @@ func (s *state{{.v}}) UnallocatedSectorNumbers(count int) ([]abi.SectorNumber, e
return sectors, nil
}

func (s *state{{.v}}) GetAllocatedSectors() (*bitfield.BitField, error) {
var allocatedSectors bitfield.BitField
if err := s.store.Get(s.store.Context(), s.State.AllocatedSectors, &allocatedSectors); err != nil {
return nil, err
}

return &allocatedSectors, nil
}

func (s *state{{.v}}) LoadDeadline(idx uint64) (Deadline, error) {
dls, err := s.State.LoadDeadlines(s.store)
if err != nil {
Expand Down Expand Up @@ -470,12 +479,15 @@ func (s *state{{.v}}) EraseAllUnproven() error {

return dls.UpdateDeadline(s.store, dindx, dl)
})
if err != nil {
return err
}

return s.State.SaveDeadlines(s.store, dls)
{{else}}
// field doesn't exist until v2
return nil
{{end}}
return nil
}

func (d *deadline{{.v}}) LoadPartition(idx uint64) (Partition, error) {
Expand Down Expand Up @@ -537,6 +549,10 @@ func (p *partition{{.v}}) RecoveringSectors() (bitfield.BitField, error) {
return p.Partition.Recoveries, nil
}

func (p *partition{{.v}}) UnprovenSectors() (bitfield.BitField, error) {
return {{if (ge .v 2)}}p.Partition.Unproven{{else}}bitfield.New(){{end}}, nil
}

func fromV{{.v}}SectorOnChainInfo(v{{.v}} miner{{.v}}.SectorOnChainInfo) SectorOnChainInfo {
{{if (ge .v 2)}}
return SectorOnChainInfo{
Expand Down
19 changes: 16 additions & 3 deletions pkg/types/specactors/builtin/miner/v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ func (s *state0) loadAllocatedSectorNumbers() (bitfield.BitField, error) {
}

func (s *state0) IsAllocated(num abi.SectorNumber) (bool, error) {
var allocatedSectors bitfield.BitField
if err := s.store.Get(s.store.Context(), s.State.AllocatedSectors, &allocatedSectors); err != nil {
allocatedSectors, err := s.loadAllocatedSectorNumbers()
if err != nil {
return false, err
}

Expand Down Expand Up @@ -311,6 +311,15 @@ func (s *state0) UnallocatedSectorNumbers(count int) ([]abi.SectorNumber, error)
return sectors, nil
}

func (s *state0) GetAllocatedSectors() (*bitfield.BitField, error) {
var allocatedSectors bitfield.BitField
if err := s.store.Get(s.store.Context(), s.State.AllocatedSectors, &allocatedSectors); err != nil {
return nil, err
}

return &allocatedSectors, nil
}

func (s *state0) LoadDeadline(idx uint64) (Deadline, error) {
dls, err := s.State.LoadDeadlines(s.store)
if err != nil {
Expand Down Expand Up @@ -435,8 +444,8 @@ func (s *state0) decodeSectorPreCommitOnChainInfo(val *cbg.Deferred) (SectorPreC
func (s *state0) EraseAllUnproven() error {

// field doesn't exist until v2

return nil

}

func (d *deadline0) LoadPartition(idx uint64) (Partition, error) {
Expand Down Expand Up @@ -491,6 +500,10 @@ func (p *partition0) RecoveringSectors() (bitfield.BitField, error) {
return p.Partition.Recoveries, nil
}

func (p *partition0) UnprovenSectors() (bitfield.BitField, error) {
return bitfield.New(), nil
}

func fromV0SectorOnChainInfo(v0 miner0.SectorOnChainInfo) SectorOnChainInfo {

return (SectorOnChainInfo)(v0)
Expand Down
21 changes: 18 additions & 3 deletions pkg/types/specactors/builtin/miner/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ func (s *state2) loadAllocatedSectorNumbers() (bitfield.BitField, error) {
}

func (s *state2) IsAllocated(num abi.SectorNumber) (bool, error) {
var allocatedSectors bitfield.BitField
if err := s.store.Get(s.store.Context(), s.State.AllocatedSectors, &allocatedSectors); err != nil {
allocatedSectors, err := s.loadAllocatedSectorNumbers()
if err != nil {
return false, err
}

Expand Down Expand Up @@ -309,6 +309,15 @@ func (s *state2) UnallocatedSectorNumbers(count int) ([]abi.SectorNumber, error)
return sectors, nil
}

func (s *state2) GetAllocatedSectors() (*bitfield.BitField, error) {
var allocatedSectors bitfield.BitField
if err := s.store.Get(s.store.Context(), s.State.AllocatedSectors, &allocatedSectors); err != nil {
return nil, err
}

return &allocatedSectors, nil
}

func (s *state2) LoadDeadline(idx uint64) (Deadline, error) {
dls, err := s.State.LoadDeadlines(s.store)
if err != nil {
Expand Down Expand Up @@ -461,10 +470,12 @@ func (s *state2) EraseAllUnproven() error {

return dls.UpdateDeadline(s.store, dindx, dl)
})
if err != nil {
return err
}

return s.State.SaveDeadlines(s.store, dls)

return nil
}

func (d *deadline2) LoadPartition(idx uint64) (Partition, error) {
Expand Down Expand Up @@ -519,6 +530,10 @@ func (p *partition2) RecoveringSectors() (bitfield.BitField, error) {
return p.Partition.Recoveries, nil
}

func (p *partition2) UnprovenSectors() (bitfield.BitField, error) {
return p.Partition.Unproven, nil
}

func fromV2SectorOnChainInfo(v2 miner2.SectorOnChainInfo) SectorOnChainInfo {

return SectorOnChainInfo{
Expand Down
21 changes: 18 additions & 3 deletions pkg/types/specactors/builtin/miner/v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ func (s *state3) loadAllocatedSectorNumbers() (bitfield.BitField, error) {
}

func (s *state3) IsAllocated(num abi.SectorNumber) (bool, error) {
var allocatedSectors bitfield.BitField
if err := s.store.Get(s.store.Context(), s.State.AllocatedSectors, &allocatedSectors); err != nil {
allocatedSectors, err := s.loadAllocatedSectorNumbers()
if err != nil {
return false, err
}

Expand Down Expand Up @@ -311,6 +311,15 @@ func (s *state3) UnallocatedSectorNumbers(count int) ([]abi.SectorNumber, error)
return sectors, nil
}

func (s *state3) GetAllocatedSectors() (*bitfield.BitField, error) {
var allocatedSectors bitfield.BitField
if err := s.store.Get(s.store.Context(), s.State.AllocatedSectors, &allocatedSectors); err != nil {
return nil, err
}

return &allocatedSectors, nil
}

func (s *state3) LoadDeadline(idx uint64) (Deadline, error) {
dls, err := s.State.LoadDeadlines(s.store)
if err != nil {
Expand Down Expand Up @@ -458,10 +467,12 @@ func (s *state3) EraseAllUnproven() error {

return dls.UpdateDeadline(s.store, dindx, dl)
})
if err != nil {
return err
}

return s.State.SaveDeadlines(s.store, dls)

return nil
}

func (d *deadline3) LoadPartition(idx uint64) (Partition, error) {
Expand Down Expand Up @@ -520,6 +531,10 @@ func (p *partition3) RecoveringSectors() (bitfield.BitField, error) {
return p.Partition.Recoveries, nil
}

func (p *partition3) UnprovenSectors() (bitfield.BitField, error) {
return p.Partition.Unproven, nil
}

func fromV3SectorOnChainInfo(v3 miner3.SectorOnChainInfo) SectorOnChainInfo {

return SectorOnChainInfo{
Expand Down
Loading

0 comments on commit 795d911

Please sign in to comment.