Skip to content

Commit

Permalink
Merge pull request #7007 from filecoin-project/fix/panic-less
Browse files Browse the repository at this point in the history
Dont panic if input is bad
  • Loading branch information
Stebalien authored Aug 10, 2021
2 parents c24d4e1 + 2340302 commit 65e3540
Show file tree
Hide file tree
Showing 17 changed files with 235 additions and 97 deletions.
109 changes: 67 additions & 42 deletions chain/actors/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sort"

"github.com/filecoin-project/go-state-types/big"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/network"
Expand Down Expand Up @@ -168,31 +169,31 @@ func SetMinVerifiedDealSize(size abi.StoragePower) {

}

func GetMaxProveCommitDuration(ver actors.Version, t abi.RegisteredSealProof) abi.ChainEpoch {
func GetMaxProveCommitDuration(ver actors.Version, t abi.RegisteredSealProof) (abi.ChainEpoch, error) {
switch ver {

case actors.Version0:

return miner0.MaxSealDuration[t]
return miner0.MaxSealDuration[t], nil

case actors.Version2:

return miner2.MaxProveCommitDuration[t]
return miner2.MaxProveCommitDuration[t], nil

case actors.Version3:

return miner3.MaxProveCommitDuration[t]
return miner3.MaxProveCommitDuration[t], nil

case actors.Version4:

return miner4.MaxProveCommitDuration[t]
return miner4.MaxProveCommitDuration[t], nil

case actors.Version5:

return miner5.MaxProveCommitDuration[t]
return miner5.MaxProveCommitDuration[t], nil

default:
panic("unsupported actors version")
return 0, xerrors.Errorf("unsupported actors version")
}
}

Expand Down Expand Up @@ -227,31 +228,40 @@ func DealProviderCollateralBounds(
size abi.PaddedPieceSize, verified bool,
rawBytePower, qaPower, baselinePower abi.StoragePower,
circulatingFil abi.TokenAmount, nwVer network.Version,
) (min, max abi.TokenAmount) {
switch actors.VersionForNetwork(nwVer) {
) (min, max abi.TokenAmount, err error) {
v, err := actors.VersionForNetwork(nwVer)
if err != nil {
return big.Zero(), big.Zero(), err
}
switch v {

case actors.Version0:

return market0.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil, nwVer)
min, max := market0.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil, nwVer)
return min, max, nil

case actors.Version2:

return market2.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil)
min, max := market2.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil)
return min, max, nil

case actors.Version3:

return market3.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil)
min, max := market3.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil)
return min, max, nil

case actors.Version4:

return market4.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil)
min, max := market4.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil)
return min, max, nil

case actors.Version5:

return market5.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil)
min, max := market5.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil)
return min, max, nil

default:
panic("unsupported actors version")
return big.Zero(), big.Zero(), xerrors.Errorf("unsupported actors version")
}
}

Expand Down Expand Up @@ -310,8 +320,11 @@ func GetMaxPoStPartitions(nv network.Version, p abi.RegisteredPoStProof) (int, e
if err != nil {
return 0, err
}
maxSectors := uint64(GetAddressedSectorsMax(nv))
return int(maxSectors / sectorsPerPart), nil
maxSectors, err := GetAddressedSectorsMax(nv)
if err != nil {
return 0, err
}
return int(uint64(maxSectors) / sectorsPerPart), nil
}

func GetDefaultSectorSize() abi.SectorSize {
Expand Down Expand Up @@ -345,82 +358,94 @@ func GetSectorMaxLifetime(proof abi.RegisteredSealProof, nwVer network.Version)
return builtin5.SealProofPoliciesV11[proof].SectorMaxLifetime
}

