-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
feat: enhance rosetta support #11590
Changes from all commits
96a7311
ea433e7
547bc58
378308e
5c0aea6
60c5dba
2377100
262ea83
0e296fb
8fc4cc0
e2f9e28
503be9c
850a035
3545ea0
4e76726
642a1cc
822d390
feac6af
be3f786
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,14 @@ import ( | |
"time" | ||
|
||
"github.com/coinbase/rosetta-sdk-go/types" | ||
clientflags "github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/spf13/pflag" | ||
|
||
crg "github.com/cosmos/cosmos-sdk/server/rosetta/lib/server" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// configuration defaults constants | ||
|
@@ -30,17 +32,24 @@ const ( | |
DefaultNetwork = "network" | ||
// DefaultOffline defines the default offline value | ||
DefaultOffline = false | ||
// DefaultEnableDefaultFeeSuggest indicates to use fee suggestion | ||
DefaultEnableDefaultFeeSuggest = false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. odd to read, we can remove Default prefix and address the changes that @alexanderbez suggested for naming |
||
// DefaultSuggestDenom defines the default denom for fee suggestion | ||
DefaultSuggestDenom = "uatom" | ||
) | ||
|
||
// configuration flags | ||
const ( | ||
FlagBlockchain = "blockchain" | ||
FlagNetwork = "network" | ||
FlagTendermintEndpoint = "tendermint" | ||
FlagGRPCEndpoint = "grpc" | ||
FlagAddr = "addr" | ||
FlagRetries = "retries" | ||
FlagOffline = "offline" | ||
FlagBlockchain = "blockchain" | ||
FlagNetwork = "network" | ||
FlagTendermintEndpoint = "tendermint" | ||
FlagGRPCEndpoint = "grpc" | ||
FlagAddr = "addr" | ||
FlagRetries = "retries" | ||
FlagOffline = "offline" | ||
FlagEnableDefaultFeeSuggest = "enable-default-fee-suggest" | ||
FlagSuggestGas = "suggest-gas" | ||
FlagSuggestDenom = "suggest-denom" | ||
) | ||
|
||
// Config defines the configuration of the rosetta server | ||
|
@@ -65,6 +74,14 @@ type Config struct { | |
Retries int | ||
// Offline defines if the server must be run in offline mode | ||
Offline bool | ||
// EnableDefaultFeeSuggest indicates to use fee suggestion | ||
EnableDefaultFeeSuggest bool | ||
// SuggestGas defines the gas limit for fee suggestion | ||
SuggestGas int | ||
// DefaultSuggestDenom defines the default denom for fee suggestion | ||
DefaultSuggestDenom string | ||
// SuggestPrices defines the gas prices for fee suggestion | ||
SuggestPrices sdk.DecCoins | ||
// Codec overrides the default data and construction api client codecs | ||
Codec *codec.ProtoCodec | ||
// InterfaceRegistry overrides the default data and construction api interface registry | ||
|
@@ -99,8 +116,18 @@ func (c *Config) validate() error { | |
if c.Network == "" { | ||
return fmt.Errorf("network not provided") | ||
} | ||
if c.Offline { | ||
return fmt.Errorf("offline mode is not supported for stargate implementation due to how sigv2 works") | ||
if c.SuggestGas <= 0 { | ||
c.SuggestGas = clientflags.DefaultGasLimit | ||
} | ||
found := false | ||
for i := 0; i < c.SuggestPrices.Len(); i++ { | ||
if c.SuggestPrices.GetDenomByIndex(i) == c.DefaultSuggestDenom { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
return fmt.Errorf("default suggest denom is not found in minimum-gas-prices") | ||
} | ||
|
||
// these are optional but it must be online | ||
|
@@ -153,14 +180,29 @@ func FromFlags(flags *pflag.FlagSet) (*Config, error) { | |
if err != nil { | ||
return nil, err | ||
} | ||
enableDefaultFeeSuggest, err := flags.GetBool(FlagEnableDefaultFeeSuggest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
suggestGas, err := flags.GetInt(FlagSuggestGas) | ||
if err != nil { | ||
return nil, err | ||
} | ||
suggestDenom, err := flags.GetString(FlagSuggestDenom) | ||
if err != nil { | ||
return nil, err | ||
} | ||
conf := &Config{ | ||
Blockchain: blockchain, | ||
Network: network, | ||
TendermintRPC: tendermintRPC, | ||
GRPCEndpoint: gRPCEndpoint, | ||
Addr: addr, | ||
Retries: retries, | ||
Offline: offline, | ||
Blockchain: blockchain, | ||
Network: network, | ||
TendermintRPC: tendermintRPC, | ||
GRPCEndpoint: gRPCEndpoint, | ||
Addr: addr, | ||
Retries: retries, | ||
Offline: offline, | ||
EnableDefaultFeeSuggest: enableDefaultFeeSuggest, | ||
SuggestGas: suggestGas, | ||
DefaultSuggestDenom: suggestDenom, | ||
} | ||
err = conf.validate() | ||
if err != nil { | ||
|
@@ -201,4 +243,7 @@ func SetFlags(flags *pflag.FlagSet) { | |
flags.String(FlagAddr, DefaultAddr, "the address rosetta will bind to") | ||
flags.Int(FlagRetries, DefaultRetries, "the number of retries that will be done before quitting") | ||
flags.Bool(FlagOffline, DefaultOffline, "run rosetta only with construction API") | ||
flags.Bool(FlagEnableDefaultFeeSuggest, DefaultEnableDefaultFeeSuggest, "enable fee suggestion") | ||
flags.Int(FlagSuggestGas, clientflags.DefaultGasLimit, "default gas for fee suggestion") | ||
flags.String(FlagSuggestDenom, DefaultSuggestDenom, "default denom to suggest fee") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"github.com/coinbase/rosetta-sdk-go/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/server/rosetta/lib/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// ConstructionCombine Combine creates a network-specific transaction from an unsigned transaction | ||
|
@@ -69,8 +70,23 @@ func (on OnlineNetwork) ConstructionMetadata(ctx context.Context, request *types | |
return nil, errors.ToRosetta(err) | ||
} | ||
|
||
price, err := sdk.ParseDecCoin(metadata["gas_price"].(string)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should be safe and have explicit type assertions on these metadata fields and return an error when the type is invalid. Otherwise, this could could panic of metadata structure changes. |
||
if err != nil { | ||
return nil, errors.ToRosetta(err) | ||
} | ||
gas := sdk.NewIntFromUint64(uint64(metadata["gas_limit"].(float64))) | ||
|
||
suggestedFee := types.Amount{ | ||
Value: price.Amount.Mul(gas).String(), | ||
Currency: &(types.Currency{ | ||
Symbol: price.Denom, | ||
Decimals: 0, | ||
}), | ||
} | ||
|
||
return &types.ConstructionMetadataResponse{ | ||
Metadata: metadata, | ||
Metadata: metadata, | ||
SuggestedFee: []*types.Amount{&suggestedFee}, | ||
}, nil | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gofmt