forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trace_handlers_test.go
210 lines (191 loc) · 5.79 KB
/
trace_handlers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"testing"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
"github.com/google/go-cmp/cmp"
gov "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
)
// an isolated test case for a proposal submission
var proposalSubmissionSteps = []Step{
{SubmitTextProposalAction{Title: "Proposal 1", Description: "Description 1"}, State{}},
}
// an isolated test case for a state check involving a proposal
var proposalInStateSteps = []Step{
{
Action: SubmitConsumerRemovalProposalAction{},
State: State{
ChainID("provi"): ChainState{
Proposals: &map[uint]Proposal{
1: ConsumerRemovalProposal{
Deposit: 10000001,
Chain: ChainID("foo"),
Status: gov.ProposalStatus_PROPOSAL_STATUS_VOTING_PERIOD.String(),
},
},
},
},
},
}
// Checks that writing, then parsing a trace results in the same trace.
func TestWriterThenParser(t *testing.T) {
tests := map[string]struct {
trace []Step
}{
"proposalSubmission": {proposalSubmissionSteps},
"proposalInState": {proposalInStateSteps},
"start_provider_chain": {stepStartProviderChain()},
"happyPath": {happyPathSteps},
"democracy": {democracyUnregisteredDenomSteps},
"slashThrottle": {slashThrottleSteps},
"multipleConsumers": {multipleConsumers},
"shorthappy": {shortHappyPathSteps},
"democracyRewardsSteps": {democracyRegisteredDenomSteps},
"changeover": {changeoverSteps},
}
dir, err := os.MkdirTemp("", "example")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
filename := filepath.Join(dir, "trace.json")
err := WriteAndReadTrace(GlobalJSONParser, GlobalJSONWriter, tc.trace, filename)
if err != nil {
log.Fatalf("got error for testcase %v: %s", name, err)
}
})
}
}
// Checks that writing a trace does not result in an error.
func TestWriteExamples(t *testing.T) {
tests := map[string]struct {
trace []Step
}{
"happyPath": {happyPathSteps},
"democracy": {democracyUnregisteredDenomSteps},
"slashThrottle": {slashThrottleSteps},
"multipleConsumers": {multipleConsumers},
"shorthappy": {shortHappyPathSteps},
"democracyRewardsSteps": {democracyRegisteredDenomSteps},
"changeover": {changeoverSteps},
"consumer-misbehaviour": {consumerMisbehaviourSteps},
"consumer-double-sign": {consumerDoubleSignSteps},
}
dir := "tracehandler_testdata"
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
filename := filepath.Join(dir, name+".json")
err := GlobalJSONWriter.WriteTraceToFile(filename, tc.trace)
if err != nil {
t.Fatalf("error writing trace to file: %v", err)
}
})
}
}
func TestMarshalAndUnmarshalChainState(t *testing.T) {
tests := map[string]struct {
chainState ChainState
}{
"consumer addition proposal": {ChainState{
ValBalances: &map[ValidatorID]uint{
ValidatorID("alice"): 9489999999,
ValidatorID("bob"): 9500000000,
},
Proposals: &map[uint]Proposal{
2: ConsumerAdditionProposal{
Deposit: 10000001,
Chain: ChainID("test"),
SpawnTime: 0,
InitialHeight: clienttypes.Height{RevisionNumber: 5, RevisionHeight: 5},
Status: gov.ProposalStatus_PROPOSAL_STATUS_VOTING_PERIOD.String(),
},
},
}},
"IBC transfer update params": {ChainState{
ValBalances: &map[ValidatorID]uint{
ValidatorID("alice"): 9889999998,
ValidatorID("bob"): 9960000001,
},
Proposals: &map[uint]Proposal{
1: IBCTransferParamsProposal{
Deposit: 10000001,
Status: gov.ProposalStatus_PROPOSAL_STATUS_VOTING_PERIOD.String(),
Params: IBCTransferParams{true, true},
},
},
}},
"consumer removal proposal": {ChainState{
Proposals: &map[uint]Proposal{
5: ConsumerRemovalProposal{
Deposit: 10000001,
Chain: ChainID("test123"),
Status: gov.ProposalStatus_PROPOSAL_STATUS_PASSED.String(),
},
},
ValBalances: &map[ValidatorID]uint{
ValidatorID("bob"): 9500000000,
},
ConsumerChains: &map[ChainID]bool{}, // Consumer chain is now removed
}},
"text-proposal": {ChainState{
ValPowers: &map[ValidatorID]uint{
ValidatorID("alice"): 509,
ValidatorID("bob"): 500,
ValidatorID("carol"): 495,
},
ValBalances: &map[ValidatorID]uint{
ValidatorID("bob"): 9500000000,
},
Proposals: &map[uint]Proposal{
// proposal does not exist
10: TextProposal{},
},
}},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
err := MarshalAndUnmarshalChainState(tc.chainState)
if err != nil {
t.Fatalf("MarshalAndUnmarshalChainState: %s", err.Error())
}
})
}
}
func MarshalAndUnmarshalChainState(chainState ChainState) error {
jsonobj, err := json.Marshal(chainState)
if err != nil {
return fmt.Errorf("error marshalling chain state: %v", err)
}
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("marshaled and unmarshaled ChainState don't match, diff=%s", 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 := GlobalJSONParser.ReadTraceFromFile(tmp_filepath)
if err != nil {
return fmt.Errorf("got error reading trace from file: %v", err)
}
diff := cmp.Diff(trace, got, cmp.AllowUnexported(Step{}))
if diff != "" {
return fmt.Errorf("Got a difference (-want +got):\n%s", diff)
}
return nil
}