Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement Actions around wallet and address commands #1682

Merged
merged 1 commit into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 60 additions & 20 deletions commands/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type addressResult struct {
Address string
}

// AddressLsResult is the result of running the address list command.
type AddressLsResult struct {
Addresses []string
}

var addrsNewCmd = &cmds.Command{
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
addr, err := GetAPI(env).Address().Addrs().New(req.Context)
Expand All @@ -62,18 +67,23 @@ var addrsLsCmd = &cmds.Command{
return err
}

var alr AddressLsResult
for _, addr := range addrs {
if err := re.Emit(&addressResult{addr.String()}); err != nil {
return err
}
alr.Addresses = append(alr.Addresses, addr.String())
Copy link
Member Author

@frrist frrist Jan 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making an assumption here (and elsewhere in this file) that a node will never have more addresses than we can hold in memory; it makes the test methods nicer.

}
return nil

return re.Emit(&alr)
},
Type: &addressResult{},
Type: &AddressLsResult{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, addr *addressResult) error {
_, err := fmt.Fprintln(w, addr.Address)
return err
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, addrs *AddressLsResult) error {
for _, addr := range addrs.Addresses {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i assume the way commands library works is that if you return an error it errors out even if you have written some output to w?

Copy link
Member Author

@frrist frrist Jan 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically. If for some reason we couldn't write to w halfway through addrs.Addresses the error would be returned. This doesn't differ much from the previous behaviour, the iteration was just done elsewhere.

_, err := fmt.Fprintln(w, addr)
if err != nil {
return err
}
}
return nil
}),
},
}
Expand Down Expand Up @@ -137,14 +147,30 @@ var walletImportCmd = &cmds.Command{
return err
}

for _, a := range addrs {
if err := re.Emit(a); err != nil {
return err
}
var alr AddressLsResult
for _, addr := range addrs {
alr.Addresses = append(alr.Addresses, addr.String())
}
return nil

return re.Emit(&alr)
},
Type: &AddressLsResult{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, addrs *AddressLsResult) error {
for _, addr := range addrs.Addresses {
_, err := fmt.Fprintln(w, addr)
if err != nil {
return err
}
}
return nil
}),
},
Type: address.Address{},
}

// WalletExportResult is the resut of running the wallet export command.
type WalletExportResult struct {
KeyInfo []*types.KeyInfo
}

var walletExportCmd = &cmds.Command{
Expand All @@ -165,12 +191,26 @@ var walletExportCmd = &cmds.Command{
if err != nil {
return err
}
for _, ki := range kis {
if err := re.Emit(ki); err != nil {
return err

var klr WalletExportResult
klr.KeyInfo = append(klr.KeyInfo, kis...)

return re.Emit(klr)
},
Type: &WalletExportResult{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, klr *WalletExportResult) error {
for _, k := range klr.KeyInfo {
a, err := k.Address()
if err != nil {
return err
}
_, err = fmt.Fprintf(w, "Address:\t%s\nPrivateKey:\t%x\nCurve:\t\t%s\n\n", a.String(), k.PrivateKey, k.Curve)
if err != nil {
return err
}
}
}
return nil
return nil
}),
},
Type: types.KeyInfo{},
}
11 changes: 4 additions & 7 deletions functional-tests/faucet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/filecoin-project/go-filecoin/commands"
th "github.com/filecoin-project/go-filecoin/testhelpers"
iptbtester "github.com/filecoin-project/go-filecoin/testhelpers/iptbtester"
)
Expand All @@ -27,10 +28,6 @@ func init() {
}
}

type addressResult struct {
Address string
}

