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

use temporary homedir for filecoin devnet #81

Merged
merged 3 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
use temporary homedir for filecoin devnet
  • Loading branch information
nonsense committed Nov 25, 2021
commit 686d9c5823c79275fa0b9d2f454eeb58c2563195
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ jobs:
default: golang
go-test-flags:
type: string
default: "-v --tags=debug -timeout 5m"
default: "-v --tags=debug -timeout 4m"
description: Flags passed to go test.
target:
type: string
Expand Down
7 changes: 6 additions & 1 deletion cmd/devnet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

home, err := os.UserHomeDir()
if err != nil {
panic(err)
}

done := make(chan struct{})
go devnet.Run(ctx, done)
go devnet.Run(ctx, home, done)

// setup a signal handler to cancel the context
interrupt := make(chan os.Signal, 1)
Expand Down
38 changes: 27 additions & 11 deletions itests/dummydeal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -65,8 +66,13 @@ func TestDummydeal(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

tempHome, err := ioutil.TempDir(os.TempDir(), "boost-devnet-")
nonsense marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(err)
}

done := make(chan struct{})
go devnet.Run(ctx, done)
go devnet.Run(ctx, tempHome, done)

// Wait for the miner to start up by polling it
minerReadyCmd := "lotus-miner sectors list"
Expand All @@ -79,6 +85,7 @@ func TestDummydeal(t *testing.T) {
}

