-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |