lotus/api/test/mining.go

241 lines
5.1 KiB
Go
Raw Normal View History

2019-09-23 15:27:30 +00:00
package test
import (
2020-04-23 21:12:42 +00:00
"bytes"
2019-09-23 15:27:30 +00:00
"context"
2020-04-23 21:12:42 +00:00
"fmt"
"math/rand"
"sync/atomic"
2019-09-23 15:27:30 +00:00
"testing"
2020-04-23 21:12:42 +00:00
"time"
logging "github.com/ipfs/go-log/v2"
2019-09-23 15:27:30 +00:00
"github.com/stretchr/testify/require"
2020-04-23 21:12:42 +00:00
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
2020-04-30 22:24:58 +00:00
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
2020-07-28 16:16:46 +00:00
"github.com/filecoin-project/lotus/miner"
2020-04-23 21:12:42 +00:00
"github.com/filecoin-project/lotus/node/impl"
2019-09-23 15:27:30 +00:00
)
//nolint:deadcode,varcheck
2020-04-23 21:12:42 +00:00
var log = logging.Logger("apitest")
2019-09-23 15:27:30 +00:00
func (ts *testSuite) testMining(t *testing.T) {
ctx := context.Background()
apis, sn := ts.makeNodes(t, OneFull, OneMiner)
2019-09-23 15:27:30 +00:00
api := apis[0]
newHeads, err := api.ChainNotify(ctx)
2019-09-23 15:27:30 +00:00
require.NoError(t, err)
initHead := (<-newHeads)[0]
baseHeight := initHead.Val.Height()
2019-09-23 15:27:30 +00:00
h1, err := api.ChainHead(ctx)
2019-09-23 15:27:30 +00:00
require.NoError(t, err)
require.Equal(t, int64(h1.Height()), int64(baseHeight))
2019-09-23 15:27:30 +00:00
MineUntilBlock(ctx, t, apis[0], sn[0], nil)
2019-09-23 15:27:30 +00:00
require.NoError(t, err)
<-newHeads
h2, err := api.ChainHead(ctx)
require.NoError(t, err)
require.Greater(t, int64(h2.Height()), int64(h1.Height()))
2019-09-23 15:27:30 +00:00
}
2020-04-23 21:12:42 +00:00
2020-04-30 22:24:58 +00:00
func (ts *testSuite) testMiningReal(t *testing.T) {
build.InsecurePoStValidation = false
defer func() {
build.InsecurePoStValidation = true
}()
ctx := context.Background()
apis, sn := ts.makeNodes(t, OneFull, OneMiner)
2020-04-30 22:24:58 +00:00
api := apis[0]
newHeads, err := api.ChainNotify(ctx)
2020-04-30 22:24:58 +00:00
require.NoError(t, err)
2020-10-27 11:19:12 +00:00
at := (<-newHeads)[0].Val.Height()
2020-04-30 22:24:58 +00:00
h1, err := api.ChainHead(ctx)
2020-04-30 22:24:58 +00:00
require.NoError(t, err)
2020-10-27 11:19:12 +00:00
require.Equal(t, int64(at), int64(h1.Height()))
2020-04-30 22:24:58 +00:00
MineUntilBlock(ctx, t, apis[0], sn[0], nil)
2020-04-30 22:24:58 +00:00
require.NoError(t, err)
<-newHeads
h2, err := api.ChainHead(ctx)
require.NoError(t, err)
2020-10-27 11:19:12 +00:00
require.Greater(t, int64(h2.Height()), int64(h1.Height()))
2020-04-30 22:24:58 +00:00
MineUntilBlock(ctx, t, apis[0], sn[0], nil)
2020-04-30 22:24:58 +00:00
require.NoError(t, err)
<-newHeads
2020-10-27 11:19:12 +00:00
h3, err := api.ChainHead(ctx)
2020-04-30 22:24:58 +00:00
require.NoError(t, err)
2020-10-27 11:19:12 +00:00
require.Greater(t, int64(h3.Height()), int64(h2.Height()))
2020-04-30 22:24:58 +00:00
}
2020-04-23 21:12:42 +00:00
func TestDealMining(t *testing.T, b APIBuilder, blocktime time.Duration, carExport bool) {
// test making a deal with a fresh miner, and see if it starts to mine
ctx := context.Background()
n, sn := b(t, OneFull, []StorageMiner{
2020-04-23 21:12:42 +00:00
{Full: 0, Preseal: PresealGenesis},
{Full: 0, Preseal: 0}, // TODO: Add support for miners on non-first full node
2020-04-23 21:12:42 +00:00
})
client := n[0].FullNode.(*impl.FullNodeAPI)
provider := sn[1]
genesisMiner := sn[0]
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{})
2020-08-07 00:05:35 +00:00
m2addr, err := sn[1].ActorAddress(context.TODO())
if err != nil {
t.Fatal(err)
}
2020-04-23 21:12:42 +00:00
go func() {
defer close(done)
2020-08-07 00:26:28 +00:00
complChan := minedTwo
2020-04-23 21:12:42 +00:00
for atomic.LoadInt32(&mine) != 0 {
2020-08-07 00:05:35 +00:00
wait := make(chan int)
mdone := func(mined bool, _ abi.ChainEpoch, err error) {
2020-08-07 00:05:35 +00:00
n := 0
if mined {
n = 1
}
wait <- n
2020-04-23 21:12:42 +00:00
}
2020-07-28 16:16:46 +00:00
if err := sn[0].MineOne(ctx, miner.MineReq{Done: mdone}); err != nil {
2020-04-23 21:12:42 +00:00
t.Error(err)
}
2020-07-28 16:16:46 +00:00
if err := sn[1].MineOne(ctx, miner.MineReq{Done: mdone}); err != nil {
2020-04-23 21:12:42 +00:00
t.Error(err)
}
expect := <-wait
expect += <-wait
time.Sleep(blocktime)
if expect == 0 {
// null block
continue
}
2020-04-23 21:12:42 +00:00
2020-08-07 00:05:35 +00:00
var nodeOneMined bool
for _, node := range sn {
mb, err := node.MiningBase(ctx)
if err != nil {
t.Error(err)
return
}
2020-04-23 21:12:42 +00:00
2020-08-07 00:05:35 +00:00
for _, b := range mb.Blocks() {
if b.Miner == m2addr {
nodeOneMined = true
break
2020-04-23 21:12:42 +00:00
}
}
2020-08-07 00:05:35 +00:00
2020-04-23 21:12:42 +00:00
}
2020-08-07 00:26:28 +00:00
if nodeOneMined && complChan != nil {
close(complChan)
complChan = nil
2020-04-23 21:12:42 +00:00
}
}
}()
deal := startDeal(t, ctx, provider, client, fcid, false, 0)
2020-04-23 21:12:42 +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-30 13:13:38 +00:00
waitDealSealed(t, ctx, provider, client, deal, false, false, nil)
2020-04-23 21:12:42 +00:00
<-minedTwo
atomic.StoreInt32(&mine, 0)
fmt.Println("shutting down mining")
<-done
}
func (ts *testSuite) testNonGenesisMiner(t *testing.T) {
ctx := context.Background()
n, sn := ts.makeNodes(t, []FullNodeOpts{
2021-04-30 16:43:06 +00:00
FullNodeWithLatestActorsAt(-1),
}, []StorageMiner{
{Full: 0, Preseal: PresealGenesis},
})
full, ok := n[0].FullNode.(*impl.FullNodeAPI)
if !ok {
t.Skip("not testing with a full node")
return
}
genesisMiner := sn[0]
bm := NewBlockMiner(ctx, t, genesisMiner, 4*time.Millisecond)
bm.MineBlocks()
t.Cleanup(bm.Stop)
gaa, err := genesisMiner.ActorAddress(ctx)
require.NoError(t, err)
gmi, err := full.StateMinerInfo(ctx, gaa, types.EmptyTSK)
require.NoError(t, err)
testm := n[0].Stb(ctx, t, TestSpt, gmi.Owner)
ta, err := testm.ActorAddress(ctx)
require.NoError(t, err)
tid, err := address.IDFromAddress(ta)
require.NoError(t, err)
require.Equal(t, uint64(1001), tid)
}