-
Notifications
You must be signed in to change notification settings - Fork 465
/
node.go
212 lines (178 loc) · 5.59 KB
/
node.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package test
import (
"context"
"math/rand"
"testing"
blockstore "github.com/ipfs/boxo/blockstore"
ds "github.com/ipfs/go-datastore"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/require"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/venus/app/node"
"github.com/filecoin-project/venus/fixtures/fortest"
"github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/pkg/util/ffiwrapper/impl"
"github.com/filecoin-project/venus/pkg/wallet"
gengen "github.com/filecoin-project/venus/tools/gengen/util"
blockstoreutil "github.com/filecoin-project/venus/venus-shared/blockstore"
"github.com/filecoin-project/venus/venus-shared/types"
)
// ChainSeed is a generalized struct for configuring node
type ChainSeed struct {
info *gengen.RenderedGenInfo
bstore blockstoreutil.Blockstore
}
// MakeChainSeed creates a chain seed struct (see above) from a given
// genesis config
func MakeChainSeed(t *testing.T, cfg *gengen.GenesisCfg) *ChainSeed {
t.Helper()
mds := ds.NewMapDatastore()
bstore := blockstoreutil.Adapt(blockstore.NewBlockstore(mds))
info, err := gengen.GenGen(context.TODO(), cfg, bstore)
require.NoError(t, err)
return &ChainSeed{
info: info,
bstore: bstore,
}
}
// GenesisInitFunc is a th.GenesisInitFunc using the chain seed
func (cs *ChainSeed) GenesisInitFunc(cst cbor.IpldStore, bs blockstoreutil.Blockstore) (*types.BlockHeader, error) {
err := blockstoreutil.CopyBlockstore(context.TODO(), cs.bstore, bs)
if err != nil {
return nil, err
}
var blk types.BlockHeader
if err := cst.Get(context.TODO(), cs.info.GenesisCid, &blk); err != nil {
return nil, err
}
return &blk, nil
}
// GiveKey gives the given key to the given node
func (cs *ChainSeed) GiveKey(ctx context.Context, t *testing.T, nd *node.Node, key int) address.Address {
t.Helper()
bcks := nd.Wallet().Wallet.Backends(wallet.DSBackendType)
require.Len(t, bcks, 1, "expected to get exactly one datastore backend")
dsb := bcks[0].(*wallet.DSBackend)
_ = dsb.SetPassword(ctx, wallet.TestPassword)
kinfo := cs.info.Keys[key]
require.NoError(t, dsb.ImportKey(ctx, kinfo))
addr, err := kinfo.Address()
require.NoError(t, err)
return addr
}
// GiveMiner gives the specified miner to the node. Returns the address and the owner addresss
func (cs *ChainSeed) GiveMiner(t *testing.T, nd *node.Node, which int) (address.Address, address.Address) {
t.Helper()
cfg := nd.Repo().Config()
m := cs.info.Miners[which]
require.NoError(t, nd.Repo().ReplaceConfig(cfg))
ownerAddr, err := cs.info.Keys[m.Owner].Address()
require.NoError(t, err)
return m.Address, ownerAddr
}
// Addr returns the address for the given key
func (cs *ChainSeed) Addr(t *testing.T, key int) address.Address {
t.Helper()
k := cs.info.Keys[key]
a, err := k.Address()
if err != nil {
t.Fatal(err)
}
return a
}
// FixtureChainSeed returns the genesis function that
func FixtureChainSeed(t *testing.T) *ChainSeed {
return MakeChainSeed(t, &fortest.TestGenGenConfig)
}
// DefaultAddressConfigOpt is a node config option setting the default address
func DefaultAddressConfigOpt(addr address.Address) node.ConfigOpt {
return func(cfg *config.Config) {
cfg.Wallet.DefaultAddress = addr
}
}
// ConnectNodes connects two nodes together
func ConnectNodes(t *testing.T, a, b *node.Node) {
t.Helper()
pi := peer.AddrInfo{
ID: b.Network().Host.ID(),
Addrs: b.Network().Host.Addrs(),
}
err := a.Network().Host.Connect(context.TODO(), pi)
if err != nil {
t.Fatal(err)
}
}
// FakeProofVerifierBuilderOpts returns default configuration for testing
func FakeProofVerifierBuilderOpts() []node.BuilderOpt {
return []node.BuilderOpt{
node.VerifierConfigOption(&impl.FakeVerifier{}),
}
}
// StartNodes starts some nodes, failing on any error.
func StartNodes(t *testing.T, nds []*node.Node) {
t.Helper()
for _, nd := range nds {
if err := nd.Start(context.Background()); err != nil {
t.Fatal(err)
}
}
}
// StopNodes initiates shutdown of some nodes.
func StopNodes(nds []*node.Node) {
for _, nd := range nds {
nd.Stop(context.Background())
}
}
// MustCreateStorageMinerResult contains the result of a CreateStorageMiner command
type MustCreateStorageMinerResult struct {
MinerAddress *address.Address
Err error
}
// PeerKeys are a list of keys for peers that can be used in testing.
var PeerKeys = []crypto.PrivKey{
mustGenKey(101),
mustGenKey(102),
}
// MakeTestGenCfg returns a genesis configuration used for tests.
// This config has one miner with numSectors sectors and two accounts,
// the first is the miner's owner/worker and the accounts both have 10000 FIL
func MakeTestGenCfg(t *testing.T, numSectors int) *gengen.GenesisCfg {
commCfgs, err := gengen.MakeCommitCfgs(numSectors)
require.NoError(t, err)
return &gengen.GenesisCfg{
KeysToGen: 2,
Miners: []*gengen.CreateStorageMinerConfig{
{
Owner: 0,
PeerID: mustPeerID(PeerKeys[0]).String(),
CommittedSectors: commCfgs,
SealProofType: constants.DevSealProofType,
MarketBalance: abi.NewTokenAmount(0),
},
},
Network: "gfctest",
PreallocatedFunds: []string{
"10000",
"10000",
},
}
}
func mustGenKey(seed int64) crypto.PrivKey {
r := rand.New(rand.NewSource(seed))
priv, _, err := crypto.GenerateEd25519Key(r)
if err != nil {
panic(err)
}
return priv
}
func mustPeerID(k crypto.PrivKey) peer.ID {
pid, err := peer.IDFromPrivateKey(k)
if err != nil {
panic(err)
}
return pid
}