Remove network version panic

This commit is contained in:
ZenGround0 2021-08-10 13:07:30 -04:00
parent bfd69f5381
commit 6da26dcefc
15 changed files with 199 additions and 93 deletions

View File

@ -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"
@ -168,31 +169,26 @@ 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")
}
}
@ -227,31 +223,35 @@ 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")
}
}
@ -310,8 +310,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 {
@ -345,82 +348,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:
return 0, xerrors.Errorf("unsupported network version")
}
}
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 error here since the concept doesn't exist yet?
return miner0.AddressedPartitionsMax, nil
case actors.Version2:
return miner2.DeclarationsMax, nil
case actors.Version3:
return miner3.DeclarationsMax, nil
case actors.Version4:
return miner4.DeclarationsMax, nil
case actors.Version5:
return miner5.DeclarationsMax, nil
default:
panic("unsupported network version")
}
}
func GetDeclarationsMax(nwVer network.Version) int {
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:
// TODO: Should we instead panic here since the concept doesn't exist yet?
return miner0.AddressedPartitionsMax
return big.Zero(), nil
case actors.Version2:
return miner2.DeclarationsMax
return big.Zero(), nil
case actors.Version3:
return miner3.DeclarationsMax
return big.Zero(), nil
case actors.Version4:
return miner4.DeclarationsMax
return big.Zero(), nil
case actors.Version5:
return miner5.DeclarationsMax
return miner5.AggregateNetworkFee(aggregateSize, baseFee), nil
default:
panic("unsupported network version")
}
}
func AggregateNetworkFee(nwVer network.Version, aggregateSize int, baseFee abi.TokenAmount) abi.TokenAmount {
switch actors.VersionForNetwork(nwVer) {
case actors.Version0:
return big.Zero()
case actors.Version2:
return big.Zero()
case actors.Version3:
return big.Zero()
case actors.Version4:
return big.Zero()
case actors.Version5:
return miner5.AggregateNetworkFee(aggregateSize, baseFee)
default:
panic("unsupported network version")
return big.Zero(), xerrors.Errorf("unsupported network version")
}
}

View File

@ -149,7 +149,10 @@ func MakeInitialStateTree(ctx context.Context, bs bstore.Blockstore, template ge
return nil, nil, xerrors.Errorf("making new state tree: %w", err)
}
av := actors.VersionForNetwork(template.NetworkVersion)
av, err := actors.VersionForNetwork(template.NetworkVersion)
if err != nil {
return nil, nil, xerrors.Errorf("getting network version: %w", err)
}
// Create system actor

View File

@ -81,7 +81,10 @@ func mkFakedSigSyscalls(base vm.SyscallBuilder) vm.SyscallBuilder {
func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sys vm.SyscallBuilder, sroot cid.Cid, miners []genesis.Miner, nv network.Version) (cid.Cid, error) {
cst := cbor.NewCborStore(cs.StateBlockstore())
av := actors.VersionForNetwork(nv)
av, err := actors.VersionForNetwork(nv)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get network version: %w", err)
}
csc := func(context.Context, abi.ChainEpoch, *state.StateTree) (abi.TokenAmount, error) {
return big.Zero(), nil
@ -291,7 +294,7 @@ func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sys vm.Syscal
return cid.Undef, xerrors.Errorf("setting power state: %w", err)
}
rewact, err := SetupRewardActor(ctx, cs.StateBlockstore(), big.Zero(), actors.VersionForNetwork(nv))
rewact, err := SetupRewardActor(ctx, cs.StateBlockstore(), big.Zero(), av)
if err != nil {
return cid.Undef, xerrors.Errorf("setup reward actor: %w", err)
}

View File

