Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/complex chain type provider #4634

Merged
merged 1 commit into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}