From f8b515d42d6dd03c61665ff67500c7d7cd0531cf Mon Sep 17 00:00:00 2001 From: alok Date: Fri, 12 Jan 2024 19:24:43 +0530 Subject: [PATCH 1/5] feat: add go and release workflow --- .github/workflows/go-ci.yml | 39 ++++++++++++++++++++++++ .github/workflows/goreleaser.yaml | 36 +++++++++++++++++++++++ .goreleaser.yaml | 49 +++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 .github/workflows/go-ci.yml create mode 100644 .github/workflows/goreleaser.yaml create mode 100644 .goreleaser.yaml diff --git a/.github/workflows/go-ci.yml b/.github/workflows/go-ci.yml new file mode 100644 index 0000000..f19ae98 --- /dev/null +++ b/.github/workflows/go-ci.yml @@ -0,0 +1,39 @@ +name: mev-commit-oracle-ci + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build-and-test: + name: Build and Test + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v2 + + - name: Set up Go 1.21.1 + uses: actions/setup-go@v2 + with: + go-version: 1.21.1 + id: go + + - name: Lint with golangci-lint v1.54.2 + uses: golangci/golangci-lint-action@v2 + with: + version: v1.54.2 + + - name: Vet + run: go vet ./... + + - name: Build + run: go build ./... + + - name: Test + run: go test -short ./... + diff --git a/.github/workflows/goreleaser.yaml b/.github/workflows/goreleaser.yaml new file mode 100644 index 0000000..dfda67b --- /dev/null +++ b/.github/workflows/goreleaser.yaml @@ -0,0 +1,36 @@ +name: goreleaser + +on: + push: + tags: + - 'v*' + +permissions: + contents: write + packages: write + +jobs: + release: + runs-on: ubuntu-latest + steps: + - if: ${{ !startsWith(github.ref, 'refs/tags/v') }} + run: echo "flags=--snapshot" >> $GITHUB_ENV + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/setup-go@v3 + with: + go-version: 1.21 + cache: true + - name: GHCR Docker Login + run: echo "${{ secrets.CR_PAT }}" | docker login ghcr.io -u ${{ secrets.GHCR_USERNAME }} --password-stdin + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - uses: goreleaser/goreleaser-action@v4 + with: + distribution: goreleaser + version: latest + args: release --clean ${{ env.flags }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..d6d6b08 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,49 @@ +version: 1 + +before: + hooks: + - go mod tidy + +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + goarch: + - amd64 + main: ./cmd + ldflags: + - -s -w + +archives: + - format: tar.gz + # this name template makes the OS and Arch compatible with the results of `uname`. + name_template: >- + {{ .ProjectName }}_ + {{- title .Os }}_ + {{- if eq .Arch "amd64" }}x86_64 + {{- else if eq .Arch "386" }}i386 + {{- else }}{{ .Arch }}{{ end }} + {{- if .Arm }}v{{ .Arm }}{{ end }} + +dockers: + - image_templates: + - ghcr.io/primevprotocol/mev-commit-oracle:v{{ .Major }}.{{ .Minor }} + - ghcr.io/primevprotocol/mev-commit-oracle:latest + dockerfile: Dockerfile.goreleaser + use: buildx + goarch: amd64 + build_flag_templates: + - "--pull" + - "--label=org.opencontainers.image.created={{.Date}}" + - "--label=org.opencontainers.image.title={{.ProjectName}}" + - "--label=org.opencontainers.image.revision={{.FullCommit}}" + - "--label=org.opencontainers.image.version={{.Version}}" + - "--platform=linux/amd64" + +changelog: + sort: asc + filters: + exclude: + - "^docs:" + - "^test:" From 231e6e208195b7306fe62f175f39d4e7a713cb9c Mon Sep 17 00:00:00 2001 From: alok Date: Fri, 12 Jan 2024 19:27:44 +0530 Subject: [PATCH 2/5] fix: add goreleaser dockerfile --- Dockerfile.goreleaser | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Dockerfile.goreleaser diff --git a/Dockerfile.goreleaser b/Dockerfile.goreleaser new file mode 100644 index 0000000..5b4bcc9 --- /dev/null +++ b/Dockerfile.goreleaser @@ -0,0 +1,7 @@ +FROM alpine:latest + +COPY mev-commit-oracle /usr/local/bin/mev-commit-oracle + +EXPOSE 8080 + +ENTRYPOINT ["mev-commit-oracle", "start"] From fff2f6ed63aaf1a61f321076fb34fa2ecbd629a6 Mon Sep 17 00:00:00 2001 From: alok Date: Fri, 12 Jan 2024 19:39:43 +0530 Subject: [PATCH 3/5] fix: lint --- pkg/apiserver/apiserver.go | 4 ++-- pkg/node/node.go | 3 +++ pkg/settler/settler.go | 1 + pkg/updater/updater.go | 1 + pkg/updater/updater_test.go | 22 ++++++++++------------ 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index 6c0e75d..165b7bb 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -99,7 +99,7 @@ func (a *Service) registerStatsEndpoints() { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write(resp) + _, _ = w.Write(resp) }) a.router.HandleFunc("/stats", func(w http.ResponseWriter, r *http.Request) { @@ -119,7 +119,7 @@ func (a *Service) registerStatsEndpoints() { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write(resp) + _, _ = w.Write(resp) }) } diff --git a/pkg/node/node.go b/pkg/node/node.go index f53cebf..deba334 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -98,12 +98,14 @@ func NewNode(opts *Options) (*Node, error) { ) if err != nil { log.Fatal().Err(err).Msg("failed to instantiate preconf contract") + cancel() return nil, err } oracleContract, err := rollupclient.NewOracle(opts.OracleContractAddr, settlementClient) if err != nil { log.Fatal().Err(err).Msg("failed to instantiate oracle contract") + cancel() return nil, err } @@ -121,6 +123,7 @@ func NewNode(opts *Options) (*Node, error) { ) if err != nil { log.Fatal().Err(err).Msg("failed to set builder mapping") + cancel() return nil, err } } diff --git a/pkg/settler/settler.go b/pkg/settler/settler.go index 4439bb3..dcda472 100644 --- a/pkg/settler/settler.go +++ b/pkg/settler/settler.go @@ -183,6 +183,7 @@ func (s *Settler) Start(ctx context.Context) <-chan struct{} { for { select { case <-ctx.Done(): + unsub() return case settlement, more := <-settlementChan: if !more { diff --git a/pkg/updater/updater.go b/pkg/updater/updater.go index 41b3b67..16ced6f 100644 --- a/pkg/updater/updater.go +++ b/pkg/updater/updater.go @@ -87,6 +87,7 @@ func (u *Updater) Start(ctx context.Context) <-chan struct{} { for { select { case <-ctx.Done(): + unsub() return case winner, more := <-winnerChan: if !more { diff --git a/pkg/updater/updater_test.go b/pkg/updater/updater_test.go index 3c0baa8..c2c92cf 100644 --- a/pkg/updater/updater_test.go +++ b/pkg/updater/updater_test.go @@ -120,19 +120,17 @@ func TestUpdater(t *testing.T) { if count == 10 { break } - select { - case settlement := <-testWinnerRegister.settlements: - if settlement.blockNum != 5 { - t.Fatal("wrong block number") - } - if settlement.builder != "test" { - t.Fatal("wrong builder") - } - if settlement.isSlash { - t.Fatal("should not be slash") - } - count++ + settlement := <-testWinnerRegister.settlements + if settlement.blockNum != 5 { + t.Fatal("wrong block number") } + if settlement.builder != "test" { + t.Fatal("wrong builder") + } + if settlement.isSlash { + t.Fatal("should not be slash") + } + count++ } select { From e5cb2010ecf1ad6f1c8187523ec25ff50b89fd55 Mon Sep 17 00:00:00 2001 From: alok Date: Sat, 13 Jan 2024 08:59:05 +0530 Subject: [PATCH 4/5] fix: comments and trimpath --- .goreleaser.yaml | 3 +++ pkg/apiserver/apiserver.go | 10 ++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index d6d6b08..de16837 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -12,6 +12,9 @@ builds: goarch: - amd64 main: ./cmd + flags: + - -v + - -trimpath ldflags: - -s -w diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index 165b7bb..ff0f38f 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -99,7 +99,10 @@ func (a *Service) registerStatsEndpoints() { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - _, _ = w.Write(resp) + _, err = w.Write(resp) + if err != nil { + log.Error().Err(err).Msg("failed to write response") + } }) a.router.HandleFunc("/stats", func(w http.ResponseWriter, r *http.Request) { @@ -119,7 +122,10 @@ func (a *Service) registerStatsEndpoints() { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - _, _ = w.Write(resp) + _, err = w.Write(resp) + if err != nil { + log.Error().Err(err).Msg("failed to write response") + } }) } From 840e3811d2f527d050fbc0b0d1333fee6b3096ce Mon Sep 17 00:00:00 2001 From: alok Date: Tue, 16 Jan 2024 02:39:55 +0530 Subject: [PATCH 5/5] fix: comments and trimpath --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b0550ae..908e207 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21.1 require ( github.com/ethereum/go-ethereum v1.13.5 github.com/lib/pq v1.10.9 + github.com/primevprotocol/contracts-abi v0.1.0 github.com/prometheus/client_golang v1.14.0 github.com/rs/zerolog v1.31.0 github.com/urfave/cli/v2 v2.27.1 @@ -42,7 +43,6 @@ require ( github.com/mattn/go-runewidth v0.0.14 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/mmcloughlin/addchain v0.4.0 // indirect - github.com/primevprotocol/contracts-abi v0.1.0 // indirect github.com/prometheus/client_model v0.3.0 // indirect github.com/prometheus/common v0.39.0 // indirect github.com/prometheus/procfs v0.9.0 // indirect