Skip to content
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

require relay pubkey #143

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
Related issue(s):
## Description


Description:
## Context & references


Problem(s) & goal(s):


Additional context & references:
## Additional comments


---

I have run these commands:
## I have run these commands

* [ ] `make lint`
* [ ] `make test`
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ jobs:
mergemock:
name: Mergemock
runs-on: ubuntu-latest
env:
CGO_CFLAGS_ALLOW: "-O -D__BLST_PORTABLE__"
CGO_CFLAGS: "-O -D__BLST_PORTABLE__"
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
Expand Down Expand Up @@ -92,6 +95,3 @@ jobs:
make run-boost-with-relay &
sleep 5
make MERGEMOCK_DIR=./mergemock run-mergemock-consensus
env:
CGO_CFLAGS_ALLOW: "-D__BLST_PORTABLE__"
CGO_CFLAGS: "-D__BLST_PORTABLE__"
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,25 @@ go install github.com/ferranbt/fastssz/sszgen@latest
go install github.com/mgechev/revive@latest
go install honnef.co/go/tools/cmd/staticcheck@master
```
## Build
## Build & Run

```
```bash
make build
./mev-boost -relays http://0x821961b64d99b997c934c22b4fd6109790acf00f7969322c4e9dbf1ca278c333148284c01c5ef551a1536ddd14b178b9@localhost:28545
```

and then run it with:
Alternatively, run mev-boost without compile step:

```bash
go run cmd/mev-boost/main.go -relays http://0x821961b64d99b997c934c22b4fd6109790acf00f7969322c4e9dbf1ca278c333148284c01c5ef551a1536ddd14b178b9@localhost:28545
```
./mev-boost

If the test or target application crashes with an "illegal instruction" exception, run/rebuild with CGO_CFLAGS environment variable set to `-O -D__BLST_PORTABLE__`. This error also happens if you are on an ARM-based system, including the Apple M1/M2 chip.


```bash
export CGO_CFLAGS_ALLOW="-O -D__BLST_PORTABLE__"
export CGO_CFLAGS="-O -D__BLST_PORTABLE__"
```

## Lint & Test
Expand Down
8 changes: 4 additions & 4 deletions cmd/mev-boost/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ var (
version = "dev" // is set during build process

// defaults
defaultListenAddr = getEnv("BOOST_LISTEN_ADDR", "localhost:18550")
defaultRelayURLs = getEnv("RELAY_URLS", "localhost:28545") // can be IP@PORT, PUBKEY@IP:PORT, https://IP, etc.
defaultRelayTimeoutMs = getEnvInt("RELAY_TIMEOUT_MS", 2000) // timeout for all the requests to the relay
defaultListenAddr = getEnv("BOOST_LISTEN_ADDR", "localhost:18550")
//defaultRelayURLs = getEnv("RELAY_URLS", "http://0x821961b64d99b997c934c22b4fd6109790acf00f7969322c4e9dbf1ca278c333148284c01c5ef551a1536ddd14b178b9@localhost:28545") // can be IP@PORT, PUBKEY@IP:PORT, https://IP, etc.
defaultRelayTimeoutMs = getEnvInt("RELAY_TIMEOUT_MS", 2000) // timeout for all the requests to the relay
defaultRelayCheck = os.Getenv("RELAY_STARTUP_CHECK") != ""

// cli flags
listenAddr = flag.String("addr", defaultListenAddr, "listen-address for mev-boost server")
relayURLs = flag.String("relays", defaultRelayURLs, "relay urls - single entry or comma-separated list (pubkey@ip:port)")
relayURLs = flag.String("relays", "", "relay urls - single entry or comma-separated list (schema://pubkey@ip:port)")
relayTimeoutMs = flag.Int("request-timeout", defaultRelayTimeoutMs, "timeout for requests to a relay [ms]")
relayCheck = flag.Bool("relay-check", defaultRelayCheck, "whether to check relay status on startup")
)
Expand Down
22 changes: 16 additions & 6 deletions server/mock_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package server
import (
"encoding/json"
"fmt"
"github.com/flashbots/go-boost-utils/bls"
"github.com/flashbots/go-boost-utils/types"
"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"net/url"
"sync"
"testing"
"time"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/flashbots/go-boost-utils/bls"
"github.com/flashbots/go-boost-utils/types"
"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
)

