Skip to content

Commit

Permalink
chore: release v0.18.6 (#1846)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgooz authored Nov 30, 2021
2 parents 31608e4 + 412c8bd commit 3bb9210
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 52 deletions.
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

0 comments on commit 3bb9210

Please sign in to comment.