@ -39,7 +39,10 @@ type ActorPredicate func(vmr.Runtime, rtt.VMActor) error
func ActorsVersionPredicate(ver actors.Version) ActorPredicate {
return func(rt vmr.Runtime, v rtt.VMActor) error {
aver := actors.VersionForNetwork(rt.NetworkVersion())
aver, err := actors.VersionForNetwork(rt.NetworkVersion())
if err != nil {
return xerrors.Errorf("unsupported network version: %w", err)
}
if aver != ver {
return xerrors.Errorf("actor %s is a version %d actor; chain only supports actor version %d at height %d and nver %d", v.Code(), ver, aver, rt.CurrEpoch(), rt.NetworkVersion())
}

View File

@ -54,7 +54,12 @@ func TryCreateAccountActor(rt *Runtime, addr address.Address) (*types.Actor, add
return nil, address.Undef, aerrors.Escalate(err, "registering actor address")
}
act, aerr := makeActor(actors.VersionForNetwork(rt.NetworkVersion()), addr)
av, err := actors.VersionForNetwork(rt.NetworkVersion())
if err != nil {
return nil, address.Undef, aerrors.Escalate(err, "unsupported network version")
}
act, aerr := makeActor(av, addr)
if aerr != nil {
return nil, address.Undef, aerr
}

View File

@ -561,7 +561,15 @@ var sectorsExtendCmd = &cli.Command{
for l, exts := range extensions {
for newExp, numbers := range exts {
scount += len(numbers)
if scount > policy.GetAddressedSectorsMax(nv) || len(p.Extensions) == policy.GetDeclarationsMax(nv) {
addressedMax, err := policy.GetAddressedSectorsMax(nv)
if err != nil {
return xerrors.Errorf("failed to get addressed sectors max")
}
declMax, err := policy.GetDeclarationsMax(nv)
if err != nil {
return xerrors.Errorf("failed to get declarations max")
}
if scount > addressedMax || len(p.Extensions) == declMax {
params = append(params, p)
p = miner3.ExtendSectorExpirationParams{}
scount = len(numbers)

View File

@ -271,7 +271,7 @@ func (bb *BlockBuilder) StateManager() *stmgr.StateManager {
}
// ActorsVersion returns the actors version for the target block.
func (bb *BlockBuilder) ActorsVersion() actors.Version {
func (bb *BlockBuilder) ActorsVersion() (actors.Version, error) {
return actors.VersionForNetwork(bb.NetworkVersion())
}

View File

@ -145,7 +145,10 @@ func (fs *FundingStage) PackMessages(ctx context.Context, bb *blockbuilder.Block
store := bb.ActorStore()
epoch := bb.Height()
actorsVersion := bb.ActorsVersion()
actorsVersion, err := bb.ActorsVersion()
if err != nil {
return err
}
var accounts, multisigs int
defer func() {

View File

@ -280,7 +280,11 @@ func (stage *ProveCommitStage) packProveCommitsMiner(
// It will drop any pre-commits that have already expired.
func (stage *ProveCommitStage) loadMiner(ctx context.Context, bb *blockbuilder.BlockBuilder, addr address.Address) error {
epoch := bb.Height()
av := bb.ActorsVersion()
av, err := bb.ActorsVersion()
if err != nil {
return err
}
minerState, err := loadMiner(bb.ActorStore(), bb.ParentStateTree(), addr)
if err != nil {
return err
@ -291,7 +295,10 @@ func (stage *ProveCommitStage) loadMiner(ctx context.Context, bb *blockbuilder.B
var total, dropped int
err = minerState.ForEachPrecommittedSector(func(info miner.SectorPreCommitOnChainInfo) error {
total++
msd := policy.GetMaxProveCommitDuration(av, info.Info.SealProof)
msd, err := policy.GetMaxProveCommitDuration(av, info.Info.SealProof)
if err != nil {
return err
}
if epoch > info.PreCommitEpoch+msd {
dropped++
return nil
@ -327,7 +334,10 @@ func (stage *ProveCommitStage) filterProveCommits(
}
nextEpoch := bb.Height()
av := bb.ActorsVersion()
av, err := bb.ActorsVersion()
if err != nil {
return nil, err
}
good := make([]abi.SectorNumber, 0, len(snos))
for _, sno := range snos {
@ -338,7 +348,10 @@ func (stage *ProveCommitStage) filterProveCommits(
if info == nil {
continue
}
msd := policy.GetMaxProveCommitDuration(av, info.Info.SealProof)
msd, err := policy.GetMaxProveCommitDuration(av, info.Info.SealProof)
if err != nil {
return nil, err
}
if nextEpoch > info.PreCommitEpoch+msd {
continue
}

View File

@ -343,7 +343,12 @@ func (b *CommitBatcher) processBatch(cfg sealiface.Config) ([]sealiface.CommitBa
return []sealiface.CommitBatchRes{res}, xerrors.Errorf("getting network version: %s", err)
}
aggFee := big.Div(big.Mul(policy.AggregateNetworkFee(nv, len(infos), bf), aggFeeNum), aggFeeDen)
aggFeeRaw, err := policy.AggregateNetworkFee(nv, len(infos), bf)
if err != nil {
log.Errorf("getting aggregate network fee: %s", err)
return []sealiface.CommitBatchRes{res}, xerrors.Errorf("getting aggregate network fee: %s", err)
}
aggFee := big.Div(big.Mul(aggFeeRaw, aggFeeNum), aggFeeDen)
needFunds := big.Add(collateral, aggFee)
needFunds, err = collateralSendAmount(b.mctx, b.api, b.maddr, cfg, needFunds)
@ -563,8 +568,18 @@ func (b *CommitBatcher) getCommitCutoff(si SectorInfo) (time.Time, error) {
log.Errorf("getting precommit info: %s", err)
return time.Now(), err
}
av, err := actors.VersionForNetwork(nv)
if err != nil {
log.Errorf("unsupported network vrsion: %s", err)
return time.Now(), err
}
mpcd, err := policy.GetMaxProveCommitDuration(av, si.SectorType)
if err != nil {
log.Errorf("getting max prove commit duration: %s", err)
return time.Now(), err
}
cutoffEpoch := pci.PreCommitEpoch + policy.GetMaxProveCommitDuration(actors.VersionForNetwork(nv), si.SectorType)
cutoffEpoch := pci.PreCommitEpoch + mpcd
for _, p := range si.Pieces {
if p.DealInfo == nil {

View File

@ -143,7 +143,14 @@ func (m *Sealing) getTicket(ctx statemachine.Context, sector SectorInfo) (abi.Se
return nil, 0, allocated, xerrors.Errorf("getTicket: StateNetworkVersion: api error, not proceeding: %+v", err)
}
msd := policy.GetMaxProveCommitDuration(actors.VersionForNetwork(nv), sector.SectorType)
av, err := actors.VersionForNetwork(nv)
if err != nil {
return nil, 0, allocated, xerrors.Errorf("getTicket: actor version for network error, not proceeding: %w", err)
}
msd, err := policy.GetMaxProveCommitDuration(av, sector.SectorType)
if err != nil {
return nil, 0, allocated, xerrors.Errorf("getTicket: max prove commit duration policy error, not proceeding: %w", err)
}
if checkProveCommitExpired(pci.PreCommitEpoch, msd, epoch) {
return nil, 0, allocated, xerrors.Errorf("ticket expired for precommitted sector")
@ -223,7 +230,16 @@ func (m *Sealing) handlePreCommit1(ctx statemachine.Context, sector SectorInfo)
return nil
}
msd := policy.GetMaxProveCommitDuration(actors.VersionForNetwork(nv), sector.SectorType)
av, err := actors.VersionForNetwork(nv)
if err != nil {
log.Errorf("handlePreCommit1: VersionForNetwork error, not proceeding: %w", err)
return nil
}
msd, err := policy.GetMaxProveCommitDuration(av, sector.SectorType)
if err != nil {
log.Errorf("handlePreCommit1: GetMaxProveCommitDuration error, not proceeding: %w", err)
return nil
}
// if height > PreCommitEpoch + msd, there is no need to recalculate
if checkProveCommitExpired(pci.PreCommitEpoch, msd, height) {
@ -309,7 +325,14 @@ func (m *Sealing) preCommitParams(ctx statemachine.Context, sector SectorInfo) (
return nil, big.Zero(), nil, ctx.Send(SectorSealPreCommit1Failed{xerrors.Errorf("failed to get network version: %w", err)})
}
msd := policy.GetMaxProveCommitDuration(actors.VersionForNetwork(nv), sector.SectorType)
av, err := actors.VersionForNetwork(nv)
if err != nil {
return nil, big.Zero(), nil, ctx.Send(SectorSealPreCommit1Failed{xerrors.Errorf("failed to get actors version: %w", err)})
}
msd, err := policy.GetMaxProveCommitDuration(av, sector.SectorType)
if err != nil {
return nil, big.Zero(), nil, ctx.Send(SectorSealPreCommit1Failed{xerrors.Errorf("failed to get max prove commit duration: %w", err)})
}
if minExpiration := sector.TicketEpoch + policy.MaxPreCommitRandomnessLookback + msd + miner.MinSectorExpiration; expiration < minExpiration {
expiration = minExpiration

View File

@ -30,8 +30,12 @@ func (a *MsigAPI) messageBuilder(ctx context.Context, from address.Address) (mul
if err != nil {
return nil, err
}
av, err := actors.VersionForNetwork(nver)
if err != nil {
return nil, err
}
return multisig.Message(actors.VersionForNetwork(nver), from), nil
return multisig.Message(av, from), nil
}
// TODO: remove gp (gasPrice) from arguments

View File

@ -1332,13 +1332,16 @@ func (m *StateModule) StateDealProviderCollateralBounds(ctx context.Context, siz
return api.DealCollateralBounds{}, xerrors.Errorf("getting reward baseline power: %w", err)
}
min, max := policy.DealProviderCollateralBounds(size,
min, max, err := policy.DealProviderCollateralBounds(size,
verified,
powClaim.RawBytePower,
powClaim.QualityAdjPower,
rewPow,
circ.FilCirculating,
m.StateManager.GetNtwkVersion(ctx, ts.Height()))
if err != nil {
return api.DealCollateralBounds{}, xerrors.Errorf("getting deal provider coll bounds: %w", err)
}
return api.DealCollateralBounds{
Min: types.BigDiv(types.BigMul(min, dealProviderCollateralNum), dealProviderCollateralDen),
Max: max,

View File

@ -88,7 +88,11 @@ func (ca *channelAccessor) messageBuilder(ctx context.Context, from address.Addr
return nil, err
}
return paych.Message(actors.VersionForNetwork(nwVersion), from), nil
av, err := actors.VersionForNetwork(nwVersion)
if err != nil {
return nil, err
}
return paych.Message(av, from), nil
}
func (ca *channelAccessor) getChannelInfo(addr address.Address) (*ChannelInfo, error) {

View File

@ -738,8 +738,12 @@ func (s *WindowPoStScheduler) batchPartitions(partitions []api.Partition, nv net
}
// Also respect the AddressedPartitionsMax (which is the same as DeclarationsMax (which is all really just MaxPartitionsPerDeadline))
if partitionsPerMsg > policy.GetDeclarationsMax(nv) {
partitionsPerMsg = policy.GetDeclarationsMax(nv)
declMax, err := policy.GetDeclarationsMax(nv)
if err != nil {
return nil, xerrors.Errorf("getting max declarations: %w", err)
}
if partitionsPerMsg > declMax {
partitionsPerMsg = declMax
}
// The number of messages will be: