lotus/api/test/deals.go

97 lines
1.9 KiB
Go
Raw Normal View History

2019-11-06 16:10:44 +00:00
package test
import (
"context"
"fmt"
"io"
"math/rand"
2019-11-07 00:18:06 +00:00
"os"
2019-11-06 16:10:44 +00:00
"testing"
"time"
2019-11-06 19:44:28 +00:00
logging "github.com/ipfs/go-log"
2019-11-06 20:39:07 +00:00
"github.com/filecoin-project/lotus/api"
2019-11-06 16:10:44 +00:00
"github.com/filecoin-project/lotus/chain/address"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/node/impl"
)
func TestDealFlow(t *testing.T, b APIBuilder) {
2019-11-07 00:18:06 +00:00
os.Setenv("BELLMAN_NO_GPU", "1")
2019-11-06 19:44:28 +00:00
logging.SetAllLoggers(logging.LevelInfo)
2019-11-07 12:03:18 +00:00
ctx := context.Background()
2019-11-06 16:10:44 +00:00
n, sn := b(t, 1, []int{0})
client := n[0].FullNode.(*impl.FullNodeAPI)
miner := sn[0]
addrinfo, err := client.NetAddrsListen(ctx)
if err != nil {
t.Fatal(err)
}
if err := miner.NetConnect(ctx, addrinfo); err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)
r := io.LimitReader(rand.New(rand.NewSource(17)), 1000)
2019-11-06 16:10:44 +00:00
fcid, err := client.ClientImportLocal(ctx, r)
if err != nil {
t.Fatal(err)
}
maddr, err := address.NewFromString("t0101")
if err != nil {
t.Fatal(err)
}
fmt.Println("FILE CID: ", fcid)
2019-11-07 00:18:06 +00:00
mine := true
done := make(chan struct{})
2019-11-06 16:10:44 +00:00
go func() {
2019-11-07 00:18:06 +00:00
defer close(done)
for mine {
2019-11-06 19:44:28 +00:00
time.Sleep(time.Second)
2019-11-07 00:18:06 +00:00
fmt.Println("mining a block now")
2019-11-06 19:44:28 +00:00
if err := n[0].MineOne(ctx); err != nil {
t.Fatal(err)
}
2019-11-06 16:10:44 +00:00
}
}()
2019-11-07 19:54:24 +00:00
deal, err := client.ClientStartDeal(ctx, fcid, maddr, types.NewInt(40000000), 100)
2019-11-06 16:10:44 +00:00
if err != nil {
t.Fatal(err)
}
2019-11-06 19:44:28 +00:00
// TODO: this sleep is only necessary because deals don't immediately get logged in the dealstore, we should fix this
time.Sleep(time.Second)
loop:
2019-11-06 19:44:28 +00:00
for {
di, err := client.ClientGetDealInfo(ctx, *deal)
if err != nil {
t.Fatal(err)
}
2019-11-06 20:39:07 +00:00
switch di.State {
case api.DealRejected:
t.Fatal("deal rejected")
case api.DealFailed:
t.Fatal("deal failed")
2019-11-06 20:54:40 +00:00
case api.DealError:
t.Fatal("deal errored")
2019-11-06 20:39:07 +00:00
case api.DealComplete:
fmt.Println("COMPLETE", di)
2019-11-07 00:18:06 +00:00
break loop
2019-11-06 20:39:07 +00:00
}
2019-11-07 00:18:06 +00:00
fmt.Println("Deal state: ", api.DealStates[di.State])
2019-11-06 19:44:28 +00:00
time.Sleep(time.Second / 2)
}
2019-11-07 00:18:06 +00:00
mine = false
fmt.Println("shutting down mining")
<-done
2019-11-06 16:10:44 +00:00
}