Skip to content

Commit

Permalink
Update cosmos-sdk to 0.38.0 + fix all api change errors
Browse files Browse the repository at this point in the history
  • Loading branch information
krhubert committed Feb 4, 2020
1 parent b4a18b1 commit ae1b6f7
Show file tree
Hide file tree
Showing 24 changed files with 195 additions and 129 deletions.
8 changes: 3 additions & 5 deletions cmd/mesg-cosmos-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (

"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/genaccounts"
genaccscli "github.com/cosmos/cosmos-sdk/x/genaccounts/client/cli"
"github.com/cosmos/cosmos-sdk/x/auth"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/mesg-foundation/engine/codec"
Expand Down Expand Up @@ -51,13 +50,12 @@ func main() {
// CLI commands to initialize the chain
rootCmd.AddCommand(
genutilcli.InitCmd(ctx, cdc, basicManager, cfg.Tendermint.Config.RootDir),
genutilcli.CollectGenTxsCmd(ctx, cdc, genaccounts.AppModuleBasic{}, cfg.Tendermint.Config.RootDir),
genutilcli.CollectGenTxsCmd(ctx, cdc, auth.GenesisAccountIterator{}, cfg.Tendermint.Config.RootDir),
genutilcli.GenTxCmd(
ctx, cdc, basicManager, staking.AppModuleBasic{},
genaccounts.AppModuleBasic{}, cfg.Tendermint.Config.RootDir, defaultCLIHome,
auth.GenesisAccountIterator{}, cfg.Tendermint.Config.RootDir, defaultCLIHome,
),
genutilcli.ValidateGenesisCmd(ctx, cdc, basicManager),
genaccscli.AddGenesisAccountCmd(ctx, cdc, cfg.Tendermint.Config.RootDir, defaultCLIHome),
)

server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators)
Expand Down
7 changes: 4 additions & 3 deletions cmd/mesg-cosmos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/lcd"
"github.com/cosmos/cosmos-sdk/client/rpc"
Expand Down Expand Up @@ -44,7 +45,7 @@ func main() {
}

// Add --chain-id to persistent flags and mark it required
rootCmd.PersistentFlags().String(client.FlagChainID, "", "Chain ID of tendermint node")
rootCmd.PersistentFlags().String(flags.FlagChainID, "", "Chain ID of tendermint node")
rootCmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) error {
return initConfig(rootCmd)
}
Expand All @@ -58,7 +59,7 @@ func main() {
lcd.ServeCommand(cdc, registerRoutes),
keys.Commands(),
version.Cmd,
client.NewCompletionCmd(rootCmd, true),
flags.NewCompletionCmd(rootCmd, true),
)

