Skip to content
This repository was archived by the owner on Jun 6, 2023. It is now read-only.
413 changes: 413 additions & 0 deletions actors/builtin/market/cbor_gen.go

Large diffs are not rendered by default.

84 changes: 56 additions & 28 deletions actors/builtin/market/deal.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package market

import (
"bytes"

addr "github.com/filecoin-project/go-address"
abi "github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/crypto"
market0 "github.com/filecoin-project/specs-actors/actors/builtin/market"
"github.com/ipfs/go-cid"
)

//var PieceCIDPrefix = cid.Prefix{
Expand All @@ -20,33 +27,54 @@ var PieceCIDPrefix = market0.PieceCIDPrefix
// minimal deals that last for a long time.
// Note: ClientCollateralPerEpoch may not be needed and removed pending future confirmation.
// There will be a Minimum value for both client and provider deal collateral.
//type DealProposal struct {
// PieceCID cid.Cid `checked:"true"` // Checked in validateDeal, CommP
// PieceSize abi.PaddedPieceSize
// VerifiedDeal bool
// Client addr.Address
// Provider addr.Address
//
// // Label is an arbitrary client chosen label to apply to the deal
// // TODO: Limit the size of this: https://github.com/filecoin-project/specs-actors/issues/897
// Label string
//
// // Nominal start epoch. Deal payment is linear between StartEpoch and EndEpoch,
// // with total amount StoragePricePerEpoch * (EndEpoch - StartEpoch).
// // Storage deal must appear in a sealed (proven) sector no later than StartEpoch,
// // otherwise it is invalid.
// StartEpoch abi.ChainEpoch
// EndEpoch abi.ChainEpoch
// StoragePricePerEpoch abi.TokenAmount
//
// ProviderCollateral abi.TokenAmount
// ClientCollateral abi.TokenAmount
//}
type DealProposal = market0.DealProposal
type DealProposal struct {
PieceCID cid.Cid `checked:"true"` // Checked in validateDeal, CommP
PieceSize abi.PaddedPieceSize
VerifiedDeal bool
Client addr.Address
Provider addr.Address

// Label is an arbitrary client chosen label to apply to the deal
Label []byte

// Nominal start epoch. Deal payment is linear between StartEpoch and EndEpoch,
// with total amount StoragePricePerEpoch * (EndEpoch - StartEpoch).
// Storage deal must appear in a sealed (proven) sector no later than StartEpoch,
// otherwise it is invalid.
StartEpoch abi.ChainEpoch
EndEpoch abi.ChainEpoch
StoragePricePerEpoch abi.TokenAmount

ProviderCollateral abi.TokenAmount
ClientCollateral abi.TokenAmount
}

// ClientDealProposal is a DealProposal signed by a client
//type ClientDealProposal struct {
// Proposal DealProposal
// ClientSignature crypto.Signature
//}
type ClientDealProposal = market0.ClientDealProposal
type ClientDealProposal struct {
Proposal DealProposal
ClientSignature crypto.Signature
}

func (p *DealProposal) Duration() abi.ChainEpoch {
return p.EndEpoch - p.StartEpoch
}

func (p *DealProposal) TotalStorageFee() abi.TokenAmount {
return big.Mul(p.StoragePricePerEpoch, big.NewInt(int64(p.Duration())))
}

func (p *DealProposal) ClientBalanceRequirement() abi.TokenAmount {
return big.Add(p.ClientCollateral, p.TotalStorageFee())
}

func (p *DealProposal) ProviderBalanceRequirement() abi.TokenAmount {
return p.ProviderCollateral
}

func (p *DealProposal) Cid() (cid.Cid, error) {
buf := new(bytes.Buffer)
if err := p.MarshalCBOR(buf); err != nil {
return cid.Undef, err
}
return abi.CidBuilder.Sum(buf.Bytes())
}
17 changes: 8 additions & 9 deletions actors/builtin/market/market_actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,9 @@ func (a Actor) AddBalance(rt Runtime, providerOrClientAddress *addr.Address) *ab
return nil
}

//type PublishStorageDealsParams struct {
// Deals []ClientDealProposal
//}
type PublishStorageDealsParams = market0.PublishStorageDealsParams
type PublishStorageDealsParams struct {
Deals []ClientDealProposal
}

//type PublishStorageDealsReturn struct {
// IDs []abi.DealID
Expand Down Expand Up @@ -382,8 +381,8 @@ func (a Actor) ActivateDeals(rt Runtime, params *ActivateDealsParams) *abi.Empty

err = msm.dealStates.Set(dealID, &DealState{
SectorStartEpoch: currEpoch,
LastUpdatedEpoch: epochUndefined,
SlashEpoch: epochUndefined,
LastUpdatedEpoch: EpochUndefined,
SlashEpoch: EpochUndefined,
})
builtin.RequireNoErr(rt, err, exitcode.ErrIllegalState, "failed to set deal state %d", dealID)
}
Expand Down Expand Up @@ -482,7 +481,7 @@ func (a Actor) OnMinerSectorsTerminate(rt Runtime, params *OnMinerSectorsTermina
}

