Skip to content

Commit 3dd44aa

Browse files
authored
Merge pull request #736 from zemyblue/Audit-GLOBAL-01
Missing Amino Codec Registration
2 parents 0ade7e4 + 3dafa5c commit 3dd44aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+802
-192
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
8585
* (x/foundation) [\#704](https://github.com/line/lbm-sdk/pull/704) update x/foundation params
8686
* (x/wasm) [\#695](https://github.com/line/lbm-sdk/pull/695) fix to prevent external filesystem dependency of simulation
8787
* (x/foundation) [\#729](https://github.com/line/lbm-sdk/pull/729) add UpdateParams to x/foundation
88+
* (amino) [\#736](https://github.com/line/lbm-sdk/pull/736) apply the missing amino codec registratoin of cosmos-sdk
8889

8990
### Bug Fixes
9091
* (x/wasm) [\#453](https://github.com/line/lbm-sdk/pull/453) modify wasm grpc query api path

baseapp/baseapp_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
dbm "github.com/tendermint/tm-db"
2222

2323
"github.com/line/lbm-sdk/codec"
24+
"github.com/line/lbm-sdk/codec/legacy"
2425
"github.com/line/lbm-sdk/snapshots"
2526
snapshottypes "github.com/line/lbm-sdk/snapshots/types"
2627
"github.com/line/lbm-sdk/store/rootmulti"
@@ -91,10 +92,10 @@ func registerTestCodec(cdc *codec.LegacyAmino) {
9192

9293
// register test types
9394
cdc.RegisterConcrete(&txTest{}, "cosmos-sdk/baseapp/txTest", nil)
94-
cdc.RegisterConcrete(&msgCounter{}, "cosmos-sdk/baseapp/msgCounter", nil)
95-
cdc.RegisterConcrete(&msgCounter2{}, "cosmos-sdk/baseapp/msgCounter2", nil)
96-
cdc.RegisterConcrete(&msgKeyValue{}, "cosmos-sdk/baseapp/msgKeyValue", nil)
97-
cdc.RegisterConcrete(&msgNoRoute{}, "cosmos-sdk/baseapp/msgNoRoute", nil)
95+
legacy.RegisterAminoMsg(cdc, &msgCounter{}, "cosmos-sdk/baseapp/msgCounter")
96+
legacy.RegisterAminoMsg(cdc, &msgCounter2{}, "cosmos-sdk/baseapp/msgCounter2")
97+
legacy.RegisterAminoMsg(cdc, &msgKeyValue{}, "cosmos-sdk/baseapp/msgKeyValue")
98+
legacy.RegisterAminoMsg(cdc, &msgNoRoute{}, "cosmos-sdk/baseapp/msgNoRoute")
9899
}
99100

100101
// aminoTxEncoder creates a amino TxEncoder for testing purposes.
@@ -1257,7 +1258,7 @@ func TestRunInvalidTransaction(t *testing.T) {
12571258
// new codec so we can encode the tx, but we shouldn't be able to decode
12581259
newCdc := codec.NewLegacyAmino()
12591260
registerTestCodec(newCdc)
1260-
newCdc.RegisterConcrete(&msgNoDecode{}, "cosmos-sdk/baseapp/msgNoDecode", nil)
1261+
legacy.RegisterAminoMsg(newCdc, &msgNoDecode{}, "cosmos-sdk/baseapp/msgNoDecode")
12611262

12621263
txBytes, err := newCdc.Marshal(tx)
12631264
require.NoError(t, err)

codec/legacy/amino_msg.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package legacy
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/line/lbm-sdk/codec"
7+
sdk "github.com/line/lbm-sdk/types"
8+
)
9+
10+
// RegisterAminoMsg first checks that the msgName is <40 chars
11+
// (else this would break ledger nano signing: https://github.com/cosmos/cosmos-sdk/issues/10870),
12+
// then registers the concrete msg type with amino.
13+
func RegisterAminoMsg(cdc *codec.LegacyAmino, msg sdk.Msg, msgName string) {
14+
if len(msgName) > 39 {
15+
panic(fmt.Errorf("msg name %s is too long to be registered with amino", msgName))
16+
}
17+
cdc.RegisterConcrete(msg, msgName, nil)
18+
}

codec/legacy/amino_msg_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package legacy_test
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/line/lbm-sdk/codec"
10+
"github.com/line/lbm-sdk/codec/legacy"
11+
"github.com/line/lbm-sdk/testutil/testdata"
12+
)
13+
14+
func TestRegisterAminoMsg(t *testing.T) {
15+
cdc := codec.NewLegacyAmino()
16+
17+
testCases := map[string]struct {
18+
msgName string
19+
expPanic bool
20+
}{
21+
"all good": {
22+
msgName: "cosmos-sdk/Test",
23+
},
24+
"msgName too long": {
25+
msgName: strings.Repeat("a", 40),
26+
expPanic: true,
27+
},
28+
}
29+
for name, tc := range testCases {
30+
t.Run(name, func(t *testing.T) {
31+
fn := func() { legacy.RegisterAminoMsg(cdc, &testdata.TestMsg{}, tc.msgName) }
32+
if tc.expPanic {
33+
require.Panics(t, fn)
34+
} else {
35+
require.NotPanics(t, fn)
36+
}
37+
})
38+
}
39+
}

codec/legacy/codec.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ import (
44
"github.com/line/lbm-sdk/codec"
55
cryptocodec "github.com/line/lbm-sdk/crypto/codec"
66
cryptotypes "github.com/line/lbm-sdk/crypto/types"
7+
sdk "github.com/line/lbm-sdk/types"
78
)
89

910
// Cdc defines a global generic sealed Amino codec to be used throughout sdk. It
1011
// has all Tendermint crypto and evidence types registered.
1112
//
1213
// TODO: Deprecated - remove this global.
13-
var Cdc *codec.LegacyAmino
14+
var Cdc = codec.NewLegacyAmino()
1415

1516
func init() {
16-
Cdc = codec.NewLegacyAmino()
1717
cryptocodec.RegisterCrypto(Cdc)
1818
codec.RegisterEvidences(Cdc)
19+
sdk.RegisterLegacyAminoCodec(Cdc)
1920
}
2021

2122
// PrivKeyFromBytes unmarshals private key bytes and returns a PrivKey

codec/legacy/doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Package legacy contains a global amino Cdc which is deprecated but
22
// still used in several places within the SDK. This package is intended
33
// to be removed at some point in the future when the global Cdc is removed.
4+
// It also contains a util function RegisterAminoMsg that checks a msg name length
5+
// before registering the concrete msg type with amino.
46
package legacy

0 commit comments

Comments
 (0)