Skip to content

Commit

Permalink
clear responsibilities (#2124)
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene authored Nov 4, 2024
1 parent 8ce1944 commit d1b658a
Show file tree
Hide file tree
Showing 18 changed files with 310 additions and 269 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ require (
github.com/go-playground/validator/v10 v10.22.1 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.2.2 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/gorilla/mux v1.8.1 // indirect
Expand Down
9 changes: 2 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xP
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 h1:XHOnouVk1mxXfQidrMEnLlPk9UMeRtyBTnEFtxkV0kU=
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
github.com/DataDog/zstd v1.5.6 h1:LbEglqepa/ipmmQJUDnSsfvA8e8IStVcGaFWDuxvGOY=
github.com/DataDog/zstd v1.5.6/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/FantasyJony/openzeppelin-merkle-tree-go v1.1.3 h1:KzMvCFet0baw6uJnxTE/His8YeRgaxlASd4/ISuTvzI=
github.com/FantasyJony/openzeppelin-merkle-tree-go v1.1.3/go.mod h1:OiwyYqbtMkQH+VzA4b8lI+qHnExJy0fIdz+59/8nFes=
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 h1:XHOnouVk1mxXfQidrMEnLlPk9UMeRtyBTnEFtxkV0kU=
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=
github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/VictoriaMetrics/fastcache v1.12.2 h1:N0y9ASrJ0F6h0QaC3o6uJb3NIZ9VKLjCM7NQbSmF7WI=
Expand Down Expand Up @@ -175,9 +173,6 @@ github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOW
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.2.2 h1:1+mZ9upx1Dh6FmUTFR1naJ77miKiXgALjWOZ3NVFPmY=
github.com/golang/glog v1.2.2/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
62 changes: 62 additions & 0 deletions tools/walletextension/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cache

import (
"fmt"
"time"

"github.com/ethereum/go-ethereum/log"
Expand All @@ -21,3 +22,64 @@ type Cache interface {
func NewCache(logger log.Logger) (Cache, error) {
return NewRistrettoCacheWithEviction(logger)
}

type Strategy uint8

const (
NoCache Strategy = iota
LatestBatch Strategy = iota
LongLiving Strategy = iota

longCacheTTL = 5 * time.Hour
shortCacheTTL = 1 * time.Minute
)

type Cfg struct {
Type Strategy
DynamicType func() Strategy
}

func WithCache[R any](cache Cache, cfg *Cfg, cacheKey []byte, onCacheMiss func() (*R, error)) (*R, error) {
if cfg == nil {
return onCacheMiss()
}

cacheType := cfg.Type
if cfg.DynamicType != nil {
cacheType = cfg.DynamicType()
}

if cacheType == NoCache {
return onCacheMiss()
}

// we implement a custom cache eviction logic for the cache strategy of type LatestBatch.
// when a new batch is created, all entries with "LatestBatch" are considered evicted.
// elements not cached for a specific batch are not evicted
isEvicted := false
ttl := longCacheTTL
if cacheType == LatestBatch {
ttl = shortCacheTTL
isEvicted = cache.IsEvicted(cacheKey, ttl)
}

if !isEvicted {
cachedValue, foundInCache := cache.Get(cacheKey)
if foundInCache {
returnValue, ok := cachedValue.(*R)
if !ok {
return nil, fmt.Errorf("unexpected error. Invalid format cached. %v", cachedValue)
}
return returnValue, nil
}
}

result, err := onCacheMiss()

// cache only non-nil values
if err == nil && result != nil {
cache.Set(cacheKey, result, ttl)
}

return result, err
}
34 changes: 17 additions & 17 deletions tools/walletextension/httpapi/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import (
"fmt"
"net/http"

"github.com/ten-protocol/go-ten/tools/walletextension/services"

"github.com/status-im/keycard-go/hexutils"

"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ten-protocol/go-ten/go/common/viewingkey"
"github.com/ten-protocol/go-ten/lib/gethfork/node"
"github.com/ten-protocol/go-ten/tools/walletextension/rpcapi"

"github.com/ten-protocol/go-ten/go/common/log"

"github.com/ten-protocol/go-ten/go/common/httputil"
"github.com/ten-protocol/go-ten/tools/walletextension/common"
)

// NewHTTPRoutes returns the http specific routes
// todo - move these to the rpc framework.
func NewHTTPRoutes(walletExt *rpcapi.Services) []node.Route {
func NewHTTPRoutes(walletExt *services.Services) []node.Route {
return []node.Route{
{
Name: common.APIVersion1 + common.PathReady,
Expand Down Expand Up @@ -66,16 +66,16 @@ func NewHTTPRoutes(walletExt *rpcapi.Services) []node.Route {
}

func httpHandler(
walletExt *rpcapi.Services,
fun func(walletExt *rpcapi.Services, conn UserConn),
walletExt *services.Services,
fun func(walletExt *services.Services, conn UserConn),
) func(resp http.ResponseWriter, req *http.Request) {
return func(resp http.ResponseWriter, req *http.Request) {
httpRequestHandler(walletExt, resp, req, fun)
}
}

// Overall request handler for http requests
func httpRequestHandler(walletExt *rpcapi.Services, resp http.ResponseWriter, req *http.Request, fun func(walletExt *rpcapi.Services, conn UserConn)) {
func httpRequestHandler(walletExt *services.Services, resp http.ResponseWriter, req *http.Request, fun func(walletExt *services.Services, conn UserConn)) {
if walletExt.IsStopping() {
return
}
Expand All @@ -87,10 +87,10 @@ func httpRequestHandler(walletExt *rpcapi.Services, resp http.ResponseWriter, re
}

// readyRequestHandler is used to check whether the server is ready
func readyRequestHandler(_ *rpcapi.Services, _ UserConn) {}
func readyRequestHandler(_ *services.Services, _ UserConn) {}

// This function handles request to /join endpoint. It is responsible to create new user (new key-pair) and store it to the db
func joinRequestHandler(walletExt *rpcapi.Services, conn UserConn) {
func joinRequestHandler(walletExt *services.Services, conn UserConn) {
// audit()
// todo (@ziga) add protection against DDOS attacks
_, err := conn.ReadRequest()
Expand All @@ -116,7 +116,7 @@ func joinRequestHandler(walletExt *rpcapi.Services, conn UserConn) {
// This function handles request to /authenticate endpoint.
// In the request we receive message, signature and address in JSON as request body and userID and address as query parameters
// We then check if message is in correct format and if signature is valid. If all checks pass we save address and signature against userID
func authenticateRequestHandler(walletExt *rpcapi.Services, conn UserConn) {
func authenticateRequestHandler(walletExt *services.Services, conn UserConn) {
// read the request
body, err := conn.ReadRequest()
if err != nil {
Expand Down Expand Up @@ -181,7 +181,7 @@ func authenticateRequestHandler(walletExt *rpcapi.Services, conn UserConn) {
// This function handles request to /query endpoint.
// In the query parameters address and userID are required. We check if provided address is registered for given userID
// and return true/false in json response
func queryRequestHandler(walletExt *rpcapi.Services, conn UserConn) {
func queryRequestHandler(walletExt *services.Services, conn UserConn) {
// read the request
_, err := conn.ReadRequest()
if err != nil {
Expand Down Expand Up @@ -233,7 +233,7 @@ func queryRequestHandler(walletExt *rpcapi.Services, conn UserConn) {

// This function handles request to /revoke endpoint.
// It requires userID as query parameter and deletes given user and all associated viewing keys
func revokeRequestHandler(walletExt *rpcapi.Services, conn UserConn) {
func revokeRequestHandler(walletExt *services.Services, conn UserConn) {
// read the request
_, err := conn.ReadRequest()
if err != nil {
Expand Down Expand Up @@ -263,7 +263,7 @@ func revokeRequestHandler(walletExt *rpcapi.Services, conn UserConn) {
}

// Handles request to /health endpoint.
func healthRequestHandler(walletExt *rpcapi.Services, conn UserConn) {
func healthRequestHandler(walletExt *services.Services, conn UserConn) {
// read the request
_, err := conn.ReadRequest()
if err != nil {
Expand All @@ -279,7 +279,7 @@ func healthRequestHandler(walletExt *rpcapi.Services, conn UserConn) {
}

// Handles request to /network-health endpoint.
func networkHealthRequestHandler(walletExt *rpcapi.Services, userConn UserConn) {
func networkHealthRequestHandler(walletExt *services.Services, userConn UserConn) {
// read the request
_, err := userConn.ReadRequest()
if err != nil {
Expand Down Expand Up @@ -321,7 +321,7 @@ func networkHealthRequestHandler(walletExt *rpcapi.Services, userConn UserConn)
}
}

func networkConfigRequestHandler(walletExt *rpcapi.Services, userConn UserConn) {
func networkConfigRequestHandler(walletExt *services.Services, userConn UserConn) {
// read the request
_, err := userConn.ReadRequest()
if err != nil {
Expand Down Expand Up @@ -373,7 +373,7 @@ func networkConfigRequestHandler(walletExt *rpcapi.Services, userConn UserConn)
}

// Handles request to /version endpoint.
func versionRequestHandler(walletExt *rpcapi.Services, userConn UserConn) {
func versionRequestHandler(walletExt *services.Services, userConn UserConn) {
// read the request
_, err := userConn.ReadRequest()
if err != nil {
Expand All @@ -388,7 +388,7 @@ func versionRequestHandler(walletExt *rpcapi.Services, userConn UserConn) {
}

// getMessageRequestHandler handles request to /getmessage endpoint.
func getMessageRequestHandler(walletExt *rpcapi.Services, conn UserConn) {
func getMessageRequestHandler(walletExt *services.Services, conn UserConn) {
// read the request
body, err := conn.ReadRequest()
if err != nil {
Expand Down
Loading

0 comments on commit d1b658a

Please sign in to comment.