Skip to content

Commit

Permalink
feat: divide set owner cmd and add set-beneficiary
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZexiao committed Jan 10, 2023
1 parent 3f4b440 commit 64573ba
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 76 deletions.
121 changes: 115 additions & 6 deletions cmd/cli/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"text/tabwriter"
"time"
Expand Down Expand Up @@ -32,6 +33,7 @@ var MinerCmd = &cli.Command{
minerSetOwnerCmd,
minerSetWorkerCmd,
minerSetControllersCmd,
minerSetBeneficiaryCmd,
},
}

Expand Down Expand Up @@ -490,8 +492,14 @@ var minerInfoCmd = &cli.Command{

var minerSetOwnerCmd = &cli.Command{
Name: "set-owner",
Usage: "set the owner address of a miner",
Usage: "set the owner address of a miner (this command should be invoked by old owner firstly, then new owner invoke with '--confirm' flag to confirm the change)",
ArgsUsage: "<minerAddress> <newOwnerAddress>",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "confirm",
Usage: "confirm to change by the new owner",
},
},
Action: func(cctx *cli.Context) error {
ctx := cctx.Context
api, err := getAPI(cctx)
Expand Down Expand Up @@ -520,12 +528,20 @@ var minerSetOwnerCmd = &cli.Command{

fmt.Println("This will take some time (maybe 10 epoch), to ensure message is chained...")

err = api.MinerSetOwner(ctx, req)
if err != nil {
return err
if cctx.Bool("confirm") {
oldOwner, err := api.MinerConfirmOwner(ctx, req)
if err != nil {
return err
}
fmt.Printf("Miner owner changed to %s from %s \n", newOwner, oldOwner)
} else {
err = api.MinerSetOwner(ctx, req)
if err != nil {
return err
}
fmt.Printf("Miner owner proposed , it should be confirm by new owner(%s), who shall invoke 'set-owner' command with with '--confirm' flag \n", newOwner)
}

fmt.Printf("Owner address changed to %s \n", newOwner)
return nil
},
}
Expand Down Expand Up @@ -567,7 +583,7 @@ var minerSetWorkerCmd = &cli.Command{
NewWorker: newWorker,
}

fmt.Println("This will take some time (maybe 10 epoch), to ensure message is chained...")
fmt.Println("This will take some time (maybe 5 epoch), to ensure message is chained...")

if cctx.Bool("confirm") {
err := api.MinerConfirmWorker(ctx, req)
Expand Down Expand Up @@ -660,3 +676,96 @@ var minerSetControllersCmd = &cli.Command{
return nil
},
}

var minerSetBeneficiaryCmd = &cli.Command{
Name: "set-beneficiary",
Usage: "set the beneficiary address of a miner (the change should be proposed by owner, and confirmed by old beneficiary and nominee)",
ArgsUsage: "<minerAddress> <newBeneficiaryAddress> <quota> <expiration>",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "confirm-by-beneficiary",
Usage: "confirm the change by the old beneficiary",
},
&cli.BoolFlag{
Name: "confirm-by-nominee",
Usage: "confirm the change by the nominee",
},
},
Action: func(cctx *cli.Context) error {
ctx := cctx.Context
api, err := getAPI(cctx)
if err != nil {
return err
}

if cctx.Bool("confirm-by-beneficiary") && cctx.Bool("confirm-by-nominee") {
return fmt.Errorf("can't confirm by beneficiary and nominee at the same time")
}

if cctx.NArg() < 2 {
return fmt.Errorf("must pass miner address and new beneficiary address as first and second arguments")
}

mAddr, err := address.NewFromString(cctx.Args().First())
if err != nil {
return err
}

newBeneficiary, err := address.NewFromString(cctx.Args().Get(1))
if err != nil {
return err
}

fmt.Println("This will take some time (maybe 5 epoch), to ensure message is chained...")
if cctx.Bool("confirm-by-beneficiary") || cctx.Bool("confirm-by-nominee") {
req := &service.MinerConfirmBeneficiaryReq{
Miner: mAddr,
NewBeneficiary: newBeneficiary,
ByNominee: cctx.Bool("confirm-by-nominee"),
}

confirmor, err := api.MinerConfirmBeneficiary(ctx, req)
if err != nil {
return err
}

fmt.Printf("Beneficiary address changed to %s has been confirm by %s \n", newBeneficiary, confirmor)

} else {
if cctx.NArg() != 4 {
return fmt.Errorf("must pass miner address, new beneficiary address, quota and expiration as arguments")
}

quota, err := types.ParseFIL(cctx.Args().Get(2))
if err != nil {
return err
}

expiration, err := strconv.ParseInt(cctx.Args().Get(3), 10, 64)
if err != nil {
return err
}

req := &service.MinerSetBeneficiaryReq{
Miner: mAddr,
ChangeBeneficiaryParams: types.ChangeBeneficiaryParams{
NewBeneficiary: newBeneficiary,
NewQuota: abi.TokenAmount(quota),
NewExpiration: abi.ChainEpoch(expiration),
},
}

pendingChange, err := api.MinerSetBeneficiary(ctx, req)
if err != nil {
return err
}
fmt.Println("Beneficiary changed proposed:")
err = printJSON(pendingChange)
if err != nil {
return err
}
}

return nil
},
}
26 changes: 15 additions & 11 deletions service/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/filecoin-project/go-fil-markets/storagemarket"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/dline"
"github.com/filecoin-project/venus/venus-shared/types"
venusTypes "github.com/filecoin-project/venus/venus-shared/types"
marketTypes "github.com/filecoin-project/venus/venus-shared/types/market"
"github.com/ipfs/go-cid"
Expand All @@ -25,17 +26,20 @@ type IService interface {
AddrOperate(ctx context.Context, params *AddrsOperateReq) error // PUT:/addr/operate
AddrList(ctx context.Context) ([]*AddrsResp, error) // GET:/addr/list

MinerInfo(ctx context.Context, mAddr address.Address) (*MinerInfoResp, error) // GET:/miner/info
MinerCreate(ctx context.Context, params *MinerCreateReq) (address.Address, error) // POST:/miner/create
MinerGetStorageAsk(ctx context.Context, mAddr address.Address) (*storagemarket.StorageAsk, error) // GET:/miner/storageask/
MinerGetRetrievalAsk(ctx context.Context, mAddr address.Address) (*retrievalmarket.Ask, error) // GET:/miner/retrievalask/
MinerSetStorageAsk(ctx context.Context, p *MinerSetAskReq) error // PUT:/miner/storageask/
MinerSetRetrievalAsk(ctx context.Context, p *MinerSetRetrievalAskReq) error // PUT:/miner/retrievalask/
MinerGetDeadlines(ctx context.Context, mAddr address.Address) (*dline.Info, error) // GET:/miner/deadline
MinerSetOwner(ctx context.Context, p *MinerSetOwnerReq) error // PUT:/miner/owner
MinerSetWorker(ctx context.Context, req *MinerSetWorkerReq) (WorkerChangeEpoch abi.ChainEpoch, err error) // PUT:/miner/worker
MinerConfirmWorker(ctx context.Context, req *MinerSetWorkerReq) error // PUT:/miner/confirmworker
MinerSetControllers(ctx context.Context, req *MinerSetControllersReq) (oldController []address.Address, err error) // PUT:/miner/controllers
MinerInfo(ctx context.Context, mAddr address.Address) (*MinerInfoResp, error) // GET:/miner/info
MinerCreate(ctx context.Context, params *MinerCreateReq) (address.Address, error) // POST:/miner/create
MinerGetStorageAsk(ctx context.Context, mAddr address.Address) (*storagemarket.StorageAsk, error) // GET:/miner/storageask/
MinerGetRetrievalAsk(ctx context.Context, mAddr address.Address) (*retrievalmarket.Ask, error) // GET:/miner/retrievalask/
MinerSetStorageAsk(ctx context.Context, p *MinerSetAskReq) error // PUT:/miner/storageask/
MinerSetRetrievalAsk(ctx context.Context, p *MinerSetRetrievalAskReq) error // PUT:/miner/retrievalask/
MinerGetDeadlines(ctx context.Context, mAddr address.Address) (*dline.Info, error) // GET:/miner/deadline
MinerSetOwner(ctx context.Context, p *MinerSetOwnerReq) error // PUT:/miner/owner
MinerConfirmOwner(ctx context.Context, p *MinerSetOwnerReq) (oldOwner address.Address, err error) // PUT:/miner/confirmowner
MinerSetWorker(ctx context.Context, req *MinerSetWorkerReq) (WorkerChangeEpoch abi.ChainEpoch, err error) // PUT:/miner/worker
MinerConfirmWorker(ctx context.Context, req *MinerSetWorkerReq) error // PUT:/miner/confirmworker
MinerSetControllers(ctx context.Context, req *MinerSetControllersReq) (oldController []address.Address, err error) // PUT:/miner/controllers
MinerSetBeneficiary(ctx context.Context, req *MinerSetBeneficiaryReq) (*types.PendingBeneficiaryChange, error) // PUT:/miner/beneficiary
MinerConfirmBeneficiary(ctx context.Context, req *MinerConfirmBeneficiaryReq) (confirmor address.Address, err error) // PUT:/miner/confirmbeneficiary

StorageDealList(ctx context.Context, miner address.Address) ([]marketTypes.MinerDeal, error) // GET:/deal/storage
StorageDealUpdateState(ctx context.Context, req StorageDealUpdateStateReq) error // PUT:/deal/storage/state
Expand Down
Loading

0 comments on commit 64573ba

Please sign in to comment.