// mockRelay is used to fake a relay's behavior.
Expand All @@ -22,8 +25,9 @@ type mockRelay struct {
t *testing.T

// KeyPair used to sign messages
secretKey *bls.SecretKey
publicKey *bls.PublicKey
secretKey *bls.SecretKey
publicKey *bls.PublicKey
RelayEntry RelayEntry

// Used to count each Request made to the relay, either if it fails or not, for each method
mu sync.Mutex
Expand Down Expand Up @@ -52,6 +56,12 @@ func newMockRelay(t *testing.T, secretKey *bls.SecretKey) *mockRelay {
// Initialize server
relay.Server = httptest.NewServer(relay.getRouter())

// Create the RelayEntry with correct pubkey
url, err := url.Parse(relay.Server.URL)
require.NoError(t, err)
urlWithKey := fmt.Sprintf("%s://%s@%s", url.Scheme, hexutil.Encode(publicKey.Compress()), url.Host)
relay.RelayEntry, err = NewRelayEntry(urlWithKey)
require.NoError(t, err)
return relay
}

Expand Down
9 changes: 3 additions & 6 deletions server/relay_entry.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package server

import (
"github.com/flashbots/go-boost-utils/types"
"net/url"
"strings"

"github.com/flashbots/go-boost-utils/types"
)

// RelayEntry represents a relay that mev-boost connects to.
Expand Down Expand Up @@ -37,10 +38,6 @@ func NewRelayEntry(relayURL string) (entry RelayEntry, err error) {
entry.Address = entry.URL.Scheme + "://" + entry.URL.Host

// Extract the relay's public key from the parsed URL.
// TODO: Remove the if condition, as it is mandatory to verify relay's message signature.
if entry.URL.User.Username() != "" {
err = entry.PublicKey.UnmarshalText([]byte(entry.URL.User.Username()))
}

err = entry.PublicKey.UnmarshalText([]byte(entry.URL.User.Username()))
return entry, err
}
33 changes: 17 additions & 16 deletions server/relay_entry_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package server

import (
"fmt"
"testing"

"github.com/flashbots/go-boost-utils/types"
"github.com/stretchr/testify/require"
"testing"
)

func TestParseRelaysURLs(t *testing.T) {
zeroPublicKey := types.PublicKey{0x0}
// Used to fake a relay's public key.
publicKey := types.PublicKey{0x01}

Expand All @@ -22,21 +23,21 @@ func TestParseRelaysURLs(t *testing.T) {
}{
{
name: "Relay URL with protocol scheme",
relayURL: "http://foo.com",
relayURL: fmt.Sprintf("http://%s@foo.com", publicKey.String()),

expectedErr: nil,
expectedAddress: "http://foo.com",
expectedPublicKey: zeroPublicKey.String(),
expectedURL: "http://foo.com",
expectedPublicKey: publicKey.String(),
expectedURL: fmt.Sprintf("http://%s@foo.com", publicKey.String()),
},
{
name: "Relay URL without protocol scheme",
name: "Relay URL without protocol scheme, without public key",
relayURL: "foo.com",

expectedErr: nil,
expectedAddress: "http://foo.com",
expectedPublicKey: zeroPublicKey.String(),
expectedURL: "http://foo.com",
expectedErr: types.ErrLength,
expectedAddress: "",
expectedPublicKey: "",
expectedURL: "",
},
{
name: "Relay URL without protocol scheme and with public key",
Expand All @@ -58,21 +59,21 @@ func TestParseRelaysURLs(t *testing.T) {
},
{
name: "Relay URL with IP and port",
relayURL: "12.345.678:9999",
relayURL: publicKey.String() + "@12.345.678:9999",

expectedErr: nil,
expectedAddress: "http://12.345.678:9999",
expectedPublicKey: zeroPublicKey.String(),
expectedURL: "http://12.345.678:9999",
expectedPublicKey: publicKey.String(),
expectedURL: "http://" + publicKey.String() + "@12.345.678:9999",
},
{
name: "Relay URL with https IP and port",
relayURL: "https://12.345.678:9999",
relayURL: "https://" + publicKey.String() + "@12.345.678:9999",

expectedErr: nil,
expectedAddress: "https://12.345.678:9999",
expectedPublicKey: zeroPublicKey.String(),
expectedURL: "https://12.345.678:9999",
expectedPublicKey: publicKey.String(),
expectedURL: "https://" + publicKey.String() + "@12.345.678:9999",
},
{
name: "Invalid relay public key",
Expand Down
16 changes: 4 additions & 12 deletions server/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/flashbots/go-boost-utils/bls"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/flashbots/go-boost-utils/bls"

"github.com/flashbots/go-boost-utils/types"
"github.com/stretchr/testify/require"
)
Expand All @@ -29,20 +29,12 @@ func newTestBackend(t *testing.T, numRelays int, relayTimeout time.Duration) *te
relayEntries := make([]RelayEntry, numRelays)
for i := 0; i < numRelays; i++ {
// Generate private key for relay
blsPrivateKey, blsPublicKey, err := bls.GenerateNewKeypair()
blsPrivateKey, _, err := bls.GenerateNewKeypair()
require.NoError(t, err)

// Create a mock relay
backend.relays[i] = newMockRelay(t, blsPrivateKey)

// Create the relay.RelayEntry used to identify the relay
relayEntries[i], err = NewRelayEntry(backend.relays[i].Server.URL)
require.NoError(t, err)

// Hardcode relay's public key
publicKeyString := hexutil.Encode(blsPublicKey.Compress())
publicKey := _HexToPubkey(publicKeyString)
relayEntries[i].PublicKey = publicKey
relayEntries[i] = backend.relays[i].RelayEntry
}
service, err := NewBoostService("localhost:12345", relayEntries, testLog, relayTimeout)
require.NoError(t, err)
Expand Down