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

test: Add json marshal/unmarshal for test traces #1314

Merged
merged 32 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d8dcf54
Add json marshal/unmarshal for test traces
p-offtermatt Sep 19, 2023
276e347
Remove auto-generated trace files
p-offtermatt Sep 19, 2023
28eec48
Add an example trace to see the input format
p-offtermatt Sep 19, 2023
ae67d35
Add comment in rapid test
p-offtermatt Sep 19, 2023
5d894e6
Remove duplicated test case
p-offtermatt Sep 19, 2023
5dfb63b
Remove TODO in favor of comment
p-offtermatt Sep 19, 2023
0948b05
Reduce code duplication
p-offtermatt Sep 19, 2023
e3e3e84
Run go mod tidy and make format
p-offtermatt Sep 19, 2023
010bec3
Correct testdata directory in gitignore
p-offtermatt Sep 19, 2023
f00e209
Remove testdata directory
p-offtermatt Sep 19, 2023
70350c2
Remove testdata from gitignore
p-offtermatt Sep 19, 2023
b02d4f7
Add tracehandler_testdata
p-offtermatt Sep 19, 2023
a519315
Remove example trace, since there are examples in the test data
p-offtermatt Sep 19, 2023
9eb0a1f
Revert unintentional change to .gitignore
p-offtermatt Sep 20, 2023
2f961cf
Remove propType field from generator
p-offtermatt Sep 20, 2023
760c67a
Add docstrings to action_rapid test and state_rapid_test to better ex…
p-offtermatt Sep 20, 2023
dcd49a4
bite -> byte
p-offtermatt Sep 20, 2023
b04adb3
Refactor: slashThrottleDequeue to slashThrottleDequeueAction
p-offtermatt Sep 20, 2023
4b4c76d
action name -> action type
p-offtermatt Sep 20, 2023
d146505
use method name as first word in docstring
p-offtermatt Sep 20, 2023
7209628
Simply remove simply
p-offtermatt Sep 20, 2023
b4e35e3
Clarify that WriteTrace overrides
p-offtermatt Sep 20, 2023
55d2686
Remove outdated comments
p-offtermatt Sep 20, 2023
5841fce
Add RegisteredConsumerRewardDenoms to rapid test
p-offtermatt Sep 20, 2023
1168aca
Add gen for submitChangeRewardDenomsProposalAction
p-offtermatt Sep 20, 2023
72729a8
Remove startChain steps
p-offtermatt Sep 20, 2023
9016327
Marshal step files with indent
p-offtermatt Sep 20, 2023
e837330
Add README that contains info about updates to trace format
p-offtermatt Sep 20, 2023
05232d9
Pull out the copied ChainState and Proposal types from methods
p-offtermatt Sep 21, 2023
f8c8329
Use shadowing to avoid relisting chainstate fields
p-offtermatt Sep 21, 2023
c77484e
Make format
p-offtermatt Sep 21, 2023
ccdd117
Remove log
p-offtermatt Sep 23, 2023
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
55 changes: 16 additions & 39 deletions tests/e2e/json_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,16 @@ type ProposalAndType struct {
Type string
}

// duplicated from the ChainState with a minor change to the Proposals field
type ChainStateWithProposalTypes struct {
ValBalances *map[ValidatorID]uint
ValPowers *map[ValidatorID]uint
RepresentativePowers *map[ValidatorID]uint
Params *[]Param
Rewards *Rewards
ConsumerChains *map[ChainID]bool
AssignedKeys *map[ValidatorID]string
ProviderKeys *map[ValidatorID]string
ConsumerChainQueueSizes *map[ChainID]uint
GlobalSlashQueueSize *uint
RegisteredConsumerRewardDenoms *[]string
Proposals *map[uint]ProposalAndType // the only thing changed from the real ChainState
}
type (
// to have a ChainState object that does not have the overriden Marshal/Unmarshal method
ChainStateCopy ChainState

// duplicated from the ChainState with a minor change to the Proposals field
ChainStateWithProposalTypes struct {
ChainStateCopy
Proposals *map[uint]ProposalAndType // the only thing changed from the real ChainState
}
)