func GetAddressedSectorsMax(nwVer network.Version) int {
switch actors.VersionForNetwork(nwVer) {
func GetAddressedSectorsMax(nwVer network.Version) (int, error) {
v, err := actors.VersionForNetwork(nwVer)
if err != nil {
return 0, err
}
switch v {

case actors.Version0:
return miner0.AddressedSectorsMax
return miner0.AddressedSectorsMax, nil

case actors.Version2:
return miner2.AddressedSectorsMax
return miner2.AddressedSectorsMax, nil

case actors.Version3:
return miner3.AddressedSectorsMax
return miner3.AddressedSectorsMax, nil

case actors.Version4:
return miner4.AddressedSectorsMax
return miner4.AddressedSectorsMax, nil

case actors.Version5:
return miner5.AddressedSectorsMax
return miner5.AddressedSectorsMax, nil

default:
panic("unsupported network version")
return 0, xerrors.Errorf("unsupported network version")
}
}

func GetDeclarationsMax(nwVer network.Version) int {
switch actors.VersionForNetwork(nwVer) {
func GetDeclarationsMax(nwVer network.Version) (int, error) {
v, err := actors.VersionForNetwork(nwVer)
if err != nil {
return 0, err
}
switch v {

case actors.Version0:

// TODO: Should we instead panic here since the concept doesn't exist yet?
return miner0.AddressedPartitionsMax
// TODO: Should we instead error here since the concept doesn't exist yet?
return miner0.AddressedPartitionsMax, nil

case actors.Version2:

return miner2.DeclarationsMax
return miner2.DeclarationsMax, nil

case actors.Version3:

return miner3.DeclarationsMax
return miner3.DeclarationsMax, nil

case actors.Version4:

return miner4.DeclarationsMax
return miner4.DeclarationsMax, nil

case actors.Version5:

return miner5.DeclarationsMax
return miner5.DeclarationsMax, nil

default:
panic("unsupported network version")
return 0, xerrors.Errorf("unsupported network version")
}
}

func AggregateNetworkFee(nwVer network.Version, aggregateSize int, baseFee abi.TokenAmount) abi.TokenAmount {
switch actors.VersionForNetwork(nwVer) {
func AggregateNetworkFee(nwVer network.Version, aggregateSize int, baseFee abi.TokenAmount) (abi.TokenAmount, error) {
v, err := actors.VersionForNetwork(nwVer)
if err != nil {
return big.Zero(), err
}
switch v {

case actors.Version0:

return big.Zero()
return big.Zero(), nil

case actors.Version2:

return big.Zero()
return big.Zero(), nil

case actors.Version3:

return big.Zero()
return big.Zero(), nil

case actors.Version4:

return big.Zero()
return big.Zero(), nil

case actors.Version5:

return miner5.AggregateNetworkFee(aggregateSize, baseFee)
return miner5.AggregateNetworkFee(aggregateSize, baseFee), nil

default:
panic("unsupported network version")
return big.Zero(), xerrors.Errorf("unsupported network version")
}
}
74 changes: 48 additions & 26 deletions chain/actors/policy/policy.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sort"

"github.com/filecoin-project/go-state-types/big"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/network"
Expand Down Expand Up @@ -117,18 +118,18 @@ func SetMinVerifiedDealSize(size abi.StoragePower) {
{{end}}
}

func GetMaxProveCommitDuration(ver actors.Version, t abi.RegisteredSealProof) abi.ChainEpoch {
func GetMaxProveCommitDuration(ver actors.Version, t abi.RegisteredSealProof) (abi.ChainEpoch, error) {
switch ver {
{{range .versions}}
case actors.Version{{.}}:
{{if (eq . 0)}}
return miner{{.}}.MaxSealDuration[t]
return miner{{.}}.MaxSealDuration[t], nil
{{else}}
return miner{{.}}.MaxProveCommitDuration[t]
return miner{{.}}.MaxProveCommitDuration[t], nil
{{end}}
{{end}}
default:
panic("unsupported actors version")
return 0, xerrors.Errorf("unsupported actors version")
}
}

Expand All @@ -150,18 +151,24 @@ func DealProviderCollateralBounds(
size abi.PaddedPieceSize, verified bool,
rawBytePower, qaPower, baselinePower abi.StoragePower,
circulatingFil abi.TokenAmount, nwVer network.Version,
) (min, max abi.TokenAmount) {
switch actors.VersionForNetwork(nwVer) {
) (min, max abi.TokenAmount, err error) {
v, err := actors.VersionForNetwork(nwVer)
if err != nil {
return big.Zero(), big.Zero(), err
}
switch v {
{{range .versions}}
case actors.Version{{.}}:
{{if (eq . 0)}}
return market{{.}}.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil, nwVer)
min, max := market{{.}}.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil, nwVer)
return min, max, nil
{{else}}
return market{{.}}.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil)
min, max := market{{.}}.DealProviderCollateralBounds(size, verified, rawBytePower, qaPower, baselinePower, circulatingFil)
return min, max, nil
{{end}}
{{end}}
default:
panic("unsupported actors version")
return big.Zero(), big.Zero(), xerrors.Errorf("unsupported actors version")
}
}

Expand Down Expand Up @@ -201,8 +208,11 @@ func GetMaxPoStPartitions(nv network.Version, p abi.RegisteredPoStProof) (int, e
if err != nil {
return 0, err
}
maxSectors := uint64(GetAddressedSectorsMax(nv))
return int(maxSectors / sectorsPerPart), nil
maxSectors, err := GetAddressedSectorsMax(nv)
if err != nil {
return 0, err
}
return int(uint64(maxSectors) / sectorsPerPart), nil
}

func GetDefaultSectorSize() abi.SectorSize {
Expand Down Expand Up @@ -236,44 +246,56 @@ func GetSectorMaxLifetime(proof abi.RegisteredSealProof, nwVer network.Version)
return builtin{{.latestVersion}}.SealProofPoliciesV11[proof].SectorMaxLifetime
}

func GetAddressedSectorsMax(nwVer network.Version) int {
switch actors.VersionForNetwork(nwVer) {
func GetAddressedSectorsMax(nwVer network.Version) (int, error) {
v, err := actors.VersionForNetwork(nwVer)
if err != nil {
return 0, err
}
switch v {
{{range .versions}}
case actors.Version{{.}}:
return miner{{.}}.AddressedSectorsMax
return miner{{.}}.AddressedSectorsMax, nil
{{end}}
default:
panic("unsupported network version")
return 0, xerrors.Errorf("unsupported network version")
}
}

func GetDeclarationsMax(nwVer network.Version) int {
switch actors.VersionForNetwork(nwVer) {
func GetDeclarationsMax(nwVer network.Version) (int, error) {
v, err := actors.VersionForNetwork(nwVer)
if err != nil {
return 0, err
}
switch v {
{{range .versions}}
case actors.Version{{.}}:
{{if (eq . 0)}}
// TODO: Should we instead panic here since the concept doesn't exist yet?
return miner{{.}}.AddressedPartitionsMax
// TODO: Should we instead error here since the concept doesn't exist yet?
return miner{{.}}.AddressedPartitionsMax, nil
{{else}}
return miner{{.}}.DeclarationsMax
return miner{{.}}.DeclarationsMax, nil
{{end}}
{{end}}
default:
panic("unsupported network version")
return 0, xerrors.Errorf("unsupported network version")
}
}

func AggregateNetworkFee(nwVer network.Version, aggregateSize int, baseFee abi.TokenAmount) abi.TokenAmount {
switch actors.VersionForNetwork(nwVer) {
func AggregateNetworkFee(nwVer network.Version, aggregateSize int, baseFee abi.TokenAmount) (abi.TokenAmount, error) {
v, err := actors.VersionForNetwork(nwVer)
if err != nil {
return big.Zero(), err
}
switch v {
{{range .versions}}
case actors.Version{{.}}:
{{if (le . 4)}}
return big.Zero()
return big.Zero(), nil
{{else}}
return miner{{.}}.AggregateNetworkFee(aggregateSize, baseFee)
return miner{{.}}.AggregateNetworkFee(aggregateSize, baseFee), nil
{{end}}
{{end}}
default:
panic("unsupported network version")
return big.Zero(), xerrors.Errorf("unsupported network version")
}
}
Loading

0 comments on commit 65e3540

Please sign in to comment.