lotus/api/test/util.go

88 lines
1.8 KiB
Go
Raw Normal View History

2020-08-13 20:37:09 +00:00
package test
import (
"context"
"testing"
"time"
2020-08-13 20:37:09 +00:00
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/abi"
2020-08-13 20:37:09 +00:00
"github.com/filecoin-project/go-address"
2021-04-05 19:34:03 +00:00
lapi "github.com/filecoin-project/lotus/api"
2020-08-13 20:37:09 +00:00
"github.com/filecoin-project/lotus/chain/types"
2020-08-25 22:07:10 +00:00
"github.com/filecoin-project/lotus/miner"
2020-08-13 20:37:09 +00:00
)
func SendFunds(ctx context.Context, t *testing.T, sender TestNode, addr address.Address, amount abi.TokenAmount) {
senderAddr, err := sender.WalletDefaultAddress(ctx)
if err != nil {
t.Fatal(err)
}
msg := &types.Message{
From: senderAddr,
To: addr,
Value: amount,
}
sm, err := sender.MpoolPushMessage(ctx, msg, nil)
if err != nil {
t.Fatal(err)
}
2021-04-05 19:34:03 +00:00
res, err := sender.StateWaitMsg(ctx, sm.Cid(), 1, lapi.LookbackNoLimit, true)
2020-08-13 20:37:09 +00:00
if err != nil {
t.Fatal(err)
}
if res.Receipt.ExitCode != 0 {
t.Fatal("did not successfully send money")
}
}
2020-08-25 22:07:10 +00:00
func MineUntilBlock(ctx context.Context, t *testing.T, fn TestNode, sn TestStorageNode, cb func(abi.ChainEpoch)) {
2020-08-25 22:07:10 +00:00
for i := 0; i < 1000; i++ {
var success bool
var err error
var epoch abi.ChainEpoch
2020-08-25 22:07:10 +00:00
wait := make(chan struct{})
2020-08-26 17:05:03 +00:00
mineErr := sn.MineOne(ctx, miner.MineReq{
Done: func(win bool, ep abi.ChainEpoch, e error) {
2020-08-25 22:07:10 +00:00
success = win
err = e
epoch = ep
2020-08-25 22:07:10 +00:00
wait <- struct{}{}
},
})
2020-08-26 17:05:03 +00:00
if mineErr != nil {
t.Fatal(mineErr)
}
2020-08-25 22:07:10 +00:00
<-wait
if err != nil {
t.Fatal(err)
}
if success {
// Wait until it shows up on the given full nodes ChainHead
nloops := 50
for i := 0; i < nloops; i++ {
ts, err := fn.ChainHead(ctx)
if err != nil {
t.Fatal(err)
}
if ts.Height() == epoch {
break
}
if i == nloops-1 {
t.Fatal("block never managed to sync to node")
}
time.Sleep(time.Millisecond * 10)
}
2020-08-25 22:07:10 +00:00
if cb != nil {
cb(epoch)
2020-08-25 22:07:10 +00:00
}
return
}
t.Log("did not mine block, trying again", i)
}
t.Fatal("failed to mine 1000 times in a row...")
}