-
Notifications
You must be signed in to change notification settings - Fork 657
/
Copy pathcodec.go
113 lines (97 loc) · 4.18 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
package testsuite
import (
"encoding/hex"
"fmt"
intertxtypes "github.com/cosmos/interchain-accounts/x/inter-tx/types"
"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module/testutil"
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"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
icacontrollertypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types"
icahosttypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types"
feetypes "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
v7migrations "github.com/cosmos/ibc-go/v7/modules/core/02-client/migrations/v7"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
solomachine "github.com/cosmos/ibc-go/v7/modules/light-clients/06-solomachine"
ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
simappparams "github.com/cosmos/ibc-go/v7/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)
intertxtypes.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)
// 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)
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
}
var txMsgData sdk.TxMsgData
if err := cdc.Unmarshal(bz, &txMsgData); err != nil {
return err
}
if len(msgs) != len(txMsgData.MsgResponses) {
return fmt.Errorf("expected %d message responses but got %d", len(msgs), len(txMsgData.MsgResponses))
}
for i, msg := range msgs {
if err := cdc.Unmarshal(txMsgData.MsgResponses[i].Value, msg); err != nil {
return err
}
}
return nil
}