lotus/api/test/deals.go
2019-11-07 00:39:34 +01:00

85 lines
1.8 KiB
Go

package test
import (
"context"
"fmt"
"io"
"math/rand"
"testing"
"time"
logging "github.com/ipfs/go-log"
"github.com/filecoin-project/lotus/api"
"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) {
logging.SetAllLoggers(logging.LevelInfo)
ctx := context.TODO()
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)), 350)
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)
go func() {
for i := 0; i < 4; i++ {
time.Sleep(time.Second)
fmt.Println("mining a block now", i)
if err := n[0].MineOne(ctx); err != nil {
t.Fatal(err)
}
}
}()
deal, err := client.ClientStartDeal(ctx, fcid, maddr, types.NewInt(400), 100)
if err != nil {
t.Fatal(err)
}
// TODO: this sleep is only necessary because deals don't immediately get logged in the dealstore, we should fix this
time.Sleep(time.Second)
for {
di, err := client.ClientGetDealInfo(ctx, *deal)
if err != nil {
t.Fatal(err)
}
switch di.State {
case api.DealRejected:
t.Fatal("deal rejected")
case api.DealFailed:
t.Fatal("deal failed")
case api.DealError:
t.Fatal("deal errored")
case api.DealComplete:
fmt.Println("COMPLETE", di)
break
}
fmt.Println("Deal state: ", di.State)
time.Sleep(time.Second / 2)
}
}