-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
214 lines (176 loc) · 5.63 KB
/
main.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"context"
"fmt"
rosettaFilecoinLib "github.com/zondax/rosetta-filecoin-lib"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
rosettaAsserter "github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/client"
logging "github.com/ipfs/go-log"
srv "github.com/zondax/rosetta-filecoin-proxy/rosetta/services"
"github.com/zondax/rosetta-filecoin-proxy/rosetta/tools"
)
var (
BlockchainName = srv.BlockChainName
ServerPort, _ = strconv.Atoi(srv.RosettaServerPort)
)
func logVersionsInfo() {
srv.Logger.Info("****************************************************")
srv.Logger.Infof("Rosetta SDK version: %s", srv.RosettaSDKVersion)
srv.Logger.Infof("Lotus version: %s", srv.LotusVersion)
srv.Logger.Infof("Git revision: %s", srv.GitRevision)
srv.Logger.Info("****************************************************")
}
func startLogger(level string) {
lvl, err := logging.LevelFromString(level)
if err != nil {
panic(err)
}
logging.SetAllLoggers(lvl)
}
func getFullNodeAPI(addr string, token string) (api.FullNode, jsonrpc.ClientCloser, error) {
headers := http.Header{}
if len(token) > 0 {
headers.Add("Authorization", "Bearer "+token)
}
return client.NewFullNodeRPCV1(context.Background(), addr, headers)
}
// newBlockchainRouter creates a Mux http.Handler from a collection
// of server controllers.
func newBlockchainRouter(
network *types.NetworkIdentifier,
asserter *rosettaAsserter.Asserter,
api api.FullNode,
rosettaLib *rosettaFilecoinLib.RosettaConstructionFilecoin,
) http.Handler {
accountAPIService := srv.NewAccountAPIService(network, &api, rosettaLib)
accountAPIController := server.NewAccountAPIController(
accountAPIService,
asserter,
)
networkAPIService := srv.NewNetworkAPIService(network, &api, srv.GetSupportedOpList())
networkAPIController := server.NewNetworkAPIController(
networkAPIService,
asserter,
)
blockAPIService := srv.NewBlockAPIService(network, &api, rosettaLib)
blockAPIController := server.NewBlockAPIController(
blockAPIService,
asserter,
)
mempoolAPIService := srv.NewMemPoolAPIService(network, &api, rosettaLib)
mempoolAPIController := server.NewMempoolAPIController(
mempoolAPIService,
asserter,
)
constructionAPIService := srv.NewConstructionAPIService(network, &api, rosettaLib)
constructionAPIController := server.NewConstructionAPIController(
constructionAPIService,
asserter,
)
return server.NewRouter(accountAPIController, networkAPIController,
blockAPIController, mempoolAPIController, constructionAPIController)
}
func startRosettaRPC(ctx context.Context, api api.FullNode) error {
netName, _ := api.StateNetworkName(ctx)
network := &types.NetworkIdentifier{
Blockchain: BlockchainName,
Network: string(netName),
}
// The asserter automatically rejects incorrectly formatted
// requests.
asserter, err := rosettaAsserter.NewServer(
srv.GetSupportedOpList(),
true,
[]*types.NetworkIdentifier{network},
nil,
false,
"",
)
if err != nil {
srv.Logger.Fatal(err)
}
// Create instance of RosettaFilecoinLib for current network
r := rosettaFilecoinLib.NewRosettaConstructionFilecoin(api)
router := newBlockchainRouter(network, asserter, api, r)
loggedRouter := server.LoggerMiddleware(router)
corsRouter := server.CorsMiddleware(loggedRouter)
server := &http.Server{Addr: fmt.Sprintf(":%d", ServerPort), Handler: corsRouter}
sigCh := make(chan os.Signal, 2)
go func() {
<-sigCh
srv.Logger.Warn("Shutting down rosetta...")
err = server.Shutdown(context.TODO())
if err != nil {
srv.Logger.Error(err)
} else {
srv.Logger.Warn("Graceful shutdown of rosetta successful")
}
}()
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
srv.Logger.Infof("Rosetta listening on port %d\n", ServerPort)
return server.ListenAndServe()
}
func connectAPI(addr string, token string) (api.FullNode, jsonrpc.ClientCloser, error) {
lotusAPI, clientCloser, err := getFullNodeAPI(addr, token)
if err != nil {
srv.Logger.Errorf("Error %s\n", err)
return nil, nil, err
}
networkName, err := lotusAPI.StateNetworkName(context.Background())
if err != nil {
srv.Logger.Warn("Could not get Lotus network name!")
}
srv.NetworkName = string(networkName)
version, err := lotusAPI.Version(context.Background())
if err != nil {
srv.Logger.Warn("Could not get Lotus api version!")
}
srv.Logger.Infof("Connected to Lotus node version: %s | Network: %s ", version.String(), srv.NetworkName)
return lotusAPI, clientCloser, nil
}
func setupActorsDatabase(api *api.FullNode) {
var db tools.Database = &tools.Cache{}
db.NewImpl(api)
tools.ActorsDB = db
}
func main() {
startLogger("info")
logVersionsInfo()
addr := os.Getenv("LOTUS_RPC_URL")
token := os.Getenv("LOTUS_RPC_TOKEN")
srv.Logger.Info("Starting Rosetta Proxy")
srv.Logger.Infof("LOTUS_RPC_URL: %s", addr)
var lotusAPI api.FullNode
var clientCloser jsonrpc.ClientCloser
var err error
retryAttempts, _ := strconv.Atoi(srv.RetryConnectAttempts)
for i := 1; i <= retryAttempts; i++ {
lotusAPI, clientCloser, err = connectAPI(addr, token)
if err == nil {
break
}
srv.Logger.Errorf("Could not connect to api. Retrying attempt %d", i)
time.Sleep(5 * time.Second)
}
if err != nil {
srv.Logger.Fatalf("Connect to Lotus api gave up after %d attempts", retryAttempts)
return
}
defer clientCloser()
setupActorsDatabase(&lotusAPI)
ctx := context.Background()
err = startRosettaRPC(ctx, lotusAPI)
if err != nil {
srv.Logger.Infof("Exit Rosetta rpc: %s", err.Error())
}
}