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

feat: e2e privacy integration #42

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
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
57 changes: 42 additions & 15 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ var (
Value: contracts.TestnetContracts.PreconfCommitmentStore,
})

optionBlockTrackerContractAddr = altsrc.NewStringFlag(&cli.StringFlag{
Name: "blocktracker-contract-addr",
Usage: "address of the block tracker contract",
EnvVars: []string{"MEV_ORACLE_BLOCKTRACKER_CONTRACT_ADDR"},
//Value: contracts.TestnetContracts.BlockTracker,
})

optionBidderRegistryContractAddr = altsrc.NewStringFlag(&cli.StringFlag{
Name: "bidder-registry-contract-addr",
Usage: "address of the bidder registry contract",
EnvVars: []string{"MEV_ORACLE_BIDDERREGISTRY_CONTRACT_ADDR"},
Value: contracts.TestnetContracts.BidderRegistry,
})

optionProviderRegistryContractAddr = altsrc.NewStringFlag(&cli.StringFlag{
Name: "provider-registry-contract-addr",
Usage: "address of the provider registry contract",
EnvVars: []string{"MEV_ORACLE_PROVIDERREGISTRY_CONTRACT_ADDR"},
Value: contracts.TestnetContracts.ProviderRegistry,
})

optionPgHost = altsrc.NewStringFlag(&cli.StringFlag{
Name: "pg-host",
Usage: "PostgreSQL host",
Expand Down Expand Up @@ -161,7 +182,7 @@ var (
Name: "laggerd-mode",
Usage: "No of blocks to lag behind for L1 chain",
EnvVars: []string{"MEV_ORACLE_LAGGERD_MODE"},
Value: 0,
Value: 64,
})

optionOverrideWinners = altsrc.NewStringSliceFlag(&cli.StringSliceFlag{
Expand Down Expand Up @@ -196,6 +217,9 @@ func main() {
optionSettlementRPCUrl,
optionOracleContractAddr,
optionPreconfContractAddr,
optionBlockTrackerContractAddr,
optionBidderRegistryContractAddr,
optionProviderRegistryContractAddr,
optionPgHost,
optionPgPort,
optionPgUser,
Expand Down Expand Up @@ -264,20 +288,23 @@ func launchOracleWithConfig(c *cli.Context) error {
logger.Info("key signer account", "address", keySigner.GetAddress().Hex(), "url", keySigner.String())

nd, err := node.NewNode(&node.Options{
Logger: logger,
KeySigner: keySigner,
HTTPPort: c.Int(optionHTTPPort.Name),
L1RPCUrl: c.String(optionL1RPCUrl.Name),
SettlementRPCUrl: c.String(optionSettlementRPCUrl.Name),
OracleContractAddr: common.HexToAddress(c.String(optionOracleContractAddr.Name)),
PreconfContractAddr: common.HexToAddress(c.String(optionPreconfContractAddr.Name)),
PgHost: c.String(optionPgHost.Name),
PgPort: c.Int(optionPgPort.Name),
PgUser: c.String(optionPgUser.Name),
PgPassword: c.String(optionPgPassword.Name),
PgDbname: c.String(optionPgDbname.Name),
LaggerdMode: c.Int(optionLaggerdMode.Name),
OverrideWinners: c.StringSlice(optionOverrideWinners.Name),
Logger: logger,
KeySigner: keySigner,
HTTPPort: c.Int(optionHTTPPort.Name),
L1RPCUrl: c.String(optionL1RPCUrl.Name),
SettlementRPCUrl: c.String(optionSettlementRPCUrl.Name),
OracleContractAddr: common.HexToAddress(c.String(optionOracleContractAddr.Name)),
PreconfContractAddr: common.HexToAddress(c.String(optionPreconfContractAddr.Name)),
BlockTrackerContractAddr: common.HexToAddress(c.String(optionBlockTrackerContractAddr.Name)),
ProviderRegistryContractAddr: common.HexToAddress(c.String(optionProviderRegistryContractAddr.Name)),
BidderRegistryContractAddr: common.HexToAddress(c.String(optionBidderRegistryContractAddr.Name)),
PgHost: c.String(optionPgHost.Name),
PgPort: c.Int(optionPgPort.Name),
PgUser: c.String(optionPgUser.Name),
PgPassword: c.String(optionPgPassword.Name),
PgDbname: c.String(optionPgDbname.Name),
LaggerdMode: c.Int(optionLaggerdMode.Name),
OverrideWinners: c.StringSlice(optionOverrideWinners.Name),
})
if err != nil {
return fmt.Errorf("failed starting node: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/ethereum/go-ethereum v1.13.5
github.com/google/go-cmp v0.6.0
github.com/lib/pq v1.10.9
github.com/primevprotocol/contracts-abi v0.2.3
github.com/primevprotocol/contracts-abi v0.2.4-0.20240423171419-a959d3727058
github.com/prometheus/client_golang v1.14.0
github.com/testcontainers/testcontainers-go v0.27.0
github.com/urfave/cli/v2 v2.27.1
Expand Down Expand Up @@ -51,6 +51,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/klauspost/compress v1.17.5 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/hashicorp/go-bexpr v0.1.10 h1:9kuI5PFotCboP3dkDYFr/wi0gg0QVbSNz5oFRpxn4uE=
github.com/hashicorp/go-bexpr v0.1.10/go.mod h1:oxlubA2vC/gFVfX1A6JGp7ls7uCDlfJn732ehYYg+g0=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/holiman/billy v0.0.0-20230718173358-1c7e68d277a7 h1:3JQNjnMRil1yD0IfZKHF9GxxWKDJGj8I0IqOUol//sw=
github.com/holiman/billy v0.0.0-20230718173358-1c7e68d277a7/go.mod h1:5GuXa7vkL8u9FkFuWdVvfR5ix8hRB7DbOAaYULamFpc=
Expand Down Expand Up @@ -314,6 +316,14 @@ github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/primevprotocol/contracts-abi v0.2.3 h1:4jZGl0hrEdP1yensIrs4Sh2DNU6EG+BBNtKvrH5liR0=
github.com/primevprotocol/contracts-abi v0.2.3/go.mod h1:dE2KkvEqC+itvPa3SCrqQfvH5Hfnfn6omNRwWDTdIp8=
github.com/primevprotocol/contracts-abi v0.2.4-0.20240327132452-d0743659d58e h1:+cPd/2WnbjpcIlyu7qRWaNuYIbsr2QSFrqB6UL4g6hU=
github.com/primevprotocol/contracts-abi v0.2.4-0.20240327132452-d0743659d58e/go.mod h1:dE2KkvEqC+itvPa3SCrqQfvH5Hfnfn6omNRwWDTdIp8=
github.com/primevprotocol/contracts-abi v0.2.4-0.20240401131709-dcd3b451314a h1:bSmtNx7BXLtnKiOP4Ku8xpKIumScyBl96bb5hcBvvV8=
github.com/primevprotocol/contracts-abi v0.2.4-0.20240401131709-dcd3b451314a/go.mod h1:dE2KkvEqC+itvPa3SCrqQfvH5Hfnfn6omNRwWDTdIp8=
github.com/primevprotocol/contracts-abi v0.2.4-0.20240419134844-6dd1a8c7bf60 h1:FEYkczFrI/CwTZRm0wARuWshpn185BEw4uFAKxlqQBg=
github.com/primevprotocol/contracts-abi v0.2.4-0.20240419134844-6dd1a8c7bf60/go.mod h1:dE2KkvEqC+itvPa3SCrqQfvH5Hfnfn6omNRwWDTdIp8=
github.com/primevprotocol/contracts-abi v0.2.4-0.20240423171419-a959d3727058 h1:IG9XeHGs8/S9tW/4dfLosOa6NL0LGODp+FvWVIWNIBE=
github.com/primevprotocol/contracts-abi v0.2.4-0.20240423171419-a959d3727058/go.mod h1:dE2KkvEqC+itvPa3SCrqQfvH5Hfnfn6omNRwWDTdIp8=
github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw=
github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
121 changes: 44 additions & 77 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package apiserver
import (
"bufio"
"context"
"encoding/json"
"expvar"
"log/slog"
"net"
"net/http"
"net/http/pprof"
"strconv"
"sync"
"time"

"github.com/primevprotocol/mev-oracle/pkg/store"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/primevprotocol/mev-oracle/pkg/events"
"github.com/primevprotocol/mev-oracle/pkg/updater"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -22,27 +23,55 @@ const (
defaultNamespace = "mev_commit_oracle"
)

type Store interface {
Settlement(context.Context, []byte) (updater.Settlement, error)
}

// Service wraps http.Server with additional functionality for metrics and
// other common middlewares.
type Service struct {
logger *slog.Logger
metricsRegistry *prometheus.Registry
router *http.ServeMux
srv *http.Server
storage *store.Store
logger *slog.Logger
metricsRegistry *prometheus.Registry
router *http.ServeMux
srv *http.Server
evtMgr events.EventManager
store Store
statMu sync.RWMutex
blockStats *lru.Cache[uint64, *BlockStats]
providerStakes *lru.Cache[string, *ProviderBalances]
bidderAllowances *lru.Cache[uint64, []*BidderAllowance]
lastBlock uint64
shutdown chan struct{}
}

// New creates a new Service.
func New(logger *slog.Logger, st *store.Store) *Service {
func New(
logger *slog.Logger,
evm events.EventManager,
store Store,
) *Service {
blockStats, _ := lru.New[uint64, *BlockStats](10000)
providerStakes, _ := lru.New[string, *ProviderBalances](1000)
bidderAllowances, _ := lru.New[uint64, []*BidderAllowance](1000)

srv := &Service{
logger: logger,
router: http.NewServeMux(),
metricsRegistry: newMetrics(),
storage: st,
logger: logger,
router: http.NewServeMux(),
metricsRegistry: newMetrics(),
evtMgr: evm,
store: store,
shutdown: make(chan struct{}),
blockStats: blockStats,
providerStakes: providerStakes,
bidderAllowances: bidderAllowances,
}

err := srv.configureDashboard()
if err != nil {
logger.Error("failed to configure dashboard", "error", err)
}

srv.registerDebugEndpoints()
srv.registerStatsEndpoints()
return srv
}

Expand All @@ -68,69 +97,6 @@ func (s *Service) registerDebugEndpoints() {
s.router.Handle("/debug/vars", expvar.Handler())
}

func (s *Service) registerStatsEndpoints() {
s.router.HandleFunc("/processed_blocks", func(w http.ResponseWriter, r *http.Request) {
pg := r.URL.Query().Get("page")
lim := r.URL.Query().Get("limit")

page, limit := 0, 10
if pg != "" {
if pgInt, err := strconv.Atoi(pg); err == nil {
page = pgInt
}
}
if lim != "" {
if limInt, err := strconv.Atoi(lim); err == nil {
limit = limInt
}
}

blocks, err := s.storage.ProcessedBlocks(limit, page)
if err != nil {
s.logger.Error("failed to get processed blocks", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

resp, err := json.Marshal(blocks)
if err != nil {
s.logger.Error("failed to marshal processed blocks", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write(resp)
if err != nil {
s.logger.Error("failed to write response", "error", err)
}
})

s.router.HandleFunc("/stats", func(w http.ResponseWriter, r *http.Request) {
stats, err := s.storage.CommitmentStats()
if err != nil {
s.logger.Error("failed to get stats", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

resp, err := json.Marshal(stats)
if err != nil {
s.logger.Error("failed to marshal stats", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write(resp)
if err != nil {
s.logger.Error("failed to write response", "error", err)
}
})
}

func newMetrics() (r *prometheus.Registry) {
r = prometheus.NewRegistry()

Expand Down Expand Up @@ -184,6 +150,7 @@ func (s *Service) Stop() error {
if s.srv == nil {
return nil
}
defer close(s.shutdown)
return s.srv.Shutdown(context.Background())
}

Expand Down
Loading
Loading