From f65df1cb338aca722cc303df6ab6b42d476c2199 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Thu, 7 Mar 2024 11:34:32 +0100 Subject: [PATCH 1/3] fix: connect over https --- config.go | 36 +++++++++++++++++++++++--- config_test.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ go.sum | 6 +++++ 4 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 config_test.go diff --git a/config.go b/config.go index 395f99f..a5bc522 100644 --- a/config.go +++ b/config.go @@ -2,12 +2,12 @@ package rosetta import ( "fmt" - "strings" "time" crgerrs "github.com/cosmos/rosetta/lib/errors" "github.com/coinbase/rosetta-sdk-go/types" + url "github.com/goware/urlx" "github.com/spf13/pflag" crg "github.com/cosmos/rosetta/lib/server" @@ -18,6 +18,12 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +const ( + HTTP = "http" + HTTPS = "https" + TCP = "tcp" +) + // configuration defaults constants const ( // DefaultBlockchain defines the default blockchain identifier name @@ -150,13 +156,37 @@ func (c *Config) validate() error { if c.TendermintRPC == "" { return crgerrs.WrapError(crgerrs.ErrConfig, "cometbft rpc not provided") } - if !strings.HasPrefix(c.TendermintRPC, "tcp://") { - c.TendermintRPC = fmt.Sprintf("tcp://%s", c.TendermintRPC) + validatedURL, err := c.validateUrl(c.TendermintRPC) + if err != nil { + return err } + c.TendermintRPC = validatedURL return nil } +func (c *Config) validateUrl(tendermintRPC string) (string, error) { + u, err := url.Parse(tendermintRPC) + if err != nil { + return "", crgerrs.WrapError(crgerrs.ErrConfig, err.Error()) + } + + if u.Port() == "443" && u.Scheme != HTTPS { + u.Scheme = HTTPS + } + + if u.Port() == "" { + switch u.Scheme { + case HTTP, TCP: + u.Host += ":80" + case HTTPS: + u.Host += ":443" + } + } + + return u.String(), nil +} + // WithCodec extends the configuration with a predefined Codec func (c *Config) WithCodec(ir codectypes.InterfaceRegistry, cdc *codec.ProtoCodec) { c.Codec = cdc diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..ac11603 --- /dev/null +++ b/config_test.go @@ -0,0 +1,69 @@ +package rosetta + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestConfig_validateUrl(t *testing.T) { + tests := []struct { + name string + tendermintRPC string + expected string + }{ + { + name: "complete url", + tendermintRPC: "https://myhost.com:443", + expected: "https://myhost.com:443", + }, + { + name: "no schema no port", + tendermintRPC: "myhost.com", + expected: "http://myhost.com:80", + }, + { + name: "http schema with no port", + tendermintRPC: "http://myHost.com", + expected: "http://myhost.com:80", + }, + { + name: "https schema with no port", + tendermintRPC: "https://myHost.com", + expected: "https://myhost.com:443", + }, + { + name: "no schema with port", + tendermintRPC: "myHost.com:2344", + expected: "http://myhost.com:2344", + }, + { + name: "no schema with port 443", + tendermintRPC: "myHost.com:443", + expected: "https://myhost.com:443", + }, + { + name: "tcp schema", + tendermintRPC: "tcp://localhost:26657", + expected: "tcp://localhost:26657", + }, + { + name: "localhost", + tendermintRPC: "localhost", + expected: "http://localhost:80", + }, + { + name: "not normalized url", + tendermintRPC: "hTTp://tHISmyWebsite.COM", + expected: "http://thismywebsite.com:80", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &Config{} + got, err := c.validateUrl(tt.tendermintRPC) + require.NoError(t, err) + require.Equal(t, tt.expected, got) + }) + } +} diff --git a/go.mod b/go.mod index ec11a0b..1a6b1d8 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/cosmos/gogoproto v1.4.11 github.com/cosmos/rosetta-sdk-go v0.10.0 github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 + github.com/goware/urlx v0.3.2 github.com/spf13/cobra v1.8.0 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.4 @@ -31,6 +32,8 @@ require ( github.com/99designs/keyring v1.2.1 // indirect github.com/DataDog/datadog-go v3.2.0+incompatible // indirect github.com/DataDog/zstd v1.5.5 // indirect + github.com/PuerkitoBio/purell v1.1.1 // indirect + github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect diff --git a/go.sum b/go.sum index e3c4f5f..9690746 100644 --- a/go.sum +++ b/go.sum @@ -34,6 +34,10 @@ github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwS github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI= +github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE= @@ -334,6 +338,8 @@ github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/goware/urlx v0.3.2 h1:gdoo4kBHlkqZNaf6XlQ12LGtQOmpKJrR04Rc3RnpJEo= +github.com/goware/urlx v0.3.2/go.mod h1:h8uwbJy68o+tQXCGZNa9D73WN8n0r9OBae5bUnLcgjw= github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-middleware v1.2.2/go.mod h1:EaizFBKfUKtMIF5iaDEhniwNedqGo9FuLFzppDr3uwI= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= From 76a82190a710a6aeef66cc58c00053c6f192dccb Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Mon, 11 Mar 2024 17:50:40 +0100 Subject: [PATCH 2/3] fixes + changelog --- CHANGELOG.md | 4 ++++ config.go | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f14e690..b75e966 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +### Bug Fixes + +* [99](https://github.com/cosmos/rosetta/pull/99) Rosetta now can connect to cometBFT over https. + ### Improvements * [93](https://github.com/cosmos/rosetta/pull/93) Removes the use of `LegacyMsg.GetSigners()` in favor of `codec.GetMsgV1Signers`. diff --git a/config.go b/config.go index a5bc522..67f44ce 100644 --- a/config.go +++ b/config.go @@ -22,6 +22,8 @@ const ( HTTP = "http" HTTPS = "https" TCP = "tcp" + + HTTPSPORT = "443" ) // configuration defaults constants @@ -156,7 +158,7 @@ func (c *Config) validate() error { if c.TendermintRPC == "" { return crgerrs.WrapError(crgerrs.ErrConfig, "cometbft rpc not provided") } - validatedURL, err := c.validateUrl(c.TendermintRPC) + validatedURL, err := c.validateURL(c.TendermintRPC) if err != nil { return err } @@ -165,13 +167,13 @@ func (c *Config) validate() error { return nil } -func (c *Config) validateUrl(tendermintRPC string) (string, error) { +func (c *Config) validateURL(tendermintRPC string) (string, error) { u, err := url.Parse(tendermintRPC) if err != nil { return "", crgerrs.WrapError(crgerrs.ErrConfig, err.Error()) } - if u.Port() == "443" && u.Scheme != HTTPS { + if u.Port() == HTTPSPORT && u.Scheme != HTTPS { u.Scheme = HTTPS } From 456feb330cc3eced6b77fea0df8494d796c0dfc5 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Mon, 11 Mar 2024 18:43:26 +0100 Subject: [PATCH 3/3] make lint happy --- CHANGELOG.md | 2 +- config_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b75e966..4184d62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,7 +38,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes -* [99](https://github.com/cosmos/rosetta/pull/99) Rosetta now can connect to cometBFT over https. +* [99](https://github.com/cosmos/rosetta/pull/99) Rosetta now can connect to cometBFT over HTTPS. ### Improvements diff --git a/config_test.go b/config_test.go index ac11603..de8a455 100644 --- a/config_test.go +++ b/config_test.go @@ -61,7 +61,7 @@ func TestConfig_validateUrl(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { c := &Config{} - got, err := c.validateUrl(tt.tendermintRPC) + got, err := c.validateURL(tt.tendermintRPC) require.NoError(t, err) require.Equal(t, tt.expected, got) })