Skip to content

Commit

Permalink
Improve OPG requests (#933)
Browse files Browse the repository at this point in the history
* Improve OPG requests

* Fix: docs
  • Loading branch information
aopoltorzhicky authored Jul 4, 2022
1 parent b9a48ff commit a7ba930
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 8 deletions.
72 changes: 72 additions & 0 deletions cmd/api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2740,6 +2740,66 @@ const docTemplate = `{
}
}
},
"/v1/implicit/{network}/{counter}": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"operations"
],
"summary": "Get implicit operation",
"operationId": "get-implicit-operation",
"parameters": [
{
"type": "string",
"description": "Network",
"name": "network",
"in": "path",
"required": true
},
{
"type": "integer",
"description": "Counter",
"name": "counter",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.Operation"
}
}
},
"204": {
"description": "No Content",
"schema": {
"$ref": "#/definitions/gin.H"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/handlers.Error"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/handlers.Error"
}
}
}
}
},
"/v1/json_schema": {
"post": {
"description": "Get JSON schema from micheline",
Expand Down Expand Up @@ -2922,6 +2982,18 @@ const docTemplate = `{
"description": "Search operation in mempool or not",
"name": "with_mempool",
"in": "query"
},
{
"type": "boolean",
"description": "Include storage diff to operations or not",
"name": "with_storage_diff",
"in": "query"
},
{
"type": "string",
"description": "Network",
"name": "network",
"in": "query"
}
],
"responses": {
Expand Down
72 changes: 72 additions & 0 deletions cmd/api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2730,6 +2730,66 @@
}
}
},
"/v1/implicit/{network}/{counter}": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"operations"
],
"summary": "Get implicit operation",
"operationId": "get-implicit-operation",
"parameters": [
{
"type": "string",
"description": "Network",
"name": "network",
"in": "path",
"required": true
},
{
"type": "integer",
"description": "Counter",
"name": "counter",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.Operation"
}
}
},
"204": {
"description": "No Content",
"schema": {
"$ref": "#/definitions/gin.H"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/handlers.Error"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/handlers.Error"
}
}
}
}
},
"/v1/json_schema": {
"post": {
"description": "Get JSON schema from micheline",
Expand Down Expand Up @@ -2912,6 +2972,18 @@
"description": "Search operation in mempool or not",
"name": "with_mempool",
"in": "query"
},
{
"type": "boolean",
"description": "Include storage diff to operations or not",
"name": "with_storage_diff",
"in": "query"
},
{
"type": "string",
"description": "Network",
"name": "network",
"in": "query"
}
],
"responses": {
Expand Down
48 changes: 48 additions & 0 deletions cmd/api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3236,6 +3236,46 @@ paths:
summary: Show indexer head for the network
tags:
- head
/v1/implicit/{network}/{counter}:
get:
consumes:
- application/json
operationId: get-implicit-operation
parameters:
- description: Network
in: path
name: network
required: true
type: string
- description: Counter
in: path
name: counter
required: true
type: integer
produces:
- application/json
responses:
"200":
description: OK
schema:
items:
$ref: '#/definitions/handlers.Operation'
type: array
"204":
description: No Content
schema:
$ref: '#/definitions/gin.H'
"400":
description: Bad Request
schema:
$ref: '#/definitions/handlers.Error'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/handlers.Error'
summary: Get implicit operation
tags:
- operations
/v1/json_schema:
post:
consumes:
Expand Down Expand Up @@ -3356,6 +3396,14 @@ paths:
in: query
name: with_mempool
type: boolean
- description: Include storage diff to operations or not
in: query
name: with_storage_diff
type: boolean
- description: Network
in: query
name: network
type: string
produces:
- application/json
responses:
Expand Down
68 changes: 61 additions & 7 deletions cmd/api/handlers/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func GetContractOperations() gin.HandlerFunc {
// @ID get-opg
// @Param hash path string true "Operation group hash" minlength(51) maxlength(51)
// @Param with_mempool query bool false "Search operation in mempool or not"
// @Param with_storage_diff query bool false "Include storage diff to operations or not"
// @Param network query string false "Network"
// @Accept json
// @Produce json
// @Success 200 {array} Operation
Expand All @@ -110,19 +112,34 @@ func GetOperation() gin.HandlerFunc {

operations := make([]operation.Operation, 0)
var foundContext *config.Context
for _, ctx := range ctxs {

network := modelTypes.NewNetwork(queryReq.Network)
if ctx, ok := ctxs[network]; ok {
op, err := ctx.Operations.GetByHash(req.Hash)
if err != nil {
if !ctx.Storage.IsRecordNotFound(err) {
handleError(c, ctx.Storage, err, 0)
return
}
continue
}
operations = append(operations, op...)
if len(operations) > 0 {
} else {
foundContext = ctx
break
operations = append(operations, op...)
}
} else {
for _, ctx := range ctxs {
op, err := ctx.Operations.GetByHash(req.Hash)
if err != nil {
if !ctx.Storage.IsRecordNotFound(err) {
handleError(c, ctx.Storage, err, 0)
return
}
continue
}
operations = append(operations, op...)
if len(operations) > 0 {
foundContext = ctx
break
}
}
}

Expand All @@ -149,7 +166,7 @@ func GetOperation() gin.HandlerFunc {
return
}

resp, err := PrepareOperations(foundContext, operations, true)
resp, err := PrepareOperations(foundContext, operations, queryReq.WithStorageDiff)
if handleError(c, foundContext.Storage, err, 0) {
return
}
Expand All @@ -158,6 +175,43 @@ func GetOperation() gin.HandlerFunc {
}
}

// GetImplicitOperation godoc
// @Summary Get implicit operation
// @DescriptionGet implicit operation
// @Tags operations
// @ID get-implicit-operation
// @Param network path string true "Network"
// @Param counter path integer true "Counter"
// @Accept json
// @Produce json
// @Success 200 {array} Operation
// @Success 204 {object} gin.H
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/implicit/{network}/{counter} [get]
func GetImplicitOperation() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.MustGet("context").(*config.Context)

var req ImplicitOperationRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

op, err := ctx.Operations.GetImplicitOperation(req.Counter)
if handleError(c, ctx.Storage, err, 0) {
return
}

resp, err := PrepareOperations(ctx, []operation.Operation{op}, false)
if handleError(c, ctx.Storage, err, 0) {
return
}

c.SecureJSON(http.StatusOK, resp)
}
}

// GetOperationErrorLocation godoc
// @Summary Get code line where operation failed
// @Description Get code line where operation failed
Expand Down
11 changes: 10 additions & 1 deletion cmd/api/handlers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ type OperationGroupContentRequest struct {
Counter int64 `uri:"counter" binding:"required" example:"123456"`
}

// ImplicitOperationRequest -
type ImplicitOperationRequest struct {
getByNetwork

Counter int64 `uri:"counter" binding:"required" example:"123456"`
}

// FormatterRequest -
type FormatterRequest struct {
Inline bool `form:"inline"`
Expand Down Expand Up @@ -164,7 +171,9 @@ type bigMapSearchRequest struct {
}

type opgRequest struct {
WithMempool bool `form:"with_mempool"`
WithMempool bool `form:"with_mempool" binding:"omitempty"`
WithStorageDiff bool `form:"with_storage_diff" binding:"omitempty"`
Network string `form:"network" binding:"omitempty,network" example:"mainnet"`
}

type getEntrypointDataRequest struct {
Expand Down
1 change: 1 addition & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (api *app) makeRouter() {
opg.GET("", handlers.ContextsMiddleware(api.Contexts), handlers.GetOperation())
opg.GET(":counter", handlers.ContextsMiddleware(api.Contexts), handlers.GetByHashAndCounter())
}
v1.GET("implicit/:network/:counter", handlers.NetworkMiddleware(api.Contexts), handlers.GetImplicitOperation())
v1.GET("search", handlers.ContextsMiddleware(api.Contexts), handlers.Search())
v1.POST("json_schema", handlers.MainnetMiddleware(api.Contexts), handlers.JSONSchema())
v1.POST("michelson", handlers.ContextsMiddleware(api.Contexts), handlers.CodeFromMichelson())
Expand Down
15 changes: 15 additions & 0 deletions internal/models/mock/operation/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/models/operation/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Repository interface {
Last(filter map[string]interface{}, lastID int64) (Operation, error)
GetByHash(hash string) ([]Operation, error)
GetByHashAndCounter(hash string, counter int64) ([]Operation, error)
GetImplicitOperation(counter int64) (Operation, error)
OPG(address string, size, lastID int64) ([]OPG, error)

// GetOperations - get operation by `filter`. `Size` - if 0 - return all, else certain `size` operations.
Expand Down
Loading

0 comments on commit a7ba930

Please sign in to comment.