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..de16837 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,52 @@ +version: 1 + +before: + hooks: + - go mod tidy + +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + goarch: + - amd64 + main: ./cmd + flags: + - -v + - -trimpath + 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:" 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"] 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 diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index 6c0e75d..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") + } }) } 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 {