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(pkg/cosmosclient): ensure account has funds after faucet transfer #1846

Merged
merged 1 commit into from
Nov 30, 2021
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/briandowns/spinner v1.11.1
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/charmbracelet/glow v1.4.0
github.com/containerd/containerd v1.5.8 // indirect
github.com/cosmos/cosmos-sdk v0.44.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/cenkalti/backoff/v4 v4.1.2 h1:6Yo7N8UP2K6LWZnW94DLVSSrbobcWdVzAYOisuDPIFo=
github.com/cenkalti/backoff/v4 v4.1.2/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
Expand Down
4 changes: 4 additions & 0 deletions starport/pkg/chaincmd/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ const (
optionValidatorGasPrices = "--gas-prices"
optionYes = "--yes"
optionHomeClient = "--home-client"
optionBroadcastMode = "--broadcast-mode"

constTendermint = "tendermint"
constJSON = "json"
constSync = "sync"
)

type KeyringBackend string
Expand Down Expand Up @@ -435,6 +437,8 @@ func (c ChainCmd) BankSendCommand(fromAddress, toAddress, amount string) step.Op
fromAddress,
toAddress,
amount,
optionBroadcastMode,
constSync,
optionYes,
)

Expand Down
100 changes: 48 additions & 52 deletions starport/pkg/cosmosclient/cosmosclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ package cosmosclient
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand All @@ -24,6 +23,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
proto "github.com/gogo/protobuf/proto"
prototypes "github.com/gogo/protobuf/types"
Expand All @@ -33,15 +33,20 @@ import (
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)

// FaucetTransferEnsureDuration is the duration that BroadcastTx will wait when a faucet transfer
// is triggered prior to broadcasting but transfer's tx is not committed in the state yet.
var FaucetTransferEnsureDuration = time.Minute * 2

const (
defaultNodeAddress = "http://localhost:26657"
defaultGasAdjustment = 1.0
defaultGasLimit = 300000
)

const (
faucetDenom = "token"
faucetMinAmount = 100
defaultFaucetAddress = "http://localhost:4500"
defaultFaucetDenom = "token"
defaultFaucetMinAmount = 100
)

// Client is a client to access your chain by querying and broadcasting transactions.
Expand All @@ -61,14 +66,13 @@ type Client struct {
addressPrefix string

nodeAddress string
apiAddress string
out io.Writer
chainID string

useFaucet bool
faucetAddress string
faucetDenom string
faucetMinAmount int64
faucetMinAmount uint64

homePath string
keyringServiceName string
Expand Down Expand Up @@ -110,19 +114,13 @@ func WithNodeAddress(addr string) Option {
}
}

func WithAPIAddress(addr string) Option {
return func(c *Client) {
c.apiAddress = addr
}
}

func WithAddressPrefix(prefix string) Option {
return func(c *Client) {
c.addressPrefix = prefix
}
}

func WithUseFaucet(faucetAddress, denom string, minAmount int64) Option {
func WithUseFaucet(faucetAddress, denom string, minAmount uint64) Option {
return func(c *Client) {
c.useFaucet = true
c.faucetAddress = faucetAddress
Expand All @@ -138,10 +136,13 @@ func WithUseFaucet(faucetAddress, denom string, minAmount int64) Option {
// New creates a new client with given options.
func New(ctx context.Context, options ...Option) (Client, error) {
c := Client{
nodeAddress: defaultNodeAddress,
keyringBackend: cosmosaccount.KeyringTest,
addressPrefix: "cosmos",
out: io.Discard,
nodeAddress: defaultNodeAddress,
keyringBackend: cosmosaccount.KeyringTest,
addressPrefix: "cosmos",
faucetAddress: defaultFaucetAddress,
faucetDenom: defaultFaucetDenom,
faucetMinAmount: defaultFaucetMinAmount,
out: io.Discard,
}

var err error
Expand Down Expand Up @@ -334,39 +335,8 @@ func (c *Client) prepareBroadcast(ctx context.Context, accountName string, _ []s
// makeSureAccountHasTokens makes sure the address has a positive balance
// it requests funds from the faucet if the address has an empty balance
func (c *Client) makeSureAccountHasTokens(ctx context.Context, address string) error {
// check the balance.
balancesEndpoint := fmt.Sprintf("%s/bank/balances/%s", c.apiAddress, address)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, balancesEndpoint, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

var balances struct {
Result []struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
} `json:"result"`
}
if err := json.NewDecoder(resp.Body).Decode(&balances); err != nil {
return err
}

// if the balance is enough do nothing.
if len(balances.Result) > 0 {
for _, c := range balances.Result {
amount, err := strconv.ParseInt(c.Amount, 10, 32)
if err != nil {
return err
}
if c.Denom == faucetDenom && amount >= faucetMinAmount {
return nil
}
}
if err := c.checkAccountBalance(ctx, address); err == nil {
return nil
}

// request coins from the faucet.
Expand All @@ -384,7 +354,33 @@ func (c *Client) makeSureAccountHasTokens(ctx context.Context, address string) e
}
}

return nil
// make sure funds are retrieved.
ctx, cancel := context.WithTimeout(ctx, FaucetTransferEnsureDuration)
defer cancel()

return backoff.Retry(func() error {
return c.checkAccountBalance(ctx, address)
}, backoff.WithContext(backoff.NewConstantBackOff(time.Second), ctx))
}

func (c *Client) checkAccountBalance(ctx context.Context, address string) (err error) {
balancesResp, err := banktypes.NewQueryClient(c.Context).AllBalances(ctx, &banktypes.QueryAllBalancesRequest{
Address: address,
})
if err != nil {
return err
}

// if the balance is enough do nothing.
if len(balancesResp.Balances) > 0 {
for _, coin := range balancesResp.Balances {
if coin.Denom == c.faucetDenom && coin.Amount.Uint64() >= c.faucetMinAmount {
return nil
}
}
}

return errors.New("account has not enough balance")
}

// handleBroadcastResult handles the result of broadcast messages result and checks if an error occurred
Expand Down