2019-06-20 13:37:57 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2022-01-15 03:17:49 +00:00
|
|
|
"encoding/json"
|
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
|
|
|
|
2022-01-15 03:17:49 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-04-30 14:23:41 +00:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2022-01-15 03:17:49 +00:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
2020-04-30 14:27:28 +00:00
|
|
|
db "github.com/tendermint/tm-db"
|
2019-06-20 13:37:57 +00:00
|
|
|
)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func TestNewApp(t *testing.T) {
|
2022-04-21 20:16:28 +00:00
|
|
|
SetSDKConfig()
|
2022-01-08 00:39:27 +00:00
|
|
|
NewApp(
|
|
|
|
log.NewTMLogger(log.NewSyncWriter(os.Stdout)),
|
|
|
|
db.NewMemDB(),
|
|
|
|
DefaultNodeHome,
|
|
|
|
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) {
|
2022-04-21 20:16:28 +00:00
|
|
|
SetSDKConfig()
|
2022-01-15 03:17:49 +00:00
|
|
|
db := db.NewMemDB()
|
2022-04-21 20:16:28 +00:00
|
|
|
app := NewApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, DefaultNodeHome, nil, MakeEncodingConfig(), DefaultOptions)
|
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),
|
2022-04-21 20:16:28 +00:00
|
|
|
ChainId: "kavatest_1-1",
|
2022-01-15 03:17:49 +00:00
|
|
|
InitialHeight: 1,
|
|
|
|
ConsensusParams: tmtypes.TM2PB.ConsensusParams(tmtypes.DefaultConsensusParams()),
|
|
|
|
Validators: nil,
|
|
|
|
AppStateBytes: stateBytes,
|
|
|
|
}
|
|
|
|
app.InitChain(initRequest)
|
|
|
|
app.Commit()
|
|
|
|
|
|
|
|
exportedApp, err := app.ExportAppStateAndValidators(false, []string{})
|
|
|
|
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)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|