Skip to content

Commit d56de85

Browse files
odeke-emAlessio Treglia
and
Alessio Treglia
authored
x/gov/simulation: fix wrong NewDecodeStore Proposal decoding + proper tests (#8603)
The code in NewDecodeStore decoded the wrong proposal due to a typographical error, but the tests used the exact same value for the key value pairs hence the typo could never be caught. I noticed it during an audit of the code, and I've fixed the tests to pass in varying values for the various key value pairs. Fixes #8570 Co-authored-by: Alessio Treglia <[email protected]>
1 parent f970056 commit d56de85

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

x/gov/simulation/decoder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string {
2222
panic(err)
2323
}
2424
var proposalB types.Proposal
25-
err = cdc.UnmarshalBinaryBare(kvA.Value, &proposalB)
25+
err = cdc.UnmarshalBinaryBare(kvB.Value, &proposalB)
2626
if err != nil {
2727
panic(err)
2828
}

x/gov/simulation/decoder_test.go

+44-24
Original file line numberDiff line numberDiff line change
@@ -27,46 +27,66 @@ func TestDecodeStore(t *testing.T) {
2727

2828
endTime := time.Now().UTC()
2929
content := types.ContentFromProposalType("test", "test", types.ProposalTypeText)
30-
proposal, err := types.NewProposal(content, 1, endTime, endTime.Add(24*time.Hour))
30+
proposalA, err := types.NewProposal(content, 1, endTime, endTime.Add(24*time.Hour))
31+
require.NoError(t, err)
32+
proposalB, err := types.NewProposal(content, 2, endTime, endTime.Add(24*time.Hour))
3133
require.NoError(t, err)
3234

3335
proposalIDBz := make([]byte, 8)
3436
binary.LittleEndian.PutUint64(proposalIDBz, 1)
3537
deposit := types.NewDeposit(1, delAddr1, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())))
3638
vote := types.NewVote(1, delAddr1, types.NewNonSplitVoteOption(types.OptionYes))
3739

38-
proposalBz, err := cdc.MarshalBinaryBare(&proposal)
40+
proposalBzA, err := cdc.MarshalBinaryBare(&proposalA)
41+
require.NoError(t, err)
42+
proposalBzB, err := cdc.MarshalBinaryBare(&proposalB)
3943
require.NoError(t, err)
40-
41-
kvPairs := kv.Pairs{
42-
Pairs: []kv.Pair{
43-
{Key: types.ProposalKey(1), Value: proposalBz},
44-
{Key: types.InactiveProposalQueueKey(1, endTime), Value: proposalIDBz},
45-
{Key: types.DepositKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(&deposit)},
46-
{Key: types.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(&vote)},
47-
{Key: []byte{0x99}, Value: []byte{0x99}},
48-
},
49-
}
5044

5145
tests := []struct {
5246
name string
47+
kvA, kvB kv.Pair
5348
expectedLog string
49+
wantPanic bool
5450
}{
55-
{"proposals", fmt.Sprintf("%v\n%v", proposal, proposal)},
56-
{"proposal IDs", "proposalIDA: 1\nProposalIDB: 1"},
57-
{"deposits", fmt.Sprintf("%v\n%v", deposit, deposit)},
58-
{"votes", fmt.Sprintf("%v\n%v", vote, vote)},
59-
{"other", ""},
51+
{
52+
"proposals",
53+
kv.Pair{Key: types.ProposalKey(1), Value: proposalBzA},
54+
kv.Pair{Key: types.ProposalKey(2), Value: proposalBzB},
55+
fmt.Sprintf("%v\n%v", proposalA, proposalB), false,
56+
},
57+
{
58+
"proposal IDs",
59+
kv.Pair{Key: types.InactiveProposalQueueKey(1, endTime), Value: proposalIDBz},
60+
kv.Pair{Key: types.InactiveProposalQueueKey(1, endTime), Value: proposalIDBz},
61+
"proposalIDA: 1\nProposalIDB: 1", false,
62+
},
63+
{
64+
"deposits",
65+
kv.Pair{Key: types.DepositKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(&deposit)},
66+
kv.Pair{Key: types.DepositKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(&deposit)},
67+
fmt.Sprintf("%v\n%v", deposit, deposit), false,
68+
},
69+
{
70+
"votes",
71+
kv.Pair{Key: types.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(&vote)},
72+
kv.Pair{Key: types.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(&vote)},
73+
fmt.Sprintf("%v\n%v", vote, vote), false,
74+
},
75+
{
76+
"other",
77+
kv.Pair{Key: []byte{0x99}, Value: []byte{0x99}},
78+
kv.Pair{Key: []byte{0x99}, Value: []byte{0x99}},
79+
"", true,
80+
},
6081
}
6182

62-
for i, tt := range tests {
63-
i, tt := i, tt
83+
for _, tt := range tests {
84+
tt := tt
6485
t.Run(tt.name, func(t *testing.T) {
65-
switch i {
66-
case len(tests) - 1:
67-
require.Panics(t, func() { dec(kvPairs.Pairs[i], kvPairs.Pairs[i]) }, tt.name)
68-
default:
69-
require.Equal(t, tt.expectedLog, dec(kvPairs.Pairs[i], kvPairs.Pairs[i]), tt.name)
86+
if tt.wantPanic {
87+
require.Panics(t, func() { dec(tt.kvA, tt.kvB) }, tt.name)
88+
} else {
89+
require.Equal(t, tt.expectedLog, dec(tt.kvA, tt.kvB), tt.name)
7090
}
7191
})
7292
}

0 commit comments

Comments
 (0)