From 25d976feb498d88d7b0b89f7fca847cde5b93fd3 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Mon, 9 Jul 2018 01:47:38 -0700 Subject: [PATCH] works? --- baseapp/baseapp_test.go | 10 +++++----- client/keys/add.go | 24 ++++++++++++------------ cmd/gaia/app/genesis.go | 12 ++++++------ server/init.go | 8 ++++---- x/auth/stdtx_test.go | 2 +- x/bank/client/cli/sendtx.go | 9 +++------ x/bank/test_helpers.go | 4 ++-- x/mock/app.go | 12 ++++++------ x/mock/app_test.go | 10 +++++----- x/mock/test_utils.go | 2 +- x/slashing/test_common.go | 6 +++--- x/stake/keeper/delegation.go | 12 ++---------- x/stake/keeper/key.go | 2 +- x/stake/types/delegation.go | 14 +++++++------- 14 files changed, 58 insertions(+), 69 deletions(-) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 66695bbf39..4448d0e339 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -285,9 +285,9 @@ type msgCounter struct { } // Implements Msg -func (msg msgCounter) Type() string { return typeMsgCounter } -func (msg msgCounter) GetSignBytes() []byte { return nil } -func (msg msgCounter) GetSigners() []sdk.Address { return nil } +func (msg msgCounter) Type() string { return typeMsgCounter } +func (msg msgCounter) GetSignBytes() []byte { return nil } +func (msg msgCounter) GetSigners() []sdk.AccAddress { return nil } func (msg msgCounter) ValidateBasic() sdk.Error { if msg.Counter >= 0 { return nil @@ -323,8 +323,8 @@ type msgCounter2 struct { } // Implements Msg -func (msg msgCounter2) Type() string { return typeMsgCounter2 } -func (msg msgCounter2) GetSignBytes() []byte { return nil } +func (msg msgCounter2) Type() string { return typeMsgCounter2 } +func (msg msgCounter2) GetSignBytes() []byte { return nil } func (msg msgCounter2) GetSigners() []sdk.AccAddress { return nil } func (msg msgCounter2) ValidateBasic() sdk.Error { if msg.Counter >= 0 { diff --git a/client/keys/add.go b/client/keys/add.go index d21f106380..82e6a120ee 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -161,12 +161,6 @@ type NewKeyBody struct { Password string `json:"password"` } -// new key response REST body -type NewKeyResponse struct { - Address sdk.AccAddress `json:"address"` - Mnemonic string `json:"mnemonic"` -} - // add new key REST handler func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) { var kb keys.Keybase @@ -215,18 +209,24 @@ func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte(err.Error())) return } - address := sdk.AccAddress(info.GetPubKey().Address().Bytes()) - bz, err := json.Marshal(NewKeyResponse{ - Address: address, - Mnemonic: mnemonic, - }) + + keyOutput, err := Bech32KeyOutput(info) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) return } - w.Write(output) + keyOutput.Seed = mnemonic + + bz, err := json.Marshal(keyOutput) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + w.Write(bz) } // function to just a new seed to display in the UI before actually persisting it in the keybase diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index baa363a448..0472c46ada 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -36,14 +36,14 @@ type GenesisAccount struct { func NewGenesisAccount(acc *auth.BaseAccount) GenesisAccount { return GenesisAccount{ - Address: sdk.MustBech32ifyAcc(acc.Address), + Address: acc.Address, Coins: acc.Coins, } } func NewGenesisAccountI(acc auth.Account) GenesisAccount { return GenesisAccount{ - Address: sdk.MustBech32ifyAcc(acc.GetAddress()), + Address: acc.GetAddress(), Coins: acc.GetCoins(), } } @@ -51,7 +51,7 @@ func NewGenesisAccountI(acc auth.Account) GenesisAccount { // convert GenesisAccount to auth.BaseAccount func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) { return &auth.BaseAccount{ - Address: sdk.MustGetAccAddressBech32(ga.Address), + Address: ga.Address, Coins: ga.Coins.Sort(), } } @@ -78,7 +78,7 @@ func GaiaAppInit() server.AppInit { type GaiaGenTx struct { Name string `json:"name"` Address sdk.AccAddress `json:"address"` - PubKey string `json:"pub_key"` + PubKey string `json:"pub_key"` } // Generate a gaia genesis transaction with flags @@ -153,7 +153,7 @@ func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (genesisState } // create the genesis account, give'm few steaks and a buncha token with there name - accAuth := auth.NewBaseAccountWithAddress(sdk.MustGetAccAddressBech32(genTx.Address)) + accAuth := auth.NewBaseAccountWithAddress(genTx.Address) accAuth.Coins = sdk.Coins{ {genTx.Name + "Token", sdk.NewInt(1000)}, {"steak", sdk.NewInt(freeFermionsAcc)}, @@ -165,7 +165,7 @@ func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (genesisState // add the validator if len(genTx.Name) > 0 { desc := stake.NewDescription(genTx.Name, "", "", "") - validator := stake.NewValidator(sdk.MustGetAccAddressBech32(genTx.Address), + validator := stake.NewValidator(genTx.Address, sdk.MustGetAccPubKeyBech32(genTx.PubKey), desc) stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens + freeFermionVal // increase the supply diff --git a/server/init.go b/server/init.go index ff8565c8a4..ef78081868 100644 --- a/server/init.go +++ b/server/init.go @@ -454,7 +454,7 @@ func GenerateCoinKey() (sdk.AccAddress, string, error) { // generate a private key, with recovery phrase info, secret, err := keybase.CreateMnemonic("name", keys.English, "pass", keys.Secp256k1) if err != nil { - return "", "", err + return sdk.AccAddress([]byte{}), "", err } addr := info.GetPubKey().Address() return sdk.AccAddress(addr), secret, nil @@ -467,21 +467,21 @@ func GenerateSaveCoinKey(clientRoot, keyName, keyPass string, overwrite bool) (s // get the keystore from the client keybase, err := clkeys.GetKeyBaseFromDir(clientRoot) if err != nil { - return "", "", err + return sdk.AccAddress([]byte{}), "", err } // ensure no overwrite if !overwrite { _, err := keybase.Get(keyName) if err == nil { - return "", "", errors.New("key already exists, overwrite is disabled") + return sdk.AccAddress([]byte{}), "", errors.New("key already exists, overwrite is disabled") } } // generate a private key, with recovery phrase info, secret, err := keybase.CreateMnemonic(keyName, keys.English, keyPass, keys.Secp256k1) if err != nil { - return "", "", err + return sdk.AccAddress([]byte{}), "", err } addr := info.GetPubKey().Address() return sdk.AccAddress(addr), secret, nil diff --git a/x/auth/stdtx_test.go b/x/auth/stdtx_test.go index 841470431d..bc94396637 100644 --- a/x/auth/stdtx_test.go +++ b/x/auth/stdtx_test.go @@ -28,7 +28,7 @@ func TestStdTx(t *testing.T) { func TestStdSignBytes(t *testing.T) { priv := crypto.GenPrivKeyEd25519() - addr := priv.PubKey().Address() + addr := sdk.AccAddress(priv.PubKey().Address()) msgs := []sdk.Msg{sdk.NewTestMsg(addr)} fee := newStdFee() signMsg := StdSignMsg{ diff --git a/x/bank/client/cli/sendtx.go b/x/bank/client/cli/sendtx.go index 8731de40e9..81a1f1fe2f 100644 --- a/x/bank/client/cli/sendtx.go +++ b/x/bank/client/cli/sendtx.go @@ -1,7 +1,7 @@ package cli import ( - "errors" + "github.com/pkg/errors" "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" @@ -37,11 +37,9 @@ func SendTxCmd(cdc *wire.Codec) *cobra.Command { return err } - bech32From := sdk.MustBech32ifyAcc(from) // Check if account was found if fromAcc == nil { - return errors.New("No account with address " + bech32From + - " was found in the state.\nAre you sure there has been a transaction involving it?") + return errors.Errorf("No account with address %s was found in the state.\nAre you sure there has been a transaction involving it?", from) } toStr := viper.GetString(flagTo) @@ -63,8 +61,7 @@ func SendTxCmd(cdc *wire.Codec) *cobra.Command { return err } if !account.GetCoins().IsGTE(coins) { - return errors.New("Address " + bech32From + - " doesn't have enough coins to pay for this transaction.") + return errors.Errorf("Address %s doesn't have enough coins to pay for this transaction.", from) } // build and sign the transaction, then broadcast to Tendermint diff --git a/x/bank/test_helpers.go b/x/bank/test_helpers.go index ecaae62817..1dad0ba267 100644 --- a/x/bank/test_helpers.go +++ b/x/bank/test_helpers.go @@ -58,7 +58,7 @@ func TotalCoinsInvariant(t *testing.T, app *mock.App, log string) { // accounts already exist. func TestAndRunSingleInputMsgSend(t *testing.T, r *rand.Rand, app *mock.App, ctx sdk.Context, keys []crypto.PrivKey, log string) (action string, err sdk.Error) { fromKey := keys[r.Intn(len(keys))] - fromAddr := fromKey.PubKey().Address() + fromAddr := sdk.AccAddress(fromKey.PubKey().Address()) toKey := keys[r.Intn(len(keys))] // Disallow sending money to yourself for { @@ -67,7 +67,7 @@ func TestAndRunSingleInputMsgSend(t *testing.T, r *rand.Rand, app *mock.App, ctx } toKey = keys[r.Intn(len(keys))] } - toAddr := toKey.PubKey().Address() + toAddr := sdk.AccAddress(toKey.PubKey().Address()) initFromCoins := app.AccountMapper.GetAccount(ctx, fromAddr).GetCoins() denomIndex := r.Intn(len(initFromCoins)) diff --git a/x/mock/app.go b/x/mock/app.go index dd8edb102f..83d17ba681 100644 --- a/x/mock/app.go +++ b/x/mock/app.go @@ -95,11 +95,11 @@ func (app *App) InitChainer(ctx sdk.Context, _ abci.RequestInitChain) abci.Respo // CreateGenAccounts generates genesis accounts loaded with coins, and returns // their addresses, pubkeys, and privkeys. -func CreateGenAccounts(numAccs int, genCoins sdk.Coins) (genAccs []auth.Account, addrs []sdk.Address, pubKeys []crypto.PubKey, privKeys []crypto.PrivKey) { +func CreateGenAccounts(numAccs int, genCoins sdk.Coins) (genAccs []auth.Account, addrs []sdk.AccAddress, pubKeys []crypto.PubKey, privKeys []crypto.PrivKey) { for i := 0; i < numAccs; i++ { privKey := crypto.GenPrivKeyEd25519() pubKey := privKey.PubKey() - addr := pubKey.Address() + addr := sdk.AccAddress(pubKey.Address()) genAcc := &auth.BaseAccount{ Address: addr, @@ -166,19 +166,19 @@ func GeneratePrivKeys(n int) (keys []crypto.PrivKey) { // GeneratePrivKeyAddressPairs generates a total of n private key, address // pairs. -func GeneratePrivKeyAddressPairs(n int) (keys []crypto.PrivKey, addrs []sdk.Address) { +func GeneratePrivKeyAddressPairs(n int) (keys []crypto.PrivKey, addrs []sdk.AccAddress) { keys = make([]crypto.PrivKey, n, n) - addrs = make([]sdk.Address, n, n) + addrs = make([]sdk.AccAddress, n, n) for i := 0; i < n; i++ { keys[i] = crypto.GenPrivKeyEd25519() - addrs[i] = keys[i].PubKey().Address() + addrs[i] = sdk.AccAddress(keys[i].PubKey().Address()) } return } // RandomSetGenesis set genesis accounts with random coin values using the // provided addresses and coin denominations. -func RandomSetGenesis(r *rand.Rand, app *App, addrs []sdk.Address, denoms []string) { +func RandomSetGenesis(r *rand.Rand, app *App, addrs []sdk.AccAddress, denoms []string) { accts := make([]auth.Account, len(addrs), len(addrs)) randCoinIntervals := []BigInterval{ {sdk.NewIntWithDecimal(1, 0), sdk.NewIntWithDecimal(1, 1)}, diff --git a/x/mock/app_test.go b/x/mock/app_test.go index 7ee82474ce..0890b53ecc 100644 --- a/x/mock/app_test.go +++ b/x/mock/app_test.go @@ -19,7 +19,7 @@ var ( // testMsg is a mock transaction that has a validation which can fail. type testMsg struct { - signers []sdk.Address + signers []sdk.AccAddress positiveNum int64 } @@ -27,7 +27,7 @@ func (tx testMsg) Type() string { return msgType } func (tx testMsg) GetMsg() sdk.Msg { return tx } func (tx testMsg) GetMemo() string { return "" } func (tx testMsg) GetSignBytes() []byte { return nil } -func (tx testMsg) GetSigners() []sdk.Address { return tx.signers } +func (tx testMsg) GetSigners() []sdk.AccAddress { return tx.signers } func (tx testMsg) GetSignatures() []auth.StdSignature { return nil } func (tx testMsg) ValidateBasic() sdk.Error { if tx.positiveNum >= 0 { @@ -53,7 +53,7 @@ func TestCheckAndDeliverGenTx(t *testing.T) { SetGenesis(mApp, accs) ctxCheck := mApp.BaseApp.NewContext(true, abci.Header{}) - msg := testMsg{signers: []sdk.Address{addrs[0]}, positiveNum: 1} + msg := testMsg{signers: []sdk.AccAddress{addrs[0]}, positiveNum: 1} acct := mApp.AccountMapper.GetAccount(ctxCheck, addrs[0]) require.Equal(t, accs[0], acct.(*auth.BaseAccount)) @@ -86,14 +86,14 @@ func TestCheckGenTx(t *testing.T) { SetGenesis(mApp, accs) - msg1 := testMsg{signers: []sdk.Address{addrs[0]}, positiveNum: 1} + msg1 := testMsg{signers: []sdk.AccAddress{addrs[0]}, positiveNum: 1} CheckGenTx( t, mApp.BaseApp, []sdk.Msg{msg1}, []int64{accs[0].GetAccountNumber()}, []int64{accs[0].GetSequence()}, true, privKeys[0], ) - msg2 := testMsg{signers: []sdk.Address{addrs[0]}, positiveNum: -1} + msg2 := testMsg{signers: []sdk.AccAddress{addrs[0]}, positiveNum: -1} CheckGenTx( t, mApp.BaseApp, []sdk.Msg{msg2}, []int64{accs[0].GetAccountNumber()}, []int64{accs[0].GetSequence()}, diff --git a/x/mock/test_utils.go b/x/mock/test_utils.go index f9620c1403..f9e1e8f5e9 100644 --- a/x/mock/test_utils.go +++ b/x/mock/test_utils.go @@ -11,7 +11,7 @@ import ( ) // CheckBalance checks the balance of an account. -func CheckBalance(t *testing.T, app *App, addr sdk.Address, exp sdk.Coins) { +func CheckBalance(t *testing.T, app *App, addr sdk.AccAddress, exp sdk.Coins) { ctxCheck := app.BaseApp.NewContext(true, abci.Header{}) res := app.AccountMapper.GetAccount(ctxCheck, addr) diff --git a/x/slashing/test_common.go b/x/slashing/test_common.go index 41a13e3886..c5320cb24f 100644 --- a/x/slashing/test_common.go +++ b/x/slashing/test_common.go @@ -29,9 +29,9 @@ var ( newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"), } addrs = []sdk.AccAddress{ - pks[0].Address(), - pks[1].Address(), - pks[2].Address(), + sdk.AccAddress(pks[0].Address()), + sdk.AccAddress(pks[1].Address()), + sdk.AccAddress(pks[2].Address()), } initCoins sdk.Int = sdk.NewInt(200) ) diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index c4b393f271..e2f96f3d9f 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -93,11 +93,7 @@ func (k Keeper) GetUnbondingDelegation(ctx sdk.Context, } // load all unbonding delegations from a particular validator -<<<<<<< HEAD -func (k Keeper) GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sdk.Address) (ubds []types.UnbondingDelegation) { -======= -func (k Keeper) GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sdk.AccAddress) (unbondingDelegations []types.UnbondingDelegation) { ->>>>>>> asdf +func (k Keeper) GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sdk.AccAddress) (ubds []types.UnbondingDelegation) { store := ctx.KVStore(k.storeKey) iterator := sdk.KVStorePrefixIterator(store, GetUBDsByValIndexKey(valAddr)) for { @@ -149,11 +145,7 @@ func (k Keeper) GetRedelegation(ctx sdk.Context, } // load all redelegations from a particular validator -<<<<<<< HEAD -func (k Keeper) GetRedelegationsFromValidator(ctx sdk.Context, valAddr sdk.Address) (reds []types.Redelegation) { -======= -func (k Keeper) GetRedelegationsFromValidator(ctx sdk.Context, valAddr sdk.AccAddress) (redelegations []types.Redelegation) { ->>>>>>> asdf +func (k Keeper) GetRedelegationsFromValidator(ctx sdk.Context, valAddr sdk.AccAddress) (reds []types.Redelegation) { store := ctx.KVStore(k.storeKey) iterator := sdk.KVStorePrefixIterator(store, GetREDsFromValSrcIndexKey(valAddr)) for { diff --git a/x/stake/keeper/key.go b/x/stake/keeper/key.go index 31f1c608fe..132add34a6 100644 --- a/x/stake/keeper/key.go +++ b/x/stake/keeper/key.go @@ -225,7 +225,7 @@ func GetREDsToValDstIndexKey(validatorDstAddr sdk.AccAddress) []byte { // get the prefix keyspace for all redelegations redelegating towards a destination validator // from a particular delegator -func GetREDsByDelToValDstIndexKey(delegatorAddr sdk.Address, validatorDstAddr sdk.AccAddress) []byte { +func GetREDsByDelToValDstIndexKey(delegatorAddr, validatorDstAddr sdk.AccAddress) []byte { return append( GetREDsToValDstIndexKey(validatorDstAddr), delegatorAddr.Bytes()...) diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index 6f923aaea3..2ceedd5f31 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -55,8 +55,8 @@ func UnmarshalDelegation(cdc *wire.Codec, key, value []byte) (delegation Delegat err = errors.New("unexpected key length") return } - delAddr := sdk.Address(addrs[:sdk.AddrLen]) - valAddr := sdk.Address(addrs[sdk.AddrLen:]) + delAddr := sdk.AccAddress(addrs[:sdk.AddrLen]) + valAddr := sdk.AccAddress(addrs[sdk.AddrLen:]) return Delegation{ DelegatorAddr: delAddr, @@ -145,8 +145,8 @@ func UnmarshalUBD(cdc *wire.Codec, key, value []byte) (ubd UnbondingDelegation, err = errors.New("unexpected key length") return } - delAddr := sdk.Address(addrs[:sdk.AddrLen]) - valAddr := sdk.Address(addrs[sdk.AddrLen:]) + delAddr := sdk.AccAddress(addrs[:sdk.AddrLen]) + valAddr := sdk.AccAddress(addrs[sdk.AddrLen:]) return UnbondingDelegation{ DelegatorAddr: delAddr, @@ -237,9 +237,9 @@ func UnmarshalRED(cdc *wire.Codec, key, value []byte) (red Redelegation, err err err = errors.New("unexpected key length") return } - delAddr := sdk.Address(addrs[:sdk.AddrLen]) - valSrcAddr := sdk.Address(addrs[sdk.AddrLen : 2*sdk.AddrLen]) - valDstAddr := sdk.Address(addrs[2*sdk.AddrLen:]) + delAddr := sdk.AccAddress(addrs[:sdk.AddrLen]) + valSrcAddr := sdk.AccAddress(addrs[sdk.AddrLen : 2*sdk.AddrLen]) + valDstAddr := sdk.AccAddress(addrs[2*sdk.AddrLen:]) return Redelegation{ DelegatorAddr: delAddr,