diff --git a/api/api.go b/api/api.go index 696de369c..a924379b2 100644 --- a/api/api.go +++ b/api/api.go @@ -9,7 +9,6 @@ import ( "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" - "github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/store" "github.com/filecoin-project/go-lotus/chain/types" @@ -47,7 +46,7 @@ type FullNode interface { // chain ChainNotify(context.Context) (<-chan *store.HeadChange, error) ChainHead(context.Context) (*types.TipSet, error) // TODO: check serialization - ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error // TODO: check serialization + ChainSubmitBlock(ctx context.Context, blk *types.BlockMsg) error // TODO: check serialization ChainGetRandomness(context.Context, *types.TipSet, []*types.Ticket, int) ([]byte, error) ChainWaitMsg(context.Context, cid.Cid) (*MsgWait, error) ChainGetBlock(context.Context, cid.Cid) (*types.BlockHeader, error) @@ -69,7 +68,7 @@ type FullNode interface { MinerRegister(context.Context, address.Address) error MinerUnregister(context.Context, address.Address) error MinerAddresses(context.Context) ([]address.Address, error) - MinerCreateBlock(context.Context, address.Address, *types.TipSet, []*types.Ticket, types.ElectionProof, []*types.SignedMessage, uint64) (*chain.BlockMsg, error) + MinerCreateBlock(context.Context, address.Address, *types.TipSet, []*types.Ticket, types.ElectionProof, []*types.SignedMessage, uint64) (*types.BlockMsg, error) // // UX ? @@ -108,12 +107,11 @@ type FullNode interface { StateReadState(ctx context.Context, act *types.Actor, ts *types.TipSet) (*ActorState, error) StateMinerSectors(context.Context, address.Address) ([]*SectorInfo, error) - StateMinerProvingSet(context.Context, address.Address) ([]*SectorInfo, error) + StateMinerProvingSet(context.Context, address.Address, *types.TipSet) ([]*SectorInfo, error) StateMinerPower(context.Context, address.Address, *types.TipSet) (MinerPower, error) StateMinerWorker(context.Context, address.Address, *types.TipSet) (address.Address, error) StateMinerPeerID(ctx context.Context, m address.Address, ts *types.TipSet) (peer.ID, error) StateMinerProvingPeriodEnd(ctx context.Context, actor address.Address, ts *types.TipSet) (uint64, error) - StateMinerProvingSet(ctx context.Context, actor address.Address, ts *types.TipSet) ([]SectorSetEntry, error) PaychGet(ctx context.Context, from, to address.Address, ensureFunds types.BigInt) (*ChannelInfo, error) PaychList(context.Context) ([]address.Address, error) @@ -273,9 +271,3 @@ type RetrievalOrder struct { Miner address.Address MinerPeerID peer.ID } - -type SectorSetEntry struct { - SectorID uint64 - CommR []byte - CommD []byte -} diff --git a/api/struct.go b/api/struct.go index 91f20759d..0a1c6f4d5 100644 --- a/api/struct.go +++ b/api/struct.go @@ -5,7 +5,6 @@ import ( "github.com/libp2p/go-libp2p-core/network" - "github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/store" "github.com/filecoin-project/go-lotus/chain/types" @@ -40,7 +39,7 @@ type FullNodeStruct struct { Internal struct { ChainNotify func(context.Context) (<-chan *store.HeadChange, error) `perm:"read"` - ChainSubmitBlock func(ctx context.Context, blk *chain.BlockMsg) error `perm:"write"` + ChainSubmitBlock func(ctx context.Context, blk *types.BlockMsg) error `perm:"write"` ChainHead func(context.Context) (*types.TipSet, error) `perm:"read"` ChainGetRandomness func(context.Context, *types.TipSet, []*types.Ticket, int) ([]byte, error) `perm:"read"` ChainWaitMsg func(context.Context, cid.Cid) (*MsgWait, error) `perm:"read"` @@ -56,7 +55,7 @@ type FullNodeStruct struct { MinerRegister func(context.Context, address.Address) error `perm:"admin"` MinerUnregister func(context.Context, address.Address) error `perm:"admin"` MinerAddresses func(context.Context) ([]address.Address, error) `perm:"write"` - MinerCreateBlock func(context.Context, address.Address, *types.TipSet, []*types.Ticket, types.ElectionProof, []*types.SignedMessage, uint64) (*chain.BlockMsg, error) `perm:"write"` + MinerCreateBlock func(context.Context, address.Address, *types.TipSet, []*types.Ticket, types.ElectionProof, []*types.SignedMessage, uint64) (*types.BlockMsg, error) `perm:"write"` WalletNew func(context.Context, string) (address.Address, error) `perm:"write"` WalletHas func(context.Context, address.Address) (bool, error) `perm:"write"` @@ -76,16 +75,15 @@ type FullNodeStruct struct { ClientRetrieve func(ctx context.Context, order RetrievalOrder, path string) error `perm:"admin"` ClientQueryAsk func(ctx context.Context, p peer.ID, miner address.Address) (*types.SignedStorageAsk, error) `perm:"read"` - StateMinerSectors func(context.Context, address.Address) ([]*SectorInfo, error) `perm:"read"` - StateMinerProvingSet func(context.Context, address.Address) ([]*SectorInfo, error) `perm:"read"` - StateMinerPower func(context.Context, address.Address, *types.TipSet) (MinerPower, error) `perm:"read"` - StateMinerWorker func(context.Context, address.Address, *types.TipSet) (address.Address, error) `perm:"read"` - StateMinerPeerID func(ctx context.Context, m address.Address, ts *types.TipSet) (peer.ID, error) `perm:"read"` - StateMinerProvingPeriodEnd func(ctx context.Context, actor address.Address, ts *types.TipSet) (uint64, error) `perm:"read"` - StateMinerProvingSet func(ctx context.Context, actor address.Address, ts *types.TipSet) ([]SectorSetEntry, error) `perm:"read"` - StateCall func(context.Context, *types.Message, *types.TipSet) (*types.MessageReceipt, error) `perm:"read"` - StateGetActor func(context.Context, address.Address, *types.TipSet) (*types.Actor, error) `perm:"read"` - StateReadState func(context.Context, *types.Actor, *types.TipSet) (*ActorState, error) `perm:"read"` + StateMinerSectors func(context.Context, address.Address) ([]*SectorInfo, error) `perm:"read"` + StateMinerProvingSet func(context.Context, address.Address, *types.TipSet) ([]*SectorInfo, error) `perm:"read"` + StateMinerPower func(context.Context, address.Address, *types.TipSet) (MinerPower, error) `perm:"read"` + StateMinerWorker func(context.Context, address.Address, *types.TipSet) (address.Address, error) `perm:"read"` + StateMinerPeerID func(ctx context.Context, m address.Address, ts *types.TipSet) (peer.ID, error) `perm:"read"` + StateMinerProvingPeriodEnd func(ctx context.Context, actor address.Address, ts *types.TipSet) (uint64, error) `perm:"read"` + StateCall func(context.Context, *types.Message, *types.TipSet) (*types.MessageReceipt, error) `perm:"read"` + StateGetActor func(context.Context, address.Address, *types.TipSet) (*types.Actor, error) `perm:"read"` + StateReadState func(context.Context, *types.Actor, *types.TipSet) (*ActorState, error) `perm:"read"` PaychGet func(ctx context.Context, from, to address.Address, ensureFunds types.BigInt) (*ChannelInfo, error) `perm:"sign"` PaychList func(context.Context) ([]address.Address, error) `perm:"read"` @@ -213,11 +211,11 @@ func (c *FullNodeStruct) MinerAddresses(ctx context.Context) ([]address.Address, return c.Internal.MinerAddresses(ctx) } -func (c *FullNodeStruct) MinerCreateBlock(ctx context.Context, addr address.Address, base *types.TipSet, tickets []*types.Ticket, eproof types.ElectionProof, msgs []*types.SignedMessage, ts uint64) (*chain.BlockMsg, error) { +func (c *FullNodeStruct) MinerCreateBlock(ctx context.Context, addr address.Address, base *types.TipSet, tickets []*types.Ticket, eproof types.ElectionProof, msgs []*types.SignedMessage, ts uint64) (*types.BlockMsg, error) { return c.Internal.MinerCreateBlock(ctx, addr, base, tickets, eproof, msgs, ts) } -func (c *FullNodeStruct) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error { +func (c *FullNodeStruct) ChainSubmitBlock(ctx context.Context, blk *types.BlockMsg) error { return c.Internal.ChainSubmitBlock(ctx, blk) } @@ -289,8 +287,8 @@ func (c *FullNodeStruct) StateMinerSectors(ctx context.Context, addr address.Add return c.Internal.StateMinerSectors(ctx, addr) } -func (c *FullNodeStruct) StateMinerProvingSet(ctx context.Context, addr address.Address) ([]*SectorInfo, error) { - return c.Internal.StateMinerProvingSet(ctx, addr) +func (c *FullNodeStruct) StateMinerProvingSet(ctx context.Context, addr address.Address, ts *types.TipSet) ([]*SectorInfo, error) { + return c.Internal.StateMinerProvingSet(ctx, addr, ts) } func (c *FullNodeStruct) StateMinerPower(ctx context.Context, a address.Address, ts *types.TipSet) (MinerPower, error) { @@ -308,10 +306,6 @@ func (c *FullNodeStruct) StateMinerProvingPeriodEnd(ctx context.Context, actor a return c.Internal.StateMinerProvingPeriodEnd(ctx, actor, ts) } -func (c *FullNodeStruct) StateMinerProvingSet(ctx context.Context, actor address.Address, ts *types.TipSet) ([]SectorSetEntry, error) { - return c.Internal.StateMinerProvingSet(ctx, actor, ts) -} - func (c *FullNodeStruct) StateCall(ctx context.Context, msg *types.Message, ts *types.TipSet) (*types.MessageReceipt, error) { return c.Internal.StateCall(ctx, msg, ts) } diff --git a/chain/cbor_gen.go b/chain/cbor_gen.go index 0a263e20a..e7f2a40b8 100644 --- a/chain/cbor_gen.go +++ b/chain/cbor_gen.go @@ -472,109 +472,3 @@ func (t *BSTipSet) UnmarshalCBOR(br io.Reader) error { return nil } - -func (t *BlockMsg) MarshalCBOR(w io.Writer) error { - if _, err := w.Write([]byte{131}); err != nil { - return err - } - - // t.t.Header (types.BlockHeader) - if err := t.Header.MarshalCBOR(w); err != nil { - return err - } - - // t.t.BlsMessages ([]cid.Cid) - if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.BlsMessages)))); err != nil { - return err - } - for _, v := range t.BlsMessages { - if err := cbg.WriteCid(w, v); err != nil { - return xerrors.Errorf("failed writing cid field t.BlsMessages: %w", err) - } - } - - // t.t.SecpkMessages ([]cid.Cid) - if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.SecpkMessages)))); err != nil { - return err - } - for _, v := range t.SecpkMessages { - if err := cbg.WriteCid(w, v); err != nil { - return xerrors.Errorf("failed writing cid field t.SecpkMessages: %w", err) - } - } - return nil -} - -func (t *BlockMsg) UnmarshalCBOR(br io.Reader) error { - - maj, extra, err := cbg.CborReadHeader(br) - if err != nil { - return err - } - if maj != cbg.MajArray { - return fmt.Errorf("cbor input should be of type array") - } - - if extra != 3 { - return fmt.Errorf("cbor input had wrong number of fields") - } - - // t.t.Header (types.BlockHeader) - - t.Header = new(types.BlockHeader) - - if err := t.Header.UnmarshalCBOR(br); err != nil { - return err - } - // t.t.BlsMessages ([]cid.Cid) - - maj, extra, err = cbg.CborReadHeader(br) - if err != nil { - return err - } - if extra > 8192 { - return fmt.Errorf("array too large") - } - - if maj != cbg.MajArray { - return fmt.Errorf("expected cbor array") - } - if extra > 0 { - t.BlsMessages = make([]cid.Cid, extra) - } - for i := 0; i < int(extra); i++ { - - c, err := cbg.ReadCid(br) - if err != nil { - return xerrors.Errorf("reading cid field t.BlsMessages failed: %w", err) - } - t.BlsMessages[i] = c - } - - // t.t.SecpkMessages ([]cid.Cid) - - maj, extra, err = cbg.CborReadHeader(br) - if err != nil { - return err - } - if extra > 8192 { - return fmt.Errorf("array too large") - } - - if maj != cbg.MajArray { - return fmt.Errorf("expected cbor array") - } - if extra > 0 { - t.SecpkMessages = make([]cid.Cid, extra) - } - for i := 0; i < int(extra); i++ { - - c, err := cbg.ReadCid(br) - if err != nil { - return xerrors.Errorf("reading cid field t.SecpkMessages failed: %w", err) - } - t.SecpkMessages[i] = c - } - - return nil -} diff --git a/chain/messagepool.go b/chain/messagepool.go index bbffd45a5..4ebde3512 100644 --- a/chain/messagepool.go +++ b/chain/messagepool.go @@ -2,9 +2,10 @@ package chain import ( "encoding/base64" - pubsub "github.com/libp2p/go-libp2p-pubsub" "sync" + pubsub "github.com/libp2p/go-libp2p-pubsub" + "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/stmgr" "github.com/filecoin-project/go-lotus/chain/types" @@ -109,7 +110,7 @@ func (mp *MessagePool) getNonceLocked(addr address.Address) (uint64, error) { return mset.startNonce + uint64(len(mset.msgs)), nil } - act, err := mp.sm.GetActor(addr) + act, err := mp.sm.GetActor(addr, nil) if err != nil { return 0, err } diff --git a/chain/stmgr/utils.go b/chain/stmgr/utils.go index a7a054e6d..bde2446e5 100644 --- a/chain/stmgr/utils.go +++ b/chain/stmgr/utils.go @@ -3,6 +3,7 @@ package stmgr import ( "context" + "github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/chain/actors" "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/types" @@ -137,7 +138,7 @@ func GetMinerProvingPeriodEnd(ctx context.Context, sm *StateManager, ts *types.T return mas.ProvingPeriodEnd, nil } -func GetMinerProvingSet(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) ([]api.SectorSetEntry, error) { +func GetMinerProvingSet(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) ([]*api.SectorInfo, error) { var mas actors.StorageMinerActorState _, err := sm.LoadActorState(ctx, maddr, &mas, ts) if err != nil { @@ -150,13 +151,13 @@ func GetMinerProvingSet(ctx context.Context, sm *StateManager, ts *types.TipSet, return nil, err } - var sset []api.SectorSetEntry + var sset []*api.SectorInfo if err := a.ForEach(func(i uint64, v *cbg.Deferred) error { var comms [][]byte if err := cbor.DecodeInto(v.Raw, &comms); err != nil { return err } - sset = append(sset, api.SectorSetEntry{ + sset = append(sset, &api.SectorInfo{ SectorID: i, CommR: comms[0], CommD: comms[1], diff --git a/chain/sub/incoming.go b/chain/sub/incoming.go index d41eac887..5bcadf16f 100644 --- a/chain/sub/incoming.go +++ b/chain/sub/incoming.go @@ -2,6 +2,7 @@ package sub import ( "context" + logging "github.com/ipfs/go-log" pubsub "github.com/libp2p/go-libp2p-pubsub" @@ -23,7 +24,7 @@ func HandleIncomingBlocks(ctx context.Context, bsub *pubsub.Subscription, s *cha continue } - blk, err := chain.DecodeBlockMsg(msg.GetData()) + blk, err := types.DecodeBlockMsg(msg.GetData()) if err != nil { log.Error("got invalid block over pubsub: ", err) continue diff --git a/chain/blockmsg.go b/chain/types/blockmsg.go similarity index 83% rename from chain/blockmsg.go rename to chain/types/blockmsg.go index 22afced32..f3114499d 100644 --- a/chain/blockmsg.go +++ b/chain/types/blockmsg.go @@ -1,15 +1,13 @@ -package chain +package types import ( "bytes" "github.com/ipfs/go-cid" - - "github.com/filecoin-project/go-lotus/chain/types" ) type BlockMsg struct { - Header *types.BlockHeader + Header *BlockHeader BlsMessages []cid.Cid SecpkMessages []cid.Cid } diff --git a/chain/types/cbor_gen.go b/chain/types/cbor_gen.go index 81a23081c..a2df6b3ff 100644 --- a/chain/types/cbor_gen.go +++ b/chain/types/cbor_gen.go @@ -1197,3 +1197,109 @@ func (t *MessageReceipt) UnmarshalCBOR(r io.Reader) error { } return nil } + +func (t *BlockMsg) MarshalCBOR(w io.Writer) error { + if _, err := w.Write([]byte{131}); err != nil { + return err + } + + // t.t.Header (types.BlockHeader) + if err := t.Header.MarshalCBOR(w); err != nil { + return err + } + + // t.t.BlsMessages ([]cid.Cid) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.BlsMessages)))); err != nil { + return err + } + for _, v := range t.BlsMessages { + if err := cbg.WriteCid(w, v); err != nil { + return xerrors.Errorf("failed writing cid field t.BlsMessages: %w", err) + } + } + + // t.t.SecpkMessages ([]cid.Cid) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.SecpkMessages)))); err != nil { + return err + } + for _, v := range t.SecpkMessages { + if err := cbg.WriteCid(w, v); err != nil { + return xerrors.Errorf("failed writing cid field t.SecpkMessages: %w", err) + } + } + return nil +} + +func (t *BlockMsg) UnmarshalCBOR(br io.Reader) error { + + maj, extra, err := cbg.CborReadHeader(br) + if err != nil { + return err + } + if maj != cbg.MajArray { + return fmt.Errorf("cbor input should be of type array") + } + + if extra != 3 { + return fmt.Errorf("cbor input had wrong number of fields") + } + + // t.t.Header (types.BlockHeader) + + t.Header = new(BlockHeader) + + if err := t.Header.UnmarshalCBOR(br); err != nil { + return err + } + // t.t.BlsMessages ([]cid.Cid) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + if extra > 0 { + t.BlsMessages = make([]cid.Cid, extra) + } + for i := 0; i < int(extra); i++ { + + c, err := cbg.ReadCid(br) + if err != nil { + return xerrors.Errorf("reading cid field t.BlsMessages failed: %w", err) + } + t.BlsMessages[i] = c + } + + // t.t.SecpkMessages ([]cid.Cid) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + if extra > 0 { + t.SecpkMessages = make([]cid.Cid, extra) + } + for i := 0; i < int(extra); i++ { + + c, err := cbg.ReadCid(br) + if err != nil { + return xerrors.Errorf("reading cid field t.SecpkMessages failed: %w", err) + } + t.SecpkMessages[i] = c + } + + return nil +} diff --git a/chain/types/tipset.go b/chain/types/tipset.go index 2d763650a..5484b476a 100644 --- a/chain/types/tipset.go +++ b/chain/types/tipset.go @@ -128,3 +128,12 @@ func (ts *TipSet) MinTicketBlock() *BlockHeader { return min } + +func (ts *TipSet) Contains(oc cid.Cid) bool { + for _, c := range ts.cids { + if c == oc { + return true + } + } + return false +} diff --git a/cli/state.go b/cli/state.go index ce799093f..970a48d05 100644 --- a/cli/state.go +++ b/cli/state.go @@ -104,7 +104,7 @@ var stateProvingSetCmd = &cli.Command{ return err } - sectors, err := api.StateMinerProvingSet(ctx, maddr) + sectors, err := api.StateMinerProvingSet(ctx, maddr, nil) if err != nil { return err } diff --git a/gen/main.go b/gen/main.go index 56323cde3..1ef741e5c 100644 --- a/gen/main.go +++ b/gen/main.go @@ -21,6 +21,7 @@ func main() { types.Merge{}, types.Actor{}, types.MessageReceipt{}, + types.BlockMsg{}, ) if err != nil { fmt.Println(err) @@ -32,7 +33,6 @@ func main() { chain.BlockSyncRequest{}, chain.BlockSyncResponse{}, chain.BSTipSet{}, - chain.BlockMsg{}, ) if err != nil { fmt.Println(err) diff --git a/miner/miner.go b/miner/miner.go index cf2b0e4ef..b9e1c04b1 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -6,7 +6,6 @@ import ( "time" "github.com/filecoin-project/go-lotus/build" - chain "github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain/actors" "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/gen" @@ -191,7 +190,7 @@ func (m *Miner) GetBestMiningCandidate() (*MiningBase, error) { }, nil } -func (m *Miner) mineOne(ctx context.Context, base *MiningBase) (*chain.BlockMsg, error) { +func (m *Miner) mineOne(ctx context.Context, base *MiningBase) (*types.BlockMsg, error) { log.Debug("attempting to mine a block on:", base.ts.Cids()) ticket, err := m.scratchTicket(ctx, base) if err != nil { @@ -288,7 +287,7 @@ func (m *Miner) scratchTicket(ctx context.Context, base *MiningBase) (*types.Tic }, nil } -func (m *Miner) createBlock(base *MiningBase, ticket *types.Ticket, proof types.ElectionProof) (*chain.BlockMsg, error) { +func (m *Miner) createBlock(base *MiningBase, ticket *types.Ticket, proof types.ElectionProof) (*types.BlockMsg, error) { pending, err := m.api.MpoolPending(context.TODO(), base.ts) if err != nil { diff --git a/node/impl/full/chain.go b/node/impl/full/chain.go index cbfc11ff4..2e429dde0 100644 --- a/node/impl/full/chain.go +++ b/node/impl/full/chain.go @@ -4,7 +4,6 @@ import ( "context" "github.com/filecoin-project/go-lotus/api" - "github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain/store" "github.com/filecoin-project/go-lotus/chain/types" "golang.org/x/xerrors" @@ -27,7 +26,7 @@ func (a *ChainAPI) ChainNotify(ctx context.Context) (<-chan *store.HeadChange, e return a.Chain.SubHeadChanges(ctx), nil } -func (a *ChainAPI) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error { +func (a *ChainAPI) ChainSubmitBlock(ctx context.Context, blk *types.BlockMsg) error { if err := a.Chain.AddBlock(blk.Header); err != nil { return xerrors.Errorf("AddBlock failed: %w", err) } diff --git a/node/impl/full/state.go b/node/impl/full/state.go index 11f6cf6dd..f7d49384c 100644 --- a/node/impl/full/state.go +++ b/node/impl/full/state.go @@ -12,7 +12,6 @@ import ( "golang.org/x/xerrors" "github.com/filecoin-project/go-lotus/api" - "github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain/actors" "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/gen" @@ -94,62 +93,8 @@ func (a *StateAPI) StateMinerSectors(ctx context.Context, addr address.Address) return sinfos, nil } -func (a *StateAPI) StateMinerProvingSet(ctx context.Context, addr address.Address) ([]*api.SectorInfo, error) { - ts := a.Chain.GetHeaviestTipSet() - - stc, err := a.StateManager.TipSetState(ts.Cids()) - if err != nil { - return nil, err - } - - cst := hamt.CSTFromBstore(a.Chain.Blockstore()) - - st, err := state.LoadStateTree(cst, stc) - if err != nil { - return nil, err - } - - act, err := st.GetActor(addr) - if err != nil { - return nil, err - } - - var minerState actors.StorageMinerActorState - if err := cst.Get(ctx, act.Head, &minerState); err != nil { - return nil, err - } - - nd, err := hamt.LoadNode(ctx, cst, minerState.ProvingSet) - if err != nil { - return nil, err - } - - var sinfos []*api.SectorInfo - // Note to self: the hamt isnt a great data structure to use here... need to implement the sector set - err = nd.ForEach(ctx, func(k string, val interface{}) error { - sid, err := strconv.ParseUint(k, 10, 64) - if err != nil { - return err - } - - bval, ok := val.([]byte) - if !ok { - return fmt.Errorf("expected to get bytes in sector set hamt") - } - - var comms [][]byte - if err := cbor.DecodeInto(bval, &comms); err != nil { - return err - } - - sinfos = append(sinfos, &api.SectorInfo{ - SectorID: sid, - CommR: comms[0], - CommD: comms[1], - }) - return nil - }) - return sinfos, nil +func (a *StateAPI) StateMinerProvingSet(ctx context.Context, addr address.Address, ts *types.TipSet) ([]*api.SectorInfo, error) { + return stmgr.GetMinerProvingSet(ctx, a.StateManager, ts, addr) } func (a *StateAPI) StateMinerPower(ctx context.Context, maddr address.Address, ts *types.TipSet) (api.MinerPower, error) { @@ -194,9 +139,6 @@ func (a *StateAPI) StateMinerProvingPeriodEnd(ctx context.Context, actor address return stmgr.GetMinerProvingPeriodEnd(ctx, a.StateManager, ts, actor) } -func (a *StateAPI) StateMinerProvingSet(ctx context.Context, actor address.Address, ts *types.TipSet) ([]api.SectorSetEntry, error) { - return stmgr.GetMinerProvingSet(ctx, a.StateManager, ts, actor) -} func (a *StateAPI) StateCall(ctx context.Context, msg *types.Message, ts *types.TipSet) (*types.MessageReceipt, error) { return a.StateManager.Call(ctx, msg, ts) } @@ -248,13 +190,13 @@ func (a *StateAPI) StateReadState(ctx context.Context, act *types.Actor, ts *typ } // This is on StateAPI because miner.Miner requires this, and MinerAPI requires miner.Miner -func (a *StateAPI) MinerCreateBlock(ctx context.Context, addr address.Address, parents *types.TipSet, tickets []*types.Ticket, proof types.ElectionProof, msgs []*types.SignedMessage, ts uint64) (*chain.BlockMsg, error) { +func (a *StateAPI) MinerCreateBlock(ctx context.Context, addr address.Address, parents *types.TipSet, tickets []*types.Ticket, proof types.ElectionProof, msgs []*types.SignedMessage, ts uint64) (*types.BlockMsg, error) { fblk, err := gen.MinerCreateBlock(ctx, a.StateManager, a.Wallet, addr, parents, tickets, proof, msgs, ts) if err != nil { return nil, err } - var out chain.BlockMsg + var out types.BlockMsg out.Header = fblk.Header for _, msg := range fblk.BlsMessages { out.BlsMessages = append(out.BlsMessages, msg.Cid()) diff --git a/node/impl/full/wallet.go b/node/impl/full/wallet.go index 38803460f..6f02f7ff5 100644 --- a/node/impl/full/wallet.go +++ b/node/impl/full/wallet.go @@ -32,7 +32,7 @@ func (a *WalletAPI) WalletList(ctx context.Context) ([]address.Address, error) { } func (a *WalletAPI) WalletBalance(ctx context.Context, addr address.Address) (types.BigInt, error) { - return a.StateManager.GetBalance(addr) + return a.StateManager.GetBalance(addr, nil) } func (a *WalletAPI) WalletSign(ctx context.Context, k address.Address, msg []byte) (*types.Signature, error) { diff --git a/paych/paych.go b/paych/paych.go index 07dd8cbf8..5b0889be9 100644 --- a/paych/paych.go +++ b/paych/paych.go @@ -223,7 +223,7 @@ func (pm *Manager) CheckVoucherSpendable(ctx context.Context, ch address.Address func (pm *Manager) loadPaychState(ctx context.Context, ch address.Address) (*types.Actor, *actors.PaymentChannelActorState, error) { var pcast actors.PaymentChannelActorState - act, err := pm.sm.LoadActorState(ctx, ch, &pcast) + act, err := pm.sm.LoadActorState(ctx, ch, &pcast, nil) if err != nil { return nil, nil, err } diff --git a/storage/miner.go b/storage/miner.go index a02a7c3be..e17a1806f 100644 --- a/storage/miner.go +++ b/storage/miner.go @@ -2,13 +2,13 @@ package storage import ( "context" - "fmt" "github.com/ipfs/go-cid" "github.com/ipfs/go-datastore" logging "github.com/ipfs/go-log" host "github.com/libp2p/go-libp2p-core/host" "github.com/pkg/errors" + "golang.org/x/xerrors" "github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/build" @@ -44,16 +44,16 @@ type storageMinerApi interface { // Call a read only method on actors (no interaction with the chain required) StateCall(ctx context.Context, msg *types.Message, ts *types.TipSet) (*types.MessageReceipt, error) - StateMinerWorker(ctx context.Context, address.Address, ts *types.TipSet) (address.Address, error) - StateMinerProvingPeriodEnd(ctx context.Context, actor address.Address, ts *types.TipSet) (uint64, error) - StateMinerProvingSet(ctx context.Context, actor address.Address, ts *types.TipSet) ([]api.SectorSetEntry, error) + StateMinerWorker(context.Context, address.Address, *types.TipSet) (address.Address, error) + StateMinerProvingPeriodEnd(context.Context, address.Address, *types.TipSet) (uint64, error) + StateMinerProvingSet(context.Context, address.Address, *types.TipSet) ([]*api.SectorInfo, error) MpoolPush(context.Context, *types.SignedMessage) error MpoolGetNonce(context.Context, address.Address) (uint64, error) ChainWaitMsg(context.Context, cid.Cid) (*api.MsgWait, error) ChainNotify(context.Context) (<-chan *store.HeadChange, error) - ChainGetRandomness(context.Context, ts *types.TipSet, []*types.Ticket, int) ([]byte, error) + ChainGetRandomness(context.Context, *types.TipSet, []*types.Ticket, int) ([]byte, error) ChainGetTipSetByHeight(context.Context, uint64, *types.TipSet) (*types.TipSet, error) WalletBalance(context.Context, address.Address) (types.BigInt, error) @@ -175,6 +175,7 @@ func (m *Miner) commitSector(ctx context.Context, sinfo sectorbuilder.SectorSeal } func (m *Miner) runPoSt(ctx context.Context) { + // TODO: most of this method can probably be replaced by the events module once it works on top of the api notifs, err := m.api.ChainNotify(ctx) if err != nil { // TODO: this is probably 'crash the node' level serious @@ -190,7 +191,11 @@ func (m *Miner) runPoSt(ctx context.Context) { } postCtx, cancel := context.WithCancel(ctx) - postWaitCh, onBlock := m.maybeDoPost(postCtx, curhead) + postWaitCh, onBlock, err := m.maybeDoPost(postCtx, curhead.Val) + if err != nil { + log.Errorf("initial 'maybeDoPost' call failed: %s", err) + return + } for { select { @@ -202,15 +207,39 @@ func (m *Miner) runPoSt(ctx context.Context) { return } - if ch.Type == store.HCApply { - m.maybeDoPost(ch.Val) + switch ch.Type { + case store.HCApply: + postWaitCh, onBlock, err = m.maybeDoPost(postCtx, ch.Val) + if err != nil { + log.Errorf("maybeDoPost failed: %s", err) + return + } + case store.HCRevert: + if onBlock != nil { + if ch.Val.Contains(onBlock.Cid()) { + // Our post may now be invalid! + cancel() // probably the right thing to do? + } + } + case store.HCCurrent: + log.Warn("got 'current' chain notification in middle of stream") } + case perr := <-postWaitCh: + if perr != nil { + log.Errorf("got error back from postWaitCh: %s", err) + // TODO: what do we even do here? + return + } + postWaitCh = nil + onBlock = nil + // yay? + log.Infof("post successfully submitted") } } } func (m *Miner) maybeDoPost(ctx context.Context, ts *types.TipSet) (<-chan error, *types.BlockHeader, error) { - ppe, err := m.api.StateMinerProvingPeriodEnd(ctx, m.actor, ts) + ppe, err := m.api.StateMinerProvingPeriodEnd(ctx, m.maddr, ts) if err != nil { return nil, nil, xerrors.Errorf("failed to get proving period end for miner: %w", err) } @@ -219,12 +248,12 @@ func (m *Miner) maybeDoPost(ctx context.Context, ts *types.TipSet) (<-chan error return nil, nil, nil } - sset, err := m.api.StateMinerProvingSet(ctx, m.actor, ts) + sset, err := m.api.StateMinerProvingSet(ctx, m.maddr, ts) if err != nil { return nil, nil, xerrors.Errorf("failed to get proving set for miner: %w", err) } - r, err := m.api.ChainGetRandomness(ctx, ts, nil, ts.Height() - ppe) + r, err := m.api.ChainGetRandomness(ctx, ts, nil, int(ts.Height()-ppe)) if err != nil { return nil, nil, xerrors.Errorf("failed to get chain randomness for post: %w", err) } @@ -236,14 +265,19 @@ func (m *Miner) maybeDoPost(ctx context.Context, ts *types.TipSet) (<-chan error ret := make(chan error, 1) go func() { - proof, err := s.secst.RunPoSt(ctx, sset, r) - if err != nil { - ret <- xerrors.Errorf("running post failed: %w", err) - return - } + proof, err := m.secst.RunPoSt(ctx, sset, r) + if err != nil { + ret <- xerrors.Errorf("running post failed: %w", err) + return + } - panic("submit post maybe?") -}() + // TODO: submit post... + _ = proof + + // make sure it succeeds... + // m.api.ChainWaitMsg() + + }() return ret, sourceTs.MinTicketBlock(), nil } @@ -268,4 +302,3 @@ func (m *Miner) runPreflightChecks(ctx context.Context) error { log.Infof("starting up miner %s, worker addr %s", m.maddr, m.worker) return nil } - diff --git a/storage/sector/store.go b/storage/sector/store.go index 115dbe20f..cbad8917c 100644 --- a/storage/sector/store.go +++ b/storage/sector/store.go @@ -163,7 +163,7 @@ func (s *Store) WaitSeal(ctx context.Context, sector uint64) (sectorbuilder.Sect return s.sb.SealStatus(sector) } -func (s *Store) RunPoSt(ctx context.Context, sectors []api.SectorSetEntry, r []byte) ([]byte, error) { +func (s *Store) RunPoSt(ctx context.Context, sectors []*api.SectorInfo, r []byte) ([]byte, error) { panic("NYI") }