-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
epoching: more test infra and some fuzz tests on keeper functionaliti…
…es (#60)
- Loading branch information
1 parent
3e12fc0
commit 5d15ff1
Showing
14 changed files
with
380 additions
and
226 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
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
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,53 @@ | ||
package datagen | ||
|
||
import ( | ||
"github.com/tendermint/tendermint/crypto" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
tmtypes "github.com/tendermint/tendermint/types" | ||
|
||
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" | ||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" | ||
) | ||
|
||
// adapted from https://github.com/ClanNetwork/clan-network/blob/d00b1cc465/testutil/simapp/pv.go | ||
// used for creating key pairs for genesis validators in tests | ||
|
||
var _ tmtypes.PrivValidator = PV{} | ||
|
||
// PV implements PrivValidator without any safety or persistence. | ||
// Only use it for testing. | ||
type PV struct { | ||
PrivKey cryptotypes.PrivKey | ||
} | ||
|
||
func NewPV() PV { | ||
return PV{ed25519.GenPrivKey()} | ||
} | ||
|
||
// GetPubKey implements PrivValidator interface | ||
func (pv PV) GetPubKey() (crypto.PubKey, error) { | ||
return cryptocodec.ToTmPubKeyInterface(pv.PrivKey.PubKey()) | ||
} | ||
|
||
// SignVote implements PrivValidator interface | ||
func (pv PV) SignVote(chainID string, vote *tmproto.Vote) error { | ||
signBytes := tmtypes.VoteSignBytes(chainID, vote) | ||
sig, err := pv.PrivKey.Sign(signBytes) | ||
if err != nil { | ||
return err | ||
} | ||
vote.Signature = sig | ||
return nil | ||
} | ||
|
||
// SignProposal implements PrivValidator interface | ||
func (pv PV) SignProposal(chainID string, proposal *tmproto.Proposal) error { | ||
signBytes := tmtypes.ProposalSignBytes(chainID, proposal) | ||
sig, err := pv.PrivKey.Sign(signBytes) | ||
if err != nil { | ||
return err | ||
} | ||
proposal.Signature = sig | ||
return nil | ||
} |
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
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,52 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/babylonchain/babylon/x/epoching/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func FuzzEpochMsgQueue(f *testing.F) { | ||
f.Add(int64(11111)) | ||
f.Add(int64(22222)) | ||
f.Add(int64(55555)) | ||
f.Add(int64(12312)) | ||
|
||
f.Fuzz(func(t *testing.T, seed int64) { | ||
rand.Seed(seed) | ||
|
||
_, ctx, keeper, _, _ := setupTestKeeper() | ||
// ensure that the epoch msg queue is correct at the genesis | ||
require.Empty(t, keeper.GetEpochMsgs(ctx)) | ||
require.Equal(t, uint64(0), keeper.GetQueueLength(ctx)) | ||
|
||
// Enqueue a random number of msgs | ||
numQueuedMsgs := rand.Uint64() % 100 | ||
for i := uint64(0); i < numQueuedMsgs; i++ { | ||
msg := types.QueuedMessage{ | ||
TxId: sdk.Uint64ToBigEndian(i), | ||
MsgId: sdk.Uint64ToBigEndian(i), | ||
} | ||
keeper.EnqueueMsg(ctx, msg) | ||
} | ||
|
||
// ensure that each msg in the queue is correct | ||
epochMsgs := keeper.GetEpochMsgs(ctx) | ||
for i, msg := range epochMsgs { | ||
require.Equal(t, sdk.Uint64ToBigEndian(uint64(i)), msg.TxId) | ||
require.Equal(t, sdk.Uint64ToBigEndian(uint64(i)), msg.MsgId) | ||
require.Nil(t, msg.Msg) | ||
} | ||
|
||
// after clearing the msg queue, ensure that the epoch msg queue is empty | ||
keeper.ClearEpochMsgs(ctx) | ||
require.Empty(t, keeper.GetEpochMsgs(ctx)) | ||
require.Equal(t, uint64(0), keeper.GetQueueLength(ctx)) | ||
}) | ||
} | ||
|
||
// TODO (stateful tests): fuzz HandleQueueMsg. initialise some validators, let them submit some msgs and trigger HandleQueueMsg | ||
// require mocking valid QueueMsgs |
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,4 @@ | ||
package keeper_test | ||
|
||
// TODO (stateful tests): slash some random validators and check if the resulting (slashed) validator sets are consistent or not | ||
// require mocking slashing |
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,43 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func FuzzEpochValSet(f *testing.F) { | ||
f.Add(int64(11111)) | ||
f.Add(int64(22222)) | ||
f.Add(int64(55555)) | ||
f.Add(int64(12312)) | ||
|
||
f.Fuzz(func(t *testing.T, seed int64) { | ||
rand.Seed(seed) | ||
|
||
app, ctx, keeper, _, _, valSet := setupTestKeeperWithValSet(t) | ||
getValSet := keeper.GetValidatorSet(ctx, 0) | ||
require.Equal(t, len(valSet.Validators), len(getValSet)) | ||
for i := range getValSet { | ||
require.Equal(t, sdk.ValAddress(valSet.Validators[i].Address), getValSet[i].Addr) | ||
} | ||
|
||
// generate a random number of new blocks | ||
numIncBlocks := rand.Uint64()%1000 + 1 | ||
for i := uint64(0); i < numIncBlocks; i++ { | ||
ctx = genAndApplyEmptyBlock(app, ctx) | ||
} | ||
|
||
// check whether the validator set remains the same or not | ||
getValSet2 := keeper.GetValidatorSet(ctx, keeper.GetEpoch(ctx).EpochNumber) | ||
require.Equal(t, len(valSet.Validators), len(getValSet2)) | ||
for i := range getValSet2 { | ||
require.Equal(t, sdk.ValAddress(valSet.Validators[i].Address), getValSet[i].Addr) | ||
} | ||
}) | ||
} | ||
|
||
// TODO (stateful tests): create some random validators and check if the resulting validator set is consistent or not | ||
// require mocking Msg(Wrapped)CreateValidator |
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,47 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/babylonchain/babylon/x/epoching/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func FuzzEpochs(f *testing.F) { | ||
f.Add(int64(11111)) | ||
f.Add(int64(22222)) | ||
f.Add(int64(55555)) | ||
f.Add(int64(12312)) | ||
|
||
f.Fuzz(func(t *testing.T, seed int64) { | ||
rand.Seed(seed) | ||
|
||
app, ctx, keeper, _, _ := setupTestKeeper() | ||
// ensure that the epoch info is correct at the genesis | ||
epoch := keeper.GetEpoch(ctx) | ||
require.Equal(t, epoch.EpochNumber, uint64(0)) | ||
require.Equal(t, epoch.FirstBlockHeight, uint64(0)) | ||
|
||
// set a random epoch interval | ||
epochInterval := rand.Uint64()%100 + 1 | ||
keeper.SetParams(ctx, types.Params{ | ||
EpochInterval: epochInterval, | ||
}) | ||
// increment a random number of new blocks | ||
numIncBlocks := rand.Uint64()%1000 + 1 | ||
for i := uint64(0); i < numIncBlocks; i++ { | ||
ctx = genAndApplyEmptyBlock(app, ctx) | ||
} | ||
|
||
// ensure that the epoch info is still correct | ||
expectedEpochNumber := numIncBlocks / epochInterval | ||
if numIncBlocks%epochInterval > 0 { | ||
expectedEpochNumber += 1 | ||
} | ||
actualNewEpoch := keeper.GetEpoch(ctx) | ||
require.Equal(t, expectedEpochNumber, actualNewEpoch.EpochNumber) | ||
require.Equal(t, epochInterval, actualNewEpoch.CurrentEpochInterval) | ||
require.Equal(t, (expectedEpochNumber-1)*epochInterval+1, actualNewEpoch.FirstBlockHeight) | ||
}) | ||
} |
Oops, something went wrong.