// if a deal is already slashed, we don't need to do anything here.
if state.SlashEpoch != epochUndefined {
if state.SlashEpoch != EpochUndefined {
rt.Log(rtt.INFO, "deal %d already slashed", dealID)
continue
}
Expand Down Expand Up @@ -551,7 +550,7 @@ func (a Actor) CronTick(rt Runtime, _ *abi.EmptyValue) *abi.EmptyValue {
}

// if this is the first cron tick for the deal, it should be in the pending state.
if state.LastUpdatedEpoch == epochUndefined {
if state.LastUpdatedEpoch == EpochUndefined {
pdErr := msm.pendingDeals.Delete(abi.CidKey(dcid))
builtin.RequireNoErr(rt, pdErr, exitcode.ErrIllegalState, "failed to delete pending proposal %v", dcid)
}
Expand All @@ -560,7 +559,7 @@ func (a Actor) CronTick(rt Runtime, _ *abi.EmptyValue) *abi.EmptyValue {
builtin.RequireState(rt, slashAmount.GreaterThanEqual(big.Zero()), "computed negative slash amount %v for deal %d", slashAmount, dealID)

if removeDeal {
builtin.RequireState(rt, nextEpoch == epochUndefined, "removed deal %d should have no scheduled epoch (got %d)", dealID, nextEpoch)
builtin.RequireState(rt, nextEpoch == EpochUndefined, "removed deal %d should have no scheduled epoch (got %d)", dealID, nextEpoch)
amountSlashed = big.Add(amountSlashed, slashAmount)

// Delete proposal and state simultaneously.
Expand Down
14 changes: 7 additions & 7 deletions actors/builtin/market/market_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/filecoin-project/specs-actors/v6/actors/util/adt"
)

const epochUndefined = abi.ChainEpoch(-1)
const EpochUndefined = abi.ChainEpoch(-1)

// BalanceLockingReason is the reason behind locking an amount.
type BalanceLockingReason int
Expand Down Expand Up @@ -108,15 +108,15 @@ func ConstructState(store adt.Store) (*State, error) {
func (m *marketStateMutation) updatePendingDealState(rt Runtime, state *DealState, deal *DealProposal, epoch abi.ChainEpoch) (amountSlashed abi.TokenAmount, nextEpoch abi.ChainEpoch, removeDeal bool) {
amountSlashed = abi.NewTokenAmount(0)

everUpdated := state.LastUpdatedEpoch != epochUndefined
everSlashed := state.SlashEpoch != epochUndefined
everUpdated := state.LastUpdatedEpoch != EpochUndefined
everSlashed := state.SlashEpoch != EpochUndefined

builtin.RequireState(rt, !everUpdated || (state.LastUpdatedEpoch <= epoch), "deal updated at future epoch %d", state.LastUpdatedEpoch)

// This would be the case that the first callback somehow triggers before it is scheduled to
// This is expected not to be able to happen
if deal.StartEpoch > epoch {
return amountSlashed, epochUndefined, false
return amountSlashed, EpochUndefined, false
}

paymentEndEpoch := deal.EndEpoch
Expand Down Expand Up @@ -164,12 +164,12 @@ func (m *marketStateMutation) updatePendingDealState(rt Runtime, state *DealStat
amountSlashed = deal.ProviderCollateral
err = m.slashBalance(deal.Provider, amountSlashed, ProviderCollateral)
builtin.RequireNoErr(rt, err, exitcode.ErrIllegalState, "slashing balance")
return amountSlashed, epochUndefined, true
return amountSlashed, EpochUndefined, true
}

if epoch >= deal.EndEpoch {
m.processDealExpired(rt, deal, state)
return amountSlashed, epochUndefined, true
return amountSlashed, EpochUndefined, true
}

// We're explicitly not inspecting the end epoch and may process a deal's expiration late, in order to prevent an outsider
Expand Down Expand Up @@ -206,7 +206,7 @@ func (m *marketStateMutation) processDealInitTimedOut(rt Runtime, deal *DealProp

// Normal expiration. Unlock collaterals for both provider and client.
func (m *marketStateMutation) processDealExpired(rt Runtime, deal *DealProposal, state *DealState) {
builtin.RequireState(rt, state.SectorStartEpoch != epochUndefined, "sector start epoch undefined")
builtin.RequireState(rt, state.SectorStartEpoch != EpochUndefined, "sector start epoch undefined")

// Note: payment has already been completed at this point (_rtProcessDealPaymentEpochsElapsed)
err := m.unlockBalance(deal.Provider, deal.ProviderCollateral, ProviderCollateral)
Expand Down
10 changes: 5 additions & 5 deletions actors/builtin/market/market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2412,7 +2412,7 @@ func TestMarketActorDeals(t *testing.T) {
rt.Verify()
}

dealProposal.Label = "foo"
dealProposal.Label = []byte("foo")

// Same deal with a different label should work
{
Expand Down Expand Up @@ -2440,7 +2440,7 @@ func TestMaxDealLabelSize(t *testing.T) {
actor.addParticipantFunds(rt, client, abi.NewTokenAmount(20000000))

dealProposal := generateDealProposal(client, provider, abi.ChainEpoch(1), abi.ChainEpoch(200*builtin.EpochsInDay))
dealProposal.Label = string(make([]byte, market.DealMaxLabelSize))
dealProposal.Label = make([]byte, market.DealMaxLabelSize)
params := &market.PublishStorageDealsParams{Deals: []market.ClientDealProposal{{Proposal: dealProposal}}}

// Label at max size should work.
Expand All @@ -2449,7 +2449,7 @@ func TestMaxDealLabelSize(t *testing.T) {
actor.publishDeals(rt, minerAddrs, publishDealReq{deal: dealProposal})
}

dealProposal.Label = string(make([]byte, market.DealMaxLabelSize+1))
dealProposal.Label = make([]byte, market.DealMaxLabelSize+1)

// Label greater than max size should fail.
{
Expand Down Expand Up @@ -3263,7 +3263,7 @@ func (h *marketActorTestHarness) generateAndPublishDealForPiece(rt *mock.Runtime
clientCollateral := big.NewInt(10)
providerCollateral := big.NewInt(10)

deal := market.DealProposal{PieceCID: pieceCID, PieceSize: pieceSize, Client: client, Provider: minerAddrs.provider, Label: "label", StartEpoch: startEpoch,
deal := market.DealProposal{PieceCID: pieceCID, PieceSize: pieceSize, Client: client, Provider: minerAddrs.provider, Label: []byte("label"), StartEpoch: startEpoch,
EndEpoch: endEpoch, StoragePricePerEpoch: storagePerEpoch, ProviderCollateral: providerCollateral, ClientCollateral: clientCollateral}

// add funds
Expand Down Expand Up @@ -3315,7 +3315,7 @@ func generateDealProposalWithCollateral(client, provider address.Address, provid
pieceSize := abi.PaddedPieceSize(2048)
storagePerEpoch := big.NewInt(10)

return market.DealProposal{PieceCID: pieceCid, PieceSize: pieceSize, Client: client, Provider: provider, Label: "label", StartEpoch: startEpoch,
return market.DealProposal{PieceCID: pieceCid, PieceSize: pieceSize, Client: client, Provider: provider, Label: []byte("label"), StartEpoch: startEpoch,
EndEpoch: endEpoch, StoragePricePerEpoch: storagePerEpoch, ProviderCollateral: providerCollateral, ClientCollateral: clientCollateral}
}

Expand Down
8 changes: 4 additions & 4 deletions actors/builtin/market/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,19 @@ func CheckStateInvariants(st *State, store adt.Store, balance abi.TokenAmount, c
"deal %d state start epoch undefined: %v", dealID, dealState)

acc.Require(
dealState.LastUpdatedEpoch == epochUndefined || dealState.LastUpdatedEpoch >= dealState.SectorStartEpoch,
dealState.LastUpdatedEpoch == EpochUndefined || dealState.LastUpdatedEpoch >= dealState.SectorStartEpoch,
"deal %d state last updated before sector start: %v", dealID, dealState)

acc.Require(
dealState.LastUpdatedEpoch == epochUndefined || dealState.LastUpdatedEpoch <= currEpoch,
dealState.LastUpdatedEpoch == EpochUndefined || dealState.LastUpdatedEpoch <= currEpoch,
"deal %d last updated epoch %d after current %d", dealID, dealState.LastUpdatedEpoch, currEpoch)

acc.Require(
dealState.SlashEpoch == epochUndefined || dealState.SlashEpoch >= dealState.SectorStartEpoch,
dealState.SlashEpoch == EpochUndefined || dealState.SlashEpoch >= dealState.SectorStartEpoch,
"deal %d state slashed before sector start: %v", dealID, dealState)

acc.Require(
dealState.SlashEpoch == epochUndefined || dealState.SlashEpoch <= currEpoch,
dealState.SlashEpoch == EpochUndefined || dealState.SlashEpoch <= currEpoch,
"deal %d state slashed after current epoch %d: %v", dealID, currEpoch, dealState)

stats, found := proposalStats[abi.DealID(dealID)]
Expand Down
6 changes: 3 additions & 3 deletions actors/builtin/market/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func (t *DealMetaArray) Get(id abi.DealID) (*DealState, bool, error) {
}
if !found {
return &DealState{
SectorStartEpoch: epochUndefined,
LastUpdatedEpoch: epochUndefined,
SlashEpoch: epochUndefined,
SectorStartEpoch: EpochUndefined,
LastUpdatedEpoch: EpochUndefined,
SlashEpoch: EpochUndefined,
}, false, nil
}
return &value, true, nil
Expand Down
Loading