Skip to content

Commit

Permalink
complex providers (#4634)
Browse files Browse the repository at this point in the history
  • Loading branch information
zl03jsj authored Dec 21, 2021
1 parent d87bba1 commit e1d2584
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
77 changes: 77 additions & 0 deletions venus-shared/chain/complex_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package chain

import (
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/venus/venus-shared/chain/params"
"github.com/filecoin-project/venus/venus-shared/testutil"
"github.com/ipfs/go-cid"
"math"
"testing"
)

func init() {
testutil.MustRegisterDefaultValueProvier(TipsetProvider())
testutil.MustRegisterDefaultValueProvier(MessageProvider())
}

func TipsetProvider() func(*testing.T) *TipSet {
const (
minBlkNumInTipset = 1
maxBlkNumInTipset = 5
)

return func(t *testing.T) *TipSet {
var (
blkNum, parentNum int
parentWeight big.Int
epoch abi.ChainEpoch
blocks []*BlockHeader
parents []cid.Cid
)

testutil.Provide(t, &parentNum, testutil.IntRangedProvider(minBlkNumInTipset, maxBlkNumInTipset))
testutil.Provide(t, &parents, testutil.WithSliceLen(parentNum))

testutil.Provide(t, &blkNum, testutil.IntRangedProvider(minBlkNumInTipset+1, maxBlkNumInTipset))
testutil.Provide(t, &blocks, testutil.WithSliceLen(blkNum),
// blocks in one tipset must be with the same parents.
func(t *testing.T) []cid.Cid {
return parents
})

testutil.Provide(t, &epoch, testutil.IntRangedProvider(0, math.MaxUint32))
testutil.Provide(t, &parentWeight, testutil.PositiveBigProvider())

// ensure that random assignments won't break the validation
for _, blk := range blocks {
blk.Height = epoch
blk.ParentWeight.Set(parentWeight.Int)
}

tipset, err := NewTipSet(blocks)

if err != nil {
t.Fatalf("create new tipset failed: %s", err.Error())
}

return tipset
}
}

func MessageProvider() func(t *testing.T) *Message {
return func(t *testing.T) *Message {
var msg Message
testutil.Provide(t, &msg,
testutil.IntRangedProvider(0, params.BlockGasLimit),
func(t *testing.T) big.Int {
ip := testutil.IntRangedProvider(0, int(params.FilBase))
return FromFil(uint64(ip(t)))
},
)
// ensure that random assignments won't break the validation
msg.Version = 0
msg.GasPremium = msg.GasFeeCap
return &msg
}
}
36 changes: 36 additions & 0 deletions venus-shared/chain/complex_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package chain

import (
"github.com/filecoin-project/venus/venus-shared/testutil"
"github.com/stretchr/testify/require"
"testing"
)

func TestTipsetProvider(t *testing.T) {
var tipset = &TipSet{}
testutil.Provide(t, &tipset)
require.Greater(t, len(tipset.blocks), 0, "blocks in a tipset must greater than 0")
}

func TestMessageProvider(t *testing.T) {
var message *Message
testutil.Provide(t, &message)
require.NotEqual(t, message.Cid().String(), "", "message cid can't be empty")
}

func TestBlockProvider(t *testing.T) {
var block *BlockHeader
testutil.Provide(t, &block)
require.NotNil(t, block, "block must not be nil")
}

func TestComplexProvider(t *testing.T) {
tests := map[string]func(*testing.T) {
"Tipset":TestTipsetProvider,
"Message":TestMessageProvider,
"Block":TestBlockProvider,
}
for testName, f := range tests {
t.Run(testName, f)
}
}

0 comments on commit e1d2584

Please sign in to comment.