2019-11-06 16:10:44 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2019-12-01 21:52:24 +00:00
|
|
|
"bytes"
|
2019-11-06 16:10:44 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-12-01 21:52:24 +00:00
|
|
|
"io/ioutil"
|
2019-11-06 16:10:44 +00:00
|
|
|
"math/rand"
|
2019-11-07 00:18:06 +00:00
|
|
|
"os"
|
2019-12-01 21:52:24 +00:00
|
|
|
"path/filepath"
|
2019-11-06 16:10:44 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-11-06 19:44:28 +00:00
|
|
|
|
2020-01-24 21:44:28 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/storagemarket"
|
2020-03-04 05:03:35 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2019-11-30 23:17:50 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-11-06 16:10:44 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/node/impl"
|
|
|
|
)
|
|
|
|
|
2019-11-30 23:17:50 +00:00
|
|
|
func init() {
|
|
|
|
logging.SetAllLoggers(logging.LevelInfo)
|
|
|
|
build.InsecurePoStValidation = true
|
|
|
|
}
|
|
|
|
|
2020-01-14 02:51:14 +00:00
|
|
|
func TestDealFlow(t *testing.T, b APIBuilder, blocktime time.Duration) {
|
2019-11-07 00:18:06 +00:00
|
|
|
os.Setenv("BELLMAN_NO_GPU", "1")
|
|
|
|
|
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)
|
|
|
|
|
2020-02-28 00:12:52 +00:00
|
|
|
data := make([]byte, 1600)
|
2019-12-02 14:26:39 +00:00
|
|
|
rand.New(rand.NewSource(5)).Read(data)
|
|
|
|
|
2019-12-01 21:52:24 +00:00
|
|
|
r := bytes.NewReader(data)
|
2019-11-06 16:10:44 +00:00
|
|
|
fcid, err := client.ClientImportLocal(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-11-30 23:17:50 +00:00
|
|
|
maddr, err := miner.ActorAddress(ctx)
|
2019-11-06 16:10:44 +00:00
|
|
|
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 {
|
2020-01-14 02:51:14 +00:00
|
|
|
time.Sleep(blocktime)
|
2019-11-30 23:17:50 +00:00
|
|
|
if err := sn[0].MineOne(ctx); err != nil {
|
2019-12-05 05:14:19 +00:00
|
|
|
t.Error(err)
|
2019-11-06 19:44:28 +00:00
|
|
|
}
|
2019-11-06 16:10:44 +00:00
|
|
|
}
|
|
|
|
}()
|
2019-12-13 19:15:56 +00:00
|
|
|
addr, err := client.WalletDefaultAddress(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-04 05:03:35 +00:00
|
|
|
deal, err := client.ClientStartDeal(ctx, &api.StartDealParams{
|
|
|
|
Data: &storagemarket.DataRef{Root: fcid},
|
|
|
|
Wallet: addr,
|
|
|
|
Miner: maddr,
|
|
|
|
EpochPrice: types.NewInt(1000000),
|
|
|
|
BlocksDuration: 100,
|
|
|
|
})
|
2019-11-06 16:10:44 +00:00
|
|
|
if err != nil {
|
2020-02-23 15:50:36 +00:00
|
|
|
t.Fatalf("%+v", err)
|
2019-11-06 16:10:44 +00:00
|
|
|
}
|
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)
|
2019-11-07 09:06:06 +00:00
|
|
|
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 {
|
2020-01-24 21:44:28 +00:00
|
|
|
case storagemarket.StorageDealProposalRejected:
|
2019-11-06 20:39:07 +00:00
|
|
|
t.Fatal("deal rejected")
|
2020-01-24 21:44:28 +00:00
|
|
|
case storagemarket.StorageDealFailing:
|
2019-11-06 20:39:07 +00:00
|
|
|
t.Fatal("deal failed")
|
2020-01-24 21:44:28 +00:00
|
|
|
case storagemarket.StorageDealError:
|
2019-11-06 20:54:40 +00:00
|
|
|
t.Fatal("deal errored")
|
2020-01-24 21:44:28 +00:00
|
|
|
case storagemarket.StorageDealActive:
|
2019-11-06 20:39:07 +00:00
|
|
|
fmt.Println("COMPLETE", di)
|
2019-11-07 00:18:06 +00:00
|
|
|
break loop
|
2019-11-06 20:39:07 +00:00
|
|
|
}
|
2020-01-24 21:44:28 +00:00
|
|
|
fmt.Println("Deal state: ", storagemarket.DealStates[di.State])
|
2019-11-06 19:44:28 +00:00
|
|
|
time.Sleep(time.Second / 2)
|
|
|
|
}
|
2019-11-07 00:18:06 +00:00
|
|
|
|
2019-12-01 21:52:24 +00:00
|
|
|
// Retrieval
|
|
|
|
|
|
|
|
offers, err := client.ClientFindData(ctx, fcid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(offers) < 1 {
|
|
|
|
t.Fatal("no offers")
|
|
|
|
}
|
|
|
|
|
|
|
|
rpath, err := ioutil.TempDir("", "lotus-retrieve-test-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(rpath)
|
|
|
|
|
|
|
|
caddr, err := client.WalletDefaultAddress(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-03-03 04:13:08 +00:00
|
|
|
ref := api.FileRef{
|
|
|
|
Path: filepath.Join(rpath, "ret"),
|
|
|
|
IsCAR: false,
|
|
|
|
}
|
|
|
|
err = client.ClientRetrieve(ctx, offers[0].Order(caddr), ref)
|
2019-12-01 21:52:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rdata, err := ioutil.ReadFile(filepath.Join(rpath, "ret"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(rdata, data) {
|
|
|
|
t.Fatal("wrong data retrieved")
|
|
|
|
}
|
|
|
|
|
2019-11-07 00:18:06 +00:00
|
|
|
mine = false
|
|
|
|
fmt.Println("shutting down mining")
|
|
|
|
<-done
|
2019-11-06 16:10:44 +00:00
|
|
|
}
|