-
Notifications
You must be signed in to change notification settings - Fork 54
/
prouter_internal_test.go
192 lines (168 loc) · 4.56 KB
/
prouter_internal_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
package app
import (
"context"
"testing"
atypes "github.com/omni-network/omni/halo/attest/types"
etypes "github.com/omni-network/omni/octane/evmengine/types"
abci "github.com/cometbft/cometbft/abci/types"
cmttypes "github.com/cometbft/cometbft/proto/tendermint/types"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/baseapp"
sdktestutil "github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
stypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require"
)
func TestProcessProposalRouter(t *testing.T) {
t.Parallel()
tests := []struct {
name string
first bool
payloadMsgs int
voteMsgs int
stakingMsgs int
accept bool
multiTx bool
}{
{
name: "first empty",
first: true,
accept: true,
},
{
name: "first not empty",
first: true,
accept: false,
payloadMsgs: 1,
},
{
name: "too many txs",
payloadMsgs: 1,
accept: false,
multiTx: true,
},
{
name: "one payload message",
payloadMsgs: 1,
accept: true,
},
{
name: "one vote message",
voteMsgs: 1,
accept: true,
},
{
name: "one of each message",
payloadMsgs: 1,
voteMsgs: 1,
accept: true,
},
{
name: "two payload messages",
payloadMsgs: 2,
accept: false,
},
{
name: "two vote messages",
voteMsgs: 2,
accept: false,
},
{
name: "staking messages",
stakingMsgs: 1,
accept: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
key := storetypes.NewKVStoreKey("test")
ctx := sdktestutil.DefaultContext(key, storetypes.NewTransientStoreKey("test_key"))
srv := &mockServer{}
encCfg := moduletestutil.MakeTestEncodingConfig()
txConfig := encCfg.TxConfig
reg := encCfg.InterfaceRegistry
etypes.RegisterInterfaces(reg)
atypes.RegisterInterfaces(reg)
stypes.RegisterInterfaces(reg)
router := baseapp.NewMsgServiceRouter()
router.SetInterfaceRegistry(reg)
etypes.RegisterMsgServiceServer(router, srv)
atypes.RegisterMsgServiceServer(router, srv)
stypes.RegisterMsgServer(router, srv)
handler := makeProcessProposalHandler(router, txConfig)
var msgs []types.Msg
for i := 0; i < tt.payloadMsgs; i++ {
msgs = append(msgs, &etypes.MsgExecutionPayload{})
}
for i := 0; i < tt.voteMsgs; i++ {
msgs = append(msgs, &atypes.MsgAddVotes{})
}
for i := 0; i < tt.stakingMsgs; i++ {
msgs = append(msgs, &stypes.MsgDelegate{})
}
newReq := func(msgs ...types.Msg) *abci.RequestProcessProposal {
b := txConfig.NewTxBuilder()
err := b.SetMsgs(msgs...)
require.NoError(t, err)
tx, err := txConfig.TxEncoder()(b.GetTx())
require.NoError(t, err)
txs := [][]byte{tx}
if len(msgs) == 0 {
txs = nil
} else if tt.multiTx {
txs = append(txs, tx)
}
height := int64(99)
if tt.first {
height = 1
}
return &abci.RequestProcessProposal{
Height: height,
Txs: txs,
ProposedLastCommit: abci.CommitInfo{
Votes: []abci.VoteInfo{
{BlockIdFlag: cmttypes.BlockIDFlagCommit, Validator: abci.Validator{Power: 1}},
},
},
}
}
accept := abci.ResponseProcessProposal_ACCEPT
reject := abci.ResponseProcessProposal_REJECT
res, err := handler(ctx, newReq(msgs...))
require.NoError(t, err)
require.Equal(t, tt.accept, res.Status == accept)
require.Equal(t, !tt.accept, res.Status == reject)
if tt.stakingMsgs > 0 {
require.Empty(t, srv.addVotes)
require.Empty(t, srv.payload)
} else if tt.payloadMsgs > 1 {
require.Equal(t, 1, srv.payload)
require.Empty(t, srv.addVotes)
} else if tt.voteMsgs > 1 {
require.Equal(t, 1, srv.addVotes)
require.Empty(t, srv.payload)
} else if tt.accept {
require.Equal(t, tt.payloadMsgs, srv.payload)
require.Equal(t, tt.voteMsgs, srv.addVotes)
}
})
}
}
var _ atypes.MsgServiceServer = &mockServer{}
var _ etypes.MsgServiceServer = &mockServer{}
var _ stypes.MsgServer = &mockServer{}
type mockServer struct {
stypes.MsgServer
addVotes int
payload int
}
func (s *mockServer) ExecutionPayload(context.Context, *etypes.MsgExecutionPayload) (*etypes.ExecutionPayloadResponse, error) {
s.payload++
return &etypes.ExecutionPayloadResponse{}, nil
}
func (s *mockServer) AddVotes(context.Context, *atypes.MsgAddVotes) (*atypes.AddVotesResponse, error) {
s.addVotes++
return &atypes.AddVotesResponse{}, nil
}