diff --git a/api/test/mining.go b/api/test/mining.go index 373266b0e..7667b3e61 100644 --- a/api/test/mining.go +++ b/api/test/mining.go @@ -132,21 +132,22 @@ func TestDealMining(t *testing.T, b APIBuilder, blocktime time.Duration, carExpo done := make(chan struct{}) minedTwo := make(chan struct{}) + m2addr, err := sn[1].ActorAddress(context.TODO()) + if err != nil { + t.Fatal(err) + } + go func() { - doneMinedTwo := false defer close(done) - prevExpect := 0 for atomic.LoadInt32(&mine) != 0 { - wait := make(chan int, 2) + wait := make(chan int) mdone := func(mined bool, err error) { - go func() { - n := 0 - if mined { - n = 1 - } - wait <- n - }() + n := 0 + if mined { + n = 1 + } + wait <- n } if err := sn[0].MineOne(ctx, miner.MineReq{Done: mdone}); err != nil { @@ -166,33 +167,27 @@ func TestDealMining(t *testing.T, b APIBuilder, blocktime time.Duration, carExpo continue } - for { - n := 0 - for i, node := range sn { - mb, err := node.MiningBase(ctx) - if err != nil { - t.Error(err) - return - } + var nodeOneMined bool + for _, node := range sn { + mb, err := node.MiningBase(ctx) + if err != nil { + t.Error(err) + return + } - if len(mb.Cids()) != expect { - log.Warnf("node %d mining base not complete (%d, want %d)", i, len(mb.Cids()), expect) - continue + for _, b := range mb.Blocks() { + if b.Miner == m2addr { + nodeOneMined = true + break } - n++ } - if n == len(sn) { - break - } - time.Sleep(blocktime) + } - if prevExpect == 2 && expect == 2 && !doneMinedTwo { + if nodeOneMined { close(minedTwo) - doneMinedTwo = true } - prevExpect = expect } }() diff --git a/miner/miner.go b/miner/miner.go index 61a5e1bb0..92fc7e2bc 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -6,10 +6,11 @@ import ( "crypto/rand" "encoding/binary" "fmt" - "github.com/filecoin-project/lotus/chain/gen/slashfilter" "sync" "time" + "github.com/filecoin-project/lotus/chain/gen/slashfilter" + "github.com/filecoin-project/go-address" "github.com/filecoin-project/specs-actors/actors/abi" "github.com/filecoin-project/specs-actors/actors/crypto" @@ -162,6 +163,16 @@ func (m *Miner) mine(ctx context.Context) { if base != nil && base.TipSet.Height() == prebase.TipSet.Height() && base.NullRounds == prebase.NullRounds { break } + if base != nil { + onDone(false, nil) + } + + // TODO: need to change the orchestration here. the problem is that + // we are waiting *after* we enter this loop and selecta mining + // candidate, which is almost certain to change in multiminer + // tests. Instead, we should block before entering the loop, so + // that when the test 'MineOne' function is triggered, we pull our + // best mining candidate at that time. // Wait until propagation delay period after block we plan to mine on onDone, injectNulls, err = m.waitFunc(ctx, prebase.TipSet.MinTimestamp())