@@ -5,13 +5,21 @@ import (
5
5
"testing"
6
6
"time"
7
7
8
+ "cosmossdk.io/x/tx/signing"
8
9
abci "github.com/cometbft/cometbft/abci/types"
9
10
dbm "github.com/cosmos/cosmos-db"
11
+ "github.com/cosmos/gogoproto/proto"
10
12
"github.com/stretchr/testify/require"
11
13
"github.com/syndtr/goleveldb/leveldb/opt"
12
14
15
+ "github.com/cosmos/cosmos-sdk/codec"
16
+ "github.com/cosmos/cosmos-sdk/codec/address"
17
+ "github.com/cosmos/cosmos-sdk/codec/types"
18
+ codectypes "github.com/cosmos/cosmos-sdk/codec/types"
13
19
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
20
+ "github.com/cosmos/cosmos-sdk/std"
14
21
sdk "github.com/cosmos/cosmos-sdk/types"
22
+ "github.com/cosmos/cosmos-sdk/x/authz"
15
23
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
16
24
17
25
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
@@ -119,6 +127,80 @@ func BenchmarkTxSending(b *testing.B) {
119
127
}
120
128
}
121
129
130
+ func BenchmarkUnpackAny (b * testing.B ) {
131
+ interfaceRegistry , err := types .NewInterfaceRegistryWithOptions (types.InterfaceRegistryOptions {
132
+ ProtoFiles : proto .HybridResolver ,
133
+ SigningOptions : signing.Options {
134
+ AddressCodec : address.Bech32Codec {
135
+ Bech32Prefix : sdk .GetConfig ().GetBech32AccountAddrPrefix (),
136
+ },
137
+ ValidatorAddressCodec : address.Bech32Codec {
138
+ Bech32Prefix : sdk .GetConfig ().GetBech32ValidatorAddrPrefix (),
139
+ },
140
+ },
141
+ })
142
+ require .NoError (b , err )
143
+
144
+ cdc := codec .NewProtoCodec (interfaceRegistry )
145
+ std .RegisterInterfaces (interfaceRegistry )
146
+
147
+ mustCreateAny := func (b * testing.B , v proto.Message ) * codectypes.Any {
148
+ b .Helper ()
149
+ any , err := codectypes .NewAnyWithValue (v )
150
+ require .NoError (b , err )
151
+ return any
152
+ }
153
+
154
+ createNested := func (b * testing.B , depth int ) * codectypes.Any {
155
+ b .Helper ()
156
+ // create nested MsgExecs
157
+ nested := authz .NewMsgExec (sdk.AccAddress {}, []sdk.Msg {})
158
+ for i := 0 ; i < depth ; i ++ {
159
+ nested = authz .NewMsgExec (sdk.AccAddress {}, []sdk.Msg {& nested })
160
+ }
161
+
162
+ return mustCreateAny (b , & nested )
163
+ }
164
+
165
+ cases := map [string ]struct {
166
+ msg * codectypes.Any
167
+ expErr bool
168
+ }{
169
+ "garbage any" : {
170
+ msg : & codectypes.Any {
171
+ TypeUrl : "aslasdf" , // TODO: use a real type URL
172
+ Value : []byte ("oiuwurjtlwerlwmt032498u50j3oehr943q;l348u58q=-afvu89 290i32-1[1]" ),
173
+ },
174
+ expErr : true ,
175
+ },
176
+ "single MsgExec" : {
177
+ msg : createNested (b , 1 ),
178
+ },
179
+ "10000 MsgExec" : {
180
+ msg : createNested (b , 10000 ),
181
+ },
182
+ "100000 MsgExec" : {
183
+ msg : createNested (b , 100000 ),
184
+ },
185
+ }
186
+
187
+ for name , tc := range cases {
188
+ b .Run (name , func (b * testing.B ) {
189
+ b .Logf ("%s msg size %v" , name , len (tc .msg .Value ))
190
+ b .ResetTimer ()
191
+ for i := 0 ; i < b .N ; i ++ {
192
+ var msg sdk.Msg
193
+ err := cdc .UnpackAny (tc .msg , & msg )
194
+ if tc .expErr {
195
+ require .Error (b , err )
196
+ } else {
197
+ require .NoError (b , err )
198
+ }
199
+ }
200
+ })
201
+ }
202
+ }
203
+
122
204
func bankSendMsg (info * AppInfo ) ([]sdk.Msg , error ) {
123
205
// Precompute all txs
124
206
rcpt := sdk .AccAddress (secp256k1 .GenPrivKey ().PubKey ().Address ())
0 commit comments