WIP: ensure blocks make it into repo before pushing to pubsub

This commit is contained in:
whyrusleeping
2019-10-14 23:21:37 +09:00
parent f9dde3d10a
commit 7c26e3c35f
7 changed files with 53 additions and 27 deletions
+1 -15
View File
@@ -9,7 +9,6 @@ import (
"golang.org/x/xerrors"
"github.com/ipfs/go-cid"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"go.uber.org/fx"
)
@@ -18,26 +17,13 @@ type ChainAPI struct {
WalletAPI
Chain *store.ChainStore
PubSub *pubsub.PubSub
Chain *store.ChainStore
}
func (a *ChainAPI) ChainNotify(ctx context.Context) (<-chan []*store.HeadChange, error) {
return a.Chain.SubHeadChanges(ctx), nil
}
func (a *ChainAPI) ChainSubmitBlock(ctx context.Context, blk *types.BlockMsg) error {
// TODO: should we have some sort of fast path to adding a local block?
b, err := blk.Serialize()
if err != nil {
return xerrors.Errorf("serializing block for pubsub publishing failed: %w", err)
}
// TODO: anything else to do here?
return a.PubSub.Publish("/fil/blocks", b)
}
func (a *ChainAPI) ChainHead(context.Context) (*types.TipSet, error) {
return a.Chain.GetHeaviestTipSet(), nil
}
+33
View File
@@ -5,13 +5,18 @@ import (
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/chain/types"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"go.uber.org/fx"
"golang.org/x/xerrors"
)
type SyncAPI struct {
fx.In
Syncer *chain.Syncer
PubSub *pubsub.PubSub
}
func (a *SyncAPI) SyncState(ctx context.Context) (*api.SyncState, error) {
@@ -23,3 +28,31 @@ func (a *SyncAPI) SyncState(ctx context.Context) (*api.SyncState, error) {
Height: ss.Height,
}, nil
}
func (a *SyncAPI) SyncSubmitBlock(ctx context.Context, blk *types.BlockMsg) error {
// TODO: should we have some sort of fast path to adding a local block?
bmsgs, err := a.Syncer.ChainStore().LoadMessagesFromCids(blk.BlsMessages)
if err != nil {
return xerrors.Errorf("failed to load bls messages: %w", err)
}
smsgs, err := a.Syncer.ChainStore().LoadSignedMessagesFromCids(blk.SecpkMessages)
if err != nil {
return xerrors.Errorf("failed to load secpk message: %w", err)
}
fb := &types.FullBlock{
Header: blk.Header,
BlsMessages: bmsgs,
SecpkMessages: smsgs,
}
a.Syncer.InformNewBlock(a.Syncer.LocalPeer(), fb)
b, err := blk.Serialize()
if err != nil {
return xerrors.Errorf("serializing block for pubsub publishing failed: %w", err)
}
// TODO: anything else to do here?
return a.PubSub.Publish("/fil/blocks", b)
}