Skip to content

Commit

Permalink
fix: Fix DDO pledge math
Browse files Browse the repository at this point in the history
  • Loading branch information
simlecode committed Aug 19, 2024
1 parent 729aa6e commit 4fbe176
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (c CommitProcessor) ProcessV2(
}
sectorsMap[p.ID.Number] = sectors[i]
if mcfg.Commitment.Prove.SendFund {
sc, err := getSectorCollateral(ctx, c.api, mid, p.ID.Number, tok)
sc, err := getSectorCollateral(ctx, c.api, c.chain, &sectors[i], mid, tok, mcfg.Sealing.UseSyntheticPoRep)
if err != nil {
plog.Errorf("get sector collateral for %d failed: %s\n", p.ID.Number, err)
failed[sectors[i].ID] = struct{}{}
Expand Down
35 changes: 32 additions & 3 deletions damocles-manager/modules/impl/commitmgr/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import (
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/builtin/v9/miner"
lminer "github.com/filecoin-project/lotus/chain/actors/builtin/miner"
v1 "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
"github.com/filecoin-project/venus/venus-shared/types"

"github.com/ipfs-force-community/damocles/damocles-manager/core"
"github.com/ipfs-force-community/damocles/damocles-manager/modules/util/pledge"
)

func (p PreCommitProcessor) preCommitInfo(
Expand Down Expand Up @@ -87,24 +91,35 @@ func (p PreCommitProcessor) preCommitInfo(
func getSectorCollateral(
ctx context.Context,
stateMgr SealingAPI,
chainAPI v1.FullNode,
sector *core.SectorState,
mid abi.ActorID,
sn abi.SectorNumber,
tok core.TipSetToken,
useSyntheticPoRep bool,
) (abi.TokenAmount, error) {
maddr, err := address.NewIDAddress(uint64(mid))
if err != nil {
return big.Zero(), fmt.Errorf("invalid miner actor id: %w", err)
}

pci, err := stateMgr.StateSectorPreCommitInfo(ctx, maddr, sn, tok)
pci, err := stateMgr.StateSectorPreCommitInfo(ctx, maddr, sector.ID.Number, tok)
if err != nil {
return big.Zero(), fmt.Errorf("getting precommit info: %w", err)
}
if pci == nil {
return big.Zero(), fmt.Errorf("precommit info not found on chain")
}

collateral, err := stateMgr.StateMinerInitialPledgeCollateral(ctx, maddr, pci.Info, tok)
sealProof, err := currentSealProof(ctx, maddr, chainAPI, useSyntheticPoRep)
if err != nil {
return big.Zero(), fmt.Errorf("getting current seal proof type: %w", err)
}
weight, err := pledge.SectorWeight(ctx, sector, sealProof, chainAPI, pci.Info.Expiration)
if err != nil {
return big.Zero(), fmt.Errorf("getting sector weight: %w", err)
}

collateral, err := pledge.CalcPledgeForPower(ctx, chainAPI, weight)
if err != nil {
return big.Zero(), fmt.Errorf("getting initial pledge collateral: %w", err)
}
Expand All @@ -117,6 +132,20 @@ func getSectorCollateral(
return collateral, nil
}

func currentSealProof(ctx context.Context, maddr address.Address, chainAPI v1.FullNode, useSyntheticPoRep bool) (abi.RegisteredSealProof, error) {
mi, err := chainAPI.StateMinerInfo(ctx, maddr, types.EmptyTSK)
if err != nil {
return 0, err
}

ver, err := chainAPI.StateNetworkVersion(ctx, types.EmptyTSK)
if err != nil {
return 0, err
}

return lminer.PreferredSealProofTypeFromWindowPoStType(ver, mi.WindowPoStProofType, useSyntheticPoRep)
}

func getSectorCollateralNiPoRep(
ctx context.Context,
stateMgr SealingAPI,
Expand Down
6 changes: 3 additions & 3 deletions damocles-manager/modules/util/pledge/pledge.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func SectorWeight(

// get verified deal infos
w, vw := big.Zero(), big.Zero()

sectorDuration := big.NewInt(int64(expiration - ts.Height()))
for _, piece := range sector.SectorPiece() {
if !piece.HasDealInfo() {
// todo StateMinerInitialPledgeCollateral doesn't add cc/padding to non-verified weight, is that correct?
Expand All @@ -106,11 +106,11 @@ func SectorWeight(

alloc, err := GetAllocation(ctx, chainAPI, ts.Key(), piece)
if err != nil || alloc == nil {
w = big.Add(w, abi.NewStoragePower(int64(pieceInfo.Size)))
w = big.Add(w, big.Mul(sectorDuration, abi.NewStoragePower(int64(pieceInfo.Size))))
continue
}

vw = big.Add(vw, abi.NewStoragePower(int64(pieceInfo.Size)))
w = big.Add(w, big.Mul(sectorDuration, abi.NewStoragePower(int64(pieceInfo.Size))))
}

// load market actor
Expand Down

0 comments on commit 4fbe176

Please sign in to comment.