Skip to content

Commit

Permalink
Explicit mining
Browse files Browse the repository at this point in the history
  • Loading branch information
travisperson committed Mar 27, 2019
1 parent 67cc880 commit b0f308b
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 39 deletions.
86 changes: 54 additions & 32 deletions commands/payment_channel_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"io/ioutil"
"math/big"
"os"
"strings"
"testing"
"time"
Expand All @@ -24,12 +23,6 @@ import (
"github.com/filecoin-project/go-filecoin/types"
)

func init() {
_, _ = os.Getwd()

series.GlobalSleepDelay = time.Second * 5
}

func VoucherFromString(data string) (paymentbroker.PaymentVoucher, error) {
_, cborVoucher, err := multibase.Decode(data)
if err != nil {
Expand All @@ -45,7 +38,7 @@ func VoucherFromString(data string) (paymentbroker.PaymentVoucher, error) {
}

// BasiFastSetup creates a environment and a single node, and environment options
func BasicFastSetup(t *testing.T, ctx context.Context, fastenvOpts fast.EnvironmentOpts) (fast.Environment, *fast.Filecoin, func() *fast.Filecoin) {
func BasicFastSetup(t *testing.T, ctx context.Context, fastenvOpts fast.EnvironmentOpts) (fast.Environment, *fast.Filecoin, func() *fast.Filecoin, context.Context) {
require := require.New(t)

// Create a directory for the test using the test name (mostly for FAST)
Expand All @@ -69,7 +62,7 @@ func BasicFastSetup(t *testing.T, ctx context.Context, fastenvOpts fast.Environm

fastenvOpts = fast.EnvironmentOpts{
InitOpts: append([]fast.ProcessInitOption{fast.POGenesisFile(genesisURI)}, fastenvOpts.InitOpts...),
DaemonOpts: append([]fast.ProcessDaemonOption{fast.POBlockTime(series.GlobalSleepDelay)}, fastenvOpts.DaemonOpts...),
DaemonOpts: append([]fast.ProcessDaemonOption{fast.POBlockTime(time.Millisecond)}, fastenvOpts.DaemonOpts...),
}

// Create a node for the test
Expand All @@ -79,6 +72,14 @@ func BasicFastSetup(t *testing.T, ctx context.Context, fastenvOpts fast.Environm
err = series.SetupGenesisNode(ctx, genesis, genesisMiner.Address, files.NewReaderFile(genesisMiner.Owner))
require.NoError(err)

var MiningOnce series.MiningOnceFunc
MiningOnce = func() {
_, err := genesis.MiningOnce(ctx)
require.NoError(err)
}

ctx = context.WithValue(ctx, series.CKMiningOnce, MiningOnce)

NewNode := func() *fast.Filecoin {
p, err := env.NewProcess(ctx, localplugin.PluginName, options, fastenvOpts)
require.NoError(err)
Expand All @@ -95,19 +96,19 @@ func BasicFastSetup(t *testing.T, ctx context.Context, fastenvOpts fast.Environm
return p
}

return env, genesis, NewNode
return env, genesis, NewNode, ctx
}

func TestPaymentChannelCreateSuccess(t *testing.T) {
t.Parallel()
require := require.New(t)

// This test should run in 20 block times
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(20*series.GlobalSleepDelay))
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer cancel()

// Get basic testing environment
env, _, NewNode := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -139,6 +140,8 @@ func WithPaymentChannel(t *testing.T, ctx context.Context, p *fast.Filecoin, fro
mcid, err := p.PaychCreate(ctx, target, amt, eol, fast.AOFromAddr(from), fast.AOPrice(big.NewFloat(0)), fast.AOLimit(300))
require.NoError(err)

series.MiningOnceFromCtx(ctx)

response, err := p.MessageWait(ctx, mcid)
require.NoError(err)

Expand All @@ -155,11 +158,11 @@ func TestPaymentChannelLs(t *testing.T) {
assert := assert.New(t)

// This test should run in 20 block times
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(20*series.GlobalSleepDelay))
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer cancel()

// Get basic testing environment
env, _, NewNode := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -199,11 +202,11 @@ func TestPaymentChannelLs(t *testing.T) {
assert := assert.New(t)

// This test should run in 20 block times
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(20*series.GlobalSleepDelay))
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer cancel()

// Get basic testing environment
env, _, NewNode := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -243,11 +246,11 @@ func TestPaymentChannelLs(t *testing.T) {
assert := assert.New(t)

// This test should run in 20 block times
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(20*series.GlobalSleepDelay))
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer cancel()

// Get basic testing environment
env, _, NewNode := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -282,11 +285,11 @@ func TestPaymentChannelVoucherSuccess(t *testing.T) {
assert := assert.New(t)

// This test should run in 20 block times
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(20*series.GlobalSleepDelay))
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer cancel()

// Get basic testing environment
env, _, NewNode := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -325,11 +328,11 @@ func TestPaymentChannelRedeemSuccess(t *testing.T) {
assert := assert.New(t)

// This test should run in 20 block times
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(20*series.GlobalSleepDelay))
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer cancel()

// Get basic testing environment
env, _, NewNode := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -359,6 +362,8 @@ func TestPaymentChannelRedeemSuccess(t *testing.T) {
mcid, err := targetDaemon.PaychRedeem(ctx, voucherStr, fast.AOFromAddr(targetAddr), fast.AOPrice(big.NewFloat(0)), fast.AOLimit(300))
require.NoError(err)

series.MiningOnceFromCtx(ctx)

_, err = targetDaemon.MessageWait(ctx, mcid)
require.NoError(err)

Expand All @@ -377,11 +382,11 @@ func TestPaymentChannelRedeemTooEarlyFails(t *testing.T) {
assert := assert.New(t)

// This test should run in 20 block times
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(20*series.GlobalSleepDelay))
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer cancel()

// Get basic testing environment
env, _, NewNode := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -411,6 +416,8 @@ func TestPaymentChannelRedeemTooEarlyFails(t *testing.T) {
mcid, err := targetDaemon.PaychRedeem(ctx, voucherStr, fast.AOFromAddr(targetAddr), fast.AOPrice(big.NewFloat(0)), fast.AOLimit(300))
require.NoError(err)

series.MiningOnceFromCtx(ctx)

_, err = targetDaemon.MessageWait(ctx, mcid)
require.NoError(err)

Expand All @@ -429,11 +436,11 @@ func TestPaymentChannelReclaimSuccess(t *testing.T) {
assert := assert.New(t)

// This test should run in 20 block times
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(40*series.GlobalSleepDelay))
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer cancel()

// Get basic testing environment
env, _, NewNode := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, p, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand All @@ -450,7 +457,14 @@ func TestPaymentChannelReclaimSuccess(t *testing.T) {
require.NoError(err)
payerAddr := addrs[0]

channelExpiry := types.NewBlockHeight(5)
bh, err := series.GetHeadBlockHeight(ctx, p)
require.NoError(err)

// Expiry is current height, plus 3
// - Setting up the payment channel
// - Redeeming one voucher
// - Expires on third block
channelExpiry := types.NewBlockHeight(3).Add(bh)
channelAmount := types.NewAttoFILFromFIL(1000)

balanceBefore, err := payerDaemon.WalletBalance(ctx, payerAddr)
Expand All @@ -466,6 +480,8 @@ func TestPaymentChannelReclaimSuccess(t *testing.T) {
mcid, err := targetDaemon.PaychRedeem(ctx, voucherStr, fast.AOFromAddr(targetAddr), fast.AOPrice(big.NewFloat(0)), fast.AOLimit(300))
require.NoError(err)

series.MiningOnceFromCtx(ctx)

_, err = targetDaemon.MessageWait(ctx, mcid)
require.NoError(err)

Expand All @@ -476,11 +492,13 @@ func TestPaymentChannelReclaimSuccess(t *testing.T) {
assert.Equal(channelAmount, channel.Amount)
assert.Equal(voucherAmount, channel.AmountRedeemed)

series.WaitForBlockHeight(ctx, payerDaemon, channel.Eol)
series.MiningOnceFromCtx(ctx)

mcid, err = payerDaemon.PaychReclaim(ctx, chanid, fast.AOFromAddr(payerAddr), fast.AOPrice(big.NewFloat(0)), fast.AOLimit(300))
require.NoError(err)

series.MiningOnceFromCtx(ctx)

_, err = payerDaemon.MessageWait(ctx, mcid)
require.NoError(err)

Expand All @@ -500,11 +518,11 @@ func TestPaymentChannelCloseSuccess(t *testing.T) {
assert := assert.New(t)

// This test should run in 20 block times
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(40*series.GlobalSleepDelay))
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer cancel()

// Get basic testing environment
env, _, NewNode := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -540,6 +558,8 @@ func TestPaymentChannelCloseSuccess(t *testing.T) {
mcid, err := targetDaemon.PaychClose(ctx, voucherStr, fast.AOFromAddr(targetAddr), fast.AOPrice(big.NewFloat(0)), fast.AOLimit(300))
require.NoError(err)

series.MiningOnceFromCtx(ctx)

_, err = targetDaemon.MessageWait(ctx, mcid)
require.NoError(err)

Expand All @@ -561,11 +581,11 @@ func TestPaymentChannelExtendSuccess(t *testing.T) {
assert := assert.New(t)

// This test should run in 20 block times
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(20*series.GlobalSleepDelay))
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer cancel()

// Get basic testing environment
env, _, NewNode := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -603,6 +623,8 @@ func TestPaymentChannelExtendSuccess(t *testing.T) {
mcid, err := payerDaemon.PaychExtend(ctx, chanid, extendAmount, extendExpiry, fast.AOFromAddr(payerAddr), fast.AOPrice(big.NewFloat(0)), fast.AOLimit(300))
require.NoError(err)

series.MiningOnceFromCtx(ctx)

_, err = payerDaemon.MessageWait(ctx, mcid)
require.NoError(err)

Expand Down
6 changes: 3 additions & 3 deletions tools/fast/action_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"encoding/json"

"github.com/filecoin-project/go-filecoin/types"
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
)

// ChainHead runs the chain head command against the filecoin process.
func (f *Filecoin) ChainHead(ctx context.Context) ([]types.Block, error) {
var out []types.Block
func (f *Filecoin) ChainHead(ctx context.Context) ([]cid.Cid, error) {
var out []cid.Cid
if err := f.RunCmdJSONWithStdin(ctx, nil, &out, "go-filecoin", "chain", "head"); err != nil {
return nil, err
}
Expand Down
6 changes: 6 additions & 0 deletions tools/fast/bin/localnet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ func main() {
return
}

err = genesis.MiningStart(ctx)
if err != nil {
exitcode = handleError(err, "failed to start mining on genesis node;")
return
}

// Create the processes that we will use to become miners
var miners []*fast.Filecoin
for i := 0; i < minerCount; i++ {
Expand Down
23 changes: 23 additions & 0 deletions tools/fast/series/get_head_block_height.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package series

import (
"context"

"github.com/filecoin-project/go-filecoin/tools/fast"
"github.com/filecoin-project/go-filecoin/types"
)

// GetHeadBlockHeight will inspect the chain head and return the height
func GetHeadBlockHeight(ctx context.Context, client *fast.Filecoin) (*types.BlockHeight, error) {
tipset, err := client.ChainHead(ctx)
if err != nil {
return nil, err
}

block, err := client.ShowBlock(ctx, tipset[0])
if err != nil {
return nil, err
}

return types.NewBlockHeight(uint64(block.Height)), nil
}
17 changes: 17 additions & 0 deletions tools/fast/series/mining_once_from_ctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package series

import (
"context"
)

var CKMiningOnce struct{} = struct{}{}

type MiningOnceFunc func()

// MiningOnceFromCtx extracts the MiningOnceFunc through key CKMiningOnce
func MiningOnceFromCtx(ctx context.Context) {
MiningOnce, ok := ctx.Value(CKMiningOnce).(MiningOnceFunc)
if ok {
MiningOnce()
}
}
2 changes: 2 additions & 0 deletions tools/fast/series/send_filecoin_from_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func SendFilecoinFromDefault(ctx context.Context, node *fast.Filecoin, addr addr
return err
}

MiningOnceFromCtx(ctx)

if _, err := node.MessageWait(ctx, mcid); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion tools/fast/series/setup_genesis_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ func SetupGenesisNode(ctx context.Context, node *fast.Filecoin, minerAddress add
return err
}

return node.MiningStart(ctx)
return nil
}
6 changes: 3 additions & 3 deletions tools/fast/series/wait_for_block_height.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
// greater than the provide height `bh`
func WaitForBlockHeight(ctx context.Context, client *fast.Filecoin, bh *types.BlockHeight) error {
for {
// Client waits around for the deal to be sealed
tipset, err := client.ChainHead(ctx)

hh, err := GetHeadBlockHeight(ctx, client)
if err != nil {
return err
}

if types.NewBlockHeight(uint64(tipset[0].Height)).GreaterEqual(bh) {
if hh.GreaterEqual(bh) {
break
}

Expand Down
Loading

0 comments on commit b0f308b

Please sign in to comment.