-
Notifications
You must be signed in to change notification settings - Fork 465
/
address_integration_test.go
143 lines (113 loc) · 4.48 KB
/
address_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package cmd_test
import (
"context"
"encoding/hex"
"encoding/json"
"os"
"strings"
"testing"
"github.com/filecoin-project/go-address"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/filecoin-project/specs-actors/v2/actors/builtin"
"github.com/filecoin-project/venus/app/node/test"
"github.com/filecoin-project/venus/cmd"
"github.com/filecoin-project/venus/fixtures/fortest"
tf "github.com/filecoin-project/venus/pkg/testhelpers/testflags"
"github.com/filecoin-project/venus/pkg/wallet"
"github.com/filecoin-project/venus/pkg/wallet/key"
)
func TestAddressNewAndList(t *testing.T) {
tf.IntegrationTest(t)
ctx := context.Background()
builder := test.NewNodeBuilder(t)
n, cmdClient, done := builder.BuildAndStartAPI(ctx)
defer done()
addrs := make([]address.Address, 10)
var err error
for i := 0; i < 10; i++ {
addrs[i], err = n.Wallet().API().WalletNewAddress(ctx, address.SECP256K1)
require.NoError(t, err)
}
list := cmdClient.RunSuccess(ctx, "wallet", "ls").ReadStdout()
for _, addr := range addrs {
assert.Contains(t, list, addr.String())
}
}
func TestWalletBalance(t *testing.T) {
tf.IntegrationTest(t)
ctx := context.Background()
builder := test.NewNodeBuilder(t)
cs := test.FixtureChainSeed(t)
builder.WithGenesisInit(cs.GenesisInitFunc)
n, cmdClient, done := builder.BuildAndStartAPI(ctx)
defer done()
addr, err := n.Wallet().API().WalletNewAddress(ctx, address.SECP256K1)
require.NoError(t, err)
t.Log("[success] not found, zero")
balance := cmdClient.RunSuccess(ctx, "wallet", "balance", addr.String()).ReadStdout()
assert.Equal(t, "0 FIL (warning: may display 0 if chain sync in progress)\n", balance)
t.Log("[success] balance 1394000000000000000000000000")
balance = cmdClient.RunSuccess(ctx, "wallet", "balance", builtin.RewardActorAddr.String()).ReadStdout()
assert.Equal(t, "1394000000 FIL\n", balance)
t.Log("[success] newly generated one")
var addrNew cmd.AddressResult
cmdClient.RunSuccessFirstLine(ctx, "wallet", "new")
balance = cmdClient.RunSuccess(ctx, "wallet", "balance", addrNew.Address.String()).ReadStdout()
assert.Equal(t, "0 FIL (warning: may display 0 if chain sync in progress)\n", balance)
}
func TestWalletLoadFromFile(t *testing.T) {
tf.IntegrationTest(t)
ctx := context.Background()
builder := test.NewNodeBuilder(t)
cs := test.FixtureChainSeed(t)
builder.WithGenesisInit(cs.GenesisInitFunc)
_, cmdClient, done := builder.BuildAndStartAPI(ctx)
defer done()
for _, p := range fortest.KeyFilePaths() {
cmdClient.RunSuccess(ctx, "wallet", "import", p)
}
list := cmdClient.RunSuccess(ctx, "wallet", "ls").ReadStdout()
for _, addr := range fortest.TestAddresses {
// assert we loaded the test address from the file
assert.Contains(t, list, addr.String())
}
// assert default amount of funds were allocated to address during genesis
balance := cmdClient.RunSuccess(ctx, "wallet", "balance", fortest.TestAddresses[0].String()).ReadStdout()
assert.Equal(t, "1000000 FIL\n", balance)
}
func TestWalletExportImportRoundTrip(t *testing.T) {
tf.IntegrationTest(t)
ctx := context.Background()
builder := test.NewNodeBuilder(t)
n, cmdClient, done := builder.BuildAndStartAPI(ctx)
defer done()
addr, err := n.Wallet().API().WalletNewAddress(ctx, address.SECP256K1)
require.NoError(t, err)
// ./venus wallet ls
// eg:
// Address Balance Nonce Default
// t3wzm53n4ui4zdgwenf7jflrtsejgpsus7rswlkvbffxhdpkixpzfzidbvinrpnjx7dgvs72ilsnpiu7yjhela 0 FIL 0 X
result := cmdClient.RunSuccessLines(ctx, "wallet", "ls")
require.Len(t, result, 2) // include the header `Address Balance Nonce Default`
resultAddr := strings.Split(result[1], " ")[0]
require.Equal(t, addr.String(), resultAddr)
exportJSON := cmdClient.RunSuccess(ctx, "wallet", "export", resultAddr, string(wallet.TestPassword)).ReadStdoutTrimNewlines()
data, err := hex.DecodeString(exportJSON)
require.NoError(t, err)
var exportResult key.KeyInfo
err = json.Unmarshal(data, &exportResult)
require.NoError(t, err)
wf, err := os.Create("walletFileTest")
require.NoError(t, err)
defer func() {
require.NoError(t, os.Remove("walletFileTest"))
}()
keyInfoByte, err := json.Marshal(exportResult)
require.NoError(t, err)
_, err = wf.WriteString(hex.EncodeToString(keyInfoByte))
require.NoError(t, err)
require.NoError(t, wf.Close())
importResult := cmdClient.RunSuccessFirstLine(ctx, "wallet", "import", wf.Name())
assert.Equal(t, resultAddr, importResult)
}