From 495b4ba84144035c93753ddd103a126641be6ec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 1 Jul 2020 16:33:59 +0200 Subject: [PATCH] Fix precommit deposit math --- cbor_gen.go | 28 +++++++++++++++++++++++++++- fsm_events.go | 3 +++ sealing.go | 2 ++ states_sealing.go | 14 ++++++-------- types.go | 2 ++ upgrade_queue.go | 25 +++++++++++++++++++++++++ 6 files changed, 65 insertions(+), 9 deletions(-) diff --git a/cbor_gen.go b/cbor_gen.go index a5cf6273e..eb6143ff7 100644 --- a/cbor_gen.go +++ b/cbor_gen.go @@ -383,7 +383,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{181}); err != nil { + if _, err := w.Write([]byte{182}); err != nil { return err } @@ -624,6 +624,22 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { 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) if len("PreCommitMessage") > cbg.MaxLength { 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) case "PreCommitMessage": diff --git a/fsm_events.go b/fsm_events.go index 678d49ec6..2b14afefc 100644 --- a/fsm_events.go +++ b/fsm_events.go @@ -5,6 +5,7 @@ import ( "golang.org/x/xerrors" "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-storage/storage" ) @@ -154,11 +155,13 @@ func (evt SectorChainPreCommitFailed) apply(*SectorInfo) type SectorPreCommitted struct { Message cid.Cid + PreCommitDeposit big.Int PreCommitInfo miner.SectorPreCommitInfo } func (evt SectorPreCommitted) apply(state *SectorInfo) { state.PreCommitMessage = &evt.Message + state.PreCommitDeposit = evt.PreCommitDeposit state.PreCommitInfo = &evt.PreCommitInfo } diff --git a/sealing.go b/sealing.go index f0feea6f1..358bc9658 100644 --- a/sealing.go +++ b/sealing.go @@ -78,6 +78,8 @@ func New(api SealingAPI, events Events, maddr address.Address, ds datastore.Batc verif: verif, unsealedInfos: make(map[abi.SectorNumber]UnsealedSectorInfo), pcp: pcp, + + toUpgrade: map[abi.SectorNumber]struct{}{}, } s.sectors = statemachine.New(namespace.Wrap(ds, datastore.NewKey(SectorStorePrefix)), s, SectorInfo{}) diff --git a/states_sealing.go b/states_sealing.go index e16d49811..8ff29dec7 100644 --- a/states_sealing.go +++ b/states_sealing.go @@ -169,11 +169,7 @@ func (m *Sealing) handlePreCommitting(ctx statemachine.Context, sector SectorInf DealIDs: sector.dealIDs(), } - replace := m.maybeUpgradableSector() - if replace != nil { - params.ReplaceCapacity = true - params.ReplaceSector = *replace - } + depositMinimum := m.tryUpgradeSector(params) enc := new(bytes.Buffer) 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) } - log.Infof("submitting precommit for sector (deposit: %s): ", sector.SectorNumber, collateral) - mcid, err := m.api.SendMsg(ctx.Context(), waddr, m.maddr, builtin.MethodsMiner.PreCommitSector, collateral, big.NewInt(1), 1000000, enc.Bytes()) + deposit := big.Max(depositMinimum, collateral) + + 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 { 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 { diff --git a/types.go b/types.go index de9fbc593..41e1e0954 100644 --- a/types.go +++ b/types.go @@ -8,6 +8,7 @@ import ( sectorstorage "github.com/filecoin-project/sector-storage" "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/runtime/exitcode" "github.com/filecoin-project/specs-storage/storage" @@ -69,6 +70,7 @@ type SectorInfo struct { Proof []byte PreCommitInfo *miner.SectorPreCommitInfo + PreCommitDeposit big.Int PreCommitMessage *cid.Cid PreCommitTipSet TipSetToken diff --git a/upgrade_queue.go b/upgrade_queue.go index 347d7d7df..a0d7dcfc8 100644 --- a/upgrade_queue.go +++ b/upgrade_queue.go @@ -4,6 +4,8 @@ import ( "golang.org/x/xerrors" "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 { @@ -39,6 +41,29 @@ func (m *Sealing) MarkForUpgrade(id abi.SectorNumber) error { 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 { m.upgradeLk.Lock() defer m.upgradeLk.Unlock()