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
46 changes: 44 additions & 2 deletions 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 All @@ -24,6 +25,10 @@ type Client struct {
chainID string
}

// AccSeq is a sequence number that could be used to build the transaction.
// It should be increased after every usage.
var AccSeq uint64
antho1404 marked this conversation as resolved.
Show resolved Hide resolved

// NewClient returns a rpc tendermint client.
func NewClient(node *node.Node, cdc *codec.Codec, kb keys.Keybase, chainID string) *Client {
return &Client{
Expand All @@ -47,8 +52,19 @@ 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) {
txBuilder := NewTxBuilder(c.cdc, accNumber, accSeq, c.kb, c.chainID)
func (c *Client) BuildAndBroadcastMsg(msg sdktypes.Msg, accName, accPassword string, accNumber uint64) (*abci.ResponseDeliverTx, error) {
antho1404 marked this conversation as resolved.
Show resolved Hide resolved
info, err := c.kb.Get(accName)
if err != nil {
return nil, err
}

acc, err := auth.NewAccountRetriever(c).GetAccount(info.GetAddress())
antho1404 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

txBuilder := NewTxBuilder(c.cdc, acc.GetAccountNumber(), acc.GetSequence(), 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 +110,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
}
4 changes: 2 additions & 2 deletions sdk/service/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ func (s *SDK) Create(req *api.CreateServiceRequest, accountName, accountPassword
return nil, err
}
// TODO: pass account by parameters
accNumber, accSeq := uint64(0), uint64(0)
accNumber := uint64(0)
antho1404 marked this conversation as resolved.
Show resolved Hide resolved
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, accNumber)
antho1404 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
if err == mempool.ErrTxInCache {
return nil, fmt.Errorf("service already exists: %w", err)
Expand Down