lotus/itests/deals_test.go

429 lines
11 KiB
Go
Raw Normal View History

package itests
import (
"bytes"
"context"
"fmt"
"math/rand"
"sync/atomic"
"testing"
"time"
"github.com/filecoin-project/go-fil-markets/storagemarket"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
"github.com/filecoin-project/lotus/chain/actors/policy"
"github.com/filecoin-project/lotus/chain/types"
2021-05-18 21:01:10 +00:00
"github.com/filecoin-project/lotus/itests/kit"
"github.com/filecoin-project/lotus/markets/storageadapter"
"github.com/filecoin-project/lotus/miner"
"github.com/filecoin-project/lotus/node"
"github.com/filecoin-project/lotus/node/impl"
market2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/market"
"github.com/stretchr/testify/require"
)
func TestDealCycle(t *testing.T) {
2021-05-18 21:01:10 +00:00
kit.QuietMiningLogs()
blockTime := 10 * time.Millisecond
// For these tests where the block time is artificially short, just use
// a deal start epoch that is guaranteed to be far enough in the future
// so that the deal starts sealing in time
dealStartEpoch := abi.ChainEpoch(2 << 12)
t.Run("TestFullDealCycle_Single", func(t *testing.T) {
2021-05-20 11:17:41 +00:00
runFullDealCycles(t, 1, kit.MockMinerBuilder, blockTime, false, false, dealStartEpoch)
})
t.Run("TestFullDealCycle_Two", func(t *testing.T) {
2021-05-20 11:17:41 +00:00
runFullDealCycles(t, 2, kit.MockMinerBuilder, blockTime, false, false, dealStartEpoch)
})
t.Run("WithExportedCAR", func(t *testing.T) {
2021-05-20 11:17:41 +00:00
runFullDealCycles(t, 1, kit.MockMinerBuilder, blockTime, true, false, dealStartEpoch)
})
t.Run("TestFastRetrievalDealCycle", func(t *testing.T) {
2021-05-20 11:17:41 +00:00
runFastRetrievalDealFlowT(t, kit.MockMinerBuilder, blockTime, dealStartEpoch)
2021-05-18 21:01:10 +00:00
})
t.Run("TestZeroPricePerByteRetrievalDealFlow", func(t *testing.T) {
2021-05-20 11:17:41 +00:00
runZeroPricePerByteRetrievalDealFlow(t, kit.MockMinerBuilder, blockTime, dealStartEpoch)
})
}
func TestAPIDealFlowReal(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
2021-05-18 21:01:10 +00:00
kit.QuietMiningLogs()
// TODO: just set this globally?
oldDelay := policy.GetPreCommitChallengeDelay()
policy.SetPreCommitChallengeDelay(5)
t.Cleanup(func() {
policy.SetPreCommitChallengeDelay(oldDelay)
})
t.Run("basic", func(t *testing.T) {
2021-05-18 21:01:10 +00:00
runFullDealCycles(t, 1, kit.Builder, time.Second, false, false, 0)
})
t.Run("fast-retrieval", func(t *testing.T) {
2021-05-18 21:01:10 +00:00
runFullDealCycles(t, 1, kit.Builder, time.Second, false, true, 0)
})
t.Run("retrieval-second", func(t *testing.T) {
2021-05-18 21:01:10 +00:00
runSecondDealRetrievalTest(t, kit.Builder, time.Second)
})
}
func TestPublishDealsBatching(t *testing.T) {
2021-05-20 15:12:42 +00:00
ctx := context.Background()
2021-05-18 21:01:10 +00:00
kit.QuietMiningLogs()
2021-05-20 11:17:41 +00:00
b := kit.MockMinerBuilder
blocktime := 10 * time.Millisecond
startEpoch := abi.ChainEpoch(2 << 12)
publishPeriod := 10 * time.Second
maxDealsPerMsg := uint64(2)
// Set max deals per publish deals message to 2
2021-05-18 21:01:10 +00:00
minerDef := []kit.StorageMiner{{
Full: 0,
Opts: node.Override(
new(*storageadapter.DealPublisher),
storageadapter.NewDealPublisher(nil, storageadapter.PublishMsgConfig{
Period: publishPeriod,
MaxDealsPerMsg: maxDealsPerMsg,
})),
2021-05-18 21:01:10 +00:00
Preseal: kit.PresealGenesis,
}}
// Create a connect client and miner node
2021-05-18 21:01:10 +00:00
n, sn := b(t, kit.OneFull, minerDef)
client := n[0].FullNode.(*impl.FullNodeAPI)
miner := sn[0]
2021-05-20 15:12:42 +00:00
kit.ConnectAndStartMining(t, blocktime, miner, client)
dh := kit.NewDealHarness(t, client, miner)
// Starts a deal and waits until it's published
runDealTillPublish := func(rseed int) {
2021-05-20 15:12:42 +00:00
res, _, err := kit.CreateImportFile(ctx, client, rseed)
require.NoError(t, err)
2021-05-20 15:12:42 +00:00
upds, err := client.ClientGetDealUpdates(ctx)
require.NoError(t, err)
2021-05-20 15:12:42 +00:00
dh.StartDeal(ctx, res.Root, false, startEpoch)
// TODO: this sleep is only necessary because deals don't immediately get logged in the dealstore, we should fix this
time.Sleep(time.Second)
done := make(chan struct{})
go func() {
for upd := range upds {
if upd.DataRef.Root == res.Root && upd.State == storagemarket.StorageDealAwaitingPreCommit {
done <- struct{}{}
}
}
}()
<-done
}
// Run three deals in parallel
done := make(chan struct{}, maxDealsPerMsg+1)
for rseed := 1; rseed <= 3; rseed++ {
rseed := rseed
go func() {
runDealTillPublish(rseed)
done <- struct{}{}
}()
}
// Wait for two of the deals to be published
for i := 0; i < int(maxDealsPerMsg); i++ {
<-done
}
// Expect a single PublishStorageDeals message that includes the first two deals
2021-05-20 15:12:42 +00:00
msgCids, err := client.StateListMessages(ctx, &api.MessageMatch{To: market.Address}, types.EmptyTSK, 1)
require.NoError(t, err)
count := 0
for _, msgCid := range msgCids {
2021-05-20 15:12:42 +00:00
msg, err := client.ChainGetMessage(ctx, msgCid)
require.NoError(t, err)
if msg.Method == market.Methods.PublishStorageDeals {
count++
var pubDealsParams market2.PublishStorageDealsParams
err = pubDealsParams.UnmarshalCBOR(bytes.NewReader(msg.Params))
require.NoError(t, err)
require.Len(t, pubDealsParams.Deals, int(maxDealsPerMsg))
}
}
require.Equal(t, 1, count)
// The third deal should be published once the publish period expires.
// Allow a little padding as it takes a moment for the state change to
// be noticed by the client.
padding := 10 * time.Second
select {
case <-time.After(publishPeriod + padding):
require.Fail(t, "Expected 3rd deal to be published once publish period elapsed")
case <-done: // Success
}
}
func TestDealMining(t *testing.T) {
// test making a deal with a fresh miner, and see if it starts to mine.
if testing.Short() {
t.Skip("skipping test in short mode")
}
2021-05-18 21:01:10 +00:00
kit.QuietMiningLogs()
2021-05-20 11:17:41 +00:00
b := kit.MockMinerBuilder
blocktime := 50 * time.Millisecond
ctx := context.Background()
2021-05-20 15:12:42 +00:00
fulls, miners := b(t,
kit.OneFull,
[]kit.StorageMiner{
{Full: 0, Preseal: kit.PresealGenesis},
{Full: 0, Preseal: 0}, // TODO: Add support for miners on non-first full node
})
client := fulls[0].FullNode.(*impl.FullNodeAPI)
genesisMiner := miners[0]
provider := miners[1]
addrinfo, err := client.NetAddrsListen(ctx)
if err != nil {
t.Fatal(err)
}
if err := provider.NetConnect(ctx, addrinfo); err != nil {
t.Fatal(err)
}
if err := genesisMiner.NetConnect(ctx, addrinfo); err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)
data := make([]byte, 600)
rand.New(rand.NewSource(5)).Read(data)
r := bytes.NewReader(data)
fcid, err := client.ClientImportLocal(ctx, r)
if err != nil {
t.Fatal(err)
}
fmt.Println("FILE CID: ", fcid)
var mine int32 = 1
done := make(chan struct{})
minedTwo := make(chan struct{})
2021-05-20 15:12:42 +00:00
m2addr, err := miners[1].ActorAddress(context.TODO())
if err != nil {
t.Fatal(err)
}
go func() {
defer close(done)
complChan := minedTwo
for atomic.LoadInt32(&mine) != 0 {
wait := make(chan int)
mdone := func(mined bool, _ abi.ChainEpoch, err error) {
n := 0
if mined {
n = 1
}
wait <- n
}
2021-05-20 15:12:42 +00:00
if err := miners[0].MineOne(ctx, miner.MineReq{Done: mdone}); err != nil {
t.Error(err)
}
2021-05-20 15:12:42 +00:00
if err := miners[1].MineOne(ctx, miner.MineReq{Done: mdone}); err != nil {
t.Error(err)
}
expect := <-wait
expect += <-wait
time.Sleep(blocktime)
if expect == 0 {
// null block
continue
}
var nodeOneMined bool
2021-05-20 15:12:42 +00:00
for _, node := range miners {
mb, err := node.MiningBase(ctx)
if err != nil {
t.Error(err)
return
}
for _, b := range mb.Blocks() {
if b.Miner == m2addr {
nodeOneMined = true
break
}
}
}
if nodeOneMined && complChan != nil {
close(complChan)
complChan = nil
}
}
}()
2021-05-20 15:12:42 +00:00
dh := kit.NewDealHarness(t, client, provider)
deal := dh.StartDeal(ctx, fcid, false, 0)
// TODO: this sleep is only necessary because deals don't immediately get logged in the dealstore, we should fix this
time.Sleep(time.Second)
2021-05-20 15:12:42 +00:00
dh.WaitDealSealed(ctx, deal, false)
<-minedTwo
atomic.StoreInt32(&mine, 0)
fmt.Println("shutting down mining")
<-done
}
2021-05-18 21:01:10 +00:00
func runFullDealCycles(t *testing.T, n int, b kit.APIBuilder, blocktime time.Duration, carExport, fastRet bool, startEpoch abi.ChainEpoch) {
2021-05-20 15:12:42 +00:00
fulls, miners := b(t, kit.OneFull, kit.OneMiner)
client, miner := fulls[0].FullNode.(*impl.FullNodeAPI), miners[0]
kit.ConnectAndStartMining(t, blocktime, miner, client)
dh := kit.NewDealHarness(t, client, miner)
baseseed := 6
for i := 0; i < n; i++ {
2021-05-20 15:12:42 +00:00
dh.MakeFullDeal(context.Background(), baseseed+i, carExport, fastRet, startEpoch)
2021-05-18 21:01:10 +00:00
}
}
func runFastRetrievalDealFlowT(t *testing.T, b kit.APIBuilder, blocktime time.Duration, startEpoch abi.ChainEpoch) {
2021-05-20 15:12:42 +00:00
ctx := context.Background()
fulls, miners := b(t, kit.OneFull, kit.OneMiner)
client, miner := fulls[0].FullNode.(*impl.FullNodeAPI), miners[0]
kit.ConnectAndStartMining(t, blocktime, miner, client)
dh := kit.NewDealHarness(t, client, miner)
2021-05-18 21:01:10 +00:00
data := make([]byte, 1600)
rand.New(rand.NewSource(int64(8))).Read(data)
r := bytes.NewReader(data)
2021-05-20 15:12:42 +00:00
fcid, err := client.ClientImportLocal(ctx, r)
2021-05-18 21:01:10 +00:00
if err != nil {
t.Fatal(err)
}
fmt.Println("FILE CID: ", fcid)
2021-05-20 15:12:42 +00:00
deal := dh.StartDeal(ctx, fcid, true, startEpoch)
dh.WaitDealPublished(ctx, deal)
2021-05-18 21:01:10 +00:00
fmt.Println("deal published, retrieving")
2021-05-20 15:12:42 +00:00
2021-05-18 21:01:10 +00:00
// Retrieval
2021-05-20 15:12:42 +00:00
info, err := client.ClientGetDealInfo(ctx, *deal)
2021-05-18 21:01:10 +00:00
require.NoError(t, err)
2021-05-20 15:12:42 +00:00
dh.TestRetrieval(ctx, fcid, &info.PieceCID, false, data)
2021-05-18 21:01:10 +00:00
}
func runSecondDealRetrievalTest(t *testing.T, b kit.APIBuilder, blocktime time.Duration) {
2021-05-20 15:12:42 +00:00
ctx := context.Background()
fulls, miners := b(t, kit.OneFull, kit.OneMiner)
client, miner := fulls[0].FullNode.(*impl.FullNodeAPI), miners[0]
kit.ConnectAndStartMining(t, blocktime, miner, client)
dh := kit.NewDealHarness(t, client, miner)
2021-05-18 21:01:10 +00:00
{
data1 := make([]byte, 800)
rand.New(rand.NewSource(int64(3))).Read(data1)
r := bytes.NewReader(data1)
2021-05-20 15:12:42 +00:00
fcid1, err := client.ClientImportLocal(ctx, r)
2021-05-18 21:01:10 +00:00
if err != nil {
t.Fatal(err)
}
data2 := make([]byte, 800)
rand.New(rand.NewSource(int64(9))).Read(data2)
r2 := bytes.NewReader(data2)
2021-05-20 15:12:42 +00:00
fcid2, err := client.ClientImportLocal(ctx, r2)
2021-05-18 21:01:10 +00:00
if err != nil {
t.Fatal(err)
}
2021-05-20 15:12:42 +00:00
deal1 := dh.StartDeal(ctx, fcid1, true, 0)
2021-05-18 21:01:10 +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)
2021-05-20 15:12:42 +00:00
dh.WaitDealSealed(ctx, deal1, true)
2021-05-18 21:01:10 +00:00
2021-05-20 15:12:42 +00:00
deal2 := dh.StartDeal(ctx, fcid2, true, 0)
2021-05-18 21:01:10 +00:00
time.Sleep(time.Second)
2021-05-20 15:12:42 +00:00
dh.WaitDealSealed(ctx, deal2, false)
2021-05-18 21:01:10 +00:00
// Retrieval
2021-05-20 15:12:42 +00:00
info, err := client.ClientGetDealInfo(ctx, *deal2)
2021-05-18 21:01:10 +00:00
require.NoError(t, err)
2021-05-20 15:12:42 +00:00
rf, _ := miner.SectorsRefs(ctx)
2021-05-18 21:01:10 +00:00
fmt.Printf("refs: %+v\n", rf)
2021-05-20 15:12:42 +00:00
dh.TestRetrieval(ctx, fcid2, &info.PieceCID, false, data2)
}
}
2021-05-18 21:01:10 +00:00
func runZeroPricePerByteRetrievalDealFlow(t *testing.T, b kit.APIBuilder, blocktime time.Duration, startEpoch abi.ChainEpoch) {
2021-05-20 15:12:42 +00:00
ctx := context.Background()
fulls, miners := b(t, kit.OneFull, kit.OneMiner)
client, miner := fulls[0].FullNode.(*impl.FullNodeAPI), miners[0]
kit.ConnectAndStartMining(t, blocktime, miner, client)
dh := kit.NewDealHarness(t, client, miner)
2021-05-18 21:01:10 +00:00
// Set price-per-byte to zero
2021-05-20 15:12:42 +00:00
ask, err := miner.MarketGetRetrievalAsk(ctx)
2021-05-18 21:01:10 +00:00
require.NoError(t, err)
ask.PricePerByte = abi.NewTokenAmount(0)
2021-05-20 15:12:42 +00:00
err = miner.MarketSetRetrievalAsk(ctx, ask)
2021-05-18 21:01:10 +00:00
require.NoError(t, err)
2021-05-20 15:12:42 +00:00
dh.MakeFullDeal(ctx, 6, false, false, startEpoch)
2021-05-18 21:01:10 +00:00
}