This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi.go
146 lines (131 loc) · 3.6 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package api
import (
"fmt"
"net/http"
"github.com/Worldcoin/hubble-commander/api/admin"
"github.com/Worldcoin/hubble-commander/api/middleware"
"github.com/Worldcoin/hubble-commander/api/rpc"
"github.com/Worldcoin/hubble-commander/bls"
"github.com/Worldcoin/hubble-commander/config"
"github.com/Worldcoin/hubble-commander/eth"
"github.com/Worldcoin/hubble-commander/metrics"
"github.com/Worldcoin/hubble-commander/models"
st "github.com/Worldcoin/hubble-commander/storage"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type API struct {
cfg *config.APIConfig
storage *st.Storage
client *eth.Client
mockSignature models.Signature
commanderMetrics *metrics.CommanderMetrics
disableSignatures bool
isAcceptingTransactions bool
isMigrating func() bool
}
func NewServer(
cfg *config.Config,
storage *st.Storage,
client *eth.Client,
commanderMetrics *metrics.CommanderMetrics,
enableBatchCreation func(enable bool),
isMigrating func() bool,
) (*http.Server, error) {
server, err := getAPIServer(
cfg.API,
storage,
client,
commanderMetrics,
cfg.Rollup.DisableSignatures,
enableBatchCreation,
isMigrating,
)
if err != nil {
return nil, err
}
var handler http.Handler = server
if cfg.Tracing.Enabled {
handler = middleware.OpenTelemetryHandler(handler)
}
if log.IsLevelEnabled(log.DebugLevel) {
handler = middleware.Logger(handler, commanderMetrics)
} else {
handler = middleware.DefaultHandler(handler, commanderMetrics)
}
mux := http.NewServeMux()
mux.Handle("/", handler)
mux.Handle("/health", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := storage.GetPendingUserState(1)
if err != nil {
w.WriteHeader(500)
return
}
w.WriteHeader(200)
}))
addr := fmt.Sprintf(":%s", cfg.API.Port)
return &http.Server{Addr: addr, Handler: mux}, nil
}
func NewTestAPI(
storage *st.Storage,
client *eth.Client,
) *API {
return &API{
cfg: &config.APIConfig{},
storage: storage,
client: client,
commanderMetrics: metrics.NewCommanderMetrics(),
disableSignatures: true,
isAcceptingTransactions: true,
}
}
func getAPIServer(
cfg *config.APIConfig,
storage *st.Storage,
client *eth.Client,
commanderMetrics *metrics.CommanderMetrics,
disableSignatures bool,
enableBatchCreation func(enable bool),
isMigrating func() bool,
) (*rpc.Server, error) {
hubbleAPI := &API{
cfg: cfg,
storage: storage,
client: client,
commanderMetrics: commanderMetrics,
disableSignatures: disableSignatures,
isAcceptingTransactions: true,
isMigrating: isMigrating,
}
if err := hubbleAPI.initSignature(); err != nil {
return nil, errors.WithMessage(err, "failed to create mock signature")
}
adminAPI := admin.NewAPI(cfg, storage, client, enableBatchCreation, hubbleAPI.enableTxsAcceptance)
server := rpc.NewServer()
if err := server.RegisterName("hubble", hubbleAPI); err != nil {
return nil, err
}
if err := server.RegisterName("admin", adminAPI); err != nil {
return nil, err
}
return server, nil
}
func (a *API) initSignature() error {
domain, err := a.client.GetDomain()
if err != nil {
return err
}
wallet, err := bls.NewRandomWallet(*domain)
if err != nil {
return err
}
signature, err := wallet.Sign([]byte{1, 2, 3, 4})
if err != nil {
return err
}
a.mockSignature = *signature.ModelsSignature()
return nil
}
func (a *API) enableTxsAcceptance(enable bool) {
a.isAcceptingTransactions = enable
}