Skip to content

Commit

Permalink
fix: change default rpc config + upgrade CORS library (#1118)
Browse files Browse the repository at this point in the history
## Description

This PR introduces several changes to the default JSON-RPC config of the
Gno node:
- allow all (`*`) origin requests by default. The reasoning behind this
is that local development setups that require the RPC layer will have
their requests rejected by the CORS policy, and will require a custom
RPC config to be specified to the node. This is more of a convenience on
the side of development, as production Gno RPC setups will have a
concrete RPC configuration that is more limited
- explicitly allow `OPTIONS` requests (preflight requests) in the
default configuration
- change the CORS library used in the project from
[gnolang/cors](https://github.com/gnolang/cors) to
[rs/cors](https://github.com/rs/cors). The
[gnolang/cors](https://github.com/gnolang/cors) library is a fork of
[rs/cors](https://github.com/rs/cors), with seemingly no functionality
changes. Therefore, to stay up to date with the latest fixes and
upstream changes, I've updated the library gno uses to the original one,
which is actively maintained by the original authors

### How can I test this out?

Essentially, you can run the local node with the default RPC params, and
try to do an HTTP request from a frontend application:

```ts
import { GnoJSONRPCProvider } from "@gnolang/gno-js-client";

const exampleMethod = async () => {
  // Local node as the endpoint
  const provider = new GnoJSONRPCProvider("http://127.0.0.1:26657");

  const blockNumber: number = await provider.getBlockNumber();

  console.log(blockNumber);
};

exampleMethod()
  .then(() => {
    console.log("success");
  })
  .catch((e) => {
    console.error(e);
  });

```

Example error:
<img width="1192" alt="Screenshot 2023-09-13 at 12 27 22"
src="https://github.com/gnolang/gno/assets/16712663/603b58c7-577d-4970-80eb-1c5984e0744b">


<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [x] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
  • Loading branch information
zivkovicmilos authored Sep 13, 2023
1 parent 207d66d commit 6190894
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/dgraph-io/badger/v3 v3.2103.4
github.com/fortytw2/leaktest v1.3.0
github.com/gdamore/tcell/v2 v2.1.0
github.com/gnolang/cors v1.8.1
github.com/gnolang/goleveldb v0.0.9
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216
github.com/golang/protobuf v1.5.3
Expand All @@ -28,6 +27,7 @@ require (
github.com/peterbourgon/ff/v3 v3.4.0
github.com/pmezard/go-difflib v1.0.0
github.com/rogpeppe/go-internal v1.11.0
github.com/rs/cors v1.10.0
github.com/stretchr/testify v1.8.4
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c
go.etcd.io/bbolt v1.3.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tm2/pkg/bft/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"time"

"github.com/gnolang/cors"
"github.com/rs/cors"

"github.com/gnolang/gno/tm2/pkg/amino"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
Expand Down Expand Up @@ -41,7 +41,7 @@ import (
verset "github.com/gnolang/gno/tm2/pkg/versionset"
)

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------

// DBContext specifies config information for loading a new DB.
type DBContext struct {
Expand Down Expand Up @@ -134,7 +134,7 @@ func CustomReactors(reactors map[string]p2p.Reactor) Option {
}
}

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------

// Node is the highest level interface to a full Tendermint node.
// It includes all configuration information and running services.
Expand Down Expand Up @@ -819,7 +819,7 @@ func (n *Node) Config() *cfg.Config {
return n.config
}

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------

func (n *Node) Listeners() []string {
return []string{
Expand Down Expand Up @@ -887,7 +887,7 @@ func makeNodeInfo(
return nodeInfo, err
}

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------

var genesisDocKey = []byte("genesisDoc")

Expand Down
6 changes: 3 additions & 3 deletions tm2/pkg/bft/rpc/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"
)

//-----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// RPCConfig

const (
Expand Down Expand Up @@ -89,8 +89,8 @@ type RPCConfig struct {
func DefaultRPCConfig() *RPCConfig {
return &RPCConfig{
ListenAddress: "tcp://127.0.0.1:26657",
CORSAllowedOrigins: []string{},
CORSAllowedMethods: []string{http.MethodHead, http.MethodGet, http.MethodPost},
CORSAllowedOrigins: []string{"*"},
CORSAllowedMethods: []string{http.MethodHead, http.MethodGet, http.MethodPost, http.MethodOptions},
CORSAllowedHeaders: []string{"Origin", "Accept", "Content-Type", "X-Requested-With", "X-Server-Time"},
GRPCListenAddress: "",
GRPCMaxOpenConnections: 900,
Expand Down

0 comments on commit 6190894

Please sign in to comment.