executor := cli.PrepareMainCmd(rootCmd, "MESG", defaultCLIHome)
Expand Down Expand Up @@ -125,7 +126,7 @@ func initConfig(cmd *cobra.Command) error {
return err
}
}
if err := viper.BindPFlag(client.FlagChainID, cmd.PersistentFlags().Lookup(client.FlagChainID)); err != nil {
if err := viper.BindPFlag(flags.FlagChainID, cmd.PersistentFlags().Lookup(flags.FlagChainID)); err != nil {
return err
}
if err := viper.BindPFlag(cli.EncodingFlag, cmd.PersistentFlags().Lookup(cli.EncodingFlag)); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func defaultConfig() (*Config, error) {
c.Cosmos.StakeTokenDenom = "atto"
c.Cosmos.Bech32MainPrefix = "mesgtest"
c.Cosmos.CoinType = 470
c.Cosmos.FullFundraiserPath = "44'/470'/0'/0/0"
c.Cosmos.FullFundraiserPath = "44'/470'/0'/0/0" // TODO: is it really useful?
c.Cosmos.PowerReduction = 18

c.DevGenesis.ChainID = "mesg-dev-chain"
Expand Down
4 changes: 2 additions & 2 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func stopRunningServices(sdk *enginesdk.SDK, address string) error {
func loadOrGenConfigAccount(kb *cosmos.Keybase, cfg *config.Config) (keys.Info, error) {
if cfg.Account.Mnemonic != "" {
logrus.WithField("module", "main").Warn("Config account mnemonic presents. Generating account with it...")
return kb.CreateAccount(cfg.Account.Name, cfg.Account.Mnemonic, "", cfg.Account.Password, cfg.Account.Number, cfg.Account.Index)
return kb.CreateAccount(cfg.Account.Name, cfg.Account.Mnemonic, "", cfg.Account.Password, keys.CreateHDPath(cfg.Account.Number, cfg.Account.Index).String(), cosmos.DefaultAlgo)
}

exist, err := kb.Exist(cfg.Account.Name)
Expand All @@ -89,7 +89,7 @@ func loadOrGenConfigAccount(kb *cosmos.Keybase, cfg *config.Config) (keys.Info,
"password": cfg.Account.Password,
"mnemonic": mnemonic,
}).Warn("Account")
return kb.CreateAccount(cfg.Account.Name, mnemonic, "", cfg.Account.Password, cfg.Account.Number, cfg.Account.Index)
return kb.CreateAccount(cfg.Account.Name, mnemonic, "", cfg.Account.Password, keys.CreateHDPath(cfg.Account.Number, cfg.Account.Index).String(), cosmos.DefaultAlgo)
}

func loadOrGenDevGenesis(basicManager *module.BasicManager, kb *cosmos.Keybase, cfg *config.Config) (*tmtypes.GenesisDoc, error) {
Expand Down
19 changes: 6 additions & 13 deletions cosmos/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authutils "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
authExported "github.com/cosmos/cosmos-sdk/x/auth/exported"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/mesg-foundation/engine/codec"
"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/x/xreflect"
"github.com/mesg-foundation/engine/x/xstrings"
abci "github.com/tendermint/tendermint/abci/types"
tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
"github.com/tendermint/tendermint/node"
rpcclient "github.com/tendermint/tendermint/rpc/client"
tenderminttypes "github.com/tendermint/tendermint/types"
Expand All @@ -34,7 +34,7 @@ type Client struct {
minGasPrices sdktypes.DecCoins

// Local state
acc auth.Account
acc authExported.Account
getAccountMutex sync.Mutex
broadcastMutex sync.Mutex
}
Expand Down Expand Up @@ -136,11 +136,7 @@ func (c *Client) BuildAndBroadcastMsg(msg sdktypes.Msg) (*abci.ResponseDeliverTx
// Stream subscribes to the provided query and returns the hash of the matching ressources.
func (c *Client) Stream(ctx context.Context, query string) (chan hash.Hash, chan error, error) {
subscriber := xstrings.RandASCIILetters(8)
q, err := tmquery.New(query)
if err != nil {
return nil, nil, err
}
msgStream, err := c.EventBus.SubscribeUnbuffered(ctx, subscriber, q)
eventStream, err := c.Subscribe(ctx, subscriber, query, 0)
if err != nil {
return nil, nil, err
}
Expand All @@ -150,8 +146,8 @@ func (c *Client) Stream(ctx context.Context, query string) (chan hash.Hash, chan
loop:
for {
select {
case msg := <-msgStream.Out():
attrs := msg.Events()[EventHashType]
case event := <-eventStream:
attrs := event.Events[EventHashType]
// The following error might be too much as MAYBE if one transaction contains many messages, the events will be merged across the whole transaction
if len(attrs) != 1 {
errC <- fmt.Errorf("event %s has %d tag(s), but only 1 is expected", EventHashType, len(attrs))
Expand All @@ -164,9 +160,6 @@ func (c *Client) Stream(ctx context.Context, query string) (chan hash.Hash, chan
hashC <- hash
}
}
case <-msgStream.Cancelled():
errC <- msgStream.Err()
break loop
case <-ctx.Done():
break loop
}
Expand All @@ -179,7 +172,7 @@ func (c *Client) Stream(ctx context.Context, query string) (chan hash.Hash, chan
}

// GetAccount returns the local account.
func (c *Client) GetAccount() (auth.Account, error) {
func (c *Client) GetAccount() (authExported.Account, error) {
c.getAccountMutex.Lock()
defer c.getAccountMutex.Unlock()
if c.acc == nil {
Expand Down
27 changes: 19 additions & 8 deletions cosmos/errors.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
package cosmos

import (
"github.com/cosmos/cosmos-sdk/types"
"fmt"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

const (
// CodespaceMesg is a cosmos codespace for all mesg errors.
CodespaceMesg types.CodespaceType = "mesg"
CodespaceMesg = "mesg"
)

// Base mesg codes.
const (
CodeInternal types.CodeType = 1000
CodeValidation types.CodeType = 2000
CodeInternal uint32 = 1000
CodeValidation uint32 = 2000
CodeMarshal uint32 = 2001
CodeUnmarshal uint32 = 2002
)

// mesg errors
var (
ErrValidation = sdkerrors.Register(CodespaceMesg, CodeValidation, "validation failed")
ErrMarshal = sdkerrors.Register(CodespaceMesg, CodeMarshal, "failed to marshal") // TODO: to replace by cosmoserrors.ErrJSONMarshal if it makes sense
ErrUnmarshal = sdkerrors.Register(CodespaceMesg, CodeUnmarshal, "failed to unmarshal") // TODO: to replace by cosmoserrors.ErrJSONUnmarshal if it makes sense
)

// NewMesgErrorf creates error with given code type and mesg codespace.
func NewMesgErrorf(ct types.CodeType, format string, a ...interface{}) types.Error {
return types.NewError(CodespaceMesg, ct, format, a...)
func NewMesgErrorf(code uint32, format string, a ...interface{}) *sdkerrors.Error {
return sdkerrors.New(CodespaceMesg, code, fmt.Sprintf(format, a...))
}

// NewMesgWrapError creates error with given code type and mesg codespace.
func NewMesgWrapError(ct types.CodeType, err error) types.Error {
return types.NewError(CodespaceMesg, ct, err.Error())
func NewMesgWrapError(code uint32, err error) *sdkerrors.Error {
return sdkerrors.New(CodespaceMesg, code, err.Error())
}
13 changes: 7 additions & 6 deletions cosmos/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

sdktypes "github.com/cosmos/cosmos-sdk/types"
authutils "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/genaccounts"
"github.com/cosmos/cosmos-sdk/x/genutil"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/mesg-foundation/engine/codec"
Expand Down Expand Up @@ -126,23 +126,24 @@ func genGenesisDoc(appState map[string]json.RawMessage, chainID string, genesisT
}

func genGenesisAppState(defaultGenesisŚtate map[string]json.RawMessage, signedStdTx authtypes.StdTx, initialBalances string) (map[string]json.RawMessage, error) {
genAccs := []genaccounts.GenesisAccount{}
for _, signer := range signedStdTx.GetSigners() {
genAccs := authexported.GenesisAccounts{}
pubkeys := signedStdTx.GetPubKeys()
for i, signer := range signedStdTx.GetSigners() {
initialB, err := sdktypes.ParseCoins(initialBalances)
if err != nil {
return nil, err
}
genAcc := genaccounts.NewGenesisAccountRaw(signer, initialB, sdktypes.NewCoins(), 0, 0, "", "")
genAcc := authtypes.NewBaseAccount(signer, initialB, pubkeys[i], 0, 0)
if err := genAcc.Validate(); err != nil {
return nil, err
}
genAccs = append(genAccs, genAcc)
}
genstate, err := codec.MarshalJSON(genaccounts.GenesisState(genAccs))
genstate, err := codec.MarshalJSON(authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs))
if err != nil {
return nil, err
}
defaultGenesisŚtate[genaccounts.ModuleName] = genstate
defaultGenesisŚtate[authtypes.ModuleName] = genstate
return genutil.SetGenTxsInAppGenesisState(codec.Codec, defaultGenesisŚtate, []authtypes.StdTx{signedStdTx})
}

Expand Down
10 changes: 7 additions & 3 deletions cosmos/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ import (
"path/filepath"
"testing"

"github.com/cosmos/cosmos-sdk/crypto/keys"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/mesg-foundation/engine/codec"
"github.com/stretchr/testify/require"
)

func TestGenesis(t *testing.T) {
// codec
authtypes.RegisterCodec(codec.Codec)
stakingtypes.RegisterCodec(codec.Codec)

// path
path, _ := ioutil.TempDir("", "TestGenesis")
defer os.RemoveAll(path)
// keybase
kb, err := NewKeybase(filepath.Join(path, "kb"))
require.NoError(t, err)
// codec
stakingtypes.RegisterCodec(codec.Codec)
// variables
var (
chainID = "test-chainID"
Expand All @@ -37,7 +41,7 @@ func TestGenesis(t *testing.T) {
)
// init account
mnemonic, _ := kb.NewMnemonic()
kb.CreateAccount(name, mnemonic, "", password, 0, 0)
kb.CreateAccount(name, mnemonic, "", password, keys.CreateHDPath(0, 0).String(), DefaultAlgo)
// start tests
t.Run("generate validator", func(t *testing.T) {
v, err := NewGenesisValidator(kb, name, password, privValidatorKeyFile, privValidatorStateFile, nodeKeyFile)
Expand Down
35 changes: 22 additions & 13 deletions cosmos/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (

clientkey "github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
"github.com/cosmos/cosmos-sdk/crypto/keys/keyerror"
"github.com/cosmos/cosmos-sdk/types"
bip39 "github.com/cosmos/go-bip39"
"github.com/tendermint/tendermint/crypto"
)

const mnemonicEntropySize = 256
const (
mnemonicEntropySize = 256
DefaultAlgo = keys.Secp256k1
)

// Keybase is a standard cosmos keybase.
type Keybase struct {
Expand Down Expand Up @@ -111,17 +113,10 @@ func (kb *Keybase) CreateMnemonic(name string, language keys.Language, passwd st
}

// CreateAccount is a lock protected version of keys.CreateAccount
func (kb *Keybase) CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd string, account, index uint32) (keys.Info, error) {
func (kb *Keybase) CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd, hdPath string, algo keys.SigningAlgo) (keys.Info, error) {
kb.mx.Lock()
defer kb.mx.Unlock()
return kb.kb.CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd, account, index)
}

// Derive is a lock protected version of keys.Derive
func (kb *Keybase) Derive(name, mnemonic, bip39Passwd, encryptPasswd string, params hd.BIP44Params) (keys.Info, error) {
kb.mx.Lock()
defer kb.mx.Unlock()
return kb.kb.Derive(name, mnemonic, bip39Passwd, encryptPasswd, params)
return kb.kb.CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd, hdPath, algo)
}

// CreateLedger is a lock protected version of keys.CreateLedger
Expand All @@ -132,10 +127,10 @@ func (kb *Keybase) CreateLedger(name string, algo keys.SigningAlgo, hrp string,
}

// CreateOffline is a lock protected version of keys.CreateOffline
func (kb *Keybase) CreateOffline(name string, pubkey crypto.PubKey) (keys.Info, error) {
func (kb *Keybase) CreateOffline(name string, pubkey crypto.PubKey, algo keys.SigningAlgo) (keys.Info, error) {
kb.mx.Lock()
defer kb.mx.Unlock()
return kb.kb.CreateOffline(name, pubkey)
return kb.kb.CreateOffline(name, pubkey, algo)
}

// CreateMulti is a lock protected version of keys.CreateMulti
Expand Down Expand Up @@ -201,6 +196,20 @@ func (kb *Keybase) ExportPrivateKeyObject(name string, passphrase string) (crypt
return kb.kb.ExportPrivateKeyObject(name, passphrase)
}

// SupportedAlgos returns a list of signing algorithms supported by the keybase
func (kb *Keybase) SupportedAlgos() []keys.SigningAlgo {
kb.mx.Lock()
defer kb.mx.Unlock()
return kb.kb.SupportedAlgos()
}

// SupportedAlgosLedger returns a list of signing algorithms supported by the keybase's ledger integration
func (kb *Keybase) SupportedAlgosLedger() []keys.SigningAlgo {
kb.mx.Lock()
defer kb.mx.Unlock()
return kb.kb.SupportedAlgosLedger()
}

// CloseDB is a lock protected version of keys.CloseDB
func (kb *Keybase) CloseDB() {
kb.mx.Lock()
Expand Down
6 changes: 4 additions & 2 deletions cosmos/keybase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"testing"

"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/stretchr/testify/require"
)

Expand All @@ -27,7 +28,7 @@ func TestKeybase(t *testing.T) {
})

t.Run("Create", func(t *testing.T) {
acc, err := kb.CreateAccount(name, mnemonic, "", password, 0, 0)
acc, err := kb.CreateAccount(name, mnemonic, "", password, keys.CreateHDPath(0, 0).String(), DefaultAlgo)
require.NoError(t, err)
require.Equal(t, name, acc.GetName())
})
Expand All @@ -45,7 +46,8 @@ func TestKeybase(t *testing.T) {
t.Run("Sign", func(t *testing.T) {
name2 := "name2"
mnemonic2, _ := kb.NewMnemonic()
kb.CreateAccount(name2, mnemonic2, "", password, 0, 0)
_, err := kb.CreateAccount(name2, mnemonic2, "", password, keys.CreateHDPath(0, 0).String(), DefaultAlgo)
require.NoError(t, err)
hash := sha256.Sum256([]byte(name + ":" + password))
hash2 := sha256.Sum256([]byte(name2 + ":" + password))
for i := 0; i < 1000; i++ {
Expand Down
Loading

0 comments on commit ae1b6f7

Please sign in to comment.