api.SyncIncomingBlocks
This commit is contained in:
parent
333847f7c9
commit
d3b980ef58
@ -38,6 +38,7 @@ type FullNode interface {
|
|||||||
// syncer
|
// syncer
|
||||||
SyncState(context.Context) (*SyncState, error)
|
SyncState(context.Context) (*SyncState, error)
|
||||||
SyncSubmitBlock(ctx context.Context, blk *types.BlockMsg) error
|
SyncSubmitBlock(ctx context.Context, blk *types.BlockMsg) error
|
||||||
|
SyncIncomingBlocks(ctx context.Context) (<-chan *types.BlockHeader, error)
|
||||||
|
|
||||||
// messages
|
// messages
|
||||||
MpoolPending(context.Context, *types.TipSet) ([]*types.SignedMessage, error)
|
MpoolPending(context.Context, *types.TipSet) ([]*types.SignedMessage, error)
|
||||||
|
@ -53,6 +53,7 @@ type FullNodeStruct struct {
|
|||||||
|
|
||||||
SyncState func(context.Context) (*SyncState, error) `perm:"read"`
|
SyncState func(context.Context) (*SyncState, error) `perm:"read"`
|
||||||
SyncSubmitBlock func(ctx context.Context, blk *types.BlockMsg) error `perm:"write"`
|
SyncSubmitBlock func(ctx context.Context, blk *types.BlockMsg) error `perm:"write"`
|
||||||
|
SyncIncomingBlocks func(ctx context.Context) (<-chan *types.BlockHeader, error) `perm:"read"`
|
||||||
|
|
||||||
MpoolPending func(context.Context, *types.TipSet) ([]*types.SignedMessage, error) `perm:"read"`
|
MpoolPending func(context.Context, *types.TipSet) ([]*types.SignedMessage, error) `perm:"read"`
|
||||||
MpoolPush func(context.Context, *types.SignedMessage) error `perm:"write"`
|
MpoolPush func(context.Context, *types.SignedMessage) error `perm:"write"`
|
||||||
@ -351,6 +352,10 @@ func (c *FullNodeStruct) SyncSubmitBlock(ctx context.Context, blk *types.BlockMs
|
|||||||
return c.Internal.SyncSubmitBlock(ctx, blk)
|
return c.Internal.SyncSubmitBlock(ctx, blk)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *FullNodeStruct) SyncIncomingBlocks(ctx context.Context) (<-chan *types.BlockHeader, error) {
|
||||||
|
return c.Internal.SyncIncomingBlocks(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *FullNodeStruct) StateMinerSectors(ctx context.Context, addr address.Address, ts *types.TipSet) ([]*ChainSectorInfo, error) {
|
func (c *FullNodeStruct) StateMinerSectors(ctx context.Context, addr address.Address, ts *types.TipSet) ([]*ChainSectorInfo, error) {
|
||||||
return c.Internal.StateMinerSectors(ctx, addr, ts)
|
return c.Internal.StateMinerSectors(ctx, addr, ts)
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
logging "github.com/ipfs/go-log"
|
logging "github.com/ipfs/go-log"
|
||||||
"github.com/libp2p/go-libp2p-core/peer"
|
"github.com/libp2p/go-libp2p-core/peer"
|
||||||
cbg "github.com/whyrusleeping/cbor-gen"
|
cbg "github.com/whyrusleeping/cbor-gen"
|
||||||
|
"github.com/whyrusleeping/pubsub"
|
||||||
"go.opencensus.io/trace"
|
"go.opencensus.io/trace"
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
@ -35,6 +36,8 @@ import (
|
|||||||
|
|
||||||
var log = logging.Logger("chain")
|
var log = logging.Logger("chain")
|
||||||
|
|
||||||
|
var localIncoming = "incoming"
|
||||||
|
|
||||||
type Syncer struct {
|
type Syncer struct {
|
||||||
// The heaviest known tipset in the network.
|
// The heaviest known tipset in the network.
|
||||||
|
|
||||||
@ -58,6 +61,8 @@ type Syncer struct {
|
|||||||
syncLock sync.Mutex
|
syncLock sync.Mutex
|
||||||
|
|
||||||
syncmgr *SyncManager
|
syncmgr *SyncManager
|
||||||
|
|
||||||
|
incoming *pubsub.PubSub
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSyncer(sm *stmgr.StateManager, bsync *blocksync.BlockSync, self peer.ID) (*Syncer, error) {
|
func NewSyncer(sm *stmgr.StateManager, bsync *blocksync.BlockSync, self peer.ID) (*Syncer, error) {
|
||||||
@ -78,6 +83,8 @@ func NewSyncer(sm *stmgr.StateManager, bsync *blocksync.BlockSync, self peer.ID)
|
|||||||
store: sm.ChainStore(),
|
store: sm.ChainStore(),
|
||||||
sm: sm,
|
sm: sm,
|
||||||
self: self,
|
self: self,
|
||||||
|
|
||||||
|
incoming: pubsub.New(50),
|
||||||
}
|
}
|
||||||
|
|
||||||
s.syncmgr = NewSyncManager(s.Sync)
|
s.syncmgr = NewSyncManager(s.Sync)
|
||||||
@ -109,6 +116,8 @@ func (syncer *Syncer) InformNewHead(from peer.ID, fts *store.FullTipSet) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
syncer.incoming.Pub(fts.TipSet().Blocks(), localIncoming)
|
||||||
|
|
||||||
if from == syncer.self {
|
if from == syncer.self {
|
||||||
// TODO: this is kindof a hack...
|
// TODO: this is kindof a hack...
|
||||||
log.Debug("got block from ourselves")
|
log.Debug("got block from ourselves")
|
||||||
@ -139,6 +148,31 @@ func (syncer *Syncer) InformNewHead(from peer.ID, fts *store.FullTipSet) {
|
|||||||
syncer.syncmgr.SetPeerHead(ctx, from, fts.TipSet())
|
syncer.syncmgr.SetPeerHead(ctx, from, fts.TipSet())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (syncer *Syncer) IncomingBlocks(ctx context.Context) (<-chan *types.BlockHeader, error) {
|
||||||
|
sub := syncer.incoming.Sub(localIncoming)
|
||||||
|
out := make(chan *types.BlockHeader, 10)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case r := <-sub:
|
||||||
|
hs := r.([]*types.BlockHeader)
|
||||||
|
for _, h := range hs {
|
||||||
|
select {
|
||||||
|
case out <- h:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (syncer *Syncer) ValidateMsgMeta(fblk *types.FullBlock) error {
|
func (syncer *Syncer) ValidateMsgMeta(fblk *types.FullBlock) error {
|
||||||
var bcids, scids []cbg.CBORMarshaler
|
var bcids, scids []cbg.CBORMarshaler
|
||||||
for _, m := range fblk.BlsMessages {
|
for _, m := range fblk.BlsMessages {
|
||||||
|
@ -250,6 +250,4 @@ func (h *handler) messages(filter string, args ...interface{}) (out []types.Mess
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var _ http.Handler = &handler{}
|
var _ http.Handler = &handler{}
|
||||||
|
@ -54,7 +54,7 @@ func (a *SyncAPI) SyncSubmitBlock(ctx context.Context, blk *types.BlockMsg) erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := a.Syncer.ValidateMsgMeta(fb); err != nil {
|
if err := a.Syncer.ValidateMsgMeta(fb); err != nil {
|
||||||
xerrors.Errorf("provided messages did not match block: %w", err)
|
return xerrors.Errorf("provided messages did not match block: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ts, err := types.NewTipSet([]*types.BlockHeader{blk.Header})
|
ts, err := types.NewTipSet([]*types.BlockHeader{blk.Header})
|
||||||
@ -73,3 +73,7 @@ func (a *SyncAPI) SyncSubmitBlock(ctx context.Context, blk *types.BlockMsg) erro
|
|||||||
// TODO: anything else to do here?
|
// TODO: anything else to do here?
|
||||||
return a.PubSub.Publish("/fil/blocks", b)
|
return a.PubSub.Publish("/fil/blocks", b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *SyncAPI) SyncIncomingBlocks(ctx context.Context) (<-chan *types.BlockHeader, error) {
|
||||||
|
return a.Syncer.IncomingBlocks(ctx)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user