func TestFaucetSendFunds(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
Expand Down Expand Up @@ -79,13 +76,13 @@ func TestFaucetSendFunds(t *testing.T) {
defer faucetcancel()

// Get address for target node
var targetAddr addressResult
var targetAddr commands.AddressLsResult
node1.MustRunCmdJSON(ctx, &targetAddr, "go-filecoin", "wallet", "addrs", "ls")

// Start Tests

// Make request for funds
msgcid := MustSendFundsFaucet(t, "localhost:9797", targetAddr.Address)
msgcid := MustSendFundsFaucet(t, "localhost:9797", targetAddr.Addresses[0])

// Wait around for message to appear
msgctx, msgcancel := context.WithTimeout(context.Background(), blockTime*3)
Expand All @@ -94,7 +91,7 @@ func TestFaucetSendFunds(t *testing.T) {

// Read wallet balance
var balanceStr string
node1.MustRunCmdJSON(ctx, &balanceStr, "go-filecoin", "wallet", "balance", targetAddr.Address)
node1.MustRunCmdJSON(ctx, &balanceStr, "go-filecoin", "wallet", "balance", targetAddr.Addresses[0])
balance, err := strconv.ParseInt(balanceStr, 10, 64)
require.NoError(err)

Expand Down
3 changes: 1 addition & 2 deletions functional-tests/lib/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ function free_port {

function import_private_key {
./go-filecoin wallet import ./fixtures/"$1".key \
--repodir="$2" \
| jq -r ""
--repodir="$2"
}

function init_local_daemon {
Expand Down
49 changes: 49 additions & 0 deletions testhelpers/fat/action_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package fat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mentioned before that you should move fat to tools/ at some point, much more appropriate there. test helpers is for free functions and the like

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have heard cases for both sides (keeping it where it is and moving it to tools), will address after completion. Issue here so we don't forget: #1695


import (
"context"
"github.com/filecoin-project/go-filecoin/address"
"github.com/filecoin-project/go-filecoin/commands"

"gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
)

// AddressNew runs the address new command against the filecoin process.
func (f *Filecoin) AddressNew(ctx context.Context) (address.Address, error) {
var newAddress address.Address
if err := f.RunCmdJSONWithStdin(ctx, nil, &newAddress, "go-filecoin", "address", "new"); err != nil {
return address.Address{}, err
}
return newAddress, nil
}

// AddressLs runs the address ls command against the filecoin process.
func (f *Filecoin) AddressLs(ctx context.Context) ([]address.Address, error) {
// the command returns an AddressListResult
var alr commands.AddressLsResult
// we expect to interact with an array of address
var out []address.Address

if err := f.RunCmdJSONWithStdin(ctx, nil, &alr, "go-filecoin", "address", "ls"); err != nil {
return nil, err
}

// transform the AddressListResult to an array of addresses
for _, addr := range alr.Addresses {
a, err := address.NewFromString(addr)
if err != nil {
return nil, err
}
out = append(out, a)
}
return out, nil
}

// AddressLookup runs the address lookup command against the filecoin process.
func (f *Filecoin) AddressLookup(ctx context.Context, addr address.Address) (peer.ID, error) {
var ownerPeer peer.ID
if err := f.RunCmdJSONWithStdin(ctx, nil, &ownerPeer, "go-filecoin", "address", "lookup", addr.String()); err != nil {
return "", err
}
return ownerPeer, nil
}
62 changes: 62 additions & 0 deletions testhelpers/fat/action_wallet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package fat

import (
"context"
"strings"

"gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"

"github.com/filecoin-project/go-filecoin/address"
"github.com/filecoin-project/go-filecoin/commands"
"github.com/filecoin-project/go-filecoin/types"
)

// WalletBalance run the wallet balance command against the filecoin process.
func (f *Filecoin) WalletBalance(ctx context.Context, addr address.Address) (*types.AttoFIL, error) {
var balance *types.AttoFIL
if err := f.RunCmdJSONWithStdin(ctx, nil, &balance, "go-filecoin", "wallet", "balance", addr.String()); err != nil {
return nil, err
}
return balance, nil
}

// WalletImport run the wallet import command against the filecoin process.
func (f *Filecoin) WalletImport(ctx context.Context, file files.File) ([]address.Address, error) {
// the command returns an AddressListResult
var alr commands.AddressLsResult
// we expect to interact with an array of address
var out []address.Address

if err := f.RunCmdJSONWithStdin(ctx, file, &alr, "go-filecoin", "wallet", "import"); err != nil {
return nil, err
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment on other PR #1687 (comment)


// transform the AddressListResult to an array of addresses
for _, addr := range alr.Addresses {
a, err := address.NewFromString(addr)
if err != nil {
return nil, err
}
out = append(out, a)
}
return out, nil
}

// WalletExport run the wallet export command against the filecoin process.
func (f *Filecoin) WalletExport(ctx context.Context, addrs []address.Address) ([]*types.KeyInfo, error) {
// the command returns an KeyInfoListResult
var klr commands.WalletExportResult
// we expect to interact with an array of KeyInfo(s)
var out []*types.KeyInfo
var sAddrs []string
for _, a := range addrs {
sAddrs = append(sAddrs, a.String())
}

if err := f.RunCmdJSONWithStdin(ctx, nil, &klr, "go-filecoin", "wallet", "export", strings.Join(sAddrs, " ")); err != nil {
return nil, err
}

// transform the KeyInfoListResult to an array of KeyInfo(s)
return append(out, klr.KeyInfo...), nil
}