2020-04-06 18:07:26 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/market"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/util/adt"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api/apibstore"
|
2020-06-04 13:54:37 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2020-04-06 18:07:26 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-04-07 00:41:33 +00:00
|
|
|
sealing "github.com/filecoin-project/storage-fsm"
|
2020-04-06 18:07:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ sealing.SealingAPI = new(SealingAPIAdapter)
|
|
|
|
|
|
|
|
type SealingAPIAdapter struct {
|
|
|
|
delegate storageMinerApi
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSealingAPIAdapter(api storageMinerApi) SealingAPIAdapter {
|
|
|
|
return SealingAPIAdapter{delegate: api}
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:23:37 +00:00
|
|
|
func (s SealingAPIAdapter) StateMinerSectorSize(ctx context.Context, maddr address.Address, tok sealing.TipSetToken) (abi.SectorSize, error) {
|
|
|
|
tsk, err := types.TipSetKeyFromBytes(tok)
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("failed to unmarshal TipSetToken to TipSetKey: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-04-16 17:36:36 +00:00
|
|
|
// TODO: update storage-fsm to just StateMinerInfo
|
|
|
|
mi, err := s.delegate.StateMinerInfo(ctx, maddr, tsk)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return mi.SectorSize, nil
|
2020-04-06 20:23:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 15:59:34 +00:00
|
|
|
func (s SealingAPIAdapter) StateMinerInitialPledgeCollateral(ctx context.Context, a address.Address, pci miner.SectorPreCommitInfo, tok sealing.TipSetToken) (big.Int, error) {
|
2020-04-23 19:39:34 +00:00
|
|
|
tsk, err := types.TipSetKeyFromBytes(tok)
|
|
|
|
if err != nil {
|
|
|
|
return big.Zero(), xerrors.Errorf("failed to unmarshal TipSetToken to TipSetKey: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-06-26 15:59:34 +00:00
|
|
|
return s.delegate.StateMinerInitialPledgeCollateral(ctx, a, pci, tsk)
|
2020-04-23 19:39:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 21:29:05 +00:00
|
|
|
func (s SealingAPIAdapter) StateMinerWorkerAddress(ctx context.Context, maddr address.Address, tok sealing.TipSetToken) (address.Address, error) {
|
|
|
|
tsk, err := types.TipSetKeyFromBytes(tok)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("failed to unmarshal TipSetToken to TipSetKey: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-04-16 17:36:36 +00:00
|
|
|
// TODO: update storage-fsm to just StateMinerInfo
|
|
|
|
mi, err := s.delegate.StateMinerInfo(ctx, maddr, tsk)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
return mi.Worker, nil
|
2020-04-10 21:29:05 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 20:22:58 +00:00
|
|
|
func (s SealingAPIAdapter) StateMinerDeadlines(ctx context.Context, maddr address.Address, tok sealing.TipSetToken) (*miner.Deadlines, error) {
|
|
|
|
tsk, err := types.TipSetKeyFromBytes(tok)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to unmarshal TipSetToken to TipSetKey: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.delegate.StateMinerDeadlines(ctx, maddr, tsk)
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:07:26 +00:00
|
|
|
func (s SealingAPIAdapter) StateWaitMsg(ctx context.Context, mcid cid.Cid) (sealing.MsgLookup, error) {
|
2020-06-03 21:42:06 +00:00
|
|
|
wmsg, err := s.delegate.StateWaitMsg(ctx, mcid, build.MessageConfidence)
|
2020-04-06 18:07:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return sealing.MsgLookup{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sealing.MsgLookup{
|
|
|
|
Receipt: sealing.MessageReceipt{
|
|
|
|
ExitCode: wmsg.Receipt.ExitCode,
|
|
|
|
Return: wmsg.Receipt.Return,
|
|
|
|
GasUsed: wmsg.Receipt.GasUsed,
|
|
|
|
},
|
|
|
|
TipSetTok: wmsg.TipSet.Key().Bytes(),
|
|
|
|
Height: wmsg.TipSet.Height(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-06-15 16:30:49 +00:00
|
|
|
func (s SealingAPIAdapter) StateComputeDataCommitment(ctx context.Context, maddr address.Address, sectorType abi.RegisteredSealProof, deals []abi.DealID, tok sealing.TipSetToken) (cid.Cid, error) {
|
2020-04-06 18:07:26 +00:00
|
|
|
tsk, err := types.TipSetKeyFromBytes(tok)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to unmarshal TipSetToken to TipSetKey: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ccparams, err := actors.SerializeParams(&market.ComputeDataCommitmentParams{
|
|
|
|
DealIDs: deals,
|
|
|
|
SectorType: sectorType,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("computing params for ComputeDataCommitment: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ccmt := &types.Message{
|
|
|
|
To: builtin.StorageMarketActorAddr,
|
|
|
|
From: maddr,
|
|
|
|
Value: types.NewInt(0),
|
|
|
|
GasPrice: types.NewInt(0),
|
|
|
|
GasLimit: 9999999999,
|
|
|
|
Method: builtin.MethodsMarket.ComputeDataCommitment,
|
|
|
|
Params: ccparams,
|
|
|
|
}
|
|
|
|
r, err := s.delegate.StateCall(ctx, ccmt, tsk)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("calling ComputeDataCommitment: %w", err)
|
|
|
|
}
|
|
|
|
if r.MsgRct.ExitCode != 0 {
|
|
|
|
return cid.Undef, xerrors.Errorf("receipt for ComputeDataCommitment had exit code %d", r.MsgRct.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var c cbg.CborCid
|
|
|
|
if err := c.UnmarshalCBOR(bytes.NewReader(r.MsgRct.Return)); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to unmarshal CBOR to CborCid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cid.Cid(c), nil
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:03:47 +00:00
|
|
|
func (s SealingAPIAdapter) StateSectorPreCommitInfo(ctx context.Context, maddr address.Address, sectorNumber abi.SectorNumber, tok sealing.TipSetToken) (*miner.SectorPreCommitOnChainInfo, error) {
|
2020-04-06 18:07:26 +00:00
|
|
|
tsk, err := types.TipSetKeyFromBytes(tok)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to unmarshal TipSetToken to TipSetKey: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
act, err := s.delegate.StateGetActor(ctx, maddr, tsk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("handleSealFailed(%d): temp error: %+v", sectorNumber, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := s.delegate.ChainReadObj(ctx, act.Head)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("handleSealFailed(%d): temp error: %+v", sectorNumber, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var state miner.State
|
|
|
|
if err := state.UnmarshalCBOR(bytes.NewReader(st)); err != nil {
|
|
|
|
return nil, xerrors.Errorf("handleSealFailed(%d): temp error: unmarshaling miner state: %+v", sectorNumber, err)
|
|
|
|
}
|
|
|
|
|
2020-04-13 21:05:34 +00:00
|
|
|
precommits, err := adt.AsMap(store.ActorStore(ctx, apibstore.NewAPIBlockstore(s.delegate)), state.PreCommittedSectors)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:07:26 +00:00
|
|
|
var pci miner.SectorPreCommitOnChainInfo
|
2020-06-04 13:54:36 +00:00
|
|
|
ok, err := precommits.Get(adt.UIntKey(uint64(sectorNumber)), &pci)
|
|
|
|
if err != nil {
|
2020-04-06 18:07:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-04 13:54:36 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-04-06 18:07:26 +00:00
|
|
|
|
|
|
|
return &pci, nil
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:06:29 +00:00
|
|
|
func (s SealingAPIAdapter) StateSectorGetInfo(ctx context.Context, maddr address.Address, sectorNumber abi.SectorNumber, tok sealing.TipSetToken) (*miner.SectorOnChainInfo, error) {
|
|
|
|
tsk, err := types.TipSetKeyFromBytes(tok)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to unmarshal TipSetToken to TipSetKey: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.delegate.StateSectorGetInfo(ctx, maddr, sectorNumber, tsk)
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:14 +00:00
|
|
|
func (s SealingAPIAdapter) StateMarketStorageDeal(ctx context.Context, dealID abi.DealID, tok sealing.TipSetToken) (market.DealProposal, error) {
|
2020-04-06 18:07:26 +00:00
|
|
|
tsk, err := types.TipSetKeyFromBytes(tok)
|
|
|
|
if err != nil {
|
2020-05-22 16:26:14 +00:00
|
|
|
return market.DealProposal{}, err
|
2020-04-06 18:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deal, err := s.delegate.StateMarketStorageDeal(ctx, dealID, tsk)
|
|
|
|
if err != nil {
|
2020-05-22 16:26:14 +00:00
|
|
|
return market.DealProposal{}, err
|
2020-04-06 18:07:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:14 +00:00
|
|
|
return deal.Proposal, nil
|
2020-04-06 18:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s SealingAPIAdapter) SendMsg(ctx context.Context, from, to address.Address, method abi.MethodNum, value, gasPrice big.Int, gasLimit int64, params []byte) (cid.Cid, error) {
|
|
|
|
msg := types.Message{
|
|
|
|
To: to,
|
|
|
|
From: from,
|
|
|
|
Value: value,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
GasLimit: gasLimit,
|
|
|
|
Method: method,
|
|
|
|
Params: params,
|
|
|
|
}
|
|
|
|
|
|
|
|
smsg, err := s.delegate.MpoolPushMessage(ctx, &msg)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return smsg.Cid(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SealingAPIAdapter) ChainHead(ctx context.Context) (sealing.TipSetToken, abi.ChainEpoch, error) {
|
|
|
|
head, err := s.delegate.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return head.Key().Bytes(), head.Height(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SealingAPIAdapter) ChainGetRandomness(ctx context.Context, tok sealing.TipSetToken, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) (abi.Randomness, error) {
|
|
|
|
tsk, err := types.TipSetKeyFromBytes(tok)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.delegate.ChainGetRandomness(ctx, tsk, personalization, randEpoch, entropy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SealingAPIAdapter) ChainReadObj(ctx context.Context, ocid cid.Cid) ([]byte, error) {
|
|
|
|
return s.delegate.ChainReadObj(ctx, ocid)
|
|
|
|
}
|