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

Add version handler for api #25

Merged
merged 2 commits into from
May 30, 2024
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM golang:1.21.6-alpine3.19 as builder

RUN apk add --no-cache make gcc musl-dev linux-headers jq bash
RUN apk add --no-cache make gcc musl-dev linux-headers jq bash git

WORKDIR /app

Expand Down
3 changes: 3 additions & 0 deletions api/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
GITCOMMIT ?= $(shell git rev-parse HEAD)
LDFLAGS := -ldflags "-X github.com/base-org/blob-archiver/api/version.GitCommit=$(GITCOMMIT)"

blob-api:
env GO111MODULE=on GOOS=$(TARGETOS) GOARCH=$(TARGETARCH) go build -v $(LDFLAGS) -o ./bin/blob-api ./cmd/main.go

Expand Down
13 changes: 13 additions & 0 deletions api/service/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/attestantio/go-eth2-client/api"
"github.com/attestantio/go-eth2-client/spec/deneb"
m "github.com/base-org/blob-archiver/api/metrics"
"github.com/base-org/blob-archiver/api/version"
"github.com/base-org/blob-archiver/common/storage"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -106,6 +107,7 @@ func NewAPI(dataStoreClient storage.DataStoreReader, beaconClient client.BeaconB
})

r.Get("/eth/v1/beacon/blob_sidecars/{id}", result.blobSidecarHandler)
r.Get("/eth/v1/node/version", result.versionHandler)

return result
}
Expand All @@ -128,6 +130,17 @@ func isKnownIdentifier(id string) bool {
return slices.Contains([]string{"genesis", "finalized", "head"}, id)
}

// versionHandler implements the /eth/v1/node/version endpoint.
func (a *API) versionHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", jsonAcceptType)
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(version.APIVersion)
if err != nil {
a.logger.Error("unable to encode version to JSON", "err", err)
errServerError.write(w)
}
}

// toBeaconBlockHash converts a string that can be a slot, hash or identifier to a beacon block hash.
func (a *API) toBeaconBlockHash(id string) (common.Hash, *httpError) {
if isHash(id) {
Expand Down
18 changes: 18 additions & 0 deletions api/service/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/base-org/blob-archiver/common/beacon/beacontest"
"github.com/base-org/blob-archiver/common/blobtest"
"github.com/base-org/blob-archiver/common/storage"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -303,6 +304,23 @@ func TestAPIService(t *testing.T) {
}
}

func TestVersionHandler(t *testing.T) {
a, _, _, cleanup := setup(t)
defer cleanup()

request := httptest.NewRequest("GET", "/eth/v1/node/version", nil)
response := httptest.NewRecorder()

a.router.ServeHTTP(response, request)

require.Equal(t, 200, response.Code)
require.Equal(t, "application/json", response.Header().Get("Content-Type"))
var v eth.APIVersionResponse
err := json.Unmarshal(response.Body.Bytes(), &v)
require.NoError(t, err)
require.Equal(t, "Blob Archiver API/unknown", v.Data.Version)
}

func TestHealthHandler(t *testing.T) {
a, _, _, cleanup := setup(t)
defer cleanup()
Expand Down
25 changes: 25 additions & 0 deletions api/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package version

import (
"fmt"

"github.com/ethereum-optimism/optimism/op-service/eth"
)

var (
GitCommit = ""
APIVersion eth.APIVersionResponse
)

func init() {
commit := GitCommit
if commit == "" {
commit = "unknown"
}

APIVersion = eth.APIVersionResponse{
Data: eth.VersionInformation{
Version: fmt.Sprintf("Blob Archiver API/%s", commit),
},
}
}
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ services:
dockerfile: Dockerfile
env_file:
- .env
ports:
- "8000:8000"
command:
- "blob-api"
depends_on:
Expand Down
40 changes: 18 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ go 1.21.6

require (
github.com/attestantio/go-eth2-client v0.21.1
github.com/ethereum-optimism/optimism v1.7.2
github.com/ethereum/go-ethereum v1.13.8
github.com/ethereum-optimism/optimism v1.7.6
github.com/ethereum/go-ethereum v1.101315.1
github.com/go-chi/chi/v5 v5.0.12
github.com/minio/minio-go/v7 v7.0.66
github.com/minio/minio-go/v7 v7.0.70
github.com/prometheus/client_golang v1.19.0
github.com/rs/zerolog v1.32.0
github.com/stretchr/testify v1.9.0
github.com/urfave/cli/v2 v2.27.1
)

require (
github.com/DataDog/zstd v1.5.2 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/VictoriaMetrics/fastcache v1.12.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
Expand All @@ -33,9 +33,9 @@ require (
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240318114348-52d3dbd1605d // indirect
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240522134500-19555bdbdc95 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/ferranbt/fastssz v0.1.3 // indirect
Expand All @@ -44,16 +44,16 @@ require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-yaml v1.9.2 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/huandu/go-clone v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/klauspost/compress v1.17.6 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
Expand All @@ -64,8 +64,6 @@ require (
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -77,28 +75,26 @@ require (
github.com/rivo/uniseg v0.4.3 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/rs/zerolog v1.32.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.16.1 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.17.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect
Expand All @@ -108,4 +104,4 @@ require (
rsc.io/tmplfunc v0.0.3 // indirect
)

replace github.com/ethereum/go-ethereum v1.13.8 => github.com/ethereum-optimism/op-geth v1.101308.3-rc.1
replace github.com/ethereum/go-ethereum v1.101315.1 => github.com/ethereum-optimism/op-geth v1.101315.1
Loading