2019-09-30 21:06:47 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/api"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain"
|
2019-10-14 14:21:37 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
|
|
|
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2019-09-30 21:06:47 +00:00
|
|
|
"go.uber.org/fx"
|
2019-10-14 14:21:37 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-09-30 21:06:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SyncAPI struct {
|
|
|
|
fx.In
|
|
|
|
|
|
|
|
Syncer *chain.Syncer
|
2019-10-14 14:21:37 +00:00
|
|
|
PubSub *pubsub.PubSub
|
2019-09-30 21:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *SyncAPI) SyncState(ctx context.Context) (*api.SyncState, error) {
|
|
|
|
ss := a.Syncer.State()
|
|
|
|
return &api.SyncState{
|
|
|
|
Base: ss.Base,
|
|
|
|
Target: ss.Target,
|
|
|
|
Stage: ss.Stage,
|
|
|
|
Height: ss.Height,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-10-14 14:21:37 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2019-10-15 12:19:10 +00:00
|
|
|
|
|
|
|
if err := a.Syncer.ValidateMsgMeta(fb); err != nil {
|
|
|
|
xerrors.Errorf("provided messages did not match block: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ts, err := types.NewTipSet([]*types.BlockHeader{blk.Header})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("somehow failed to make a tipset out of a single block: %w", err)
|
|
|
|
}
|
|
|
|
if err := a.Syncer.Sync(ctx, ts); err != nil {
|
|
|
|
return xerrors.Errorf("sync to submitted block failed: %w", err)
|
|
|
|
}
|
2019-10-14 14:21:37 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|