forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathldevnet.go
More file actions
100 lines (87 loc) · 2.96 KB
/
ldevnet.go
File metadata and controls
100 lines (87 loc) · 2.96 KB
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
package tests
import (
"context"
"fmt"
"os"
"strconv"
"time"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/chain/types"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/stretchr/testify/require"
"github.com/textileio/powergate/lotus"
"github.com/textileio/powergate/util"
)
// TestingTWithCleanup is an augmented require.TestingT with a Cleanup function.
type TestingTWithCleanup interface {
require.TestingT
Cleanup(func())
}
// LaunchDevnetDocker launches the devnet docker image.
func LaunchDevnetDocker(t TestingTWithCleanup, numMiners int, ipfsMaddr string, mountVolumes bool) *dockertest.Resource {
pool, err := dockertest.NewPool("")
require.NoError(t, err)
envs := []string{
devnetEnv("NUMMINERS", strconv.Itoa(numMiners)),
devnetEnv("SPEED", "300"),
devnetEnv("IPFSADDR", ipfsMaddr),
devnetEnv("BIGSECTORS", false),
}
var mounts []string
if mountVolumes {
mounts = append(mounts, "/tmp/powergate:/tmp/powergate")
}
repository := "textile/lotus-devnet"
tag := "sha-ce1d788"
lotusDevnet, err := pool.RunWithOptions(&dockertest.RunOptions{Repository: repository, Tag: tag, Env: envs, Mounts: mounts})
require.NoError(t, err)
err = lotusDevnet.Expire(180)
require.NoError(t, err)
time.Sleep(time.Second * time.Duration(2+numMiners))
t.Cleanup(func() {
err := pool.Purge(lotusDevnet)
require.NoError(t, err)
})
debug := false
if debug {
go func() {
opts := docker.LogsOptions{
Context: context.Background(),
Stderr: true,
Stdout: true,
Follow: true,
Timestamps: true,
RawTerminal: true,
Container: lotusDevnet.Container.ID,
OutputStream: os.Stdout,
}
err := pool.Client.Logs(opts)
require.NoError(t, err)
}()
}
return lotusDevnet
}
// CreateLocalDevnetWithIPFS creates a local devnet connected to an IPFS node.
func CreateLocalDevnetWithIPFS(t TestingTWithCleanup, numMiners int, ipfsMaddr string, mountVolumes bool) (lotus.ClientBuilder, address.Address, []address.Address) {
lotusDevnet := LaunchDevnetDocker(t, numMiners, ipfsMaddr, mountVolumes)
cb, err := lotus.NewBuilder(util.MustParseAddr("/ip4/127.0.0.1/tcp/"+lotusDevnet.GetPort("7777/tcp")), "", 1)
require.NoError(t, err)
c, cls, err := cb()
require.NoError(t, err)
t.Cleanup(func() { cls() })
ctx := context.Background()
addr, err := c.WalletDefaultAddress(ctx)
require.NoError(t, err)
miners, err := c.StateListMiners(ctx, types.EmptyTSK)
require.NoError(t, err)
return cb, addr, miners
}
// CreateLocalDevnet returns an API client that targets a local devnet with numMiners number
// of miners. Refer to http://github.com/textileio/local-devnet for more information.
func CreateLocalDevnet(t TestingTWithCleanup, numMiners int) (lotus.ClientBuilder, address.Address, []address.Address) {
return CreateLocalDevnetWithIPFS(t, numMiners, "", true)
}
func devnetEnv(name string, value interface{}) string {
return fmt.Sprintf("TEXLOTUSDEVNET_%s=%s", name, value)
}