Skip to content

Commit

Permalink
feat: cosmos 046 upgrade (#343)
Browse files Browse the repository at this point in the history
- upgrade to cosmos-sdk 046
  • Loading branch information
RafilxTenfen authored Aug 30, 2022
1 parent 9d0da9a commit b22361c
Show file tree
Hide file tree
Showing 30 changed files with 7,519 additions and 3,138 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RUN make install

# Fetch umeed binary
FROM base-builder AS umeed-builder
ARG UMEE_VERSION=v2.0.0
ARG UMEE_VERSION=main
ENV PACKAGES curl eudev-dev
RUN apk add --no-cache $PACKAGES
WORKDIR /downloads/
Expand Down
8 changes: 0 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ else
@go test -mod=readonly $(ARGS) $(TEST_PACKAGES)
endif

build-docker-test:
@echo "--> Building docker image..."
@docker build -f Dockerfile.test -t peggo-test .

docker-test:
@echo "--> Running tests in docker..."
@docker run peggo-test

test-integration:
@echo "--> Running tests"
@go test -mod=readonly -race ./test/... -v
Expand Down
14 changes: 9 additions & 5 deletions cmd/peggo/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ func deployGravityCmd() *cobra.Command {
}

// COSMOS RPC

cosmosChainID := konfig.String(flagCosmosChainID)
clientCtx, err := client.NewClientContext(cosmosChainID, "", nil)
clientCtx, err := client.NewClientContext(konfig.String(flagCosmosChainID), "", nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -174,11 +172,18 @@ func deployGravityCmd() *cobra.Command {
return fmt.Errorf("failed deploy Gravity Bridge contract: %w", err)
}

powerStr := ""
for _, power := range powers {
powerStr += power.String() + " ,"
}

_, _ = fmt.Fprintf(os.Stderr, `Gravity Bridge contract successfully deployed!
Address: %s
Input: %+v, %+v, [%s]
Transaction: %s
`,
address.Hex(),
gravityIDBytes32, validators, powerStr,
tx.Hash().Hex(),
)

Expand Down Expand Up @@ -212,8 +217,7 @@ func deployERC20Cmd() *cobra.Command {
}

// query for the name and symbol on-chain via the token's metadata
cosmosChainID := konfig.String(flagCosmosChainID)
clientCtx, err := client.NewClientContext(cosmosChainID, "", nil)
clientCtx, err := client.NewClientContext(konfig.String(flagCosmosChainID), "", nil)
if err != nil {
return err
}
Expand Down
23 changes: 17 additions & 6 deletions cmd/peggo/client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ import (
cosmostypes "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/pkg/errors"

umeeapp "github.com/umee-network/umee/v2/app"
)

// NewClientContext creates a new Cosmos Client context, where chainID
// corresponds to Cosmos chain ID, fromSpec is either name of the key, or bech32-address
// of the Cosmos account. Keyring is required to contain the specified key.
func NewClientContext(chainID, fromSpec string, kb keyring.Keyring) (client.Context, error) {
var keyInfo keyring.Info
func NewClientContext(
chainID,
fromSpec string,
kb keyring.Keyring,
) (client.Context, error) {
var keyInfo *keyring.Record

if kb != nil {
addr, err := cosmostypes.AccAddressFromBech32(fromSpec)
Expand All @@ -36,9 +41,10 @@ func NewClientContext(chainID, fromSpec string, kb keyring.Keyring) (client.Cont
}

encodingConfig := umeeapp.MakeEncodingConfig()

clientCtx := client.Context{
ChainID: chainID,
JSONCodec: encodingConfig.Marshaler,
Codec: encodingConfig.Codec,
InterfaceRegistry: encodingConfig.InterfaceRegistry,
Output: os.Stderr,
OutputFormat: "json",
Expand All @@ -53,10 +59,15 @@ func NewClientContext(chainID, fromSpec string, kb keyring.Keyring) (client.Cont
}

if keyInfo != nil {
addr, err := keyInfo.GetAddress()
if err != nil {
return clientCtx, err
}

clientCtx = clientCtx.WithKeyring(kb)
clientCtx = clientCtx.WithFromAddress(keyInfo.GetAddress())
clientCtx = clientCtx.WithFromName(keyInfo.GetName())
clientCtx = clientCtx.WithFrom(keyInfo.GetName())
clientCtx = clientCtx.WithFromAddress(addr)
clientCtx = clientCtx.WithFromName(keyInfo.Name)
clientCtx = clientCtx.WithFrom(keyInfo.Name)
}

return clientCtx, nil
Expand Down
18 changes: 14 additions & 4 deletions cmd/peggo/client/cosmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

type CosmosClient interface {
Expand All @@ -36,7 +37,11 @@ func NewCosmosClient(
protoAddr string,
options ...CosmosClientOption,
) (CosmosClient, error) {
conn, err := grpc.Dial(protoAddr, grpc.WithInsecure(), grpc.WithContextDialer(dialerFunc))
conn, err := grpc.Dial(
protoAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(dialerFunc),
)
if err != nil {
err := errors.Wrapf(err, "failed to connect to the gRPC: %s", protoAddr)
return nil, err
Expand Down Expand Up @@ -252,7 +257,7 @@ func (c *cosmosClient) broadcastTx(
txf = txf.WithGas(adjusted)
}

txn, err := tx.BuildUnsignedTx(txf, msgs...)
txn, err := txf.BuildUnsignedTx(msgs...)
if err != nil {
err = errors.Wrap(err, "failed to BuildUnsignedTx")
return nil, err
Expand Down Expand Up @@ -292,10 +297,14 @@ func (c *cosmosClient) broadcastTx(
resultTx, err := clientCtx.Client.Tx(awaitCtx, txHash, false)
if err != nil {
if errRes := client.CheckTendermintError(err, txBytes); errRes != nil {
c.logger.Error().Str("tendermint errRes", errRes.RawLog)
return errRes, err
}

// log.WithError(err).Warningln("Tx Error for Hash:", res.TxHash)
c.logger.Error().Err(err).
Str("Tx Hash", res.TxHash).
Str("Raw Log", res.RawLog).
Msg("Tx error on broadcastTx")

t.Reset(defaultBroadcastStatusPoll)
continue
Expand Down Expand Up @@ -418,7 +427,8 @@ func (c *cosmosClient) runBatchBroadcast() {

if res.Code != 0 {
err = errors.Errorf("error %d (%s): %s", res.Code, res.Codespace, res.RawLog)
c.logger.Err(err).Str("tx_hash", res.TxHash).Msg("failed to (sync) broadcast batch tx")
c.logger.Err(err).Str("tx_hash", res.TxHash).
Msg("failed to (sync) broadcast tx batch error code != 0")
} else {
c.logger.Debug().Str("tx_hash", res.TxHash).Msg("batch tx committed successfully")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/peggo/client/tx_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func NewTxFactory(clientCtx client.Context) tx.Factory {
return new(tx.Factory).
return tx.Factory{}.
WithKeybase(clientCtx.Keyring).
WithTxConfig(clientCtx.TxConfig).
WithAccountRetriever(clientCtx.AccountRetriever).
Expand Down
9 changes: 8 additions & 1 deletion cmd/peggo/flags.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package peggo

import (
"fmt"
"net/url"
"strings"

"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/knadh/koanf"
"github.com/rs/zerolog"
"github.com/spf13/pflag"

umeeapp "github.com/umee-network/umee/v2/app"
)

const (
Expand Down Expand Up @@ -60,7 +63,11 @@ func cosmosFlagSet() *pflag.FlagSet {
fs.String(flagCosmosChainID, "", "The chain ID of the cosmos network")
fs.String(flagCosmosGRPC, "tcp://localhost:9090", "The gRPC endpoint of a cosmos node")
fs.String(flagTendermintRPC, "http://localhost:26657", "The Tendermint RPC endpoint of a Cosmos node")
fs.String(flagCosmosGasPrices, "", "The gas prices to use for Cosmos transaction fees")
fs.String(
flagCosmosGasPrices,
fmt.Sprintf("0.001%s", umeeapp.BondDenom),
"The gas prices to use for Cosmos transaction fees",
)

return fs
}
Expand Down
35 changes: 25 additions & 10 deletions cmd/peggo/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"syscall"

"github.com/cosmos/cosmos-sdk/codec"
sdkcrypto "github.com/cosmos/cosmos-sdk/crypto"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand All @@ -27,6 +28,8 @@ import (
"github.com/rs/zerolog"
"github.com/umee-network/peggo/orchestrator/ethereum/keystore"
"golang.org/x/term"

umeeapp "github.com/umee-network/umee/v2/app"
)

const defaultKeyringKeyName = "validator"
Expand All @@ -42,6 +45,7 @@ func initCosmosKeyring(konfig *koanf.Koanf) (sdk.AccAddress, keyring.Keyring, er
cosmosPassphrase := konfig.String(flagCosmosFromPassphrase)
cosmosKeyringDir := konfig.String(flagCosmosKeyringDir)
cosmosUseLedger := konfig.Bool(flagCosmosUseLedger)
encodingConfig := umeeapp.MakeEncodingConfig()

switch {
case len(cosmosPK) > 0:
Expand Down Expand Up @@ -84,7 +88,7 @@ func initCosmosKeyring(konfig *koanf.Koanf) (sdk.AccAddress, keyring.Keyring, er
}

// wrap a PK into a Keyring
kb, err := keyringForPrivKey(keyName, cosmosAccPk)
kb, err := keyringForPrivKey(keyName, cosmosAccPk, encodingConfig.Codec)
return addressFromPk, kb, err

case len(cosmosFrom) > 0:
Expand Down Expand Up @@ -116,12 +120,13 @@ func initCosmosKeyring(konfig *koanf.Koanf) (sdk.AccAddress, keyring.Keyring, er
konfig.String(flagCosmosKeyring),
absoluteKeyringDir,
passReader,
encodingConfig.Codec,
)
if err != nil {
return emptyCosmosAddress, nil, fmt.Errorf("failed to create keyring: %w", err)
}

var keyInfo keyring.Info
var keyInfo *keyring.Record
if fromIsAddress {
if keyInfo, err = kb.KeyByAddress(addressFrom); err != nil {
return emptyCosmosAddress, nil, fmt.Errorf(
Expand All @@ -140,27 +145,33 @@ func initCosmosKeyring(konfig *koanf.Koanf) (sdk.AccAddress, keyring.Keyring, er

switch keyType := keyInfo.GetType(); keyType {
case keyring.TypeLocal:
addr, err := keyInfo.GetAddress()
// kb has a key and it's totally usable
return addr, kb, err
// kb has a key and it's totally usable
return keyInfo.GetAddress(), kb, nil

case keyring.TypeLedger:
addr, err := keyInfo.GetAddress()
if err != nil {
return nil, nil, err
}
// The keyring stores references to ledger keys, so we must explicitly
// check that. The keyring doesn't know how to scan HD keys - they must be
// added manually before.
if cosmosUseLedger {
return keyInfo.GetAddress(), kb, nil
return addr, kb, nil
}

return emptyCosmosAddress, nil, fmt.Errorf("'%s' key is a ledger reference, enable ledger option", keyInfo.GetName())
return emptyCosmosAddress, nil, fmt.Errorf("'%s' key is a ledger reference, enable ledger option", keyInfo.Name)

case keyring.TypeOffline:
return emptyCosmosAddress, nil, fmt.Errorf("'%s' key is an offline key, not supported yet", keyInfo.GetName())
return emptyCosmosAddress, nil, fmt.Errorf("'%s' key is an offline key, not supported yet", keyInfo.Name)

case keyring.TypeMulti:
return emptyCosmosAddress, nil, fmt.Errorf("'%s' key is an multisig key, not supported yet", keyInfo.GetName())
return emptyCosmosAddress, nil, fmt.Errorf("'%s' key is an multisig key, not supported yet", keyInfo.Name)

default:
return emptyCosmosAddress, nil, fmt.Errorf("'%s' key has unsupported type: %s", keyInfo.GetName(), keyType)
return emptyCosmosAddress, nil, fmt.Errorf("'%s' key has unsupported type: %s", keyInfo.Name, keyType)
}

default:
Expand Down Expand Up @@ -383,15 +394,19 @@ func (r *passReader) Read(p []byte) (n int, err error) {

// keyringForPrivKey creates a temporary in-mem keyring for a PrivKey.
// Allows to init Context when the key has been provided in plaintext and parsed.
func keyringForPrivKey(name string, privKey sdkcryptotypes.PrivKey) (keyring.Keyring, error) {
func keyringForPrivKey(
name string,
privKey sdkcryptotypes.PrivKey,
cdc codec.Codec,
) (keyring.Keyring, error) {
tmpPhrase, err := randPhrase(64)
if err != nil {
return nil, err
}

armored := sdkcrypto.EncryptArmorPrivKey(privKey, tmpPhrase, privKey.Type())

kb := keyring.NewInMemory()
kb := keyring.NewInMemory(cdc)
if err := kb.ImportPrivKey(name, armored, tmpPhrase); err != nil {
err = errors.Wrap(err, "failed to import privkey")
return nil, err
Expand Down
22 changes: 15 additions & 7 deletions cmd/peggo/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"

umeedpfconfig "github.com/umee-network/umee/price-feeder/config"
umeepfprovider "github.com/umee-network/umee/price-feeder/oracle/provider"

"github.com/umee-network/peggo/cmd/peggo/client"
"github.com/umee-network/peggo/orchestrator"
Expand Down Expand Up @@ -60,8 +60,7 @@ func getOrchestratorCmd() *cobra.Command {
return fmt.Errorf("failed to initialize Cosmos keyring: %w", err)
}

cosmosChainID := konfig.String(flagCosmosChainID)
clientCtx, err := client.NewClientContext(cosmosChainID, orchAddress.String(), cosmosKeyring)
clientCtx, err := client.NewClientContext(konfig.String(flagCosmosChainID), orchAddress.String(), cosmosKeyring)
if err != nil {
return err
}
Expand Down Expand Up @@ -199,7 +198,7 @@ func getOrchestratorCmd() *cobra.Command {
trapSignal(cancel)

providers := konfig.Strings(flagOracleProviders)
o, err := oracle.New(ctx, logger, providers)
o, err := oracle.New(ctx, logger, stringsToProviderName(providers))
if err != nil {
return err
}
Expand Down Expand Up @@ -278,9 +277,9 @@ func getOrchestratorCmd() *cobra.Command {
cmd.Flags().Int64(flagEthBlocksPerLoop, 2000, "Number of Ethereum blocks to process per orchestrator loop")
cmd.Flags().String(flagCoinGeckoAPI, "https://api.coingecko.com/api/v3", "Specify the coingecko API endpoint")
//nolint: lll
cmd.Flags().StringSlice(flagOracleProviders, []string{umeedpfconfig.ProviderBinance, umeedpfconfig.ProviderHuobi},
fmt.Sprintf("Specify the providers to use in the oracle, options \"%s\"", strings.Join([]string{umeedpfconfig.ProviderBinance, umeedpfconfig.ProviderHuobi,
umeedpfconfig.ProviderKraken, umeedpfconfig.ProviderGate, umeedpfconfig.ProviderOkx, umeedpfconfig.ProviderOsmosis}, ",")))
cmd.Flags().StringSlice(flagOracleProviders, []string{string(umeepfprovider.ProviderBinance), string(umeepfprovider.ProviderKraken)},
fmt.Sprintf("Specify the providers to use in the oracle, options \"%s\"", strings.Join([]string{string(umeepfprovider.ProviderBinance), string(umeepfprovider.ProviderHuobi),
string(umeepfprovider.ProviderKraken), string(umeepfprovider.ProviderGate), string(umeepfprovider.ProviderOkx), string(umeepfprovider.ProviderOsmosis)}, ",")))
cmd.Flags().Duration(flagEthPendingTXWait, 20*time.Minute, "Time for a pending tx to be considered stale")
cmd.Flags().String(flagEthAlchemyWS, "", "Specify the Alchemy websocket endpoint")
cmd.Flags().Float64(flagProfitMultiplier, 1.0, "Multiplier to apply to relayer profit")
Expand Down Expand Up @@ -341,3 +340,12 @@ func validateRelayValsetsMode(mode string) (relayer.ValsetRelayMode, error) {
return relayer.ValsetRelayModeNone, fmt.Errorf("invalid relay valsets mode: %s", mode)
}
}

func stringsToProviderName(providersName []string) []umeepfprovider.Name {
names := make([]umeepfprovider.Name, len(providersName))
for i, name := range providersName {
names[i] = umeepfprovider.Name(name)
}

return names
}
Loading

0 comments on commit b22361c

Please sign in to comment.