-
Notifications
You must be signed in to change notification settings - Fork 538
/
codec.go
136 lines (116 loc) · 5.36 KB
/
codec.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
package testsuite
import (
"bytes"
"encoding/hex"
"github.com/cosmos/gogoproto/jsonpb"
"github.com/cosmos/gogoproto/proto"
upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module/testutil"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
grouptypes "github.com/cosmos/cosmos-sdk/x/group"
proposaltypes "github.com/cosmos/cosmos-sdk/x/params/types/proposal"
wasmtypes "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
icacontrollertypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/types"
icahosttypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/types"
feetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
v7migrations "github.com/cosmos/ibc-go/v8/modules/core/02-client/migrations/v7"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
solomachine "github.com/cosmos/ibc-go/v8/modules/light-clients/06-solomachine"
ibctmtypes "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
localhost "github.com/cosmos/ibc-go/v8/modules/light-clients/09-localhost"
ibctesting "github.com/cosmos/ibc-go/v8/testing"
simappparams "github.com/cosmos/ibc-go/v8/testing/simapp/params"
)
// Codec returns the global E2E protobuf codec.
func Codec() *codec.ProtoCodec {
cdc, _ := codecAndEncodingConfig()
return cdc
}
// EncodingConfig returns the global E2E encoding config.
func EncodingConfig() simappparams.EncodingConfig {
_, cfg := codecAndEncodingConfig()
return cfg
}
// SDKEncodingConfig returns the global E2E encoding config.
func SDKEncodingConfig() *testutil.TestEncodingConfig {
_, cfg := codecAndEncodingConfig()
return &testutil.TestEncodingConfig{
InterfaceRegistry: cfg.InterfaceRegistry,
Codec: cfg.Codec,
TxConfig: cfg.TxConfig,
Amino: cfg.Amino,
}
}
func codecAndEncodingConfig() (*codec.ProtoCodec, simappparams.EncodingConfig) {
cfg := simappparams.MakeTestEncodingConfig()
// ibc types
icacontrollertypes.RegisterInterfaces(cfg.InterfaceRegistry)
icahosttypes.RegisterInterfaces(cfg.InterfaceRegistry)
feetypes.RegisterInterfaces(cfg.InterfaceRegistry)
solomachine.RegisterInterfaces(cfg.InterfaceRegistry)
v7migrations.RegisterInterfaces(cfg.InterfaceRegistry)
transfertypes.RegisterInterfaces(cfg.InterfaceRegistry)
clienttypes.RegisterInterfaces(cfg.InterfaceRegistry)
channeltypes.RegisterInterfaces(cfg.InterfaceRegistry)
connectiontypes.RegisterInterfaces(cfg.InterfaceRegistry)
ibctmtypes.RegisterInterfaces(cfg.InterfaceRegistry)
localhost.RegisterInterfaces(cfg.InterfaceRegistry)
wasmtypes.RegisterInterfaces(cfg.InterfaceRegistry)
// all other types
upgradetypes.RegisterInterfaces(cfg.InterfaceRegistry)
banktypes.RegisterInterfaces(cfg.InterfaceRegistry)
govv1beta1.RegisterInterfaces(cfg.InterfaceRegistry)
govv1.RegisterInterfaces(cfg.InterfaceRegistry)
authtypes.RegisterInterfaces(cfg.InterfaceRegistry)
cryptocodec.RegisterInterfaces(cfg.InterfaceRegistry)
grouptypes.RegisterInterfaces(cfg.InterfaceRegistry)
proposaltypes.RegisterInterfaces(cfg.InterfaceRegistry)
authz.RegisterInterfaces(cfg.InterfaceRegistry)
txtypes.RegisterInterfaces(cfg.InterfaceRegistry)
cdc := codec.NewProtoCodec(cfg.InterfaceRegistry)
return cdc, cfg
}
// UnmarshalMsgResponses attempts to unmarshal the tx msg responses into the provided message types.
func UnmarshalMsgResponses(txResp sdk.TxResponse, msgs ...codec.ProtoMarshaler) error {
cdc := Codec()
bz, err := hex.DecodeString(txResp.Data)
if err != nil {
return err
}
return ibctesting.UnmarshalMsgResponses(cdc, bz, msgs...)
}
// MustProtoMarshalJSON provides an auxiliary function to return Proto3 JSON encoded
// bytes of a message. This function should be used when marshalling a proto.Message
// from the e2e tests. This function strips out unknown fields. This is useful for
// backwards compatibility tests where the types imported by the e2e package have
// new fields that older versions do not recognize.
func MustProtoMarshalJSON(msg proto.Message) []byte {
anyResolver := codectypes.NewInterfaceRegistry()
// EmitDefaults is set to false to prevent marshalling of unpopulated fields (memo)
// OrigName and the anyResovler match the fields the original SDK function would expect
// in order to minimize changes.
// OrigName is true since there is no particular reason to use camel case
// The any resolver is empty, but provided anyways.
jm := &jsonpb.Marshaler{OrigName: true, EmitDefaults: false, AnyResolver: anyResolver}
err := codectypes.UnpackInterfaces(msg, codectypes.ProtoJSONPacker{JSONPBMarshaler: jm})
if err != nil {
panic(err)
}
buf := new(bytes.Buffer)
if err := jm.Marshal(buf, msg); err != nil {
panic(err)
}
return buf.Bytes()
}