// MarshalJSON marshals a step into JSON while including the type of the action.
func (step Step) MarshalJSON() ([]byte, error) {
Expand All @@ -52,6 +47,7 @@ func (step *Step) UnmarshalJSON(data []byte) error {
Action json.RawMessage
State State
}
print(string(data))
p-offtermatt marked this conversation as resolved.
Show resolved Hide resolved
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
Expand Down Expand Up @@ -290,19 +286,8 @@ func UnmarshalMapToActionType(rawAction json.RawMessage, actionTypeString string

// MarshalJSON transforms the ChainState into a ChainStateWithProposalTypes by adding type info to the proposals
func (c ChainState) MarshalJSON() ([]byte, error) {
chainStateWithProposalTypes := ChainStateWithProposalTypes{
ValBalances: c.ValBalances,
ValPowers: c.ValPowers,
RepresentativePowers: c.RepresentativePowers,
Params: c.Params,
Rewards: c.Rewards,
ConsumerChains: c.ConsumerChains,
AssignedKeys: c.AssignedKeys,
ProviderKeys: c.ProviderKeys,
ConsumerChainQueueSizes: c.ConsumerChainQueueSizes,
GlobalSlashQueueSize: c.GlobalSlashQueueSize,
RegisteredConsumerRewardDenoms: c.RegisteredConsumerRewardDenoms,
}
chainStateCopy := ChainStateCopy(c)
chainStateWithProposalTypes := ChainStateWithProposalTypes{chainStateCopy, nil}
if c.Proposals != nil {
proposalsWithTypes := make(map[uint]ProposalAndType)
for k, v := range *c.Proposals {
Expand All @@ -324,17 +309,9 @@ func (c *ChainState) UnmarshalJSON(data []byte) error {
if err != nil {
return err
}
c.ValBalances = chainStateWithProposalTypes.ValBalances
c.ValPowers = chainStateWithProposalTypes.ValPowers
c.RepresentativePowers = chainStateWithProposalTypes.RepresentativePowers
c.Params = chainStateWithProposalTypes.Params
c.Rewards = chainStateWithProposalTypes.Rewards
c.ConsumerChains = chainStateWithProposalTypes.ConsumerChains
c.AssignedKeys = chainStateWithProposalTypes.AssignedKeys
c.ProviderKeys = chainStateWithProposalTypes.ProviderKeys
c.ConsumerChainQueueSizes = chainStateWithProposalTypes.ConsumerChainQueueSizes
c.GlobalSlashQueueSize = chainStateWithProposalTypes.GlobalSlashQueueSize
c.RegisteredConsumerRewardDenoms = chainStateWithProposalTypes.RegisteredConsumerRewardDenoms

chainState := ChainState(chainStateWithProposalTypes.ChainStateCopy)
*c = chainState

if chainStateWithProposalTypes.Proposals != nil {
proposals := make(map[uint]Proposal)
Expand Down
31 changes: 5 additions & 26 deletions tests/e2e/trace_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,14 @@ func TestWriterThenParser(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
filename := filepath.Join(dir, "trace.json")
err := WriteReadCompareTrace(tc.trace, filename, name)
err := WriteAndReadTrace(GlobalJSONParser, GlobalJSONWriter, tc.trace, filename)
if err != nil {
log.Fatal(err)
log.Fatalf("got error for testcase %v: %s", name, err)
}
})
}
}

// Write a trace to a file, then reads it back and compares to the original.
func WriteReadCompareTrace(trace []Step, filename, name string) error {
err := WriteAndReadTrace(GlobalJSONParser, GlobalJSONWriter, trace, filename)
if err != nil {
return fmt.Errorf("in testcase %v, got error writing trace to file: %v", name, err)
}

got, err := GlobalJSONParser.ReadTraceFromFile(filename)
if err != nil {
return fmt.Errorf("in testcase %v, got error reading trace from file: %v", name, err)
}
diff := cmp.Diff(trace, got, cmp.AllowUnexported(Step{}))
if diff != "" {
return fmt.Errorf("Got a difference for testcase %s (-want +got):\n%s", name, diff)
}

return nil
}

// Checks that writing a trace does not result in an error.
func TestWriteExamples(t *testing.T) {
tests := map[string]struct {
Expand Down Expand Up @@ -236,15 +217,13 @@ func WriteAndReadTrace(parser TraceParser, writer TraceWriter, trace []Step, tmp
return fmt.Errorf("error writing trace to file: %v", err)
}

got, err := parser.ReadTraceFromFile(tmp_filepath)
got, err := GlobalJSONParser.ReadTraceFromFile(tmp_filepath)
if err != nil {
return fmt.Errorf("error reading trace from file: %v", err)
return fmt.Errorf("got error reading trace from file: %v", err)
}

diff := cmp.Diff(trace, got, cmp.AllowUnexported(Step{}))
if diff != "" {
return fmt.Errorf(diff)
return fmt.Errorf("Got a difference (-want +got):\n%s", diff)
}

return nil
}