From e4ae9d55f46dbf247dd48fc1b1816810ac7feaa3 Mon Sep 17 00:00:00 2001 From: Philip Offtermatt <57488781+p-offtermatt@users.noreply.github.com> Date: Fri, 12 May 2023 15:50:06 +0200 Subject: [PATCH] test: add rapid pbt for trace parser (#913) * Add other steps to read/write test * Improve error logging in trace handler test * Push ActionType handling into Unmarshal/Marshal and out of own class * Rename generic trace handler files to be clear they are for json * Rename to marshalling to be more generic * Add marshal/unmarshal for proposals * Export unexported field * Add test for marshal/unmarshalling chain state * Fix pointer issues * Fix typo: action -> proposal * Log proposal string in test * Use json.RawMessage instead of map[string]any * For uniformity, also use RawMessage for step unmarshalling * Add tests for extra proposal types * Add more proposal types to test and unify names * Add handling for ParamsProposal * Regenerate traces * Chore: Export forgotten field * Use string, not int, to help marshal/unmarshal * Add rapid to go.mod and .sum * Add rapidpbt for chainState marshalling * Rename file to make clear it only relates to chainState * Add error return to TraceWriter and TraceParser * gst * Add generators for actions and steps, utilize in test driver * Restrict range for time * Add test for time marshal/unmarshal * Make time have a lower bound, since negative numbers are not supported by RFC3339 * Improve label string for argument to time.Unix * Correct lower bound for time: 1900 years negative instead of 2000 * Convert timestamp to utc * Format file * Ignore testdata folder * Add go comment * Add docstring to GetTraceGen --- go.mod | 17 ++ go.sum | 4 + tests/e2e/.gitignore | 1 + tests/e2e/action_rapid_test.go | 348 +++++++++++++++++++++++++++++++ tests/e2e/json_parser.go | 2 +- tests/e2e/json_writer.go | 2 +- tests/e2e/state_rapid_test.go | 232 +++++++++++++++++++++ tests/e2e/step_rapid_test.go | 51 +++++ tests/e2e/trace_handlers_test.go | 59 ++++-- 9 files changed, 699 insertions(+), 17 deletions(-) create mode 100644 tests/e2e/.gitignore create mode 100644 tests/e2e/action_rapid_test.go create mode 100644 tests/e2e/state_rapid_test.go create mode 100644 tests/e2e/step_rapid_test.go diff --git a/go.mod b/go.mod index 191720dd5a..af13164fb4 100644 --- a/go.mod +++ b/go.mod @@ -89,6 +89,7 @@ require ( github.com/golang/snappy v0.0.4 // indirect github.com/google/btree v1.1.2 // indirect github.com/google/go-cmp v0.5.9 + github.com/google/gofuzz v1.2.0 // indirect github.com/google/orderedcode v0.0.1 // indirect github.com/google/uuid v1.3.0 // indirect github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect @@ -126,6 +127,14 @@ require ( github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/go-testing-interface v1.14.1 // indirect github.com/mitchellh/mapstructure v1.5.0 + github.com/linxGnu/grocksdb v1.7.10 // indirect + github.com/magiconair/properties v1.8.6 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.16 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect + github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect + github.com/minio/highwayhash v1.0.2 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mtibben/percent v0.2.1 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/petermattis/goid v0.0.0-20230317030725-371a4b8eda08 // indirect @@ -168,6 +177,14 @@ require ( require ( github.com/spf13/viper v1.16.0 google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529 + ) +require pgregory.net/rapid v0.5.7 + +replace ( + github.com/cosmos/cosmos-sdk => github.com/cosmos/cosmos-sdk v0.45.15-ics + github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 + github.com/tendermint/tendermint => github.com/cometbft/cometbft v0.34.27 + google.golang.org/grpc => google.golang.org/grpc v1.33.2 ) require ( diff --git a/go.sum b/go.sum index 03e76f9812..dc8175040d 100644 --- a/go.sum +++ b/go.sum @@ -1879,6 +1879,10 @@ nhooyr.io/websocket v1.8.6 h1:s+C3xAMLwGmlI31Nyn/eAehUlZPwfYZu2JXM621Q5/k= nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= pgregory.net/rapid v0.5.5 h1:jkgx1TjbQPD/feRoK+S/mXw9e1uj6WilpHrXJowi6oA= pgregory.net/rapid v0.5.5/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04= +pgregory.net/rapid v0.5.3 h1:163N50IHFqr1phZens4FQOdPgfJscR7a562mjQqeo4M= +pgregory.net/rapid v0.5.3/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04= +pgregory.net/rapid v0.5.7 h1:p7/XbOgyFY1I/3Q12UTXfos70VZTcgc3WeoyiEru5cs= +pgregory.net/rapid v0.5.7/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= diff --git a/tests/e2e/.gitignore b/tests/e2e/.gitignore new file mode 100644 index 0000000000..d383c56fff --- /dev/null +++ b/tests/e2e/.gitignore @@ -0,0 +1 @@ +testdata diff --git a/tests/e2e/action_rapid_test.go b/tests/e2e/action_rapid_test.go new file mode 100644 index 0000000000..ae33ac867a --- /dev/null +++ b/tests/e2e/action_rapid_test.go @@ -0,0 +1,348 @@ +package main + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" + "pgregory.net/rapid" +) + +func GetActionGen() *rapid.Generator[any] { + return rapid.OneOf( + GetSendTokensActionGen().AsAny(), + GetStartChainActionGen().AsAny(), + GetSubmitTextProposalActionGen().AsAny(), + GetSubmitConsumerAdditionProposalActionGen().AsAny(), + GetSubmitConsumerRemovalProposalActionGen().AsAny(), + GetSubmitParamChangeProposalActionGen().AsAny(), + GetSubmitEquivocationProposalActionGen().AsAny(), + GetVoteGovProposalActionGen().AsAny(), + GetStartConsumerChainActionGen().AsAny(), + GetAddChainToRelayerActionGen().AsAny(), + GetAddIbcConnectionActionGen().AsAny(), + GetAddIbcChannelActionGen().AsAny(), + GetStartHermesActionGen().AsAny(), + GetTransferChannelCompleteActionGen().AsAny(), + GetRelayPacketsActionGen().AsAny(), + GetRelayRewardPacketsToProviderActionGen().AsAny(), + GetDelegateTokensActionGen().AsAny(), + GetUnbondTokensActionGen().AsAny(), + GetRedelegateTokensActionGen().AsAny(), + GetDowntimeSlashActionGen().AsAny(), + GetUnjailValidatorActionGen().AsAny(), + GetRegisterRepresentativeActionGen().AsAny(), + GetDoublesignSlashActionGen().AsAny(), + GetAssignConsumerPubKeyActionGen().AsAny(), + GetSlashThrottleDequeueGen().AsAny(), + ) +} + +func GetSendTokensActionGen() *rapid.Generator[SendTokensAction] { + return rapid.Custom(func(t *rapid.T) SendTokensAction { + return SendTokensAction{ + Amount: rapid.Uint().Draw(t, "Amount"), + Chain: GetChainIDGen().Draw(t, "Chain"), + From: GetValidatorIDGen().Draw(t, "From"), + To: GetValidatorIDGen().Draw(t, "To"), + } + }) +} + +func GetStartChainActionGen() *rapid.Generator[StartChainAction] { + return rapid.Custom(func(t *rapid.T) StartChainAction { + return StartChainAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + Validators: GetStartChainValidatorsGen().Draw(t, "Validators"), + GenesisChanges: rapid.String().Draw(t, "GenesisChanges"), + SkipGentx: rapid.Bool().Draw(t, "SkipGentx"), + } + }) +} + +func GetStartChainValidatorsGen() *rapid.Generator[[]StartChainValidator] { + return rapid.Custom(func(t *rapid.T) []StartChainValidator { + return rapid.SliceOf(GetStartChainValidatorGen()).Draw(t, "StartChainValidators") + }) +} + +func GetStartChainValidatorGen() *rapid.Generator[StartChainValidator] { + return rapid.Custom(func(t *rapid.T) StartChainValidator { + return StartChainValidator{ + Id: GetValidatorIDGen().Draw(t, "Id"), + Allocation: rapid.Uint().Draw(t, "Allocation"), + Stake: rapid.Uint().Draw(t, "Stake"), + } + }) +} + +func GetSubmitTextProposalActionGen() *rapid.Generator[submitTextProposalAction] { + return rapid.Custom(func(t *rapid.T) submitTextProposalAction { + return submitTextProposalAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + From: GetValidatorIDGen().Draw(t, "From"), + Deposit: rapid.Uint().Draw(t, "Deposit"), + PropType: rapid.String().Draw(t, "PropType"), + Title: rapid.String().Draw(t, "Title"), + Description: rapid.String().Draw(t, "Description"), + } + }) +} + +func GetSubmitConsumerAdditionProposalActionGen() *rapid.Generator[submitConsumerAdditionProposalAction] { + return rapid.Custom(func(t *rapid.T) submitConsumerAdditionProposalAction { + return submitConsumerAdditionProposalAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + From: GetValidatorIDGen().Draw(t, "From"), + Deposit: rapid.Uint().Draw(t, "Deposit"), + ConsumerChain: GetChainIDGen().Draw(t, "ConsumerChain"), + SpawnTime: rapid.Uint().Draw(t, "SpawnTime"), + InitialHeight: GetHeightGen().Draw(t, "InitialHeight"), + } + }) +} + +func GetSubmitConsumerRemovalProposalActionGen() *rapid.Generator[submitConsumerRemovalProposalAction] { + return rapid.Custom(func(t *rapid.T) submitConsumerRemovalProposalAction { + return submitConsumerRemovalProposalAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + From: GetValidatorIDGen().Draw(t, "From"), + Deposit: rapid.Uint().Draw(t, "Deposit"), + ConsumerChain: GetChainIDGen().Draw(t, "ConsumerChain"), + StopTimeOffset: time.Duration(rapid.Int64().Draw(t, "StopTimeOffset")), + } + }) +} + +func GetSubmitParamChangeProposalActionGen() *rapid.Generator[submitParamChangeProposalAction] { + return rapid.Custom(func(t *rapid.T) submitParamChangeProposalAction { + return submitParamChangeProposalAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + From: GetValidatorIDGen().Draw(t, "From"), + Deposit: rapid.Uint().Draw(t, "Deposit"), + Subspace: rapid.String().Draw(t, "Subspace"), + Key: rapid.String().Draw(t, "Key"), + Value: rapid.String().Draw(t, "Value"), // TODO: make this more generic + } + }) +} + +func GetSubmitEquivocationProposalActionGen() *rapid.Generator[submitEquivocationProposalAction] { + return rapid.Custom(func(t *rapid.T) submitEquivocationProposalAction { + return submitEquivocationProposalAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + From: GetValidatorIDGen().Draw(t, "From"), + Deposit: rapid.Uint().Draw(t, "Deposit"), + Height: rapid.Int64().Draw(t, "Height"), + Time: GetTimeGen().Draw(t, "Time"), + Power: rapid.Int64().Draw(t, "Power"), + } + }) +} + +func TestMarshalAndUnmarshalTime(t *testing.T) { + rapid.Check(t, func(t *rapid.T) { + time1 := GetTimeGen().Draw(t, "time") + data, err := time1.MarshalJSON() + require.NoError(t, err) + var time2 time.Time + err = time2.UnmarshalJSON(data) + require.NoError(t, err) + require.True(t, time1.Equal(time2)) + }) +} + +func GetTimeGen() *rapid.Generator[time.Time] { + return rapid.Custom(func(t *rapid.T) time.Time { + return time.Unix(rapid.Int64Range(-5.9959e+10, 1.5779e+11).Draw(t, "unix time"), 0).UTC() + }) +} + +func GetVoteGovProposalActionGen() *rapid.Generator[voteGovProposalAction] { + return rapid.Custom(func(t *rapid.T) voteGovProposalAction { + return voteGovProposalAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + From: rapid.SliceOf(GetValidatorIDGen()).Draw(t, "From"), + Vote: rapid.SliceOf(rapid.String()).Draw(t, "Vote"), + PropNumber: rapid.Uint().Draw(t, "PropNumber"), + } + }) +} + +func GetStartConsumerChainActionGen() *rapid.Generator[startConsumerChainAction] { + return rapid.Custom(func(t *rapid.T) startConsumerChainAction { + return startConsumerChainAction{ + ConsumerChain: GetChainIDGen().Draw(t, "ConsumerChain"), + ProviderChain: GetChainIDGen().Draw(t, "ProviderChain"), + Validators: GetStartChainValidatorsGen().Draw(t, "Validators"), + GenesisChanges: rapid.String().Draw(t, "GenesisChanges"), + } + }) +} + +func GetAddChainToRelayerActionGen() *rapid.Generator[addChainToRelayerAction] { + return rapid.Custom(func(t *rapid.T) addChainToRelayerAction { + return addChainToRelayerAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + Validator: GetValidatorIDGen().Draw(t, "Validator"), + } + }) +} + +func GetAddIbcConnectionActionGen() *rapid.Generator[addIbcConnectionAction] { + return rapid.Custom(func(t *rapid.T) addIbcConnectionAction { + return addIbcConnectionAction{ + ChainA: GetChainIDGen().Draw(t, "ChainA"), + ChainB: GetChainIDGen().Draw(t, "ChainB"), + ClientA: rapid.Uint().Draw(t, "ClientA"), + ClientB: rapid.Uint().Draw(t, "ClientB"), + } + }) +} + +func GetAddIbcChannelActionGen() *rapid.Generator[addIbcChannelAction] { + return rapid.Custom(func(t *rapid.T) addIbcChannelAction { + return addIbcChannelAction{ + ChainA: GetChainIDGen().Draw(t, "ChainA"), + ChainB: GetChainIDGen().Draw(t, "ChainB"), + ConnectionA: rapid.Uint().Draw(t, "ConnectionA"), + PortA: rapid.String().Draw(t, "PortA"), + PortB: rapid.String().Draw(t, "PortB"), + Order: rapid.String().Draw(t, "Order"), + } + }) +} + +func GetStartHermesActionGen() *rapid.Generator[startHermesAction] { + return rapid.Just(startHermesAction{}) +} + +func GetTransferChannelCompleteActionGen() *rapid.Generator[transferChannelCompleteAction] { + return rapid.Custom(func(t *rapid.T) transferChannelCompleteAction { + return transferChannelCompleteAction{ + ChainA: GetChainIDGen().Draw(t, "ChainA"), + ChainB: GetChainIDGen().Draw(t, "ChainB"), + ConnectionA: rapid.Uint().Draw(t, "ConnectionA"), + PortA: rapid.String().Draw(t, "PortA"), + PortB: rapid.String().Draw(t, "PortB"), + Order: rapid.String().Draw(t, "Order"), + ChannelA: rapid.Uint().Draw(t, "ChannelA"), + ChannelB: rapid.Uint().Draw(t, "ChannelB"), + } + }) +} + +func GetRelayPacketsActionGen() *rapid.Generator[relayPacketsAction] { + return rapid.Custom(func(t *rapid.T) relayPacketsAction { + return relayPacketsAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + Port: rapid.String().Draw(t, "Port"), + Channel: rapid.Uint().Draw(t, "Channel"), + } + }) +} + +func GetRelayRewardPacketsToProviderActionGen() *rapid.Generator[relayRewardPacketsToProviderAction] { + return rapid.Custom(func(t *rapid.T) relayRewardPacketsToProviderAction { + return relayRewardPacketsToProviderAction{ + ConsumerChain: GetChainIDGen().Draw(t, "ConsumerChain"), + ProviderChain: GetChainIDGen().Draw(t, "ProviderChain"), + Port: rapid.String().Draw(t, "Port"), + Channel: rapid.Uint().Draw(t, "Channel"), + } + }) +} + +func GetDelegateTokensActionGen() *rapid.Generator[delegateTokensAction] { + return rapid.Custom(func(t *rapid.T) delegateTokensAction { + return delegateTokensAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + Amount: rapid.Uint().Draw(t, "Amount"), + From: GetValidatorIDGen().Draw(t, "From"), + To: GetValidatorIDGen().Draw(t, "To"), + } + }) +} + +func GetUnbondTokensActionGen() *rapid.Generator[unbondTokensAction] { + return rapid.Custom(func(t *rapid.T) unbondTokensAction { + return unbondTokensAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + Amount: rapid.Uint().Draw(t, "Amount"), + Sender: GetValidatorIDGen().Draw(t, "Sender"), + UnbondFrom: GetValidatorIDGen().Draw(t, "UnbondFrom"), + } + }) +} + +func GetRedelegateTokensActionGen() *rapid.Generator[redelegateTokensAction] { + return rapid.Custom(func(t *rapid.T) redelegateTokensAction { + return redelegateTokensAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + Amount: rapid.Uint().Draw(t, "Amount"), + Src: GetValidatorIDGen().Draw(t, "Src"), + Dst: GetValidatorIDGen().Draw(t, "Dst"), + TxSender: GetValidatorIDGen().Draw(t, "TxSender"), + } + }) +} + +func GetDowntimeSlashActionGen() *rapid.Generator[downtimeSlashAction] { + return rapid.Custom(func(t *rapid.T) downtimeSlashAction { + return downtimeSlashAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + Validator: GetValidatorIDGen().Draw(t, "Validator"), + } + }) +} + +func GetUnjailValidatorActionGen() *rapid.Generator[unjailValidatorAction] { + return rapid.Custom(func(t *rapid.T) unjailValidatorAction { + return unjailValidatorAction{ + Validator: GetValidatorIDGen().Draw(t, "Validator"), + Provider: GetChainIDGen().Draw(t, "Provider"), + } + }) +} + +func GetRegisterRepresentativeActionGen() *rapid.Generator[registerRepresentativeAction] { + return rapid.Custom(func(t *rapid.T) registerRepresentativeAction { + return registerRepresentativeAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + Representatives: rapid.SliceOf(GetValidatorIDGen()).Draw(t, "Representatives"), + Stakes: rapid.SliceOf(rapid.Uint()).Draw(t, "Stakes"), + } + }) +} + +func GetDoublesignSlashActionGen() *rapid.Generator[doublesignSlashAction] { + return rapid.Custom(func(t *rapid.T) doublesignSlashAction { + return doublesignSlashAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + Validator: GetValidatorIDGen().Draw(t, "Validator"), + } + }) +} + +func GetAssignConsumerPubKeyActionGen() *rapid.Generator[assignConsumerPubKeyAction] { + return rapid.Custom(func(t *rapid.T) assignConsumerPubKeyAction { + return assignConsumerPubKeyAction{ + Chain: GetChainIDGen().Draw(t, "Chain"), + Validator: GetValidatorIDGen().Draw(t, "Validator"), + ConsumerPubkey: rapid.String().Draw(t, "ConsumerPubkey"), + ReconfigureNode: rapid.Bool().Draw(t, "ReconfigureNode"), + ExpectError: rapid.Bool().Draw(t, "ExpectError"), + } + }) +} + +func GetSlashThrottleDequeueGen() *rapid.Generator[slashThrottleDequeue] { + return rapid.Custom(func(t *rapid.T) slashThrottleDequeue { + return slashThrottleDequeue{ + Chain: GetChainIDGen().Draw(t, "Chain"), + CurrentQueueSize: rapid.Int().Draw(t, "CurrentQueueSize"), + NextQueueSize: rapid.Int().Draw(t, "NextQueueSize"), + Timeout: time.Duration(rapid.Int().Draw(t, "Timeout")) * time.Millisecond, + } + }) +} diff --git a/tests/e2e/json_parser.go b/tests/e2e/json_parser.go index ba7e6ee029..c88178257f 100644 --- a/tests/e2e/json_parser.go +++ b/tests/e2e/json_parser.go @@ -8,7 +8,7 @@ import ( // TraceParser provides an interface for parsers that read sequences of Steps from files. type TraceParser interface { - ReadTraceFromFile(path string) []Step + ReadTraceFromFile(filepath string) ([]Step, error) } // JSONParser is a simple parser that reads steps by unmarshalling from a file. diff --git a/tests/e2e/json_writer.go b/tests/e2e/json_writer.go index ccdfaa53ea..5a188523ea 100644 --- a/tests/e2e/json_writer.go +++ b/tests/e2e/json_writer.go @@ -7,7 +7,7 @@ import ( // TraceWriter is an interface for writers that write steps to files. type TraceWriter interface { - WriteTraceToFile(filepath string, trace []Step) + WriteTraceToFile(filepath string, trace []Step) error } // JSONWriter is a simple writer that simply marshals the array of Step objects. diff --git a/tests/e2e/state_rapid_test.go b/tests/e2e/state_rapid_test.go new file mode 100644 index 0000000000..158e67dd1f --- /dev/null +++ b/tests/e2e/state_rapid_test.go @@ -0,0 +1,232 @@ +package main + +import ( + "testing" + + clienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types" + "pgregory.net/rapid" +) + +func TestChainStateMarshalling(t *testing.T) { + rapid.Check(t, func(t *rapid.T) { + chainState := GetChainStateGen().Draw(t, "ChainState") + err := MarshalAndUnmarshalChainState(chainState) + if err != nil { + t.Fatalf("error marshalling and unmarshalling chain state: %v", err) + } + }) +} + +func GetStateGen() *rapid.Generator[State] { + return rapid.Custom(func(t *rapid.T) State { + return rapid.MapOf(GetChainIDGen(), GetChainStateGen()).Draw(t, "State") + }) +} + +func GetChainStateGen() *rapid.Generator[ChainState] { + return rapid.Custom( + func(t *rapid.T) ChainState { + valBalances := GetValBalancesGen().Draw(t, "ValBalances") + proposals := GetProposalsGen().Draw(t, "Proposals") + valPowers := GetValPowersGen().Draw(t, "ValPowers") + representativePowers := GetRepresentativePowersGen().Draw(t, "RepresentativePowers") + params := GetParamsGen().Draw(t, "Params") + rewards := GetRewardsGen().Draw(t, "Rewards") + consumerChains := GetConsumerChainsGen().Draw(t, "ConsumerChains") + assignedKeys := GetAssignedKeysGen().Draw(t, "AssignedKeys") + providerKeys := GetProviderKeysGen().Draw(t, "ProviderKeys") + consumerChainQueueSizes := GetConsumerChainQueueSizesGen().Draw(t, "ConsumerChainQueueSizes") + globalSlashQueueSize := rapid.Uint().Draw(t, "GlobalSlashQueueSize") + + return ChainState{ + ValBalances: &valBalances, + Proposals: &proposals, + ValPowers: &valPowers, + RepresentativePowers: &representativePowers, + Params: ¶ms, + Rewards: &rewards, + ConsumerChains: &consumerChains, + AssignedKeys: &assignedKeys, + ProviderKeys: &providerKeys, + ConsumerChainQueueSizes: &consumerChainQueueSizes, + GlobalSlashQueueSize: &globalSlashQueueSize, + } + }) +} + +func GetConsumerChainQueueSizesGen() *rapid.Generator[map[ChainID]uint] { + return rapid.Custom(func(t *rapid.T) map[ChainID]uint { + return rapid.MapOf(GetChainIDGen(), rapid.Uint()).Draw(t, "ConsumerChainQueueSizes") + }) +} + +func GetProviderKeysGen() *rapid.Generator[map[ValidatorID]string] { + return rapid.Custom(func(t *rapid.T) map[ValidatorID]string { + return rapid.MapOf(GetValidatorIDGen(), rapid.String()).Draw(t, "ProviderKeys") + }) +} + +func GetAssignedKeysGen() *rapid.Generator[map[ValidatorID]string] { + return rapid.Custom(func(t *rapid.T) map[ValidatorID]string { + return rapid.MapOf(GetValidatorIDGen(), rapid.String()).Draw(t, "AssignedKeys") + }) +} + +func GetChainIDGen() *rapid.Generator[ChainID] { + return rapid.Custom(func(t *rapid.T) ChainID { + return ChainID(rapid.String().Draw(t, "ChainID")) + }) +} + +func GetConsumerChainsGen() *rapid.Generator[map[ChainID]bool] { + return rapid.Custom(func(t *rapid.T) map[ChainID]bool { + return rapid.MapOf(GetChainIDGen(), rapid.Bool()).Draw(t, "ConsumerChains") + }) +} + +func GetRewardsGen() *rapid.Generator[Rewards] { + return rapid.Custom(func(t *rapid.T) Rewards { + return Rewards{ + IsIncrementalReward: rapid.Bool().Draw(t, "IsIncrementalReward"), + IsNativeDenom: rapid.Bool().Draw(t, "IsNativeDenom"), + IsRewarded: rapid.MapOf(GetValidatorIDGen(), rapid.Bool()).Draw(t, "IsRewarded"), + } + }) +} + +func GetParamsGen() *rapid.Generator[[]Param] { + return rapid.Custom(func(t *rapid.T) []Param { + return rapid.SliceOf(GetParamGen()).Draw(t, "Params") + }) +} + +func GetParamGen() *rapid.Generator[Param] { + return rapid.Custom(func(t *rapid.T) Param { + return Param{ + Key: rapid.String().Draw(t, "Key"), + Value: rapid.String().Draw(t, "Value"), + } + }) +} + +func GetRepresentativePowersGen() *rapid.Generator[map[ValidatorID]uint] { + return rapid.Custom(func(t *rapid.T) map[ValidatorID]uint { + return rapid.MapOf( + GetValidatorIDGen(), + rapid.Uint(), + ).Draw(t, "RepresentativePowers") + }) +} + +func GetValPowersGen() *rapid.Generator[map[ValidatorID]uint] { + return rapid.Custom(func(t *rapid.T) map[ValidatorID]uint { + return rapid.MapOf( + GetValidatorIDGen(), + rapid.Uint(), + ).Draw(t, "ValPowers") + }) +} + +func GetValBalancesGen() *rapid.Generator[map[ValidatorID]uint] { + return rapid.Custom(func(t *rapid.T) map[ValidatorID]uint { + return rapid.MapOf( + GetValidatorIDGen(), + rapid.Uint(), + ).Draw(t, "ValBalances") + }) +} + +func GetValidatorIDGen() *rapid.Generator[ValidatorID] { + return rapid.Custom(func(t *rapid.T) ValidatorID { + return ValidatorID(rapid.String().Draw(t, "ValidatorID")) + }) +} + +func GetProposalsGen() *rapid.Generator[map[uint]Proposal] { + return rapid.Custom(func(t *rapid.T) map[uint]Proposal { + return rapid.MapOf( + rapid.Uint(), + GetProposalGen(), + ).Draw(t, "Proposals") + }) +} + +func GetProposalGen() *rapid.Generator[Proposal] { + return rapid.Custom(func(t *rapid.T) Proposal { + gen := rapid.OneOf( + GetConsumerAdditionProposalGen().AsAny(), + GetConsumerRemovalProposalGen().AsAny(), + GetEquivocationProposalGen().AsAny(), + GetTextProposalGen().AsAny(), + GetParamsProposalGen().AsAny(), + ) + return gen.Draw(t, "Proposal").(Proposal) + }) +} + +func GetConsumerAdditionProposalGen() *rapid.Generator[ConsumerAdditionProposal] { + return rapid.Custom(func(t *rapid.T) ConsumerAdditionProposal { + return ConsumerAdditionProposal{ + Deposit: rapid.Uint().Draw(t, "Deposit"), + Chain: GetChainIDGen().Draw(t, "Chain"), + SpawnTime: rapid.Int().Draw(t, "SpawnTime"), + InitialHeight: GetHeightGen().Draw(t, "InitialHeight"), + Status: rapid.String().Draw(t, "Status"), + } + }) +} + +func GetConsumerRemovalProposalGen() *rapid.Generator[ConsumerRemovalProposal] { + return rapid.Custom(func(t *rapid.T) ConsumerRemovalProposal { + return ConsumerRemovalProposal{ + Deposit: rapid.Uint().Draw(t, "Deposit"), + Chain: GetChainIDGen().Draw(t, "Chain"), + StopTime: rapid.Int().Draw(t, "StopTime"), + Status: rapid.String().Draw(t, "Status"), + } + }) +} + +func GetEquivocationProposalGen() *rapid.Generator[EquivocationProposal] { + return rapid.Custom(func(t *rapid.T) EquivocationProposal { + return EquivocationProposal{ + Power: rapid.Uint().Draw(t, "Power"), + Height: rapid.Uint().Draw(t, "Height"), + ConsensusAddress: rapid.String().Draw(t, "ConesnsuAddress"), + Deposit: rapid.Uint().Draw(t, "Deposit"), + Status: rapid.String().Draw(t, "Status"), + } + }) +} + +func GetTextProposalGen() *rapid.Generator[TextProposal] { + return rapid.Custom(func(t *rapid.T) TextProposal { + return TextProposal{ + Title: rapid.String().Draw(t, "Title"), + Description: rapid.String().Draw(t, "Description"), + Deposit: rapid.Uint().Draw(t, "Deposit"), + Status: rapid.String().Draw(t, "Status"), + } + }) +} + +func GetParamsProposalGen() *rapid.Generator[ParamsProposal] { + return rapid.Custom(func(t *rapid.T) ParamsProposal { + return ParamsProposal{ + Subspace: rapid.String().Draw(t, "Subspace"), + Key: rapid.String().Draw(t, "Key"), + Value: rapid.String().Draw(t, "Value"), + Deposit: rapid.Uint().Draw(t, "Deposit"), + Status: rapid.String().Draw(t, "Status"), + } + }) +} + +func GetHeightGen() *rapid.Generator[clienttypes.Height] { + return rapid.Custom(func(t *rapid.T) clienttypes.Height { + return clienttypes.Height{ + RevisionNumber: rapid.Uint64().Draw(t, "RevisionNumber"), + RevisionHeight: rapid.Uint64().Draw(t, "RevisionHeight"), + } + }) +} diff --git a/tests/e2e/step_rapid_test.go b/tests/e2e/step_rapid_test.go new file mode 100644 index 0000000000..dde5f2f465 --- /dev/null +++ b/tests/e2e/step_rapid_test.go @@ -0,0 +1,51 @@ +package main + +import ( + "log" + "os" + "path/filepath" + "testing" + + "pgregory.net/rapid" +) + +// TestReadAndWriteTrace uses rapid to do property based testing +// of reading and writing traces. +// It generates a random trace, writes it to a file, then reads it back. +// It then compares the original trace to the read trace. +// If the traces are not equal, rapid will generate a minimal example +// that causes the test to fail. +func TestReadAndWriteTrace(t *testing.T) { + parser := JSONParser{} + writer := JSONWriter{} + + dir, err := os.MkdirTemp("", "example") + if err != nil { + log.Fatal(err) + } + defer os.RemoveAll(dir) // clean up + + rapid.Check(t, func(t *rapid.T) { + trace := GetTraceGen().Draw(t, "Trace") + filename := filepath.Join(dir, "trace.json") + err := WriteAndReadTrace(parser, writer, trace, filename) + if err != nil { + t.Fatalf("error writing and reading trace: %v", err) + } + }) +} + +// This can be used to test writing and parsing traces, but does not make much sense +// for testing trace execution, since the generated traces are almost guaranteed to be nonsensical. +func GetTraceGen() *rapid.Generator[[]Step] { + return rapid.SliceOf(GetStepGen()) +} + +func GetStepGen() *rapid.Generator[Step] { + return rapid.Custom(func(t *rapid.T) Step { + return Step{ + Action: GetActionGen().Draw(t, "Action"), + State: GetStateGen().Draw(t, "State"), + } + }) +} diff --git a/tests/e2e/trace_handlers_test.go b/tests/e2e/trace_handlers_test.go index c2f2ac335e..b28d617181 100644 --- a/tests/e2e/trace_handlers_test.go +++ b/tests/e2e/trace_handlers_test.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "fmt" "log" "os" "path/filepath" @@ -63,8 +64,8 @@ func TestWriterThenParser(t *testing.T) { for name, tc := range tests { t.Run(name, func(t *testing.T) { - filename := filepath.Join(dir, name) - err := writer.WriteTraceToFile(filename, tc.trace) + filename := filepath.Join(dir, "trace.json") + err := WriteAndReadTrace(parser, writer, tc.trace, filename) if err != nil { t.Fatalf("in testcase %v, got error writing trace to file: %v", name, err) } @@ -197,22 +198,50 @@ func TestMarshalAndUnmarshalChainState(t *testing.T) { for name, tc := range tests { t.Run(name, func(t *testing.T) { - jsonobj, err := json.Marshal(tc.chainState) + err := MarshalAndUnmarshalChainState(tc.chainState) if err != nil { - t.Fatalf("error marshalling chain state: %v", err) + t.Fatalf(err.Error()) } + }) + } +} - var got *ChainState - err = json.Unmarshal(jsonobj, &got) - if err != nil { - t.Fatalf("error unmarshalling chain state: %v", err) - } +func MarshalAndUnmarshalChainState(chainState ChainState) error { + jsonobj, err := json.Marshal(chainState) + if err != nil { + return fmt.Errorf("error marshalling chain state: %v", err) + } - diff := cmp.Diff(tc.chainState, *got) - if diff != "" { - log.Print(string(jsonobj)) - t.Fatalf(diff) - } - }) + var got *ChainState + err = json.Unmarshal(jsonobj, &got) + if err != nil { + return fmt.Errorf("error unmarshalling chain state: %v", err) } + + diff := cmp.Diff(chainState, *got) + if diff != "" { + log.Print(string(jsonobj)) + return fmt.Errorf(diff) + } + + return nil +} + +func WriteAndReadTrace(parser TraceParser, writer TraceWriter, trace []Step, tmp_filepath string) error { + err := writer.WriteTraceToFile(tmp_filepath, trace) + if err != nil { + return fmt.Errorf("error writing trace to file: %v", err) + } + + got, err := parser.ReadTraceFromFile(tmp_filepath) + if err != nil { + return fmt.Errorf("error reading trace from file: %v", err) + } + + diff := cmp.Diff(trace, got, cmp.AllowUnexported(Step{})) + if diff != "" { + return fmt.Errorf(diff) + } + + return nil }