Skip to content

Commit

Permalink
🚚 nori
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelStark committed Dec 1, 2023
1 parent 2a628ee commit c7fc5a1
Show file tree
Hide file tree
Showing 47 changed files with 859 additions and 251 deletions.
44 changes: 22 additions & 22 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# @eth-optimism/starknet-proxyd
# @eth-optimism/nori

## 3.14.1

### Patch Changes

- 5602deec7: chore(deps): bump github.com/prometheus/client_golang from 1.11.0 to 1.11.1 in /starknet-proxyd
- 5602deec7: chore(deps): bump github.com/prometheus/client_golang from 1.11.0 to 1.11.1 in /nori
- 6b3cf2070: Remove useless logging

## 3.14.0
Expand All @@ -16,7 +16,7 @@

### Patch Changes

- b9bb1a98a: starknet-proxyd: Add req_id to log
- b9bb1a98a: nori: Add req_id to log

## 3.13.0

Expand Down Expand Up @@ -99,22 +99,22 @@

### Patch Changes

- d79d40c4: starknet-proxyd: Proxy requests using batch JSON-RPC
- d79d40c4: nori: Proxy requests using batch JSON-RPC

## 3.8.5

### Patch Changes

- 2a062b11: starknet-proxyd: Log ssanitized RPC requests
- d9f058ce: starknet-proxyd: Reduced RPC request logging
- a4bfd9e7: starknet-proxyd: Limit the number of concurrent RPCs to backends
- 2a062b11: nori: Log ssanitized RPC requests
- d9f058ce: nori: Reduced RPC request logging
- a4bfd9e7: nori: Limit the number of concurrent RPCs to backends

## 3.8.4

### Patch Changes

- 08329ba2: starknet-proxyd: Record redis cache operation latency
- ae112021: starknet-proxyd: Request-scoped context for fast batch RPC short-circuiting
- 08329ba2: nori: Record redis cache operation latency
- ae112021: nori: Request-scoped context for fast batch RPC short-circuiting

## 3.8.3

Expand Down Expand Up @@ -144,42 +144,42 @@

### Minor Changes

- 3c2926b1: Add debug cache status header to starknet-proxyd responses
- 3c2926b1: Add debug cache status header to nori responses

## 3.6.0

### Minor Changes

- 096c5f20: starknet-proxyd: Allow cached RPCs to be evicted by redis
- 096c5f20: nori: Allow cached RPCs to be evicted by redis
- 71d64834: Add caching for block-dependent RPCs
- fd2e1523: starknet-proxyd: Cache block-dependent RPCs
- fd2e1523: nori: Cache block-dependent RPCs
- 1760613c: Add integration tests and batching

## 3.5.0

### Minor Changes

- 025a3c0d: Add request/response payload size metrics to starknet-proxyd
- daf8db0b: cache immutable RPC responses in starknet-proxyd
- 8aa89bf3: Add X-Forwarded-For header when proxying RPCs on starknet-proxyd
- 025a3c0d: Add request/response payload size metrics to nori
- daf8db0b: cache immutable RPC responses in nori
- 8aa89bf3: Add X-Forwarded-For header when proxying RPCs on nori

## 3.4.1

### Patch Changes

- 415164e1: Force starknet-proxyd build
- 415164e1: Force nori build

## 3.4.0

### Minor Changes

- 4b56ed84: Various starknet-proxyd fixes
- 4b56ed84: Various nori fixes

## 3.3.0

### Minor Changes

- 7b7ffd2e: Allows string RPC ids on starknet-proxyd
- 7b7ffd2e: Allows string RPC ids on nori

## 3.2.0

Expand All @@ -191,14 +191,14 @@

### Patch Changes

- 1b79aa62: Release starknet-proxyd
- 1b79aa62: Release nori

## 3.1.1

### Patch Changes

- b8802054: Trigger release of starknet-proxyd
- 34fcb277: Bump starknet-proxyd to test release build workflow
- b8802054: Trigger release of nori
- 34fcb277: Bump nori to test release build workflow

## 3.1.0

Expand All @@ -225,7 +225,7 @@

### Minor Changes

- 8cc824e5: Updates starknet-proxyd to include additional error metrics.
- 8cc824e5: Updates nori to include additional error metrics.
- 9ba4c5e0: Update metrics, support authenticated endpoints
- 78d0f3f0: Put special errors in a dedicated metric, pass along the content-type header

Expand Down
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ ARG GITVERSION=docker

RUN apk add make jq git gcc musl-dev linux-headers

COPY ./starknet-proxyd /app
COPY ./nori /app

WORKDIR /app

RUN make starknet-proxyd
RUN make nori

FROM alpine:3.18

RUN apk add bind-tools jq curl bash git redis

COPY ./starknet-proxyd/entrypoint.sh /bin/entrypoint.sh
COPY ./nori/entrypoint.sh /bin/entrypoint.sh

RUN apk update && \
apk add ca-certificates && \
chmod +x /bin/entrypoint.sh

EXPOSE 8080

VOLUME /etc/starknet-proxyd
VOLUME /etc/nori

