Fix precommit deposit math

This commit is contained in:
Łukasz Magiera 2020-07-01 16:33:59 +02:00
parent 3030e3a74f
commit 495b4ba841
6 changed files with 65 additions and 9 deletions

View File

@ -383,7 +383,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error {
_, err := w.Write(cbg.CborNull) _, err := w.Write(cbg.CborNull)
return err return err
} }
if _, err := w.Write([]byte{181}); err != nil { if _, err := w.Write([]byte{182}); err != nil {
return err return err
} }
@ -624,6 +624,22 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error {
return err return err
} }
// t.PreCommitDeposit (big.Int) (struct)
if len("PreCommitDeposit") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"PreCommitDeposit\" was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("PreCommitDeposit")))); err != nil {
return err
}
if _, err := w.Write([]byte("PreCommitDeposit")); err != nil {
return err
}
if err := t.PreCommitDeposit.MarshalCBOR(w); err != nil {
return err
}
// t.PreCommitMessage (cid.Cid) (struct) // t.PreCommitMessage (cid.Cid) (struct)
if len("PreCommitMessage") > cbg.MaxLength { if len("PreCommitMessage") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"PreCommitMessage\" was too long") return xerrors.Errorf("Value in field \"PreCommitMessage\" was too long")
@ -1103,6 +1119,16 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error {
} }
} }
}
// t.PreCommitDeposit (big.Int) (struct)
case "PreCommitDeposit":
{
if err := t.PreCommitDeposit.UnmarshalCBOR(br); err != nil {
return xerrors.Errorf("unmarshaling t.PreCommitDeposit: %w", err)
}
} }
// t.PreCommitMessage (cid.Cid) (struct) // t.PreCommitMessage (cid.Cid) (struct)
case "PreCommitMessage": case "PreCommitMessage":

View File

@ -5,6 +5,7 @@ import (
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/specs-actors/actors/abi" "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/miner" "github.com/filecoin-project/specs-actors/actors/builtin/miner"
"github.com/filecoin-project/specs-storage/storage" "github.com/filecoin-project/specs-storage/storage"
) )
@ -154,11 +155,13 @@ func (evt SectorChainPreCommitFailed) apply(*SectorInfo)
type SectorPreCommitted struct { type SectorPreCommitted struct {
Message cid.Cid Message cid.Cid
PreCommitDeposit big.Int
PreCommitInfo miner.SectorPreCommitInfo PreCommitInfo miner.SectorPreCommitInfo
} }
func (evt SectorPreCommitted) apply(state *SectorInfo) { func (evt SectorPreCommitted) apply(state *SectorInfo) {
state.PreCommitMessage = &evt.Message state.PreCommitMessage = &evt.Message
state.PreCommitDeposit = evt.PreCommitDeposit
state.PreCommitInfo = &evt.PreCommitInfo state.PreCommitInfo = &evt.PreCommitInfo
} }

View File

@ -78,6 +78,8 @@ func New(api SealingAPI, events Events, maddr address.Address, ds datastore.Batc
verif: verif, verif: verif,
unsealedInfos: make(map[abi.SectorNumber]UnsealedSectorInfo), unsealedInfos: make(map[abi.SectorNumber]UnsealedSectorInfo),
pcp: pcp, pcp: pcp,
toUpgrade: map[abi.SectorNumber]struct{}{},
} }
s.sectors = statemachine.New(namespace.Wrap(ds, datastore.NewKey(SectorStorePrefix)), s, SectorInfo{}) s.sectors = statemachine.New(namespace.Wrap(ds, datastore.NewKey(SectorStorePrefix)), s, SectorInfo{})

View File

