2022-08-29 14:25:30 +00:00
|
|
|
// stm: #integration
|
2021-06-23 17:13:29 +00:00
|
|
|
package itests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-06-23 17:13:29 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/storagemarket"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-06-14 15:00:51 +00:00
|
|
|
market2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/market"
|
|
|
|
|
2021-06-23 17:13:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2022-06-14 17:21:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/wallet/key"
|
2021-06-23 17:13:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/itests/kit"
|
|
|
|
"github.com/filecoin-project/lotus/markets/storageadapter"
|
|
|
|
"github.com/filecoin-project/lotus/node"
|
2021-07-07 17:12:03 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
|
|
|
"github.com/filecoin-project/lotus/node/modules"
|
2022-06-14 17:32:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/ctladdr"
|
2021-06-23 17:13:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPublishDealsBatching(t *testing.T) {
|
2021-12-13 12:41:04 +00:00
|
|
|
//stm: @CHAIN_SYNCER_LOAD_GENESIS_001, @CHAIN_SYNCER_FETCH_TIPSET_001,
|
|
|
|
//stm: @CHAIN_SYNCER_START_001, @CHAIN_SYNCER_SYNC_001, @BLOCKCHAIN_BEACON_VALIDATE_BLOCK_VALUES_01
|
|
|
|
//stm: @CHAIN_SYNCER_COLLECT_CHAIN_001, @CHAIN_SYNCER_COLLECT_HEADERS_001, @CHAIN_SYNCER_VALIDATE_TIPSET_001
|
|
|
|
//stm: @CHAIN_SYNCER_NEW_PEER_HEAD_001, @CHAIN_SYNCER_VALIDATE_MESSAGE_META_001, @CHAIN_SYNCER_STOP_001
|
2021-12-14 10:33:33 +00:00
|
|
|
|
|
|
|
//stm: @CHAIN_INCOMING_HANDLE_INCOMING_BLOCKS_001, @CHAIN_INCOMING_VALIDATE_BLOCK_PUBSUB_001, @CHAIN_INCOMING_VALIDATE_MESSAGE_PUBSUB_001
|
2021-06-23 17:13:29 +00:00
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
publishPeriod = 10 * time.Second
|
|
|
|
maxDealsPerMsg = uint64(2) // Set max deals per publish deals message to 2
|
|
|
|
startEpoch = abi.ChainEpoch(2 << 12)
|
|
|
|
)
|
|
|
|
|
|
|
|
kit.QuietMiningLogs()
|
|
|
|
|
2022-06-14 17:21:34 +00:00
|
|
|
publisherKey, err := key.GenerateKey(types.KTSecp256k1)
|
2021-07-07 17:12:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
opts := node.Options(
|
|
|
|
node.Override(new(*storageadapter.DealPublisher),
|
|
|
|
storageadapter.NewDealPublisher(nil, storageadapter.PublishMsgConfig{
|
|
|
|
Period: publishPeriod,
|
|
|
|
MaxDealsPerMsg: maxDealsPerMsg,
|
|
|
|
}),
|
|
|
|
),
|
2022-06-14 17:32:29 +00:00
|
|
|
node.Override(new(*ctladdr.AddressSelector), modules.AddressSelector(&config.MinerAddressConfig{
|
2021-07-07 17:12:03 +00:00
|
|
|
DealPublishControl: []string{
|
|
|
|
publisherKey.Address.String(),
|
|
|
|
},
|
|
|
|
DisableOwnerFallback: true,
|
|
|
|
DisableWorkerFallback: true,
|
|
|
|
})),
|
2021-06-23 17:13:29 +00:00
|
|
|
)
|
|
|
|
|
2021-07-07 17:12:03 +00:00
|
|
|
client, miner, ens := kit.EnsembleMinimal(t, kit.Account(publisherKey, types.FromFil(10)), kit.MockProofs(), kit.ConstructorOpts(opts))
|
2021-06-23 17:13:29 +00:00
|
|
|
ens.InterconnectAll().BeginMining(10 * time.Millisecond)
|
|
|
|
|
2021-07-07 17:12:03 +00:00
|
|
|
_, err = client.WalletImport(ctx, &publisherKey.KeyInfo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-07 19:37:32 +00:00
|
|
|
miner.SetControlAddresses(publisherKey.Address)
|
2021-07-07 17:12:03 +00:00
|
|
|
|
2021-06-30 11:52:19 +00:00
|
|
|
dh := kit.NewDealHarness(t, client, miner, miner)
|
2021-06-23 17:13:29 +00:00
|
|
|
|
|
|
|
// Starts a deal and waits until it's published
|
|
|
|
runDealTillPublish := func(rseed int) {
|
|
|
|
res, _ := client.CreateImportFile(ctx, rseed, 0)
|
|
|
|
|
|
|
|
upds, err := client.ClientGetDealUpdates(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-24 01:02:00 +00:00
|
|
|
dp := dh.DefaultStartDealParams()
|
|
|
|
dp.Data.Root = res.Root
|
|
|
|
dp.DealStartEpoch = startEpoch
|
|
|
|
dh.StartDeal(ctx, dp)
|
2021-06-23 17:13:29 +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)
|
|
|
|
|
|
|
|
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-12-10 15:08:25 +00:00
|
|
|
//stm: @CHAIN_STATE_LIST_MESSAGES_001
|
2021-06-23 17:13:29 +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 {
|
|
|
|
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))
|
2021-07-07 18:01:40 +00:00
|
|
|
require.Equal(t, publisherKey.Address.String(), msg.From.String())
|
2021-06-23 17:13:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|