2022-05-05 00:51:24 +03:00
|
|
|
package integration_tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-08-27 04:39:30 +03:00
|
|
|
"io"
|
2022-05-05 00:51:24 +03:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2022-06-08 18:09:32 +03:00
|
|
|
"time"
|
|
|
|
|
2024-02-07 00:02:02 +03:00
|
|
|
"github.com/BurntSushi/toml"
|
2022-06-08 18:09:32 +03:00
|
|
|
"github.com/gorilla/websocket"
|
2024-02-07 00:02:02 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/exp/slog"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2022-05-05 00:51:24 +03:00
|
|
|
|
2024-08-22 20:19:26 +03:00
|
|
|
"github.com/ethereum-optimism/infra/proxyd"
|
2022-05-05 00:51:24 +03:00
|
|
|
)
|
|
|
|
|
2022-06-08 18:09:32 +03:00
|
|
|
type ProxydHTTPClient struct {
|
2022-08-04 20:34:43 +03:00
|
|
|
url string
|
|
|
|
headers http.Header
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
|
2022-06-08 18:09:32 +03:00
|
|
|
func NewProxydClient(url string) *ProxydHTTPClient {
|
2022-08-04 20:34:43 +03:00
|
|
|
return NewProxydClientWithHeaders(url, make(http.Header))
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProxydClientWithHeaders(url string, headers http.Header) *ProxydHTTPClient {
|
|
|
|
clonedHeaders := headers.Clone()
|
|
|
|
clonedHeaders.Set("Content-Type", "application/json")
|
|
|
|
return &ProxydHTTPClient{
|
|
|
|
url: url,
|
|
|
|
headers: clonedHeaders,
|
|
|
|
}
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
|
2022-06-08 18:09:32 +03:00
|
|
|
func (p *ProxydHTTPClient) SendRPC(method string, params []interface{}) ([]byte, int, error) {
|
2022-05-05 00:51:24 +03:00
|
|
|
rpcReq := NewRPCReq("999", method, params)
|
|
|
|
body, err := json.Marshal(rpcReq)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return p.SendRequest(body)
|
|
|
|
}
|
|
|
|
|
2022-06-08 18:09:32 +03:00
|
|
|
func (p *ProxydHTTPClient) SendBatchRPC(reqs ...*proxyd.RPCReq) ([]byte, int, error) {
|
2022-05-05 00:51:24 +03:00
|
|
|
body, err := json.Marshal(reqs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return p.SendRequest(body)
|
|
|
|
}
|
|
|
|
|
2022-06-08 18:09:32 +03:00
|
|
|
func (p *ProxydHTTPClient) SendRequest(body []byte) ([]byte, int, error) {
|
2022-08-04 20:34:43 +03:00
|
|
|
req, err := http.NewRequest("POST", p.url, bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
req.Header = p.headers
|
|
|
|
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
2022-05-05 00:51:24 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
code := res.StatusCode
|
2022-08-27 04:39:30 +03:00
|
|
|
resBody, err := io.ReadAll(res.Body)
|
2022-05-05 00:51:24 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return resBody, code, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RequireEqualJSON(t *testing.T, expected []byte, actual []byte) {
|
|
|
|
expJSON := canonicalizeJSON(t, expected)
|
|
|
|
actJSON := canonicalizeJSON(t, actual)
|
|
|
|
require.Equal(t, string(expJSON), string(actJSON))
|
|
|
|
}
|
|
|
|
|
|
|
|
func canonicalizeJSON(t *testing.T, in []byte) []byte {
|
|
|
|
var any interface{}
|
|
|
|
if in[0] == '[' {
|
|
|
|
any = make([]interface{}, 0)
|
|
|
|
} else {
|
|
|
|
any = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(in, &any)
|
|
|
|
require.NoError(t, err)
|
|
|
|
out, err := json.Marshal(any)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadConfig(name string) *proxyd.Config {
|
|
|
|
config := new(proxyd.Config)
|
|
|
|
_, err := toml.DecodeFile(fmt.Sprintf("testdata/%s.toml", name), config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRPCReq(id string, method string, params []interface{}) *proxyd.RPCReq {
|
|
|
|
jsonParams, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proxyd.RPCReq{
|
|
|
|
JSONRPC: proxyd.JSONRPCVersion,
|
|
|
|
Method: method,
|
|
|
|
Params: jsonParams,
|
|
|
|
ID: []byte(id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 18:09:32 +03:00
|
|
|
type ProxydWSClient struct {
|
|
|
|
conn *websocket.Conn
|
|
|
|
msgCB ProxydWSClientOnMessage
|
|
|
|
closeCB ProxydWSClientOnClose
|
|
|
|
}
|
|
|
|
|
|
|
|
type WSMessage struct {
|
|
|
|
Type int
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
2024-02-07 00:02:02 +03:00
|
|
|
type (
|
|
|
|
ProxydWSClientOnMessage func(msgType int, data []byte)
|
|
|
|
ProxydWSClientOnClose func(err error)
|
|
|
|
)
|
2022-06-08 18:09:32 +03:00
|
|
|
|
|
|
|
func NewProxydWSClient(
|
|
|
|
url string,
|
|
|
|
msgCB ProxydWSClientOnMessage,
|
|
|
|
closeCB ProxydWSClientOnClose,
|
|
|
|
) (*ProxydWSClient, error) {
|
|
|
|
conn, _, err := websocket.DefaultDialer.Dial(url, nil) // nolint:bodyclose
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &ProxydWSClient{
|
|
|
|
conn: conn,
|
|
|
|
msgCB: msgCB,
|
|
|
|
closeCB: closeCB,
|
|
|
|
}
|
|
|
|
go c.readPump()
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProxydWSClient) readPump() {
|
|
|
|
for {
|
|
|
|
mType, msg, err := h.conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
if h.closeCB != nil {
|
|
|
|
h.closeCB(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if h.msgCB != nil {
|
|
|
|
h.msgCB(mType, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProxydWSClient) HardClose() {
|
|
|
|
h.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProxydWSClient) SoftClose() error {
|
|
|
|
return h.WriteMessage(websocket.CloseMessage, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProxydWSClient) WriteMessage(msgType int, msg []byte) error {
|
|
|
|
return h.conn.WriteMessage(msgType, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProxydWSClient) WriteControlMessage(msgType int, msg []byte) error {
|
|
|
|
return h.conn.WriteControl(msgType, msg, time.Now().Add(time.Minute))
|
|
|
|
}
|
|
|
|
|
2022-05-05 00:51:24 +03:00
|
|
|
func InitLogger() {
|
2024-03-08 03:29:59 +03:00
|
|
|
log.SetDefault(log.NewLogger(slog.NewJSONHandler(
|
|
|
|
os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})))
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|