@ -169,11 +169,7 @@ func (m *Sealing) handlePreCommitting(ctx statemachine.Context, sector SectorInf
DealIDs: sector.dealIDs(), DealIDs: sector.dealIDs(),
} }
replace := m.maybeUpgradableSector() depositMinimum := m.tryUpgradeSector(params)
if replace != nil {
params.ReplaceCapacity = true
params.ReplaceSector = *replace
}
enc := new(bytes.Buffer) enc := new(bytes.Buffer)
if err := params.MarshalCBOR(enc); err != nil { if err := params.MarshalCBOR(enc); err != nil {
@ -185,13 +181,15 @@ func (m *Sealing) handlePreCommitting(ctx statemachine.Context, sector SectorInf
return xerrors.Errorf("getting initial pledge collateral: %w", err) return xerrors.Errorf("getting initial pledge collateral: %w", err)
} }
log.Infof("submitting precommit for sector (deposit: %s): ", sector.SectorNumber, collateral) deposit := big.Max(depositMinimum, collateral)
mcid, err := m.api.SendMsg(ctx.Context(), waddr, m.maddr, builtin.MethodsMiner.PreCommitSector, collateral, big.NewInt(1), 1000000, enc.Bytes())
log.Infof("submitting precommit for sector (deposit: %s): ", sector.SectorNumber, deposit)
mcid, err := m.api.SendMsg(ctx.Context(), waddr, m.maddr, builtin.MethodsMiner.PreCommitSector, deposit, big.NewInt(1), 1000000, enc.Bytes())
if err != nil { if err != nil {
return ctx.Send(SectorChainPreCommitFailed{xerrors.Errorf("pushing message to mpool: %w", err)}) return ctx.Send(SectorChainPreCommitFailed{xerrors.Errorf("pushing message to mpool: %w", err)})
} }
return ctx.Send(SectorPreCommitted{Message: mcid, PreCommitInfo: *params}) return ctx.Send(SectorPreCommitted{Message: mcid, PreCommitDeposit: deposit, PreCommitInfo: *params})
} }
func (m *Sealing) handlePreCommitWait(ctx statemachine.Context, sector SectorInfo) error { func (m *Sealing) handlePreCommitWait(ctx statemachine.Context, sector SectorInfo) error {

View File

@ -8,6 +8,7 @@ import (
sectorstorage "github.com/filecoin-project/sector-storage" sectorstorage "github.com/filecoin-project/sector-storage"
"github.com/filecoin-project/specs-actors/actors/abi" "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/miner" "github.com/filecoin-project/specs-actors/actors/builtin/miner"
"github.com/filecoin-project/specs-actors/actors/runtime/exitcode" "github.com/filecoin-project/specs-actors/actors/runtime/exitcode"
"github.com/filecoin-project/specs-storage/storage" "github.com/filecoin-project/specs-storage/storage"
@ -69,6 +70,7 @@ type SectorInfo struct {
Proof []byte Proof []byte
PreCommitInfo *miner.SectorPreCommitInfo PreCommitInfo *miner.SectorPreCommitInfo
PreCommitDeposit big.Int
PreCommitMessage *cid.Cid PreCommitMessage *cid.Cid
PreCommitTipSet TipSetToken PreCommitTipSet TipSetToken

View File

@ -4,6 +4,8 @@ import (
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/specs-actors/actors/abi" "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/miner"
) )
func (m *Sealing) MarkForUpgrade(id abi.SectorNumber) error { func (m *Sealing) MarkForUpgrade(id abi.SectorNumber) error {
@ -39,6 +41,29 @@ func (m *Sealing) MarkForUpgrade(id abi.SectorNumber) error {
return nil return nil
} }
func (m *Sealing) tryUpgradeSector(params *miner.SectorPreCommitInfo) big.Int {
replace := m.maybeUpgradableSector()
if replace != nil {
params.ReplaceCapacity = true
params.ReplaceSector = *replace
ri, err := m.GetSectorInfo(*replace)
if err != nil {
log.Errorf("error calling GetSectorInfo for replaced sector: %+v", err)
return big.Zero()
}
if params.Expiration < ri.PreCommitInfo.Expiration {
// TODO: Some limit on this
params.Expiration = ri.PreCommitInfo.Expiration
}
return ri.PreCommitDeposit
}
return big.Zero()
}
func (m *Sealing) maybeUpgradableSector() *abi.SectorNumber { func (m *Sealing) maybeUpgradableSector() *abi.SectorNumber {
m.upgradeLk.Lock() m.upgradeLk.Lock()
defer m.upgradeLk.Unlock() defer m.upgradeLk.Unlock()