Skip to content

Commit

Permalink
Merge pull request #1 from samcm/feat/caching
Browse files Browse the repository at this point in the history
feat: HTTP & Internal caching
  • Loading branch information
samcm authored Aug 9, 2022
2 parents e02f06b + 737aa5c commit 55bd16d
Show file tree
Hide file tree
Showing 17 changed files with 726 additions and 253 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Go Test

on:
pull_request:

workflow_dispatch:
branches: [ '**' ]

jobs:
full_ci:
strategy:
matrix:
go_version: [ 1.18.x ]

runs-on: ubuntu-20.04

steps:
- name: checkout
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go_version }}

- name: run tests
run: go test -json ./... > test.json

- name: Annotate tests
if: always()
uses: guyarb/golang-test-annotations@v0.5.1
with:
test-results: test.json

35 changes: 13 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,49 +1,40 @@
module github.com/samcm/checkpointz

go 1.19
go 1.17

// replace github.com/samcm/beacon => /Users/samcm/go/src/github.com/samcm/beacon

require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/attestantio/go-eth2-client v0.13.0 // indirect
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/ethereum/go-ethereum v1.10.20 // indirect
github.com/attestantio/go-eth2-client v0.13.0
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9
github.com/go-co-op/gocron v1.16.2
github.com/julienschmidt/httprouter v1.3.0
github.com/pkg/errors v0.9.1
github.com/samcm/beacon v0.0.0-20220805054207-a8d17f0ee98b
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.5.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/fatih/color v1.13.0 // indirect
github.com/ferranbt/fastssz v0.1.1 // indirect
github.com/go-co-op/gocron v1.16.2 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/goccy/go-yaml v1.9.5 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/klauspost/cpuid/v2 v2.1.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prysmaticlabs/go-bitfield v0.0.0-20210809151128-385d8c5e3fb7 // indirect
github.com/r3labs/sse/v2 v2.8.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rs/zerolog v1.27.0 // indirect
github.com/samcm/beacon v0.0.0-20220805054207-a8d17f0ee98b // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
70 changes: 28 additions & 42 deletions go.sum

Large diffs are not rendered by default.

29 changes: 25 additions & 4 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (h *Handler) wrappedHandler(handler func(ctx context.Context, r *http.Reque
return
}

for header, value := range response.Headers {
w.Header().Set(header, value)
}

if err := WriteContentAwareResponse(w, data, contentType); err != nil {
h.log.WithError(err).Error("Failed to write response")
}
Expand All @@ -84,12 +88,12 @@ func (h *Handler) handleEthV2BeaconBlocks(ctx context.Context, r *http.Request,
return NewUnsupportedMediaTypeResponse(nil), err
}

req, err := eth.NewBlockIdentifier(p.ByName("block_id"))
blockID, err := eth.NewBlockIdentifier(p.ByName("block_id"))
if err != nil {
return NewBadRequestResponse(nil), err
}

block, err := h.eth.BeaconBlock(ctx, req)
block, err := h.eth.BeaconBlock(ctx, blockID)
if err != nil {
return NewInternalServerErrorResponse(nil), err
}
Expand Down Expand Up @@ -130,11 +134,28 @@ func (h *Handler) handleEthV2DebugBeaconStates(ctx context.Context, r *http.Requ
return NewInternalServerErrorResponse(nil), err
}

return NewSuccessResponse(ContentTypeResolvers{
if state == nil {
return NewInternalServerErrorResponse(nil), errors.New("state not found")
}

rsp := NewSuccessResponse(ContentTypeResolvers{
ContentTypeSSZ: func() ([]byte, error) {
return *state, nil
},
}), nil
})

switch id.Type() {
case eth.StateIDRoot, eth.StateIDGenesis, eth.StateIDSlot:
// TODO(sam.calder-mason): This should be calculated using the Weak-Subjectivity period.
rsp.SetCacheControl("public, s-max-age=6000")
case eth.StateIDFinalized:
// TODO(sam.calder-mason): This should be calculated using the Weak-Subjectivity period.
rsp.SetCacheControl("public, s-max-age=180")
case eth.StateIDHead:
rsp.SetCacheControl("public, s-max-age=30")
}

return rsp, nil
}

func (h *Handler) handleCheckpointzStatus(ctx context.Context, r *http.Request, p httprouter.Params, contentType ContentType) (*HTTPResponse, error) {
Expand Down
15 changes: 14 additions & 1 deletion pkg/api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ type ContentTypeResolvers map[ContentType]ContentTypeResolver

type HTTPResponse struct {
resolvers ContentTypeResolvers
StatusCode int `json:"status_code"`
StatusCode int `json:"status_code"`
Headers map[string]string `json:"headers"`
}

func (r HTTPResponse) MarshalAs(contentType ContentType) ([]byte, error) {
Expand All @@ -21,30 +22,42 @@ func (r HTTPResponse) MarshalAs(contentType ContentType) ([]byte, error) {
return r.resolvers[contentType]()
}

func (r HTTPResponse) SetEtag(etag string) {
r.Headers["ETag"] = etag
}

func (r HTTPResponse) SetCacheControl(v string) {
r.Headers["Cache-Control"] = v
}

func NewSuccessResponse(resolvers ContentTypeResolvers) *HTTPResponse {
return &HTTPResponse{
resolvers: resolvers,
StatusCode: http.StatusOK,
Headers: make(map[string]string),
}
}

func NewInternalServerErrorResponse(resolvers ContentTypeResolvers) *HTTPResponse {
return &HTTPResponse{
resolvers: resolvers,
StatusCode: http.StatusInternalServerError,
Headers: make(map[string]string),
}
}

func NewBadRequestResponse(resolvers ContentTypeResolvers) *HTTPResponse {
return &HTTPResponse{
resolvers: resolvers,
StatusCode: http.StatusBadRequest,
Headers: make(map[string]string),
}
}

func NewUnsupportedMediaTypeResponse(resolvers ContentTypeResolvers) *HTTPResponse {
return &HTTPResponse{
resolvers: resolvers,
StatusCode: http.StatusUnsupportedMediaType,
Headers: make(map[string]string),
}
}
33 changes: 0 additions & 33 deletions pkg/beacon/bundle.go

This file was deleted.

91 changes: 0 additions & 91 deletions pkg/beacon/bundles.go

This file was deleted.

Loading

0 comments on commit 55bd16d

Please sign in to comment.