COPY --from=builder /app/bin/starknet-proxyd /bin/starknet-proxyd
COPY --from=builder /app/bin/nori /bin/nori

ENTRYPOINT ["/bin/entrypoint.sh"]
CMD ["/bin/starknet-proxyd", "/etc/starknet-proxyd/starknet-proxyd.toml"]
CMD ["/bin/nori", "/etc/nori/nori.toml"]
4 changes: 2 additions & 2 deletions Dockerfile.ignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# ignore everything but starknet-proxyd, starknet-proxyd defines all its dependencies in the go.mod
# ignore everything but nori, nori defines all its dependencies in the go.mod
*
!/starknet-proxyd
!/nori
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ LDFLAGSSTRING +=-X main.GitDate=$(GITDATE)
LDFLAGSSTRING +=-X main.GitVersion=$(GITVERSION)
LDFLAGS := -ldflags "$(LDFLAGSSTRING)"

starknet-proxyd:
go build -v $(LDFLAGS) -o ./bin/starknet-proxyd ./cmd/starknet-proxyd
.PHONY: starknet-proxyd
nori:
go build -v $(LDFLAGS) -o ./bin/nori ./cmd/nori
.PHONY: nori

fmt:
go mod tidy
Expand Down
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# rpc-proxy
# Nori

This tool implements `starknet-proxyd`, an RPC request router and proxy. It does the following things:
Nori (のり) - Derived from "Norikae" (乗り換え), meaning transfer or switch, this name reflects the idea of efficiently switching or balancing network requests, akin to a load balancer or router.

It's essentially a fork of [Optimism proxyd](https://github.com/ethereum-optimism/optimism/tree/develop/proxyd), adapted to work with Starknet.

This tool implements `nori`, an RPC request router and proxy. It does the following things:

1. Whitelists RPC methods.
2. Routes RPC methods to groups of backend services.
Expand All @@ -14,20 +18,20 @@ This tool implements `starknet-proxyd`, an RPC request router and proxy. It does

## Usage

Run `make starknet-proxyd` to build the binary. No additional dependencies are necessary.
Run `make nori` to build the binary. No additional dependencies are necessary.

To configure `starknet-proxyd` for use, you'll need to create a configuration file to define your proxy backends and routing rules. Check out [example.config.toml](./example.config.toml) for how to do this alongside a full list of all options with commentary.
To configure `nori` for use, you'll need to create a configuration file to define your proxy backends and routing rules. Check out [example.config.toml](./example.config.toml) for how to do this alongside a full list of all options with commentary.

Once you have a config file, start the daemon via `starknet-proxyd <path-to-config>.toml`.
Once you have a config file, start the daemon via `nori <path-to-config>.toml`.


## Consensus awareness

Starting on v4.0.0, `starknet-proxyd` is aware of the consensus state of its backends. This helps minimize chain reorgs experienced by clients.
Starting on v4.0.0, `nori` is aware of the consensus state of its backends. This helps minimize chain reorgs experienced by clients.

To enable this behavior, you must set `consensus_aware` value to `true` in the backend group.

When consensus awareness is enabled, `starknet-proxyd` will poll the backends for their states and resolve a consensus group based on:
When consensus awareness is enabled, `nori` will poll the backends for their states and resolve a consensus group based on:
* the common ancestor `latest` block, i.e. if a backend is experiencing a fork, the fork won't be visible to the clients
* the lowest `safe` block
* the lowest `finalized` block
Expand All @@ -52,10 +56,10 @@ and won't receive any traffic during this period.

## Tag rewrite

When consensus awareness is enabled, `starknet-proxyd` will enforce the consensus state transparently for all the clients.
When consensus awareness is enabled, `nori` will enforce the consensus state transparently for all the clients.

For example, if a client requests the `eth_getBlockByNumber` method with the `latest` tag,
`starknet-proxyd` will rewrite the request to use the resolved latest block from the consensus group
`nori` will rewrite the request to use the resolved latest block from the consensus group
and forward it to the backend.

The following request methods are rewritten:
Expand Down Expand Up @@ -92,7 +96,7 @@ Cache use Redis and can be enabled for the following immutable methods:
## Meta method `consensus_getReceipts`

To support backends with different specifications in the same backend group,
starknet-proxyd exposes a convenient method to fetch receipts abstracting away
nori exposes a convenient method to fetch receipts abstracting away
what specific backend will serve the request.

