2020-01-15 20:49:11 +00:00
|
|
|
package sealing
|
|
|
|
|
|
|
|
import (
|
2021-07-12 16:46:05 +00:00
|
|
|
"context"
|
2020-02-23 00:47:47 +00:00
|
|
|
"math/bits"
|
2020-04-06 18:07:26 +00:00
|
|
|
|
2022-06-16 13:50:41 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2021-07-12 16:46:05 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2021-07-12 16:46:05 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2022-06-16 13:50:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2022-06-16 09:12:33 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2022-06-14 17:41:59 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/pipeline/sealiface"
|
2020-01-15 20:49:11 +00:00
|
|
|
)
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func fillersFromRem(in abi.UnpaddedPieceSize) ([]abi.UnpaddedPieceSize, error) {
|
2020-01-15 20:49:11 +00:00
|
|
|
// Convert to in-sector bytes for easier math:
|
|
|
|
//
|
|
|
|
// Sector size to user bytes ratio is constant, e.g. for 1024B we have 1016B
|
|
|
|
// of user-usable data.
|
|
|
|
//
|
|
|
|
// (1024/1016 = 128/127)
|
|
|
|
//
|
|
|
|
// Given that we can get sector size by simply adding 1/127 of the user
|
|
|
|
// bytes
|
|
|
|
//
|
|
|
|
// (we convert to sector bytes as they are nice round binary numbers)
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
toFill := uint64(in + (in / 127))
|
2020-01-15 20:49:11 +00:00
|
|
|
|
|
|
|
// We need to fill the sector with pieces that are powers of 2. Conveniently
|
|
|
|
// computers store numbers in binary, which means we can look at 1s to get
|
|
|
|
// all the piece sizes we need to fill the sector. It also means that number
|
|
|
|
// of pieces is the number of 1s in the number of remaining bytes to fill
|
2020-02-08 02:18:32 +00:00
|
|
|
out := make([]abi.UnpaddedPieceSize, bits.OnesCount64(toFill))
|
2020-01-15 20:49:11 +00:00
|
|
|
for i := range out {
|
|
|
|
// Extract the next lowest non-zero bit
|
|
|
|
next := bits.TrailingZeros64(toFill)
|
|
|
|
psize := uint64(1) << next
|
|
|
|
// e.g: if the number is 0b010100, psize will be 0b000100
|
|
|
|
|
|
|
|
// set that bit to 0 by XORing it, so the next iteration looks at the
|
|
|
|
// next bit
|
|
|
|
toFill ^= psize
|
|
|
|
|
|
|
|
// Add the piece size to the list of pieces we need to create
|
2020-02-08 02:18:32 +00:00
|
|
|
out[i] = abi.PaddedPieceSize(psize).Unpadded()
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Sealing) ListSectors() ([]SectorInfo, error) {
|
|
|
|
var sectors []SectorInfo
|
|
|
|
if err := m.sectors.List(§ors); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sectors, nil
|
|
|
|
}
|
|
|
|
|
2020-02-09 06:06:32 +00:00
|
|
|
func (m *Sealing) GetSectorInfo(sid abi.SectorNumber) (SectorInfo, error) {
|
2020-01-15 20:49:11 +00:00
|
|
|
var out SectorInfo
|
2020-02-23 00:47:47 +00:00
|
|
|
err := m.sectors.Get(uint64(sid)).Get(&out)
|
2020-01-15 20:49:11 +00:00
|
|
|
return out, err
|
|
|
|
}
|
2021-07-12 16:46:05 +00:00
|
|
|
|
|
|
|
func collateralSendAmount(ctx context.Context, api interface {
|
2022-06-16 09:12:33 +00:00
|
|
|
StateMinerAvailableBalance(context.Context, address.Address, types.TipSetKey) (big.Int, error)
|
2021-07-12 16:46:05 +00:00
|
|
|
}, maddr address.Address, cfg sealiface.Config, collateral abi.TokenAmount) (abi.TokenAmount, error) {
|
|
|
|
if cfg.CollateralFromMinerBalance {
|
2021-07-13 15:06:41 +00:00
|
|
|
if cfg.DisableCollateralFallback {
|
|
|
|
return big.Zero(), nil
|
|
|
|
}
|
|
|
|
|
2022-06-16 09:12:33 +00:00
|
|
|
avail, err := api.StateMinerAvailableBalance(ctx, maddr, types.EmptyTSK)
|
2021-07-13 15:06:41 +00:00
|
|
|
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()
|
2021-07-12 16:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
collateral = big.Sub(collateral, avail)
|
|
|
|
if collateral.LessThan(big.Zero()) {
|
|
|
|
collateral = big.Zero()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return collateral, nil
|
|
|
|
}
|
2022-06-16 13:50:41 +00:00
|
|
|
|
2023-04-10 18:52:39 +00:00
|
|
|
func simulateMsgGas(ctx context.Context, sa interface {
|
|
|
|
GasEstimateMessageGas(context.Context, *types.Message, *api.MessageSendSpec, types.TipSetKey) (*types.Message, error)
|
|
|
|
},
|
|
|
|
from, to address.Address, method abi.MethodNum, value, maxFee abi.TokenAmount, params []byte) (*types.Message, error) {
|
|
|
|
msg := types.Message{
|
|
|
|
To: to,
|
|
|
|
From: from,
|
|
|
|
Value: value,
|
|
|
|
Method: method,
|
|
|
|
Params: params,
|
|
|
|
}
|
|
|
|
|
|
|
|
return sa.GasEstimateMessageGas(ctx, &msg, nil, types.EmptyTSK)
|
|
|
|
}
|
|
|
|
|
2022-06-16 13:50:41 +00:00
|
|
|
func sendMsg(ctx context.Context, sa interface {
|
|
|
|
MpoolPushMessage(context.Context, *types.Message, *api.MessageSendSpec) (*types.SignedMessage, error)
|
|
|
|
}, from, to address.Address, method abi.MethodNum, value, maxFee abi.TokenAmount, params []byte) (cid.Cid, error) {
|
|
|
|
msg := types.Message{
|
|
|
|
To: to,
|
|
|
|
From: from,
|
|
|
|
Value: value,
|
|
|
|
Method: method,
|
|
|
|
Params: params,
|
|
|
|
}
|
|
|
|
|
|
|
|
smsg, err := sa.MpoolPushMessage(ctx, &msg, &api.MessageSendSpec{MaxFee: maxFee})
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return smsg.Cid(), nil
|
|
|
|
}
|