forked from ipfs-force-community/droplet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ipfs-force-community#21 from dtynn/dtynn/feat/seal…
…er_integration Dtynn/feat/sealer integration
- Loading branch information
Showing
42 changed files
with
959 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os/signal" | ||
"strconv" | ||
"syscall" | ||
|
||
"github.com/dtynn/dix" | ||
"github.com/filecoin-project/go-address" | ||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/dtynn/venus-cluster/venus-sealer/dep" | ||
"github.com/dtynn/venus-cluster/venus-sealer/pkg/chain" | ||
"github.com/dtynn/venus-cluster/venus-sealer/pkg/homedir" | ||
"github.com/dtynn/venus-cluster/venus-sealer/pkg/logging" | ||
"github.com/dtynn/venus-cluster/venus-sealer/pkg/messager" | ||
) | ||
|
||
var Log = logging.New("sealer") | ||
|
||
var HomeFlag = &cli.StringFlag{ | ||
Name: "home", | ||
Value: "~/.venus-sealer", | ||
} | ||
|
||
var NetFlag = &cli.StringFlag{ | ||
Name: "net", | ||
Value: "mainnet", | ||
} | ||
|
||
type stopper = func() | ||
|
||
func NewSigContext(parent context.Context) (context.Context, context.CancelFunc) { | ||
return signal.NotifyContext(parent, syscall.SIGABRT, syscall.SIGTERM, syscall.SIGINT) | ||
} | ||
|
||
func DepsFromCLICtx(cctx *cli.Context) dix.Option { | ||
return dix.Options( | ||
dix.Override(new(*cli.Context), cctx), | ||
dix.Override(new(*homedir.Home), HomeFromCLICtx), | ||
) | ||
} | ||
|
||
func HomeFromCLICtx(cctx *cli.Context) (*homedir.Home, error) { | ||
home, err := homedir.Open(cctx.String(HomeFlag.Name)) | ||
if err != nil { | ||
return nil, fmt.Errorf("open home: %w", err) | ||
} | ||
|
||
if err := home.Init(); err != nil { | ||
return nil, fmt.Errorf("init home: %w", err) | ||
} | ||
|
||
return home, nil | ||
} | ||
|
||
type API struct { | ||
Chain chain.API | ||
Messager messager.API | ||
} | ||
|
||
func extractAPI(cctx *cli.Context) (*API, context.Context, stopper, error) { | ||
logging.SetupForSub("sealer") | ||
|
||
gctx, gcancel := NewSigContext(cctx.Context) | ||
|
||
var capi chain.API | ||
var mapi messager.API | ||
|
||
stopper, err := dix.New( | ||
gctx, | ||
DepsFromCLICtx(cctx), | ||
dix.Override(new(dep.GlobalContext), gctx), | ||
dep.API(&capi, &mapi), | ||
) | ||
|
||
if err != nil { | ||
gcancel() | ||
return nil, nil, nil, fmt.Errorf("construct sealer api: %w", err) | ||
} | ||
|
||
return &API{ | ||
Chain: capi, | ||
Messager: mapi, | ||
}, gctx, func() { | ||
stopper(cctx.Context) | ||
gcancel() | ||
}, nil | ||
} | ||
|
||
func RPCCallError(method string, err error) error { | ||
return fmt.Errorf("rpc %s: %w", method, err) | ||
} | ||
|
||
var ErrEmptyAddressString = fmt.Errorf("empty address string") | ||
|
||
func ShouldAddress(s string, checkEmpty bool, allowActor bool) (address.Address, error) { | ||
if checkEmpty && s == "" { | ||
return address.Undef, ErrEmptyAddressString | ||
} | ||
|
||
if allowActor { | ||
id, err := strconv.ParseUint(s, 10, 64) | ||
if err == nil { | ||
return address.NewIDAddress(id) | ||
} | ||
} | ||
|
||
return address.NewFromString(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package internal | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
type PrintHelpErr struct { | ||
Ctx *cli.Context | ||
Err error | ||
} | ||
|
||
func (e *PrintHelpErr) Error() string { | ||
return e.Err.Error() | ||
} | ||
|
||
func (e *PrintHelpErr) Unwrap() error { | ||
return e.Err | ||
} | ||
|
||
func (e *PrintHelpErr) Is(o error) bool { | ||
_, ok := o.(*PrintHelpErr) | ||
return ok | ||
} | ||
|
||
func ShowHelp(cctx *cli.Context, err error) error { | ||
return &PrintHelpErr{ | ||
Ctx: cctx, | ||
Err: err, | ||
} | ||
} | ||
|
||
func ShowHelpf(cctx *cli.Context, format string, args ...interface{}) error { | ||
return ShowHelp(cctx, fmt.Errorf(format, args...)) | ||
} | ||
|
||
func RunApp(app *cli.App) { | ||
if err := app.Run(os.Args); err != nil { | ||
var phe *PrintHelpErr | ||
if errors.As(err, &phe) { | ||
_ = cli.ShowCommandHelp(phe.Ctx, phe.Ctx.Command.Name) | ||
fmt.Fprintf(os.Stderr, "ERROR: %+v\n", err) | ||
} else { | ||
Log.Errorf("%+v", err) | ||
} | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package internal | ||
|
||
import "github.com/urfave/cli/v2" | ||
|
||
var UtilCmd = &cli.Command{ | ||
Name: "util", | ||
Subcommands: []*cli.Command{ | ||
utilChainCmd, | ||
utilMinerCmd, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package internal | ||
|
||
import ( | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var utilChainCmd = &cli.Command{ | ||
Name: "chain", | ||
Subcommands: []*cli.Command{ | ||
utilChainHeadCmd, | ||
}, | ||
} | ||
|
||
var utilChainHeadCmd = &cli.Command{ | ||
Name: "head", | ||
Action: func(cctx *cli.Context) error { | ||
api, gctx, stop, err := extractAPI(cctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer stop() | ||
|
||
head, err := api.Chain.ChainHead(gctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
Log.Infof("ts: %s", head.Key()) | ||
|
||
return nil | ||
}, | ||
} |
Oops, something went wrong.