From f0d32f2c01ea79f98d64a27a1026869d2d337fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 13 Aug 2020 10:20:09 +0200 Subject: [PATCH 1/3] miner: fix/rename miner actor withdraw --- cmd/lotus-storage-miner/actor.go | 76 +++++++++++++++++++++++++++++ cmd/lotus-storage-miner/rewards.go | 78 ------------------------------ 2 files changed, 76 insertions(+), 78 deletions(-) delete mode 100644 cmd/lotus-storage-miner/rewards.go diff --git a/cmd/lotus-storage-miner/actor.go b/cmd/lotus-storage-miner/actor.go index 18e3796a3..6f2cefad3 100644 --- a/cmd/lotus-storage-miner/actor.go +++ b/cmd/lotus-storage-miner/actor.go @@ -2,6 +2,8 @@ package main import ( "fmt" + "github.com/filecoin-project/specs-actors/actors/builtin" + "golang.org/x/xerrors" ma "github.com/multiformats/go-multiaddr" "github.com/urfave/cli/v2" @@ -19,6 +21,7 @@ var actorCmd = &cli.Command{ Usage: "manipulate the miner actor", Subcommands: []*cli.Command{ actorSetAddrsCmd, + actorWithdrawCmd, }, } @@ -91,3 +94,76 @@ var actorSetAddrsCmd = &cli.Command{ }, } + + +var actorWithdrawCmd = &cli.Command{ + Name: "withdraw", + Usage: "withdraw available balance", + ArgsUsage: "[amount (FIL)]", + Action: func(cctx *cli.Context) error { + nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx) + if err != nil { + return err + } + defer closer() + + api, acloser, err := lcli.GetFullNodeAPI(cctx) + if err != nil { + return err + } + defer acloser() + + ctx := lcli.ReqContext(cctx) + + maddr, err := nodeApi.ActorAddress(ctx) + if err != nil { + return err + } + + mi, err := api.StateMinerInfo(ctx, maddr, types.EmptyTSK) + if err != nil { + return err + } + + available, err := api.StateMinerAvailableBalance(ctx, maddr, types.EmptyTSK) + if err != nil { + return err + } + + amount := available + if cctx.Args().Present() { + f, err := types.ParseFIL(cctx.Args().First()) + if err != nil { + return xerrors.Errorf("parsing 'amount' argument: %w", err) + } + + amount = abi.TokenAmount(f) + + if amount.GreaterThan(available) { + return xerrors.Errorf("can't withdraw more funds than available; requested: %s; available: %s", amount, available) + } + } + + params, err := actors.SerializeParams(&miner.WithdrawBalanceParams{ + AmountRequested: amount, // Default to attempting to withdraw all the extra funds in the miner actor + }) + if err != nil { + return err + } + + smsg, err := api.MpoolPushMessage(ctx, &types.Message{ + To: maddr, + From: mi.Owner, + Value: types.NewInt(0), + Method: builtin.MethodsMiner.WithdrawBalance, + Params: params, + }, nil) + if err != nil { + return err + } + + fmt.Printf("Requested rewards withdrawal in message %s\n", smsg.Cid()) + + return nil + }, +} diff --git a/cmd/lotus-storage-miner/rewards.go b/cmd/lotus-storage-miner/rewards.go deleted file mode 100644 index 279965e1e..000000000 --- a/cmd/lotus-storage-miner/rewards.go +++ /dev/null @@ -1,78 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/urfave/cli/v2" - - "github.com/filecoin-project/specs-actors/actors/builtin" - "github.com/filecoin-project/specs-actors/actors/builtin/miner" - - "github.com/filecoin-project/lotus/chain/actors" - "github.com/filecoin-project/lotus/chain/types" - lcli "github.com/filecoin-project/lotus/cli" -) - -var rewardsCmd = &cli.Command{ - Name: "rewards", - Subcommands: []*cli.Command{ - rewardsRedeemCmd, - }, -} - -var rewardsRedeemCmd = &cli.Command{ - Name: "redeem", - Usage: "Redeem block rewards", - Action: func(cctx *cli.Context) error { - nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx) - if err != nil { - return err - } - defer closer() - - api, acloser, err := lcli.GetFullNodeAPI(cctx) - if err != nil { - return err - } - defer acloser() - - ctx := lcli.ReqContext(cctx) - - maddr, err := nodeApi.ActorAddress(ctx) - if err != nil { - return err - } - - mi, err := api.StateMinerInfo(ctx, maddr, types.EmptyTSK) - if err != nil { - return err - } - - mact, err := api.StateGetActor(ctx, maddr, types.EmptyTSK) - if err != nil { - return err - } - - params, err := actors.SerializeParams(&miner.WithdrawBalanceParams{ - AmountRequested: mact.Balance, // Default to attempting to withdraw all the extra funds in the miner actor - }) - if err != nil { - return err - } - - smsg, err := api.MpoolPushMessage(ctx, &types.Message{ - To: maddr, - From: mi.Owner, - Value: types.NewInt(0), - Method: builtin.MethodsMiner.WithdrawBalance, - Params: params, - }, nil) - if err != nil { - return err - } - - fmt.Printf("Requested rewards withdrawal in message %s\n", smsg.Cid()) - - return nil - }, -} From 936d861df58f9f1edf39c10524f280e34bcd2393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 13 Aug 2020 10:20:41 +0200 Subject: [PATCH 2/3] gofmt --- chain/events/state/diff_adt.go | 5 ++--- chain/events/state/diff_adt_test.go | 8 +++----- chain/events/state/predicates.go | 13 +++++-------- cmd/lotus-chainwatch/processor/common_actors.go | 2 +- cmd/lotus-storage-miner/actor.go | 5 ++--- 5 files changed, 13 insertions(+), 20 deletions(-) diff --git a/chain/events/state/diff_adt.go b/chain/events/state/diff_adt.go index 9e66eede3..1b921dd9e 100644 --- a/chain/events/state/diff_adt.go +++ b/chain/events/state/diff_adt.go @@ -41,7 +41,7 @@ func DiffAdtArray(preArr, curArr *adt.Array, out AdtArrayDiff) error { } // no modification - if !bytes.Equal(prevVal.Raw,curVal.Raw) { + if !bytes.Equal(prevVal.Raw, curVal.Raw) { if err := out.Modify(uint64(i), prevVal, curVal); err != nil { return err } @@ -95,13 +95,12 @@ func DiffAdtMap(preMap, curMap *adt.Map, out AdtMapDiff) error { } // no modification - if !bytes.Equal(prevVal.Raw,curVal.Raw) { + if !bytes.Equal(prevVal.Raw, curVal.Raw) { if err := out.Modify(key, prevVal, curVal); err != nil { return err } } - return curMap.Delete(k) }); err != nil { return err diff --git a/chain/events/state/diff_adt_test.go b/chain/events/state/diff_adt_test.go index a430d2de7..56a03bf33 100644 --- a/chain/events/state/diff_adt_test.go +++ b/chain/events/state/diff_adt_test.go @@ -71,7 +71,6 @@ func TestDiffAdtArray(t *testing.T) { assert.EqualValues(t, []byte{1}, changes.Removed[1].val) } - func TestDiffAdtMap(t *testing.T) { ctxstoreA := newContextStore() ctxstoreB := newContextStore() @@ -128,14 +127,13 @@ func TestDiffAdtMap(t *testing.T) { } type TestDiffMap struct { - Added []adtMapDiffResult + Added []adtMapDiffResult Modified []TestAdtMapDiffModified - Removed []adtMapDiffResult + Removed []adtMapDiffResult } var _ AdtMapDiff = &TestDiffMap{} - func (t *TestDiffMap) AsKey(key string) (adt.Keyer, error) { k, err := adt.ParseUIntKey(key) if err != nil { @@ -218,7 +216,7 @@ type adtMapDiffResult struct { type TestAdtMapDiffModified struct { From adtMapDiffResult - To adtMapDiffResult + To adtMapDiffResult } type adtArrayDiffResult struct { diff --git a/chain/events/state/predicates.go b/chain/events/state/predicates.go index 544ff7b14..bf85f1f1a 100644 --- a/chain/events/state/predicates.go +++ b/chain/events/state/predicates.go @@ -649,22 +649,20 @@ type AddressPair struct { } type InitActorAddressChanges struct { - Added []AddressPair + Added []AddressPair Modified []AddressChange - Removed []AddressPair + Removed []AddressPair } type AddressChange struct { From AddressPair - To AddressPair + To AddressPair } type DiffInitActorStateFunc func(ctx context.Context, oldState *init_.State, newState *init_.State) (changed bool, user UserData, err error) - - func (i *InitActorAddressChanges) AsKey(key string) (adt.Keyer, error) { - addr , err := address.NewFromBytes([]byte(key)) + addr, err := address.NewFromBytes([]byte(key)) if err != nil { return nil, err } @@ -720,7 +718,7 @@ func (i *InitActorAddressChanges) Modify(key string, from, to *typegen.Deferred) ID: fromIDAddr, PK: pkAddr, }, - To: AddressPair{ + To: AddressPair{ ID: toIDAddr, PK: pkAddr, }, @@ -786,4 +784,3 @@ func (sp *StatePredicates) OnAddressMapChange() DiffInitActorStateFunc { return true, addressChanges, nil } } - diff --git a/cmd/lotus-chainwatch/processor/common_actors.go b/cmd/lotus-chainwatch/processor/common_actors.go index b2e86ddc2..3fbdbf170 100644 --- a/cmd/lotus-chainwatch/processor/common_actors.go +++ b/cmd/lotus-chainwatch/processor/common_actors.go @@ -176,7 +176,7 @@ func (p Processor) storeActorAddresses(ctx context.Context, actors map[cid.Cid]A for _, updates := range addressesToUpdate { if _, err := updateTx.Exec( fmt.Sprintf("update id_address_map set id=%s, address=%s where id=%s and address=%s", updates.New.ID, updates.New.PK, updates.Old.ID, updates.Old.PK), - ); err != nil { + ); err != nil { return err } } diff --git a/cmd/lotus-storage-miner/actor.go b/cmd/lotus-storage-miner/actor.go index 6f2cefad3..21d09b5ac 100644 --- a/cmd/lotus-storage-miner/actor.go +++ b/cmd/lotus-storage-miner/actor.go @@ -95,10 +95,9 @@ var actorSetAddrsCmd = &cli.Command{ }, } - var actorWithdrawCmd = &cli.Command{ - Name: "withdraw", - Usage: "withdraw available balance", + Name: "withdraw", + Usage: "withdraw available balance", ArgsUsage: "[amount (FIL)]", Action: func(cctx *cli.Context) error { nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx) From 02650c7f25708125f673b0371a65d617fcd0596a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 13 Aug 2020 10:25:21 +0200 Subject: [PATCH 3/3] miner: drop rewardsCmd from miner cmd list --- cmd/lotus-storage-miner/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/lotus-storage-miner/main.go b/cmd/lotus-storage-miner/main.go index 5848bb0eb..fa6758463 100644 --- a/cmd/lotus-storage-miner/main.go +++ b/cmd/lotus-storage-miner/main.go @@ -33,7 +33,6 @@ func main() { runCmd, stopCmd, lcli.WithCategory("chain", actorCmd), - lcli.WithCategory("chain", rewardsCmd), lcli.WithCategory("chain", infoCmd), lcli.WithCategory("market", storageDealsCmd), lcli.WithCategory("market", retrievalDealsCmd),