arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/api/handlers.go
api/handlers.goRaw
  1package api
  2
  3import (
  4	"database/sql"
  5	"errors"
  6	"net/http"
  7
  8	"github.com/gin-gonic/gin"
  9	"github.com/hermeznetwork/hermez-node/db/historydb"
 10)
 11
 12const (
 13	// maxLimit is the max permited items to be returned in paginated responses
 14	maxLimit uint = 2049
 15
 16	// dfltOrder indicates how paginated endpoints are ordered if not specified
 17	dfltOrder = historydb.OrderAsc
 18
 19	// dfltLimit indicates the limit of returned items in paginated responses if the query param limit is not provided
 20	dfltLimit uint = 20
 21
 22	// 2^32 -1
 23	maxUint32 = 4294967295
 24)
 25
 26var (
 27	// ErrNillBidderAddr is used when a nil bidderAddr is received in the getCoordinator method
 28	ErrNillBidderAddr = errors.New("biderAddr can not be nil")
 29)
 30
 31func postAccountCreationAuth(c *gin.Context) {
 32	// Parse body
 33	var apiAuth accountCreationAuthAPI
 34	if err := c.ShouldBindJSON(&apiAuth); err != nil {
 35		retBadReq(err, c)
 36		return
 37	}
 38	// API to common + verify signature
 39	dbAuth, err := accountCreationAuthAPIToCommon(&apiAuth)
 40	if err != nil {
 41		retBadReq(err, c)
 42		return
 43	}
 44	// Insert to DB
 45	if err := l2.AddAccountCreationAuth(dbAuth); err != nil {
 46		retSQLErr(err, c)
 47		return
 48	}
 49	// Return OK
 50	c.Status(http.StatusOK)
 51}
 52
 53func getAccountCreationAuth(c *gin.Context) {
 54	// Get hezEthereumAddress
 55	addr, err := parseParamHezEthAddr(c)
 56	if err != nil {
 57		retBadReq(err, c)
 58		return
 59	}
 60	// Fetch auth from l2DB
 61	dbAuth, err := l2.GetAccountCreationAuth(*addr)
 62	if err != nil {
 63		retSQLErr(err, c)
 64		return
 65	}
 66	apiAuth := accountCreationAuthToAPI(dbAuth)
 67	// Build succesfull response
 68	c.JSON(http.StatusOK, apiAuth)
 69}
 70
 71func postPoolTx(c *gin.Context) {
 72	// Parse body
 73	var receivedTx receivedPoolTx
 74	if err := c.ShouldBindJSON(&receivedTx); err != nil {
 75		retBadReq(err, c)
 76		return
 77	}
 78	// Transform from received to insert format and validate
 79	writeTx, err := receivedTx.toDBWritePoolL2Tx()
 80	if err != nil {
 81		retBadReq(err, c)
 82		return
 83	}
 84	// Insert to DB
 85	if err := l2.AddTx(writeTx); err != nil {
 86		retSQLErr(err, c)
 87		return
 88	}
 89	// Return TxID
 90	c.JSON(http.StatusOK, writeTx.TxID.String())
 91}
 92
 93func getPoolTx(c *gin.Context) {
 94	// Get TxID
 95	txID, err := parseParamTxID(c)
 96	if err != nil {
 97		retBadReq(err, c)
 98		return
 99	}
100	// Fetch tx from l2DB
101	dbTx, err := l2.GetTx(txID)
102	if err != nil {
103		retSQLErr(err, c)
104		return
105	}
106	apiTx := poolL2TxReadToSend(dbTx)
107	// Build succesfull response
108	c.JSON(http.StatusOK, apiTx)
109}
110
111func getAccounts(c *gin.Context) {
112
113}
114
115func getAccount(c *gin.Context) {
116
117}
118
119func getExits(c *gin.Context) {
120	// Get query parameters
121	// Account filters
122	tokenID, addr, bjj, idx, err := parseAccountFilters(c)
123	if err != nil {
124		retBadReq(err, c)
125		return
126	}
127	// BatchNum
128	batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
129	if err != nil {
130		retBadReq(err, c)
131		return
132	}
133	// Pagination
134	fromItem, order, limit, err := parsePagination(c)
135	if err != nil {
136		retBadReq(err, c)
137		return
138	}
139
140	// Fetch exits from historyDB
141	exits, pagination, err := h.GetExits(
142		addr, bjj, tokenID, idx, batchNum, fromItem, limit, order,
143	)
144	if err != nil {
145		retSQLErr(err, c)
146		return
147	}
148
149	// Build succesfull response
150	apiExits := historyExitsToAPI(exits)
151	c.JSON(http.StatusOK, &exitsAPI{
152		Exits:      apiExits,
153		Pagination: pagination,
154	})
155}
156
157func getExit(c *gin.Context) {
158	// Get batchNum and accountIndex
159	batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
160	if err != nil {
161		retBadReq(err, c)
162		return
163	}
164	idx, err := parseParamIdx(c)
165	if err != nil {
166		retBadReq(err, c)
167		return
168	}
169	// Fetch tx from historyDB
170	exit, err := h.GetExit(batchNum, idx)
171	if err != nil {
172		retSQLErr(err, c)
173		return
174	}
175	apiExits := historyExitsToAPI([]historydb.HistoryExit{*exit})
176	// Build succesfull response
177	c.JSON(http.StatusOK, apiExits[0])
178}
179
180func getHistoryTx(c *gin.Context) {
181	// Get TxID
182	txID, err := parseParamTxID(c)
183	if err != nil {
184		retBadReq(err, c)
185		return
186	}
187	// Fetch tx from historyDB
188	tx, err := h.GetHistoryTx(txID)
189	if err != nil {
190		retSQLErr(err, c)
191		return
192	}
193	apiTxs := historyTxsToAPI([]historydb.HistoryTx{*tx})
194	// Build succesfull response
195	c.JSON(http.StatusOK, apiTxs[0])
196}
197
198func getSlots(c *gin.Context) {
199
200}
201
202func getBids(c *gin.Context) {
203
204}
205
206func getNextForgers(c *gin.Context) {
207
208}
209
210func getState(c *gin.Context) {
211
212}
213
214func getConfig(c *gin.Context) {
215	c.JSON(http.StatusOK, cg)
216}
217
218func getRecommendedFee(c *gin.Context) {
219
220}
221
222func getCoordinators(c *gin.Context) {
223	// Pagination
224	fromItem, order, limit, err := parsePagination(c)
225	if err != nil {
226		retBadReq(err, c)
227		return
228	}
229
230	// Fetch coordinators from historyDB
231	coordinators, pagination, err := h.GetCoordinators(fromItem, limit, order)
232	if err != nil {
233		retSQLErr(err, c)
234		return
235	}
236
237	// Build succesfull response
238	apiCoordinators := coordinatorsToAPI(coordinators)
239	c.JSON(http.StatusOK, &coordinatorsAPI{
240		Coordinators: apiCoordinators,
241		Pagination:   pagination,
242	})
243}
244
245func getCoordinator(c *gin.Context) {
246	// Get bidderAddr
247	const name = "bidderAddr"
248	bidderAddr, err := parseParamEthAddr(name, c)
249
250	if err != nil {
251		retBadReq(err, c)
252		return
253	} else if bidderAddr == nil {
254		retBadReq(ErrNillBidderAddr, c)
255		return
256	}
257
258	coordinator, err := h.GetCoordinator(*bidderAddr)
259	if err != nil {
260		retSQLErr(err, c)
261		return
262	}
263
264	apiCoordinator := coordinatorsToAPI([]historydb.HistoryCoordinator{*coordinator})
265	c.JSON(http.StatusOK, apiCoordinator[0])
266}
267
268func retSQLErr(err error, c *gin.Context) {
269	if err == sql.ErrNoRows {
270		c.JSON(http.StatusNotFound, errorMsg{
271			Message: err.Error(),
272		})
273	} else {
274		c.JSON(http.StatusInternalServerError, errorMsg{
275			Message: err.Error(),
276		})
277	}
278}
279
280func retBadReq(err error, c *gin.Context) {
281	c.JSON(http.StatusBadRequest, errorMsg{
282		Message: err.Error(),
283	})
284}