Skip to content

Commit

Permalink
sealing: collateral buffer / falback config
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Jul 13, 2021
1 parent 83f2368 commit 7526a07
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 42 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ workflows:
name: test-itest-nonce
suite: itest-nonce
target: "./itests/nonce_test.go"

- test:
name: test-itest-paych_api
suite: itest-paych_api
Expand All @@ -835,12 +835,12 @@ workflows:
name: test-itest-sector_finalize_early
suite: itest-sector_finalize_early
target: "./itests/sector_finalize_early_test.go"

- test:
name: test-itest-sector_miner_collateral
suite: itest-sector_miner_collateral
target: "./itests/sector_miner_collateral_test.go"

- test:
name: test-itest-sector_pledge
suite: itest-sector_pledge
Expand Down
43 changes: 33 additions & 10 deletions extern/storage-sealing/commit_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ import (
"sync"
"time"

"github.com/filecoin-project/go-state-types/network"

"github.com/filecoin-project/lotus/chain/actors"

"github.com/ipfs/go-cid"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/network"
miner5 "github.com/filecoin-project/specs-actors/v5/actors/builtin/miner"
proof5 "github.com/filecoin-project/specs-actors/v5/actors/runtime/proof"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/actors/policy"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
"github.com/filecoin-project/lotus/extern/storage-sealing/sealiface"
"github.com/filecoin-project/lotus/node/config"
Expand All @@ -46,6 +45,7 @@ type CommitBatcherApi interface {
StateSectorPreCommitInfo(ctx context.Context, maddr address.Address, sectorNumber abi.SectorNumber, tok TipSetToken) (*miner.SectorPreCommitOnChainInfo, error)
StateMinerInitialPledgeCollateral(context.Context, address.Address, miner.SectorPreCommitInfo, TipSetToken) (big.Int, error)
StateNetworkVersion(ctx context.Context, tok TipSetToken) (network.Version, error)
StateMinerAvailableBalance(context.Context, address.Address, TipSetToken) (big.Int, error)
}

type AggregateInput struct {
Expand Down Expand Up @@ -341,9 +341,9 @@ func (b *CommitBatcher) processBatch(cfg sealiface.Config) ([]sealiface.CommitBa
aggFee := big.Div(big.Mul(policy.AggregateNetworkFee(nv, len(infos), bf), aggFeeNum), aggFeeDen)

needFunds := big.Add(collateral, aggFee)

if cfg.CollateralFromMinerBalance {
needFunds = big.Zero()
needFunds, err = collateralSendAmount(b.mctx, b.api, b.maddr, cfg, needFunds)
if err != nil {
return []sealiface.CommitBatchRes{res}, err
}

goodFunds := big.Add(maxFee, needFunds)
Expand Down Expand Up @@ -371,6 +371,20 @@ func (b *CommitBatcher) processIndividually(cfg sealiface.Config) ([]sealiface.C
return nil, xerrors.Errorf("couldn't get miner info: %w", err)
}

avail := types.TotalFilecoinInt

if cfg.CollateralFromMinerBalance && !cfg.DisableCollateralFallback {
avail, err = b.api.StateMinerAvailableBalance(b.mctx, b.maddr, nil)
if err != nil {
return nil, xerrors.Errorf("getting available miner balance: %w", err)
}

avail = big.Sub(avail, cfg.AvailableBalanceBuffer)
if avail.LessThan(big.Zero()) {
avail = big.Zero()
}
}

tok, _, err := b.api.ChainHead(b.mctx)
if err != nil {
return nil, err
Expand All @@ -384,7 +398,7 @@ func (b *CommitBatcher) processIndividually(cfg sealiface.Config) ([]sealiface.C
FailedSectors: map[abi.SectorNumber]string{},
}

mcid, err := b.processSingle(cfg, mi, sn, info, tok)
mcid, err := b.processSingle(cfg, mi, &avail, sn, info, tok)
if err != nil {
log.Errorf("process single error: %+v", err) // todo: return to user
r.FailedSectors[sn] = err.Error()
Expand All @@ -398,7 +412,7 @@ func (b *CommitBatcher) processIndividually(cfg sealiface.Config) ([]sealiface.C
return res, nil
}

func (b *CommitBatcher) processSingle(cfg sealiface.Config, mi miner.MinerInfo, sn abi.SectorNumber, info AggregateInput, tok TipSetToken) (cid.Cid, error) {
func (b *CommitBatcher) processSingle(cfg sealiface.Config, mi miner.MinerInfo, avail *abi.TokenAmount, sn abi.SectorNumber, info AggregateInput, tok TipSetToken) (cid.Cid, error) {
enc := new(bytes.Buffer)
params := &miner.ProveCommitSectorParams{
SectorNumber: sn,
Expand All @@ -415,7 +429,16 @@ func (b *CommitBatcher) processSingle(cfg sealiface.Config, mi miner.MinerInfo,
}

if cfg.CollateralFromMinerBalance {
collateral = big.Zero()
c := big.Sub(collateral, *avail)
*avail = big.Sub(*avail, collateral)
collateral = c

if collateral.LessThan(big.Zero()) {
collateral = big.Zero()
}
if (*avail).LessThan(big.Zero()) {
*avail = big.Zero()
}
}

goodFunds := big.Add(collateral, big.Int(b.feeCfg.MaxCommitGasFee))
Expand Down
15 changes: 15 additions & 0 deletions extern/storage-sealing/mocks/mock_commit_batcher.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions extern/storage-sealing/mocks/mock_precommit_batcher.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions extern/storage-sealing/precommit_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"sync"
"time"

"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors/policy"

"github.com/ipfs/go-cid"
"golang.org/x/xerrors"

Expand All @@ -20,7 +17,9 @@ import (
miner5 "github.com/filecoin-project/specs-actors/v5/actors/builtin/miner"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/actors/policy"
"github.com/filecoin-project/lotus/extern/storage-sealing/sealiface"
"github.com/filecoin-project/lotus/node/config"
)
Expand All @@ -30,6 +29,7 @@ import (
type PreCommitBatcherApi interface {
SendMsg(ctx context.Context, from, to address.Address, method abi.MethodNum, value, maxFee abi.TokenAmount, params []byte) (cid.Cid, error)
StateMinerInfo(context.Context, address.Address, TipSetToken) (miner.MinerInfo, error)
StateMinerAvailableBalance(context.Context, address.Address, TipSetToken) (big.Int, error)
ChainHead(ctx context.Context) (TipSetToken, abi.ChainEpoch, error)
}

Expand Down Expand Up @@ -225,8 +225,10 @@ func (b *PreCommitBatcher) processBatch(cfg sealiface.Config) ([]sealiface.PreCo
params.Sectors = append(params.Sectors, *p.pci)
deposit = big.Add(deposit, p.deposit)
}
if cfg.CollateralFromMinerBalance {
deposit = big.Zero()

deposit, err := collateralSendAmount(b.mctx, b.api, b.maddr, cfg, deposit)
if err != nil {
return []sealiface.PreCommitBatchRes{res}, err
}

enc := new(bytes.Buffer)
Expand Down
2 changes: 2 additions & 0 deletions extern/storage-sealing/sealiface/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Config struct {
FinalizeEarly bool

CollateralFromMinerBalance bool
AvailableBalanceBuffer abi.TokenAmount
DisableCollateralFallback bool

BatchPreCommits bool
MaxPreCommitBatch int
Expand Down
1 change: 1 addition & 0 deletions extern/storage-sealing/sealing.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type SealingAPI interface {
StateMinerPreCommitDepositForPower(context.Context, address.Address, miner.SectorPreCommitInfo, TipSetToken) (big.Int, error)
StateMinerInitialPledgeCollateral(context.Context, address.Address, miner.SectorPreCommitInfo, TipSetToken) (big.Int, error)
StateMinerInfo(context.Context, address.Address, TipSetToken) (miner.MinerInfo, error)
StateMinerAvailableBalance(context.Context, address.Address, TipSetToken) (big.Int, error)
StateMinerSectorAllocated(context.Context, address.Address, abi.SectorNumber, TipSetToken) (bool, error)
StateMarketStorageDeal(context.Context, abi.DealID, TipSetToken) (*api.MarketDeal, error)
StateMarketStorageDealProposal(context.Context, abi.DealID, TipSetToken) (market.DealProposal, error)
Expand Down
11 changes: 6 additions & 5 deletions extern/storage-sealing/states_sealing.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ func (m *Sealing) handlePreCommitting(ctx statemachine.Context, sector SectorInf
return err
}

deposit := pcd
if cfg.CollateralFromMinerBalance {
deposit = big.Zero() // pay using available miner balance
deposit, err := collateralSendAmount(ctx.Context(), m.api, m.maddr, cfg, pcd)
if err != nil {
return err
}

enc := new(bytes.Buffer)
Expand Down Expand Up @@ -633,8 +633,9 @@ func (m *Sealing) handleSubmitCommit(ctx statemachine.Context, sector SectorInfo
collateral = big.Zero()
}

if cfg.CollateralFromMinerBalance {
collateral = big.Zero() // pay using available miner balance
collateral, err = collateralSendAmount(ctx.Context(), m.api, m.maddr, cfg, collateral)
if err != nil {
return err
}

goodFunds := big.Add(collateral, big.Int(m.feeCfg.MaxCommitGasFee))
Expand Down
36 changes: 36 additions & 0 deletions extern/storage-sealing/utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package sealing

import (
"context"
"math/bits"

"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"

"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/extern/storage-sealing/sealiface"
)

func fillersFromRem(in abi.UnpaddedPieceSize) ([]abi.UnpaddedPieceSize, error) {
Expand Down Expand Up @@ -55,3 +63,31 @@ func (m *Sealing) GetSectorInfo(sid abi.SectorNumber) (SectorInfo, error) {
err := m.sectors.Get(uint64(sid)).Get(&out)
return out, err
}

func collateralSendAmount(ctx context.Context, api interface {
StateMinerAvailableBalance(context.Context, address.Address, TipSetToken) (big.Int, error)
}, maddr address.Address, cfg sealiface.Config, collateral abi.TokenAmount) (abi.TokenAmount, error) {
if cfg.CollateralFromMinerBalance {
avail := types.TotalFilecoinInt

if !cfg.DisableCollateralFallback {
var err error
avail, err = api.StateMinerAvailableBalance(ctx, maddr, nil)
if err != nil {
return big.Zero(), xerrors.Errorf("getting available miner balance: %w", err)
}

avail = big.Sub(avail, cfg.AvailableBalanceBuffer)
if avail.LessThan(big.Zero()) {
avail = big.Zero()
}
}

collateral = big.Sub(collateral, avail)
if collateral.LessThan(big.Zero()) {
collateral = big.Zero()
}
}

return collateral, nil
}
2 changes: 1 addition & 1 deletion itests/deals_pricing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestQuotePriceForUnsealedRetrieval(t *testing.T) {
var (
ctx = context.Background()
blocktime = time.Second
blocktime = 50 * time.Millisecond
)

kit.QuietMiningLogs()
Expand Down
3 changes: 3 additions & 0 deletions itests/sector_miner_collateral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func TestMinerBalanceCollateral(t *testing.T) {
MaxCommitBatch: nSectors,

CollateralFromMinerBalance: enabled,
AvailableBalanceBuffer: big.Zero(),
DisableCollateralFallback: false,
AggregateAboveBaseFee: big.Zero(),
}, nil
}, nil
})),
Expand Down
19 changes: 13 additions & 6 deletions node/config/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ type SealingConfig struct {

// Whether to use available miner balance for sector collateral instead of sending it with each message
CollateralFromMinerBalance bool
// Minimum available balance to keep in the miner actor before sending it with messages
AvailableBalanceBuffer types.FIL
// Don't send collateral with messages even if there is no available balance in the miner actor
DisableCollateralFallback bool

// enable / disable precommit batching (takes effect after nv13)
BatchPreCommits bool
Expand Down Expand Up @@ -320,13 +324,16 @@ func DefaultStorageMiner() *StorageMiner {
Common: defCommon(),

Sealing: SealingConfig{
MaxWaitDealsSectors: 2, // 64G with 32G sectors
MaxSealingSectors: 0,
MaxSealingSectorsForDeals: 0,
WaitDealsDelay: Duration(time.Hour * 6),
AlwaysKeepUnsealedCopy: true,
FinalizeEarly: false,
MaxWaitDealsSectors: 2, // 64G with 32G sectors
MaxSealingSectors: 0,
MaxSealingSectorsForDeals: 0,
WaitDealsDelay: Duration(time.Hour * 6),
AlwaysKeepUnsealedCopy: true,
FinalizeEarly: false,

CollateralFromMinerBalance: false,
AvailableBalanceBuffer: types.FIL(big.Zero()),
DisableCollateralFallback: false,

BatchPreCommits: true,
MaxPreCommitBatch: miner5.PreCommitSectorBatchMaxSize, // up to 256 sectors
Expand Down
Loading

0 comments on commit 7526a07

Please sign in to comment.