2019-06-20 13:37:57 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2022-01-15 03:17:49 +00:00
|
|
|
"encoding/json"
|
2023-05-27 02:06:26 +00:00
|
|
|
"fmt"
|
2019-06-20 13:37:57 +00:00
|
|
|
"os"
|
2022-01-15 03:17:49 +00:00
|
|
|
"sort"
|
2019-06-20 13:37:57 +00:00
|
|
|
"testing"
|
2022-01-15 03:17:49 +00:00
|
|
|
"time"
|
2019-06-20 13:37:57 +00:00
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
"github.com/0glabs/0g-chain/chaincfg"
|
2024-02-06 22:54:10 +00:00
|
|
|
db "github.com/cometbft/cometbft-db"
|
|
|
|
abci "github.com/cometbft/cometbft/abci/types"
|
|
|
|
"github.com/cometbft/cometbft/libs/log"
|
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
2023-05-27 02:06:26 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2024-02-06 22:54:10 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/sims"
|
2023-05-27 02:06:26 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
|
|
|
|
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
|
2024-02-16 17:49:58 +00:00
|
|
|
solomachine "github.com/cosmos/ibc-go/v7/modules/light-clients/06-solomachine"
|
2024-02-06 22:54:10 +00:00
|
|
|
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
|
2023-05-27 02:06:26 +00:00
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
2022-01-15 03:17:49 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-06-20 13:37:57 +00:00
|
|
|
)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func TestNewApp(t *testing.T) {
|
2024-05-01 05:53:58 +00:00
|
|
|
chaincfg.SetSDKConfig()
|
2022-01-08 00:39:27 +00:00
|
|
|
NewApp(
|
|
|
|
log.NewTMLogger(log.NewSyncWriter(os.Stdout)),
|
|
|
|
db.NewMemDB(),
|
2024-05-01 05:53:58 +00:00
|
|
|
chaincfg.DefaultNodeHome,
|
2022-01-08 00:39:27 +00:00
|
|
|
nil,
|
|
|
|
MakeEncodingConfig(),
|
2022-04-21 20:16:28 +00:00
|
|
|
DefaultOptions,
|
2019-07-18 17:36:31 +00:00
|
|
|
)
|
2019-06-20 13:37:57 +00:00
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
|
2022-01-15 03:17:49 +00:00
|
|
|
func TestExport(t *testing.T) {
|
2024-05-01 05:53:58 +00:00
|
|
|
chaincfg.SetSDKConfig()
|
2022-01-15 03:17:49 +00:00
|
|
|
db := db.NewMemDB()
|
2024-05-01 05:53:58 +00:00
|
|
|
app := NewApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, chaincfg.DefaultNodeHome, nil, MakeEncodingConfig(), DefaultOptions, baseapp.SetChainID(TestChainId))
|
2022-01-15 03:17:49 +00:00
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
genesisState := GenesisStateWithSingleValidator(&TestApp{App: *app}, NewDefaultGenesisState())
|
|
|
|
|
|
|
|
stateBytes, err := json.Marshal(genesisState)
|
2022-01-15 03:17:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
initRequest := abci.RequestInitChain{
|
|
|
|
Time: time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
|
2024-02-06 22:54:10 +00:00
|
|
|
ChainId: TestChainId,
|
2022-01-15 03:17:49 +00:00
|
|
|
InitialHeight: 1,
|
2024-02-06 22:54:10 +00:00
|
|
|
ConsensusParams: sims.DefaultConsensusParams,
|
2022-01-15 03:17:49 +00:00
|
|
|
Validators: nil,
|
|
|
|
AppStateBytes: stateBytes,
|
|
|
|
}
|
|
|
|
app.InitChain(initRequest)
|
|
|
|
app.Commit()
|
|
|
|
|
2024-02-06 22:54:10 +00:00
|
|
|
exportedApp, err := app.ExportAppStateAndValidators(false, []string{}, []string{})
|
2022-01-15 03:17:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Assume each module is exported correctly, so only check modules in genesis are present in export
|
|
|
|
initialModules, err := unmarshalJSONKeys(initRequest.AppStateBytes)
|
|
|
|
require.NoError(t, err)
|
2024-02-06 22:54:10 +00:00
|
|
|
// note ibctm is only registered in the BasicManager and not module manager so can be ignored
|
|
|
|
initialModules = removeIbcTmModule(initialModules)
|
2022-01-15 03:17:49 +00:00
|
|
|
exportedModules, err := unmarshalJSONKeys(exportedApp.AppState)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.ElementsMatch(t, initialModules, exportedModules)
|
|
|
|
|
|
|
|
assert.Equal(t, initRequest.InitialHeight+1, exportedApp.Height) // app.Commit() increments height
|
|
|
|
assert.Equal(t, initRequest.ConsensusParams, exportedApp.ConsensusParams)
|
2023-04-04 00:08:45 +00:00
|
|
|
assert.Len(t, exportedApp.Validators, 1) // no validators set in default genesis
|
2022-01-15 03:17:49 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 02:06:26 +00:00
|
|
|
// TestLegacyMsgAreAminoRegistered checks if all known msg types are registered on the app's amino codec.
|
|
|
|
// It doesn't check if they are registered on the module codecs used for signature checking.
|
|
|
|
func TestLegacyMsgAreAminoRegistered(t *testing.T) {
|
|
|
|
tApp := NewTestApp()
|
|
|
|
|
|
|
|
lcdc := tApp.LegacyAmino()
|
|
|
|
|
|
|
|
// Use the proto codec as the canonical list of msg types.
|
|
|
|
protoCodec := tApp.AppCodec().(*codec.ProtoCodec)
|
|
|
|
protoRegisteredMsgs := protoCodec.InterfaceRegistry().ListImplementations(sdk.MsgInterfaceProtoName)
|
|
|
|
|
|
|
|
for i, msgName := range protoRegisteredMsgs {
|
|
|
|
// Skip msgs from dependencies that were never amino registered.
|
|
|
|
if msgName == sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}) ||
|
|
|
|
msgName == sdk.MsgTypeURL(&vestingtypes.MsgCreatePeriodicVestingAccount{}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an encoded json msg, then unmarshal it to instantiate the msg type.
|
|
|
|
jsonMsg := []byte(fmt.Sprintf(`{"@type": "%s"}`, msgName))
|
|
|
|
|
|
|
|
var msg sdk.Msg
|
|
|
|
err := protoCodec.UnmarshalInterfaceJSON(jsonMsg, &msg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Only check legacy msgs for amino registration.
|
|
|
|
// Only legacy msg can be signed with amino.
|
|
|
|
_, ok := msg.(legacytx.LegacyMsg)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the msg is registered in amino by checking a repeat registration call panics.
|
|
|
|
panicValue, ok := catchPanic(func() {
|
|
|
|
lcdc.RegisterConcrete(interface{}(msg), fmt.Sprintf("aUniqueRegistrationName%d", i), nil)
|
|
|
|
})
|
|
|
|
assert.True(t, ok, "registration did not panic, msg %s is not registered in amino", msgName)
|
|
|
|
if ok {
|
|
|
|
require.IsTypef(t, "", panicValue, "msg %s amino registration panicked with unexpected type", msgName)
|
|
|
|
aminoErrMsgPrefix := "TypeInfo already exists for"
|
|
|
|
require.Containsf(t, panicValue, aminoErrMsgPrefix, "msg %s amino registration panicked for unexpected reason", msgName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// catchPanic returns the panic value of the passed function. The second return indicates if the function panicked.
|
|
|
|
func catchPanic(f func()) (panicValue interface{}, didPanic bool) {
|
|
|
|
didPanic = true
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
panicValue = recover()
|
|
|
|
}()
|
|
|
|
|
|
|
|
f()
|
|
|
|
didPanic = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-15 03:17:49 +00:00
|
|
|
// unmarshalJSONKeys extracts keys from the top level of a json blob.
|
|
|
|
func unmarshalJSONKeys(jsonBytes []byte) ([]string, error) {
|
|
|
|
var jsonMap map[string]json.RawMessage
|
|
|
|
err := json.Unmarshal(jsonBytes, &jsonMap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := make([]string, 0, len(jsonMap))
|
2022-04-21 20:16:28 +00:00
|
|
|
for k := range jsonMap {
|
2022-01-15 03:17:49 +00:00
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
return keys, nil
|
|
|
|
}
|
2024-02-06 22:54:10 +00:00
|
|
|
|
|
|
|
func removeIbcTmModule(modules []string) []string {
|
|
|
|
var result []string
|
|
|
|
for _, str := range modules {
|
2024-02-16 17:49:58 +00:00
|
|
|
if str != ibctm.ModuleName && str != solomachine.ModuleName {
|
2024-02-06 22:54:10 +00:00
|
|
|
result = append(result, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|