cmd := exec.CommandContext(ctx, "sh", "-c", minerReadyCmd)
cmd.Env = []string{fmt.Sprintf("HOME=%s", tempHome)}
_, err := cmd.CombinedOutput()
if err != nil {
// Still not ready
Expand All @@ -94,7 +101,7 @@ func TestDummydeal(t *testing.T) {
break
}

f := newTestFramework(ctx, t)
f := newTestFramework(ctx, t, tempHome)
f.start()

// Create a CAR file
Expand Down Expand Up @@ -128,28 +135,30 @@ func TestDummydeal(t *testing.T) {
}

type testFramework struct {
ctx context.Context
t *testing.T
stop func()
ctx context.Context
t *testing.T
homedir string
stop func()

boost api.Boost
fullNode lapi.FullNode
clientAddr address.Address
minerAddr address.Address
}

func newTestFramework(ctx context.Context, t *testing.T) *testFramework {
func newTestFramework(ctx context.Context, t *testing.T, homedir string) *testFramework {
return &testFramework{
ctx: ctx,
t: t,
ctx: ctx,
t: t,
homedir: homedir,
}
}

func (f *testFramework) start() {
addr := "ws://127.0.0.1:1234/rpc/v1"

// Get a FullNode API
fullnodeApiString, err := devnet.GetFullnodeEndpoint(f.ctx)
fullnodeApiString, err := devnet.GetFullnodeEndpoint(f.ctx, f.homedir)
require.NoError(f.t, err)

apiinfo := cliutil.ParseApiInfo(fullnodeApiString)
Expand All @@ -162,7 +171,14 @@ func (f *testFramework) start() {
err = fullnodeApi.LogSetLevel(f.ctx, "actors", "DEBUG")
require.NoError(f.t, err)

// Get the default wallet from the devnet daemon
wallets, err := fullnodeApi.WalletList(f.ctx)
require.NoError(f.t, err)

// Set the default wallet for the devnet daemon
err = fullnodeApi.WalletSetDefault(f.ctx, wallets[0])
require.NoError(f.t, err)

// Make sure that default wallet has been setup successfully
defaultWallet, err := fullnodeApi.WalletDefaultAddress(f.ctx)
require.NoError(f.t, err)

Expand Down Expand Up @@ -204,7 +220,7 @@ func (f *testFramework) start() {

f.clientAddr = clientAddr

minerEndpoint, err := devnet.GetMinerEndpoint(f.ctx)
minerEndpoint, err := devnet.GetMinerEndpoint(f.ctx, f.homedir)
require.NoError(f.t, err)

minerApiInfo := cliutil.ParseApiInfo(minerEndpoint)
Expand Down
83 changes: 43 additions & 40 deletions pkg/devnet/devnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package devnet
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
Expand All @@ -15,9 +16,11 @@ import (

var log = logging.Logger("devnet")

func Run(ctx context.Context, done chan struct{}) {
func Run(ctx context.Context, tempHome string, done chan struct{}) {
var wg sync.WaitGroup

log.Debugw("using temp home dir", "dir", tempHome)

// The parameter files can be as large as 1GiB.
// If this is the first time lotus runs,
// and the machine doesn't have particularly fast internet,
Expand All @@ -31,7 +34,7 @@ func Run(ctx context.Context, done chan struct{}) {

log.Debugw("lotus fetch-params 8388608")
cmd := exec.CommandContext(ctx, "lotus", "fetch-params", "8338608")
cmd.Env = append(os.Environ(), "GOLOG_LOG_LEVEL=error")
cmd.Env = []string{fmt.Sprintf("HOME=%s", tempHome), "GOLOG_LOG_LEVEL=error"}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
Expand All @@ -40,40 +43,36 @@ func Run(ctx context.Context, done chan struct{}) {
cancel()
}

home, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

wg.Add(4)
go func() {
runLotusDaemon(ctx, home)
runLotusDaemon(ctx, tempHome)
log.Debugw("shut down lotus daemon")
wg.Done()
}()

go func() {
runLotusMiner(ctx, home)
runLotusMiner(ctx, tempHome)
log.Debugw("shut down lotus miner")
wg.Done()
}()

go func() {
publishDealsPeriodicallyCmd(ctx)
publishDealsPeriodicallyCmd(ctx, tempHome)
wg.Done()
}()

go func() {
setDefaultWalletCmd(ctx)
wg.Done()
}()
//TODO: Fix setDefaultWalletCmd to work with a temporary $HOME
//go func() {
//setDefaultWalletCmd(ctx, tempHome)
//wg.Done()
//}()

wg.Wait()

done <- struct{}{}
}

func runCmdsWithLog(ctx context.Context, name string, commands [][]string) {
func runCmdsWithLog(ctx context.Context, name string, commands [][]string, homeDir string) {
logFile, err := os.Create(name + ".log")
if err != nil {
log.Fatal(err)
Expand All @@ -85,6 +84,7 @@ func runCmdsWithLog(ctx context.Context, name string, commands [][]string) {
cmd := exec.CommandContext(ctx, cmdArgs[0], cmdArgs[1:]...)
cmd.Stdout = logFile
cmd.Stderr = logFile
cmd.Env = []string{fmt.Sprintf("HOME=%s", homeDir)}
// If ctx.Err()!=nil, we cancelled the command via SIGINT.
if err := cmd.Run(); err != nil && ctx.Err() == nil {
log.Errorw("check logfile for details", "err", err, "logfile", logFile.Name())
Expand All @@ -103,7 +103,7 @@ func runLotusDaemon(ctx context.Context, home string) {
"--genesis-template=localnet.json", "--bootstrap=false"},
}

runCmdsWithLog(ctx, "lotus-daemon", cmds)
runCmdsWithLog(ctx, "lotus-daemon", cmds, home)
}

func runLotusMiner(ctx context.Context, home string) {
Expand Down Expand Up @@ -131,10 +131,10 @@ func runLotusMiner(ctx context.Context, home string) {
{"lotus-miner", "run", "--nosync"},
}

runCmdsWithLog(ctx, "lotus-miner", cmds)
runCmdsWithLog(ctx, "lotus-miner", cmds, home)
}

func publishDealsPeriodicallyCmd(ctx context.Context) {
func publishDealsPeriodicallyCmd(ctx context.Context, homeDir string) {
for {
select {
case <-ctx.Done():
Expand All @@ -144,37 +144,39 @@ func publishDealsPeriodicallyCmd(ctx context.Context) {

cmd := exec.CommandContext(ctx, "lotus-miner",
"storage-deals", "pending-publish", "--publish-now")
cmd.Env = []string{fmt.Sprintf("HOME=%s", homeDir)}
_ = cmd.Run() // we ignore errors
}
}

func setDefaultWalletCmd(ctx context.Context) {
// TODO: do this without a shell
setDefaultWalletCmd := "lotus wallet list | grep t3 | awk '{print $1}' | xargs lotus wallet set-default"

for {
select {
case <-ctx.Done():
return
case <-time.After(5 * time.Second):
}

cmd := exec.CommandContext(ctx, "sh", "-c", setDefaultWalletCmd)
_, err := cmd.CombinedOutput()
if err != nil {
continue
}
// TODO: stop once we've set the default wallet once.
}
}

func GetMinerEndpoint(ctx context.Context) (string, error) {
//func setDefaultWalletCmd(ctx context.Context, _ string) {
//// TODO: do this without a shell
//setDefaultWalletCmd := "lotus wallet list | grep t3 | awk '{print $1}' | xargs lotus wallet set-default"

//for {
//select {
//case <-ctx.Done():
//return
//case <-time.After(5 * time.Second):
//}

//cmd := exec.CommandContext(ctx, "sh", "-c", setDefaultWalletCmd)
//_, err := cmd.CombinedOutput()
//if err != nil {
//continue
//}
//// TODO: stop once we've set the default wallet once.
//}
//}

func GetMinerEndpoint(ctx context.Context, homedir string) (string, error) {
cmdArgs := []string{"lotus-miner", "auth", "api-info", "--perm=admin"}

var out bytes.Buffer

log.Debugw("getting auth token", "command", strings.Join(cmdArgs, " "))
cmd := exec.CommandContext(ctx, cmdArgs[0], cmdArgs[1:]...)
cmd.Env = []string{fmt.Sprintf("HOME=%s", homedir)}
cmd.Stdout = &out
cmd.Stderr = &out
if err := cmd.Run(); err != nil {
Expand All @@ -187,13 +189,14 @@ func GetMinerEndpoint(ctx context.Context) (string, error) {
return ai, nil
}

func GetFullnodeEndpoint(ctx context.Context) (string, error) {
func GetFullnodeEndpoint(ctx context.Context, homedir string) (string, error) {
cmdArgs := []string{"lotus", "auth", "api-info", "--perm=admin"}

var out bytes.Buffer

log.Debugw("getting auth token", "command", strings.Join(cmdArgs, " "))
cmd := exec.CommandContext(ctx, cmdArgs[0], cmdArgs[1:]...)
cmd.Env = []string{fmt.Sprintf("HOME=%s", homedir)}
cmd.Stdout = &out
cmd.Stderr = &out
if err := cmd.Run(); err != nil {
Expand Down