From 3b183be2f3fdbc44e497cbde569f0c8d6ec5d37d Mon Sep 17 00:00:00 2001 From: cairo <101215230+cairoeth@users.noreply.github.com> Date: Sat, 23 Mar 2024 23:16:17 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20add=20CI=20workflows=20and=20lin?= =?UTF-8?q?t=20progress?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/lint.yaml | 35 ++++++++ Makefile | 4 + preconf-operator/cmd/operator/main.go | 4 +- preconf-operator/core/chainio/avs_writer.go | 6 +- preconf-operator/core/chainio/bindings.go | 3 +- preconf-operator/receiverapi/receiverapi.go | 4 +- preconf-operator/sse/client_rpc.go | 2 +- .../preconshare/bundle_validation_test.go | 84 +++++++++---------- preconf-share/preconshare/database_test.go | 44 ++++------ rpc/server/request_processor.go | 2 +- rpc/server/server.go | 6 +- rpc/server/url_params.go | 2 +- rpc/server/util.go | 2 +- rpc/tests/e2e_test.go | 15 ++-- rpc/testutils/mock_rpcbackend.go | 2 +- rpc/testutils/rpctesthelpers.go | 2 +- rpc/types/types.go | 6 +- test_tx.py | 29 ++++--- 18 files changed, 143 insertions(+), 109 deletions(-) create mode 100644 .github/workflows/lint.yaml diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..0564cb0 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,35 @@ +name: Lint + +on: [push, pull_request] + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ^1.20 + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v3 + + - name: Install gofumpt + run: go install mvdan.cc/gofumpt@v0.4.0 + + - name: Install staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@v0.4.2 + + - name: Install golangci-lint + run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.2 + + - name: Lint + run: make lint + + - name: Ensure go mod tidy runs without changes + run: | + go mod tidy + git diff-index HEAD + git diff-index --quiet HEAD \ No newline at end of file diff --git a/Makefile b/Makefile index d7d30ec..9b438a8 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,10 @@ run-anvil: run-all: make -j run-anvil run-share run-operator +# Run all in background and example (used by CI) +run-ci: + make run-all & && sleep 200 && python test_tx.py && exit 0 + test: go test ./... diff --git a/preconf-operator/cmd/operator/main.go b/preconf-operator/cmd/operator/main.go index c9b0b45..17ac959 100644 --- a/preconf-operator/cmd/operator/main.go +++ b/preconf-operator/cmd/operator/main.go @@ -36,11 +36,11 @@ func operatorMain(ctx_cli *cli.Context) error { if err != nil { return err } - configJson, err := json.MarshalIndent(nodeConfig, "", " ") + configJSON, err := json.MarshalIndent(nodeConfig, "", " ") if err != nil { log.Fatalf(err.Error()) } - log.Println("Config:", string(configJson)) + log.Println("Config:", string(configJSON)) log.Println("Initializing operator") operator, err := operator.NewOperatorFromConfig(nodeConfig) diff --git a/preconf-operator/core/chainio/avs_writer.go b/preconf-operator/core/chainio/avs_writer.go index fc01552..7760ce2 100644 --- a/preconf-operator/core/chainio/avs_writer.go +++ b/preconf-operator/core/chainio/avs_writer.go @@ -29,13 +29,13 @@ func BuildAvsWriterFromConfig(c *config.Config) (*AvsWriter, error) { return BuildAvsWriter(c.TxMgr, c.IncredibleSquaringRegistryCoordinatorAddr, c.OperatorStateRetrieverAddr, c.EthHttpClient, c.Logger) } -func BuildAvsWriter(txMgr txmgr.TxManager, registryCoordinatorAddr, operatorStateRetrieverAddr gethcommon.Address, ethHttpClient eth.EthClient, logger logging.Logger) (*AvsWriter, error) { - avsServiceBindings, err := NewAvsManagersBindings(registryCoordinatorAddr, operatorStateRetrieverAddr, ethHttpClient, logger) +func BuildAvsWriter(txMgr txmgr.TxManager, registryCoordinatorAddr, operatorStateRetrieverAddr gethcommon.Address, ethHTTPClient eth.EthClient, logger logging.Logger) (*AvsWriter, error) { + avsServiceBindings, err := NewAvsManagersBindings(registryCoordinatorAddr, operatorStateRetrieverAddr, ethHTTPClient, logger) if err != nil { logger.Error("Failed to create contract bindings", "err", err) return nil, err } - avsRegistryWriter, err := avsregistry.BuildAvsRegistryChainWriter(registryCoordinatorAddr, operatorStateRetrieverAddr, logger, ethHttpClient, txMgr) + avsRegistryWriter, err := avsregistry.BuildAvsRegistryChainWriter(registryCoordinatorAddr, operatorStateRetrieverAddr, logger, ethHTTPClient, txMgr) if err != nil { return nil, err } diff --git a/preconf-operator/core/chainio/bindings.go b/preconf-operator/core/chainio/bindings.go index 5acd2ed..4b294fd 100644 --- a/preconf-operator/core/chainio/bindings.go +++ b/preconf-operator/core/chainio/bindings.go @@ -6,7 +6,6 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" - gethcommon "github.com/ethereum/go-ethereum/common" regcoord "github.com/Layr-Labs/eigensdk-go/contracts/bindings/RegistryCoordinator" erc20mock "github.com/cairoeth/preconfirmations/contracts/bindings/ERC20Mock" @@ -21,7 +20,7 @@ type AvsManagersBindings struct { logger logging.Logger } -func NewAvsManagersBindings(registryCoordinatorAddr, operatorStateRetrieverAddr gethcommon.Address, ethclient eth.EthClient, logger logging.Logger) (*AvsManagersBindings, error) { +func NewAvsManagersBindings(registryCoordinatorAddr, operatorStateRetrieverAddr common.Address, ethclient eth.EthClient, logger logging.Logger) (*AvsManagersBindings, error) { contractRegistryCoordinator, err := regcoord.NewContractRegistryCoordinator(registryCoordinatorAddr, ethclient) if err != nil { return nil, err diff --git a/preconf-operator/receiverapi/receiverapi.go b/preconf-operator/receiverapi/receiverapi.go index 8fd5a0e..c01dfd6 100644 --- a/preconf-operator/receiverapi/receiverapi.go +++ b/preconf-operator/receiverapi/receiverapi.go @@ -21,8 +21,8 @@ type ReceiveApi struct { } type ReceiveResponse struct { - jsonrpc string `json:"jsonrpc"` - id int `json:"id"` + jsonrpc string + id int } type ReceiveTx struct { diff --git a/preconf-operator/sse/client_rpc.go b/preconf-operator/sse/client_rpc.go index 6b46a93..e2edf94 100644 --- a/preconf-operator/sse/client_rpc.go +++ b/preconf-operator/sse/client_rpc.go @@ -50,7 +50,7 @@ func (s *Subscription) readEvents() { for { msg, err := s.pubsub.ReceiveMessage(context.Background()) if err != nil { - fmt.Printf("Error occured receiving message from Preconf-Share stream", err) + fmt.Printf("Error occured receiving message from preconf-share stream %v", err) } var event MatchMakerEvent diff --git a/preconf-share/preconshare/bundle_validation_test.go b/preconf-share/preconshare/bundle_validation_test.go index ca6a596..5759536 100644 --- a/preconf-share/preconshare/bundle_validation_test.go +++ b/preconf-share/preconshare/bundle_validation_test.go @@ -18,111 +18,111 @@ func TestMergeInclusionIntervals(t *testing.T) { { name: "same intervals", top: RequestInclusion{ - BlockNumber: hexutil.Uint64(1), - MaxBlock: hexutil.Uint64(2), + DesiredBlock: hexutil.Uint64(1), + MaxBlock: hexutil.Uint64(2), }, bottom: RequestInclusion{ - BlockNumber: hexutil.Uint64(1), - MaxBlock: hexutil.Uint64(2), + DesiredBlock: hexutil.Uint64(1), + MaxBlock: hexutil.Uint64(2), }, expectedTop: RequestInclusion{ - BlockNumber: hexutil.Uint64(1), - MaxBlock: hexutil.Uint64(2), + DesiredBlock: hexutil.Uint64(1), + MaxBlock: hexutil.Uint64(2), }, err: nil, }, { name: "overlap, top to the right", top: RequestInclusion{ - BlockNumber: hexutil.Uint64(1), - MaxBlock: hexutil.Uint64(3), + DesiredBlock: hexutil.Uint64(1), + MaxBlock: hexutil.Uint64(3), }, bottom: RequestInclusion{ - BlockNumber: hexutil.Uint64(2), - MaxBlock: hexutil.Uint64(4), + DesiredBlock: hexutil.Uint64(2), + MaxBlock: hexutil.Uint64(4), }, expectedTop: RequestInclusion{ - BlockNumber: hexutil.Uint64(2), - MaxBlock: hexutil.Uint64(3), + DesiredBlock: hexutil.Uint64(2), + MaxBlock: hexutil.Uint64(3), }, err: nil, }, { name: "overlap, top to the left", top: RequestInclusion{ - BlockNumber: hexutil.Uint64(2), - MaxBlock: hexutil.Uint64(4), + DesiredBlock: hexutil.Uint64(2), + MaxBlock: hexutil.Uint64(4), }, bottom: RequestInclusion{ - BlockNumber: hexutil.Uint64(1), - MaxBlock: hexutil.Uint64(3), + DesiredBlock: hexutil.Uint64(1), + MaxBlock: hexutil.Uint64(3), }, expectedTop: RequestInclusion{ - BlockNumber: hexutil.Uint64(2), - MaxBlock: hexutil.Uint64(3), + DesiredBlock: hexutil.Uint64(2), + MaxBlock: hexutil.Uint64(3), }, err: nil, }, { name: "overlap, bottom inside top", top: RequestInclusion{ - BlockNumber: hexutil.Uint64(1), - MaxBlock: hexutil.Uint64(4), + DesiredBlock: hexutil.Uint64(1), + MaxBlock: hexutil.Uint64(4), }, bottom: RequestInclusion{ - BlockNumber: hexutil.Uint64(2), - MaxBlock: hexutil.Uint64(3), + DesiredBlock: hexutil.Uint64(2), + MaxBlock: hexutil.Uint64(3), }, expectedTop: RequestInclusion{ - BlockNumber: hexutil.Uint64(2), - MaxBlock: hexutil.Uint64(3), + DesiredBlock: hexutil.Uint64(2), + MaxBlock: hexutil.Uint64(3), }, err: nil, }, { name: "overlap, top inside bottom", top: RequestInclusion{ - BlockNumber: hexutil.Uint64(2), - MaxBlock: hexutil.Uint64(3), + DesiredBlock: hexutil.Uint64(2), + MaxBlock: hexutil.Uint64(3), }, bottom: RequestInclusion{ - BlockNumber: hexutil.Uint64(1), - MaxBlock: hexutil.Uint64(4), + DesiredBlock: hexutil.Uint64(1), + MaxBlock: hexutil.Uint64(4), }, expectedTop: RequestInclusion{ - BlockNumber: hexutil.Uint64(2), - MaxBlock: hexutil.Uint64(3), + DesiredBlock: hexutil.Uint64(2), + MaxBlock: hexutil.Uint64(3), }, }, { name: "no overlap, top to the right", top: RequestInclusion{ - BlockNumber: hexutil.Uint64(1), - MaxBlock: hexutil.Uint64(2), + DesiredBlock: hexutil.Uint64(1), + MaxBlock: hexutil.Uint64(2), }, bottom: RequestInclusion{ - BlockNumber: hexutil.Uint64(3), - MaxBlock: hexutil.Uint64(4), + DesiredBlock: hexutil.Uint64(3), + MaxBlock: hexutil.Uint64(4), }, expectedTop: RequestInclusion{ - BlockNumber: hexutil.Uint64(1), - MaxBlock: hexutil.Uint64(2), + DesiredBlock: hexutil.Uint64(1), + MaxBlock: hexutil.Uint64(2), }, err: ErrInvalidInclusion, }, { name: "no overlap, top to the left", top: RequestInclusion{ - BlockNumber: hexutil.Uint64(3), - MaxBlock: hexutil.Uint64(4), + DesiredBlock: hexutil.Uint64(3), + MaxBlock: hexutil.Uint64(4), }, bottom: RequestInclusion{ - BlockNumber: hexutil.Uint64(1), - MaxBlock: hexutil.Uint64(2), + DesiredBlock: hexutil.Uint64(1), + MaxBlock: hexutil.Uint64(2), }, expectedTop: RequestInclusion{ - BlockNumber: hexutil.Uint64(3), - MaxBlock: hexutil.Uint64(4), + DesiredBlock: hexutil.Uint64(3), + MaxBlock: hexutil.Uint64(4), }, err: ErrInvalidInclusion, }, diff --git a/preconf-share/preconshare/database_test.go b/preconf-share/preconshare/database_test.go index 9bdbeca..bdf34c0 100644 --- a/preconf-share/preconshare/database_test.go +++ b/preconf-share/preconshare/database_test.go @@ -2,7 +2,7 @@ package preconshare import ( "context" - "math/big" + "database/sql" "testing" "time" @@ -15,6 +15,18 @@ import ( var testPostgresDSN = cli.GetEnv("TEST_POSTGRES_DSN", "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable") +type DBSbundleBuilder struct { + Hash []byte `db:"hash"` + Cancelled bool `db:"cancelled"` + Block int64 `db:"block"` + MaxBlock int64 `db:"max_block"` + SimStateBlock sql.NullInt64 `db:"sim_state_block"` + SimEffGasPrice sql.NullString `db:"sim_eff_gas_price"` + SimProfit sql.NullString `db:"sim_profit"` + Body []byte `db:"body"` + InsertedAt time.Time `db:"inserted_at"` +} + func TestDBBackend_GetBundle(t *testing.T) { b, err := NewDBBackend(testPostgresDSN) require.NoError(t, err) @@ -73,9 +85,8 @@ func TestDBBackend_InsertBundleForStats(t *testing.T) { }, Body: []RequestBody{{Tx: (*hexutil.Bytes)(&tx)}}, Privacy: &RequestPrivacy{ - Hints: HintHash, - Builders: nil, - WantRefund: nil, + Hints: HintHash, + Operators: nil, }, Metadata: &RequestMetadata{ RequestHash: bundleHash, @@ -212,31 +223,6 @@ func TestDBBackend_InsertBundleForBuilder(t *testing.T) { _, err = b.db.Exec("DELETE FROM sbundle_builder WHERE hash = $1", bundleHash.Bytes()) require.NoError(t, err) - receivedAt := time.Now() - tx := common.Hex2Bytes("0x0102030405060708091011121314151617181920212223242526272829303132") - signer := common.HexToAddress("0x0102030405060708091011121314151617181920") - - bundle := SendRequestArgs{ - Version: "v0.1", - Inclusion: RequestInclusion{ - DesiredBlock: 6, - MaxBlock: 8, - }, - Body: []RequestBody{{Tx: (*hexutil.Bytes)(&tx)}}, - Privacy: &RequestPrivacy{ - Hints: HintHash, - Builders: nil, - WantRefund: nil, - }, - Metadata: &RequestMetadata{ - BundleHash: bundleHash, - BodyHashes: []common.Hash{bundleHash}, - Signer: signer, - OriginID: "test-origin", - ReceivedAt: hexutil.Uint64(receivedAt.UnixMicro()), - }, - } - var dbBundle DBSbundleBuilder err = b.db.Get(&dbBundle, "SELECT * FROM sbundle_builder WHERE hash = $1", bundleHash.Bytes()) require.NoError(t, err) diff --git a/rpc/server/request_processor.go b/rpc/server/request_processor.go index 35f8816..57e5b68 100644 --- a/rpc/server/request_processor.go +++ b/rpc/server/request_processor.go @@ -471,7 +471,7 @@ func (r *RpcRequest) writeRpcError(msg string, errCode int) { r.jsonRes = &types.JsonRpcResponse{ Id: r.jsonReq.Id, Version: "2.0", - Error: &types.JsonRpcError{ + Error: &types.JSONRPCError{ Code: errCode, Message: msg, }, diff --git a/rpc/server/server.go b/rpc/server/server.go index f69c49c..a3ac892 100644 --- a/rpc/server/server.go +++ b/rpc/server/server.go @@ -47,7 +47,7 @@ type RpcEndPointServer struct { listenAddress string logger *zap.Logger proxyTimeoutSeconds int - proxyUrl string + proxyURL string relaySigningKey *ecdsa.PrivateKey relayUrl string startTime time.Time @@ -98,7 +98,7 @@ func NewRpcEndPointServer(cfg Configuration) (*RpcEndPointServer, error) { listenAddress: cfg.ListenAddress, logger: cfg.Logger, proxyTimeoutSeconds: cfg.ProxyTimeoutSeconds, - proxyUrl: cfg.ProxyUrl, + proxyURL: cfg.ProxyUrl, relaySigningKey: cfg.RelaySigningKey, relayUrl: cfg.RelayUrl, startTime: Now(), @@ -237,7 +237,7 @@ func (s *RpcEndPointServer) HandleHttpRequest(respw http.ResponseWriter, req *ht return } - request := NewRpcRequestHandler(s.logger, &respw, req, s.proxyUrl, s.proxyTimeoutSeconds, s.relaySigningKey, s.relayUrl, s.db, s.builderNameProvider.BuilderNames(), s.chainID) + request := NewRpcRequestHandler(s.logger, &respw, req, s.proxyURL, s.proxyTimeoutSeconds, s.relaySigningKey, s.relayUrl, s.db, s.builderNameProvider.BuilderNames(), s.chainID) request.process() } diff --git a/rpc/server/url_params.go b/rpc/server/url_params.go index 6c2139d..d65ae68 100644 --- a/rpc/server/url_params.go +++ b/rpc/server/url_params.go @@ -139,7 +139,7 @@ func AuctionPreferenceErrorToJSONRPCResponse(jsonReq *types.JsonRpcRequest, err message := fmt.Sprintf("Invalid auction preference in the rpc endpoint url. %s", err.Error()) return &types.JsonRpcResponse{ Id: jsonReq.Id, - Error: &types.JsonRpcError{Code: types.JsonRpcInvalidRequest, Message: message}, + Error: &types.JSONRPCError{Code: types.JsonRpcInvalidRequest, Message: message}, Version: "2.0", } } diff --git a/rpc/server/util.go b/rpc/server/util.go index 4aae4af..4470403 100644 --- a/rpc/server/util.go +++ b/rpc/server/util.go @@ -132,7 +132,7 @@ func respBytesToJsonRPCResponse(respBytes []byte) (*types.JsonRpcResponse, error errorResp := new(types.RelayErrorResponse) if err := json.Unmarshal(respBytes, errorResp); err == nil && errorResp.Error != "" { // relay returned an error, convert to standard JSON-RPC error now - jsonRpcResp.Error = &types.JsonRpcError{Message: errorResp.Error} + jsonRpcResp.Error = &types.JSONRPCError{Message: errorResp.Error} return jsonRpcResp, nil } diff --git a/rpc/tests/e2e_test.go b/rpc/tests/e2e_test.go index 6af9304..4ce6ce5 100644 --- a/rpc/tests/e2e_test.go +++ b/rpc/tests/e2e_test.go @@ -31,11 +31,12 @@ var RpcBackendServerUrl string var relaySigningKey *ecdsa.PrivateKey func init() { - var err error - relaySigningKey, err = crypto.HexToECDSA("7bdeed70a07d5a45546e83a88dd430f71348592e747d2d3eb23f32db003eb0e1") - if err != nil { - log.Error("failed to create signing key", zap.Error(err)) - } + // var err error + relaySigningKey, _ = crypto.HexToECDSA("7bdeed70a07d5a45546e83a88dd430f71348592e747d2d3eb23f32db003eb0e1") + // if err != nil { + // logger.Crit("failed to create signing key", "err", err) + // log.Crit("failed to create signing key", "err", err) + // } } // func setServerTimeNowOffset(td time.Duration) { @@ -67,10 +68,12 @@ func testServerSetup(db database.Store) { txApiServer := httptest.NewServer(http.HandlerFunc(testutils.MockTxApiHandler)) server.ProtectTxApiHost = txApiServer.URL + logger, _ := zap.NewDevelopment() + // Create a fresh RPC endpoint server rpcServer, err := server.NewRpcEndPointServer(server.Configuration{ DB: db, - Logger: log.New("testlogger"), + Logger: logger, ProxyTimeoutSeconds: 10, ProxyUrl: RpcBackendServerUrl, RedisUrl: redisServer.Addr(), diff --git a/rpc/testutils/mock_rpcbackend.go b/rpc/testutils/mock_rpcbackend.go index 8f7b83a..c4ce9f1 100644 --- a/rpc/testutils/mock_rpcbackend.go +++ b/rpc/testutils/mock_rpcbackend.go @@ -98,7 +98,7 @@ func RpcBackendHandler(w http.ResponseWriter, req *http.Request) { log.Println("returnError:", msg) res := types.JsonRpcResponse{ Id: id, - Error: &types.JsonRpcError{ + Error: &types.JSONRPCError{ Code: -32603, Message: msg, }, diff --git a/rpc/testutils/rpctesthelpers.go b/rpc/testutils/rpctesthelpers.go index db41552..7d527f7 100644 --- a/rpc/testutils/rpctesthelpers.go +++ b/rpc/testutils/rpctesthelpers.go @@ -89,7 +89,7 @@ func SendRpcAndParseResponseTo(url string, req *types.JsonRpcRequest) (*types.Js errorResp := new(types.RelayErrorResponse) if err := json.Unmarshal(respData, errorResp); err == nil && errorResp.Error != "" { // relay returned an error, convert to standard JSON-RPC error now - jsonRpcResp.Error = &types.JsonRpcError{Message: errorResp.Error} + jsonRpcResp.Error = &types.JSONRPCError{Message: errorResp.Error} return jsonRpcResp, nil } diff --git a/rpc/types/types.go b/rpc/types/types.go index 19d82ca..1b279cd 100644 --- a/rpc/types/types.go +++ b/rpc/types/types.go @@ -43,17 +43,17 @@ func NewJsonRpcRequest1(id interface{}, method string, param interface{}) *JsonR type JsonRpcResponse struct { Id interface{} `json:"id"` Result json.RawMessage `json:"result,omitempty"` - Error *JsonRpcError `json:"error,omitempty"` + Error *JSONRPCError `json:"error,omitempty"` Version string `json:"jsonrpc"` } // RpcError: https://www.jsonrpc.org/specification#error_object -type JsonRpcError struct { +type JSONRPCError struct { Code int `json:"code"` Message string `json:"message"` } -func (err JsonRpcError) Error() string { +func (err JSONRPCError) Error() string { return fmt.Sprintf("Error %d (%s)", err.Code, err.Message) } diff --git a/test_tx.py b/test_tx.py index 0b14eae..a365b90 100644 --- a/test_tx.py +++ b/test_tx.py @@ -37,20 +37,27 @@ def request(): "jsonrpc": "2.0" }) + json_response = r.json() + + if json_response["result"]["preconfSignature"] is None: + raise Exception("No preconfirmation") + print(f"Status Code (request): {r.status_code}, Response: {r.json()}") -def threaded_process_range(): - store = {} - threads = [] +# def threaded_process_range(): +# store = {} +# threads = [] - # create the threads - threads.append(Thread(target=request)) +# # create the threads +# threads.append(Thread(target=request)) - # start the threads - [t.start() for t in threads] - # wait for the threads to finish - [t.join() for t in threads] - return store +# # start the threads +# [t.start() for t in threads] +# # wait for the threads to finish +# [t.join() for t in threads] +# return store -threaded_process_range() \ No newline at end of file +# threaded_process_range() + +request() \ No newline at end of file