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

Use sequence number from cosmos auth module #1462

Merged
merged 8 commits into from
Nov 7, 2019
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
45 changes: 44 additions & 1 deletion cosmos/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/node"
Expand Down Expand Up @@ -47,8 +48,24 @@ func (c *Client) Query(path string, data cmn.HexBytes, ptr interface{}) error {
}

// BuildAndBroadcastMsg builds and signs message and broadcast it to node.
func (c *Client) BuildAndBroadcastMsg(msg sdktypes.Msg, accName, accPassword string, accNumber, accSeq uint64) (*abci.ResponseDeliverTx, error) {
func (c *Client) BuildAndBroadcastMsg(msg sdktypes.Msg, accName, accPassword string) (*abci.ResponseDeliverTx, error) {
info, err := c.kb.Get(accName)
if err != nil {
return nil, err
}

accRetriever := auth.NewAccountRetriever(c)
accNumber, accSeq := uint64(0), uint64(0)
err = accRetriever.EnsureExists(info.GetAddress())
if err == nil {
accNumber, accSeq, err = accRetriever.GetAccountNumberSequence(info.GetAddress())
if err != nil {
return nil, err
}
}

txBuilder := NewTxBuilder(c.cdc, accNumber, accSeq, c.kb, c.chainID)

// TODO: cannot sign 2 tx at the same time. Maybe keybase cannot be access at the same time. Add a lock?
signedTx, err := txBuilder.BuildAndSignStdTx(msg, accName, accPassword)
if err != nil {
Expand Down Expand Up @@ -94,3 +111,29 @@ func (c *Client) BuildAndBroadcastMsg(msg sdktypes.Msg, accName, accPassword str
return nil, errors.New("i/o timeout")
}
}

// QueryWithData performs a query to a Tendermint node with the provided path
// and a data payload. It returns the result and height of the query upon success
// or an error if the query fails.
func (c *Client) QueryWithData(path string, data []byte) ([]byte, int64, error) {
return c.query(path, data)
}

// query performs a query to a Tendermint node with the provided store name
// and path. It returns the result and height of the query upon success
// or an error if the query fails. In addition, it will verify the returned
// proof if TrustNode is disabled. If proof verification fails or the query
// height is invalid, an error will be returned.
func (c *Client) query(path string, key cmn.HexBytes) (res []byte, height int64, err error) {
antho1404 marked this conversation as resolved.
Show resolved Hide resolved
result, err := c.ABCIQuery(path, key)
if err != nil {
return res, height, err
}

resp := result.Response
if !resp.IsOK() {
return res, resp.Height, errors.New(resp.Log)
}

return resp.Value, resp.Height, nil
}
6 changes: 2 additions & 4 deletions sdk/runner/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func (s *SDK) Create(req *api.CreateRunnerRequest, accountName, accountPassword
return nil, err
}
// TODO: pass account by parameters
accNumber, accSeq := uint64(0), uint64(0)
user, err := cosmostypes.AccAddressFromBech32(account.Address)
if err != nil {
return nil, err
Expand Down Expand Up @@ -94,7 +93,7 @@ func (s *SDK) Create(req *api.CreateRunnerRequest, accountName, accountPassword
}

msg := newMsgCreateRunner(s.cdc, user, req.ServiceHash, envHash)
tx, err := s.client.BuildAndBroadcastMsg(msg, accountName, accountPassword, accNumber, accSeq)
tx, err := s.client.BuildAndBroadcastMsg(msg, accountName, accountPassword)
if err != nil {
defer onError()
if err == mempool.ErrTxInCache {
Expand All @@ -112,7 +111,6 @@ func (s *SDK) Delete(req *api.DeleteRunnerRequest, accountName, accountPassword
return err
}
// TODO: pass account by parameters
accNumber, accSeq := uint64(0), uint64(0)
user, err := cosmostypes.AccAddressFromBech32(account.Address)
if err != nil {
return err
Expand All @@ -125,7 +123,7 @@ func (s *SDK) Delete(req *api.DeleteRunnerRequest, accountName, accountPassword
}

msg := newMsgDeleteRunner(s.cdc, user, req.Hash)
_, err = s.client.BuildAndBroadcastMsg(msg, accountName, accountPassword, accNumber, accSeq)
_, err = s.client.BuildAndBroadcastMsg(msg, accountName, accountPassword)
if err != nil {
if err == mempool.ErrTxInCache {
return fmt.Errorf("runner already deleted: %w", err)
Expand Down
3 changes: 1 addition & 2 deletions sdk/service/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ func (s *SDK) Create(req *api.CreateServiceRequest, accountName, accountPassword
return nil, err
}
// TODO: pass account by parameters
accNumber, accSeq := uint64(0), uint64(0)
owner, err := cosmostypes.AccAddressFromBech32(account.Address)
if err != nil {
return nil, err
}
msg := newMsgCreateService(s.cdc, req, owner)
tx, err := s.client.BuildAndBroadcastMsg(msg, accountName, accountPassword, accNumber, accSeq)
tx, err := s.client.BuildAndBroadcastMsg(msg, accountName, accountPassword)
if err != nil {
if err == mempool.ErrTxInCache {
return nil, fmt.Errorf("service already exists: %w", err)
Expand Down