Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix account number used for signing tx #1598

Merged
merged 6 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Dockerfile

# build
/bin
!/bin/engine

# repo files
README.md
CHANGELOG.md
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type Config struct {
Account struct {
Name string `validate:"required"`
Password string `validate:"required"`
Number uint32
Index uint32
Mnemonic string
}
}
Expand Down Expand Up @@ -97,6 +99,8 @@ func defaultConfig() (*Config, error) {

c.Account.Name = "engine"
c.Account.Password = "pass"
c.Account.Number = uint32(0)
c.Account.Index = uint32(0)

return &c, nil
}
Expand Down
2 changes: 2 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestDefaultConfig(t *testing.T) {
require.Equal(t, "engine", c.Name)
require.Equal(t, "engine", c.Account.Name)
require.Equal(t, "pass", c.Account.Password)
require.Equal(t, uint32(0), c.Account.Number)
require.Equal(t, uint32(0), c.Account.Index)
}

func TestEnv(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,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, cosmos.AccNumber, cosmos.AccIndex)
return kb.CreateAccount(cfg.Account.Name, cfg.Account.Mnemonic, "", cfg.Account.Password, cfg.Account.Number, cfg.Account.Index)
}

exist, err := kb.Exist(cfg.Account.Name)
Expand All @@ -82,7 +82,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, cosmos.AccNumber, cosmos.AccIndex)
return kb.CreateAccount(cfg.Account.Name, mnemonic, "", cfg.Account.Password, cfg.Account.Number, cfg.Account.Index)
}

func loadOrGenDevGenesis(app *cosmos.App, kb *cosmos.Keybase, cfg *config.Config) (*tmtypes.GenesisDoc, error) {
Expand Down
18 changes: 10 additions & 8 deletions cosmos/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,19 @@ func (c *Client) GetAccount() (auth.Account, error) {
accKb.GetAddress(),
nil,
accKb.GetPubKey(),
AccNumber,
0,
0,
)
}
localSeq := c.acc.GetSequence()
if accR, err := auth.NewAccountRetriever(c).GetAccount(c.acc.GetAddress()); err == nil {
c.acc = accR
// replace seq if sup
if localSeq > c.acc.GetSequence() {
c.acc.SetSequence(localSeq)
}
accR, err := auth.NewAccountRetriever(c).GetAccount(c.acc.GetAddress())
if err != nil {
return nil, err
}
c.acc = accR
// replace seq if sup
if localSeq > c.acc.GetSequence() {
c.acc.SetSequence(localSeq)
}
return c.acc, nil
}
Expand All @@ -212,7 +214,7 @@ func (c *Client) sign(msg sdktypes.Msg) (tenderminttypes.Tx, error) {
return nil, err
}

txBuilder := NewTxBuilder(sequence, c.kb, c.chainID, minGasPrices)
txBuilder := NewTxBuilder(acc.GetAccountNumber(), sequence, c.kb, c.chainID, minGasPrices)

// simulate tx to estimate the gas
if txBuilder.SimulateAndExecute() {
Expand Down
3 changes: 2 additions & 1 deletion cosmos/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ func GenGenesis(kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, ch
msgs = append(msgs, genCreateValidatorMsg(acc.GetAddress(), validator.Name, validator.ValPubKey))
}
// generate genesis transaction
accNumber := uint64(0)
sequence := uint64(0)
b := NewTxBuilder(sequence, kb, chainID, sdktypes.DecCoins{})
b := NewTxBuilder(accNumber, sequence, kb, chainID, sdktypes.DecCoins{})
signedMsg, err := b.BuildSignMsg(msgs)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion cosmos/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestGenesis(t *testing.T) {
)
// init account
mnemonic, _ := kb.NewMnemonic()
kb.CreateAccount(name, mnemonic, "", password, AccNumber, AccIndex)
kb.CreateAccount(name, mnemonic, "", password, 0, 0)
// start tests
t.Run("generate validator", func(t *testing.T) {
v, err := NewGenesisValidator(kb, name, password, privValidatorKeyFile, privValidatorStateFile, nodeKeyFile)
Expand Down
9 changes: 1 addition & 8 deletions cosmos/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ import (
"github.com/tendermint/tendermint/crypto"
)

const (
// AccNumber is the account number of the account in keybase.
AccNumber = 0
// AccIndex is the account index of the account in keybase.
AccIndex = 0

mnemonicEntropySize = 256
)
const mnemonicEntropySize = 256

// Keybase is a standard cosmos keybase.
type Keybase struct {
Expand Down
2 changes: 1 addition & 1 deletion cosmos/keybase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestKeybase(t *testing.T) {
})

t.Run("Create", func(t *testing.T) {
acc, err := kb.CreateAccount(name, mnemonic, "", password, AccNumber, AccIndex)
acc, err := kb.CreateAccount(name, mnemonic, "", password, 0, 0)
require.NoError(t, err)
require.Equal(t, name, acc.GetName())
})
Expand Down
4 changes: 2 additions & 2 deletions cosmos/txbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type TxBuilder struct {
}

// NewTxBuilder returns a new initialized TxBuilder.
func NewTxBuilder(accSeq uint64, kb keys.Keybase, chainID string, minGasPrices sdktypes.DecCoins) TxBuilder {
func NewTxBuilder(accNumber, accSeq uint64, kb keys.Keybase, chainID string, minGasPrices sdktypes.DecCoins) TxBuilder {
return TxBuilder{
authtypes.NewTxBuilder(
authutils.GetTxEncoder(codec.Codec),
AccNumber,
accNumber,
accSeq,
flags.DefaultGasLimit,
flags.DefaultGasAdjustment,
Expand Down