Each backend specifies their preferred method to fetch receipts with `consensus_receipts_target` config,
Expand Down Expand Up @@ -132,7 +136,7 @@ Response example
}
```

See [op-node receipt fetcher](https://github.com/ethereum-optimism/optimism/blob/186e46a47647a51a658e699e9ff047d39444c2de/op-node/sources/receipts.go#L186-L253).
See [op-node receipt fetcher](https://github.com/abdelhamidbakhta/nori/blob/186e46a47647a51a658e699e9ff047d39444c2de/op-node/sources/receipts.go#L186-L253).


## Metrics
Expand All @@ -143,4 +147,4 @@ The metrics port is configurable via the `metrics.port` and `metrics.host` keys

## Adding Backend SSL Certificates in Docker

The Docker image runs on Alpine Linux. If you get SSL errors when connecting to a backend within Docker, you may need to add additional certificates to Alpine's certificate store. To do this, bind mount the certificate bundle into a file in `/usr/local/share/ca-certificates`. The `entrypoint.sh` script will then update the store with whatever is in the `ca-certificates` directory prior to starting `starknet-proxyd`.
The Docker image runs on Alpine Linux. If you get SSL errors when connecting to a backend within Docker, you may need to add additional certificates to Alpine's certificate store. To do this, bind mount the certificate bundle into a file in `/usr/local/share/ca-certificates`. The `entrypoint.sh` script will then update the store with whatever is in the `ca-certificates` directory prior to starting `nori`.
22 changes: 11 additions & 11 deletions backend.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package starknet-proxyd
package nori

import (
"bytes"
Expand All @@ -25,7 +25,7 @@ import (
"github.com/xaionaro-go/weightedshuffle"
"golang.org/x/sync/semaphore"

sw "github.com/abdelhamidbakhta/starknet-proxyd/pkg/avg-sliding-window"
sw "github.com/abdelhamidbakhta/nori/pkg/avg-sliding-window"
)

const (
Expand Down Expand Up @@ -149,7 +149,7 @@ type Backend struct {
maxWSConns int
outOfServiceInterval time.Duration
stripTrailingXFF bool
starknet-proxydIP string
noriIP string

skipPeerCountCheck bool
forcedCandidate bool
Expand Down Expand Up @@ -231,9 +231,9 @@ func WithStrippedTrailingXFF() BackendOpt {
}
}

func Withstarknet-proxydIP(ip string) BackendOpt {
func WithnoriIP(ip string) BackendOpt {
return func(b *Backend) {
b.starknet-proxydIP = ip
b.noriIP = ip
}
}

Expand Down Expand Up @@ -333,8 +333,8 @@ func NewBackend(

backend.Override(opts...)

if !backend.stripTrailingXFF && backend.starknet-proxydIP == "" {
log.Warn("proxied requests' XFF header will not contain the starknet-proxyd ip address")
if !backend.stripTrailingXFF && backend.noriIP == "" {
log.Warn("proxied requests' XFF header will not contain the nori ip address")
}

return backend
Expand Down Expand Up @@ -544,8 +544,8 @@ func (b *Backend) doForward(ctx context.Context, rpcReqs []*RPCReq, isBatch bool
xForwardedFor := GetXForwardedFor(ctx)
if b.stripTrailingXFF {
xForwardedFor = stripXFF(xForwardedFor)
} else if b.starknet-proxydIP != "" {
xForwardedFor = fmt.Sprintf("%s, %s", xForwardedFor, b.starknet-proxydIP)
} else if b.noriIP != "" {
xForwardedFor = fmt.Sprintf("%s, %s", xForwardedFor, b.noriIP)
}

httpReq.Header.Set("content-type", "application/json")
Expand Down Expand Up @@ -1011,7 +1011,7 @@ func (w *WSProxier) clientPump(ctx context.Context, errC chan error) {
"err", err,
)
msg = mustMarshalJSON(NewRPCErrorRes(id, err))
RecordRPCError(ctx, Backendstarknet-proxyd, method, err)
RecordRPCError(ctx, Backendnori, method, err)

// Send error response to client
err = w.writeClientConn(msgType, msg)
Expand All @@ -1025,7 +1025,7 @@ func (w *WSProxier) clientPump(ctx context.Context, errC chan error) {
// Send eth_accounts requests directly to the client
if req.Method == "eth_accounts" {
msg = mustMarshalJSON(NewRPCRes(req.ID, emptyArrayResponse))
RecordRPCForward(ctx, Backendstarknet-proxyd, "eth_accounts", RPCRequestSourceWS)
RecordRPCForward(ctx, Backendnori, "eth_accounts", RPCRequestSourceWS)
err = w.writeClientConn(msgType, msg)
if err != nil {
errC <- err
Expand Down
5 changes: 3 additions & 2 deletions backend_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package starknet-proxyd
package nori

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestStripXFF(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package starknet-proxyd
package nori

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package starknet-proxyd
package nori

import (
"context"
Expand Down
4 changes: 2 additions & 2 deletions cmd/proxyd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
),
)

log.Info("starting starknet-proxyd", "version", GitVersion, "commit", GitCommit, "date", GitDate)
log.Info("starting nori", "version", GitVersion, "commit", GitCommit, "date", GitDate)

if len(os.Args) < 2 {
log.Crit("must specify a config file on the command line")
Expand Down Expand Up @@ -68,7 +68,7 @@ func main() {

_, shutdown, err := starknet - proxyd.Start(config)
if err != nil {
log.Crit("error starting starknet-proxyd", "err", err)
log.Crit("error starting nori", "err", err)
}

sig := make(chan os.Signal, 1)
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package starknet-proxyd
package nori

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion consensus_poller.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package starknet-proxyd
package nori

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion consensus_tracker.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package starknet-proxyd
package nori

import (
"context"
Expand Down
Loading

0 comments on commit c7fc5a1

Please sign in to comment.