2020-01-10 18:21:46 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
"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/market"
|
2020-01-10 18:21:46 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
sharedamount "github.com/filecoin-project/go-fil-markets/shared/tokenamount"
|
|
|
|
sharedtypes "github.com/filecoin-project/go-fil-markets/shared/types"
|
|
|
|
"github.com/filecoin-project/go-fil-markets/storagemarket"
|
2020-02-09 06:06:32 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-01-10 18:21:46 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func FromSharedTokenAmount(in sharedamount.TokenAmount) types.BigInt {
|
|
|
|
return types.BigInt{Int: in.Int}
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func ToSharedTokenAmount(in abi.TokenAmount) sharedamount.TokenAmount {
|
2020-01-10 18:21:46 +00:00
|
|
|
return sharedamount.TokenAmount{Int: in.Int}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToSharedSignedVoucher(in *types.SignedVoucher) (*sharedtypes.SignedVoucher, error) {
|
|
|
|
var encoded bytes.Buffer
|
|
|
|
err := in.MarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var out sharedtypes.SignedVoucher
|
|
|
|
err = out.UnmarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromSharedSignedVoucher(in *sharedtypes.SignedVoucher) (*types.SignedVoucher, error) {
|
|
|
|
var encoded bytes.Buffer
|
|
|
|
err := in.MarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var out types.SignedVoucher
|
|
|
|
err = out.UnmarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToSharedSignature(in *types.Signature) (*sharedtypes.Signature, error) {
|
|
|
|
var encoded bytes.Buffer
|
|
|
|
err := in.MarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var out sharedtypes.Signature
|
|
|
|
err = out.UnmarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromSharedSignature(in *sharedtypes.Signature) (*types.Signature, error) {
|
|
|
|
var encoded bytes.Buffer
|
|
|
|
err := in.MarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var out types.Signature
|
|
|
|
err = out.UnmarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToSharedStorageAsk(in *types.SignedStorageAsk) (*sharedtypes.SignedStorageAsk, error) {
|
|
|
|
var encoded bytes.Buffer
|
|
|
|
err := in.MarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var out sharedtypes.SignedStorageAsk
|
|
|
|
err = out.UnmarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromSignedStorageAsk(in *sharedtypes.SignedStorageAsk) (*types.SignedStorageAsk, error) {
|
|
|
|
var encoded bytes.Buffer
|
|
|
|
err := in.MarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var out types.SignedStorageAsk
|
|
|
|
err = out.UnmarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func NewStorageProviderInfo(address address.Address, miner address.Address, sectorSize abi.SectorSize, peer peer.ID) storagemarket.StorageProviderInfo {
|
2020-01-10 18:21:46 +00:00
|
|
|
return storagemarket.StorageProviderInfo{
|
|
|
|
Address: address,
|
|
|
|
Worker: miner,
|
2020-02-08 02:18:32 +00:00
|
|
|
SectorSize: uint64(sectorSize),
|
2020-01-10 18:21:46 +00:00
|
|
|
PeerID: peer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func FromOnChainDeal(proposal market.DealProposal, state market.DealState) storagemarket.StorageDeal {
|
2020-01-10 18:21:46 +00:00
|
|
|
return storagemarket.StorageDeal{
|
2020-02-08 02:18:32 +00:00
|
|
|
PieceRef: proposal.PieceCID.Bytes(),
|
|
|
|
PieceSize: uint64(proposal.PieceSize.Unpadded()),
|
|
|
|
Client: proposal.Client,
|
|
|
|
Provider: proposal.Provider,
|
|
|
|
StoragePricePerEpoch: ToSharedTokenAmount(proposal.StoragePricePerEpoch),
|
|
|
|
StorageCollateral: ToSharedTokenAmount(proposal.ProviderCollateral),
|
|
|
|
ActivationEpoch: uint64(state.SectorStartEpoch),
|
2020-01-10 18:21:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 06:06:32 +00:00
|
|
|
func ToSharedBalance(bal api.MarketBalance) storagemarket.Balance {
|
2020-01-10 18:21:46 +00:00
|
|
|
return storagemarket.Balance{
|
2020-02-09 06:06:32 +00:00
|
|
|
Locked: ToSharedTokenAmount(bal.Locked),
|
|
|
|
Available: ToSharedTokenAmount(big.Sub(bal.Escrow, bal.Locked)),
|
2020-01-10 18:21:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToSharedStorageDealProposal(proposal *actors.StorageDealProposal) (*storagemarket.StorageDealProposal, error) {
|
|
|
|
var encoded bytes.Buffer
|
|
|
|
err := proposal.MarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var out storagemarket.StorageDealProposal
|
|
|
|
err = out.UnmarshalCBOR(&encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2020-02-09 06:06:32 +00:00
|
|
|
func FromSharedStorageDealProposal(proposal *storagemarket.StorageDealProposal) (*market.ClientDealProposal, error) {
|
|
|
|
panic("todo")
|
|
|
|
return nil, nil
|
2020-01-10 18:21:46 +00:00
|
|
|
}
|