Skip to content

Commit

Permalink
Merge branch 'main' into hlib/update-libp2p-028
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Jun 14, 2023
2 parents ce1d252 + 7ef26dc commit d673443
Show file tree
Hide file tree
Showing 28 changed files with 322 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
go-version: ${{ env.GO_VERSION }}

- name: golangci-lint
uses: golangci/golangci-lint-action@v3.4.0
uses: golangci/golangci-lint-action@v3.6.0
with:
version: v1.52.2

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: mheap/github-action-required-labels@v4
- uses: mheap/github-action-required-labels@v5
with:
mode: minimum
count: 1
Expand Down
2 changes: 2 additions & 0 deletions api/docgen/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func init() {
}
addToExampleValues(valAddr)

addToExampleValues(state.Address{Address: addr})

var txResponse *state.TxResponse
err = json.Unmarshal([]byte(exampleTxResponse), &txResponse)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions api/gateway/das.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
)

func (h *Handler) handleDASStateRequest(w http.ResponseWriter, r *http.Request) {
logDeprecation(dasStateEndpoint, "das.SamplingStats")
stats, err := h.das.SamplingStats(r.Context())
if err != nil {
writeError(w, http.StatusInternalServerError, dasStateEndpoint, err)
Expand Down
39 changes: 22 additions & 17 deletions api/gateway/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,32 @@ import (
"net/http"
)

func (h *Handler) RegisterEndpoints(rpc *Server) {
func (h *Handler) RegisterEndpoints(rpc *Server, deprecatedEndpointsEnabled bool) {
if deprecatedEndpointsEnabled {
log.Warn("Deprecated endpoints will be removed from the gateway in the next release. Use the RPC instead.")
// state endpoints
rpc.RegisterHandlerFunc(balanceEndpoint, h.handleBalanceRequest, http.MethodGet)
rpc.RegisterHandlerFunc(submitPFBEndpoint, h.handleSubmitPFB, http.MethodPost)

// staking queries
rpc.RegisterHandlerFunc(fmt.Sprintf("%s/{%s}", queryDelegationEndpoint, addrKey), h.handleQueryDelegation,
http.MethodGet)
rpc.RegisterHandlerFunc(fmt.Sprintf("%s/{%s}", queryUnbondingEndpoint, addrKey), h.handleQueryUnbonding,
http.MethodGet)
rpc.RegisterHandlerFunc(queryRedelegationsEndpoint, h.handleQueryRedelegations,
http.MethodPost)

// DASer endpoints
// only register if DASer service is available
if h.das != nil {
rpc.RegisterHandlerFunc(dasStateEndpoint, h.handleDASStateRequest, http.MethodGet)
}
}

// state endpoints
rpc.RegisterHandlerFunc(balanceEndpoint, h.handleBalanceRequest, http.MethodGet)
rpc.RegisterHandlerFunc(fmt.Sprintf("%s/{%s}", balanceEndpoint, addrKey), h.handleBalanceRequest,
http.MethodGet)
rpc.RegisterHandlerFunc(submitTxEndpoint, h.handleSubmitTx, http.MethodPost)
rpc.RegisterHandlerFunc(submitPFBEndpoint, h.handleSubmitPFB, http.MethodPost)

// staking queries
rpc.RegisterHandlerFunc(fmt.Sprintf("%s/{%s}", queryDelegationEndpoint, addrKey), h.handleQueryDelegation,
http.MethodGet)
rpc.RegisterHandlerFunc(fmt.Sprintf("%s/{%s}", queryUnbondingEndpoint, addrKey), h.handleQueryUnbonding,
http.MethodGet)
rpc.RegisterHandlerFunc(queryRedelegationsEndpoint, h.handleQueryRedelegations,
http.MethodPost)

// share endpoints
rpc.RegisterHandlerFunc(fmt.Sprintf("%s/{%s}/height/{%s}", namespacedSharesEndpoint, nIDKey, heightKey),
Expand All @@ -39,10 +50,4 @@ func (h *Handler) RegisterEndpoints(rpc *Server) {
rpc.RegisterHandlerFunc(fmt.Sprintf("%s/{%s}", headerByHeightEndpoint, heightKey), h.handleHeaderRequest,
http.MethodGet)
rpc.RegisterHandlerFunc(headEndpoint, h.handleHeadRequest, http.MethodGet)

// DASer endpoints
// only register if DASer service is available
if h.das != nil {
rpc.RegisterHandlerFunc(dasStateEndpoint, h.handleDASStateRequest, http.MethodGet)
}
}
12 changes: 11 additions & 1 deletion api/gateway/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ func (h *Handler) handleBalanceRequest(w http.ResponseWriter, r *http.Request) {
}
addr = valAddr.Bytes()
}
bal, err = h.state.BalanceForAddress(r.Context(), addr)
bal, err = h.state.BalanceForAddress(r.Context(), state.Address{Address: addr})
} else {
logDeprecation(balanceEndpoint, "state.Balance")
bal, err = h.state.Balance(r.Context())
}
if err != nil {
Expand Down Expand Up @@ -122,6 +123,7 @@ func (h *Handler) handleSubmitTx(w http.ResponseWriter, r *http.Request) {
}

func (h *Handler) handleSubmitPFB(w http.ResponseWriter, r *http.Request) {
logDeprecation(submitPFBEndpoint, "blob.Submit or state.SubmitPayForBlob")
// decode request
var req submitPFBRequest
err := json.NewDecoder(r.Body).Decode(&req)
Expand Down Expand Up @@ -171,6 +173,7 @@ func (h *Handler) handleSubmitPFB(w http.ResponseWriter, r *http.Request) {
}

func (h *Handler) handleQueryDelegation(w http.ResponseWriter, r *http.Request) {
logDeprecation(queryDelegationEndpoint, "state.QueryDelegation")
// read and parse request
vars := mux.Vars(r)
addrStr, exists := vars[addrKey]
Expand Down Expand Up @@ -202,6 +205,7 @@ func (h *Handler) handleQueryDelegation(w http.ResponseWriter, r *http.Request)
}

func (h *Handler) handleQueryUnbonding(w http.ResponseWriter, r *http.Request) {
logDeprecation(queryUnbondingEndpoint, "state.QueryUnbonding")
// read and parse request
vars := mux.Vars(r)
addrStr, exists := vars[addrKey]
Expand Down Expand Up @@ -233,6 +237,7 @@ func (h *Handler) handleQueryUnbonding(w http.ResponseWriter, r *http.Request) {
}

func (h *Handler) handleQueryRedelegations(w http.ResponseWriter, r *http.Request) {
logDeprecation(queryRedelegationsEndpoint, "state.QueryRedelegations")
var req queryRedelegationsRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
Expand Down Expand Up @@ -264,3 +269,8 @@ func (h *Handler) handleQueryRedelegations(w http.ResponseWriter, r *http.Reques
log.Errorw("writing response", "endpoint", queryRedelegationsEndpoint, "err", err)
}
}

func logDeprecation(endpoint string, alternative string) {
log.Warn("The " + endpoint + " endpoint is deprecated and will be removed in the next release. Please " +
"use " + alternative + " from the RPC instead.")
}
36 changes: 22 additions & 14 deletions api/rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@ import (
"github.com/celestiaorg/celestia-node/nodebuilder/state"
)

// TODO: this duplication of strings many times across the codebase can be avoided with issue #1176
var client Client
var Modules = map[string]interface{}{
"share": &client.Share.Internal,
"state": &client.State.Internal,
"header": &client.Header.Internal,
"fraud": &client.Fraud.Internal,
"das": &client.DAS.Internal,
"p2p": &client.P2P.Internal,
"node": &client.Node.Internal,
"blob": &client.Blob.Internal,
}
var (
// staticClient is used for generating the OpenRPC spec.
staticClient Client
Modules = moduleMap(&staticClient)
)

type Client struct {
Fraud fraud.API
Expand Down Expand Up @@ -61,7 +54,7 @@ func (m *multiClientCloser) closeAll() {
}
}

// Close closes the connections to all namespaces registered on the client.
// Close closes the connections to all namespaces registered on the staticClient.
func (c *Client) Close() {
c.closer.closeAll()
}
Expand All @@ -80,7 +73,8 @@ func NewClient(ctx context.Context, addr string, token string) (*Client, error)

func newClient(ctx context.Context, addr string, authHeader http.Header) (*Client, error) {
var multiCloser multiClientCloser
for name, module := range Modules {
var client Client
for name, module := range moduleMap(&client) {
closer, err := jsonrpc.NewClient(ctx, addr, name, module, authHeader)
if err != nil {
return nil, err
Expand All @@ -90,3 +84,17 @@ func newClient(ctx context.Context, addr string, authHeader http.Header) (*Clien

return &client, nil
}

func moduleMap(client *Client) map[string]interface{} {
// TODO: this duplication of strings many times across the codebase can be avoided with issue #1176
return map[string]interface{}{
"share": &client.Share.Internal,
"state": &client.State.Internal,
"header": &client.Header.Internal,
"fraud": &client.Fraud.Internal,
"das": &client.DAS.Internal,
"p2p": &client.P2P.Internal,
"node": &client.Node.Internal,
"blob": &client.Blob.Internal,
}
}
16 changes: 4 additions & 12 deletions cmd/celestia/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand All @@ -15,7 +14,6 @@ import (
"strconv"
"strings"

"github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"

"github.com/celestiaorg/nmt/namespace"
Expand Down Expand Up @@ -392,18 +390,12 @@ func sendJSONRPCRequest(namespace, method string, params []interface{}) {
}

func parseAddressFromString(addrStr string) (state.Address, error) {
var addr state.AccAddress
addr, err := types.AccAddressFromBech32(addrStr)
var address state.Address
err := address.UnmarshalJSON([]byte(addrStr))
if err != nil {
// first check if it is a validator address and can be converted
valAddr, err := types.ValAddressFromBech32(addrStr)
if err != nil {
return nil, errors.New("address must be a valid account or validator address ")
}
return valAddr, nil
return address, err
}

return addr, nil
return address, nil
}

func parseSignatureForHelpstring(methodSig reflect.StructField) string {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-retryablehttp v0.7.2
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d
github.com/imdario/mergo v0.3.15
github.com/imdario/mergo v0.3.16
github.com/ipfs/go-blockservice v0.5.0
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-datastore v0.6.0
Expand Down Expand Up @@ -57,7 +57,7 @@ require (
github.com/multiformats/go-multihash v0.2.2
github.com/open-rpc/meta-schema v0.0.0-20201029221707-1b72ef2ea333
github.com/prometheus/client_golang v1.14.0
github.com/pyroscope-io/client v0.7.0
github.com/pyroscope-io/client v0.7.1
github.com/pyroscope-io/otel-profiling-go v0.4.0
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
Expand All @@ -74,6 +74,7 @@ require (
go.uber.org/fx v1.19.3
go.uber.org/zap v1.24.0
golang.org/x/crypto v0.9.0
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
golang.org/x/sync v0.2.0
golang.org/x/text v0.9.0
google.golang.org/grpc v1.53.0
Expand Down Expand Up @@ -309,7 +310,6 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/dig v1.17.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.4.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,8 @@ github.com/iancoleman/orderedmap v0.1.0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/icrowley/fake v0.0.0-20180203215853-4178557ae428/go.mod h1:uhpZMVGznybq1itEKXj6RYw9I71qK4kH+OGMjRC4KEo=
github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/improbable-eng/grpc-web v0.15.0 h1:BN+7z6uNXZ1tQGcNAuaU1YjsLTApzkjt2tzCixLaUPQ=
github.com/improbable-eng/grpc-web v0.15.0/go.mod h1:1sy9HKV4Jt9aEs9JSnkWlRJPuPtwNr0l57L4f878wP8=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down Expand Up @@ -1778,8 +1778,8 @@ github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI=
github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/pyroscope-io/client v0.7.0 h1:LWuuqPQ1oa6x7BnmUOuo/aGwdX85QGhWZUBYWWW3zdk=
github.com/pyroscope-io/client v0.7.0/go.mod h1:4h21iOU4pUOq0prKyDlvYRL+SCKsBc5wKiEtV+rJGqU=
github.com/pyroscope-io/client v0.7.1 h1:yFRhj3vbgjBxehvxQmedmUWJQ4CAfCHhn+itPsuWsHw=
github.com/pyroscope-io/client v0.7.1/go.mod h1:4h21iOU4pUOq0prKyDlvYRL+SCKsBc5wKiEtV+rJGqU=
github.com/pyroscope-io/godeltaprof v0.1.0 h1:UBqtjt0yZi4jTxqZmLAs34XG6ycS3vUTlhEUSq4NHLE=
github.com/pyroscope-io/godeltaprof v0.1.0/go.mod h1:psMITXp90+8pFenXkKIpNhrfmI9saQnPbba27VIaiQE=
github.com/pyroscope-io/otel-profiling-go v0.4.0 h1:Hk/rbUqOWoByoWy1tt4r5BX5xoKAvs5drr0511Ki8ic=
Expand Down
2 changes: 2 additions & 0 deletions nodebuilder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ConfigLoader func() (*Config, error)
// Config is main configuration structure for a Node.
// It combines configuration units for all Node subsystems.
type Config struct {
Node node.Config
Core core.Config
State state.Config
P2P p2p.Config
Expand All @@ -39,6 +40,7 @@ type Config struct {
// NOTE: Currently, configs are identical, but this will change.
func DefaultConfig(tp node.Type) *Config {
commonConfig := &Config{
Node: node.DefaultConfig(tp),
Core: core.DefaultConfig(),
State: state.DefaultConfig(),
P2P: p2p.DefaultConfig(tp),
Expand Down
7 changes: 4 additions & 3 deletions nodebuilder/gateway/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

type Config struct {
Address string
Port string
Enabled bool
Address string
Port string
Enabled bool
deprecatedEndpoints bool
}

func DefaultConfig() Config {
Expand Down
3 changes: 2 additions & 1 deletion nodebuilder/gateway/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (

// Handler constructs a new RPC Handler from the given services.
func Handler(
cfg *Config,
state state.Module,
share share.Module,
header header.Module,
daser *das.DASer,
serv *gateway.Server,
) {
handler := gateway.NewHandler(state, share, header, daser)
handler.RegisterEndpoints(serv)
handler.RegisterEndpoints(serv, cfg.deprecatedEndpoints)
handler.RegisterMiddleware(serv)
}

Expand Down
16 changes: 13 additions & 3 deletions nodebuilder/gateway/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var (
enabledFlag = "gateway"
addrFlag = "gateway.addr"
portFlag = "gateway.port"
enabledFlag = "gateway"
addrFlag = "gateway.addr"
portFlag = "gateway.port"
deprecatedEndpoints = "gateway.deprecated-endpoints"
)

// Flags gives a set of hardcoded node/gateway package flags.
Expand All @@ -20,6 +21,11 @@ func Flags() *flag.FlagSet {
false,
"Enables the REST gateway",
)
flags.Bool(
deprecatedEndpoints,
false,
"Enables deprecated endpoints on the gateway. These will be removed in the next release.",
)
flags.String(
addrFlag,
"",
Expand All @@ -40,6 +46,10 @@ func ParseFlags(cmd *cobra.Command, cfg *Config) {
if cmd.Flags().Changed(enabledFlag) && err == nil {
cfg.Enabled = enabled
}
deprecatedEndpointsEnabled, err := cmd.Flags().GetBool(deprecatedEndpoints)
if cmd.Flags().Changed(deprecatedEndpoints) && err == nil {
cfg.deprecatedEndpoints = deprecatedEndpointsEnabled
}
addr, port := cmd.Flag(addrFlag), cmd.Flag(portFlag)
if !cfg.Enabled && (addr.Changed || port.Changed) {
log.Warn("custom address or port provided without enabling gateway, setting config values")
Expand Down
Loading

0 comments on commit d673443

Please sign in to comment.