Skip to content

Commit

Permalink
receipts api (#193)
Browse files Browse the repository at this point in the history
* receipts api

* fix

* receipt count

* refine codes

* add blockNumber
  • Loading branch information
Lawliet-Chan committed Jul 31, 2024
1 parent 4fd9cab commit bdcd6cb
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 9 deletions.
101 changes: 101 additions & 0 deletions core/kernel/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package kernel

import (
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/yu-org/yu/common"
"github.com/yu-org/yu/core/types"
"net/http"
"strconv"
)

var (
Success = 0

BlockFailure = 10001
TxnFailure = 10002
ReceiptFailure = 10003
)

type APIResponse struct {
Code int `json:"code"`
ErrMsg string `json:"err_msg"`
Data any `json:"data"`
}

func RenderSuccess(ctx *gin.Context, data any) {
RenderJson(ctx, Success, nil, data)
}

func RenderError(ctx *gin.Context, code int, err error) {
RenderJson(ctx, code, err, nil)
}

func RenderJson(ctx *gin.Context, code int, err error, data any) {
var errMsg string
if err != nil {
errMsg = err.Error()
}
resp := APIResponse{
Code: code,
ErrMsg: errMsg,
Data: data,
}
ctx.JSON(http.StatusOK, resp)
}

func (k *Kernel) GetReceipts(ctx *gin.Context) {
receipts, err := k.getReceipts(ctx)
if err != nil {
RenderError(ctx, ReceiptFailure, err)
return
}
RenderSuccess(ctx, receipts)
}

func (k *Kernel) GetReceiptsCount(ctx *gin.Context) {
receipts, err := k.getReceipts(ctx)
if err != nil {
RenderError(ctx, ReceiptFailure, err)
return
}
RenderSuccess(ctx, len(receipts))
}

func (k *Kernel) getReceipts(ctx *gin.Context) ([]*types.Receipt, error) {
var (
block *types.CompactBlock
err error
)

blockHashStr := ctx.Query("block_hash")
blockNumberStr := ctx.Query("block_number")
if blockNumberStr != "" {
blockNumber, err := strconv.Atoi(blockNumberStr)
if err != nil {
return nil, err
}
block, err = k.Chain.GetCompactBlockByHeight(common.BlockNum(blockNumber))
if err != nil {
return nil, err
}
} else if blockHashStr != "" {
blockHash := common.HexToHash(blockHashStr)
block, err = k.Chain.GetCompactBlock(blockHash)
if err != nil {
return nil, err
}
} else {
return nil, errors.New("none request params")
}

var receipts []*types.Receipt
for _, txHash := range block.TxnsHashes {
receipt, err := k.TxDB.GetReceipt(txHash)
if err != nil {
return nil, err
}
receipts = append(receipts, receipt)
}
return receipts, nil
}
15 changes: 10 additions & 5 deletions core/kernel/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ import (
func (k *Kernel) HandleHttp() {
r := gin.Default()

// POST request
r.POST(WrApiPath, func(c *gin.Context) {
api := r.Group(RootApiPath)
// POST writing call
api.POST(WrCallType, func(c *gin.Context) {
k.handleHttpWr(c)
})
// POST request
r.POST(RdApiPath, func(c *gin.Context) {
// POST reading call
api.POST(RdCallType, func(c *gin.Context) {
k.handleHttpRd(c)
})

api.GET("receipts", k.GetReceipts)
api.GET("receipts_count", k.GetReceiptsCount)

if k.cfg.IsAdmin {
r.GET(StopChainPath, func(c *gin.Context) {
admin := api.Group(AdminType)
admin.GET("stop", func(c *gin.Context) {
k.stopChan <- struct{}{}
})
}
Expand Down
4 changes: 0 additions & 4 deletions core/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ var (
SubResultsPath = "/subscribe/results"
)

var (
StopChainPath = filepath.Join(AdminApiPath, "stop")
)

type SignedWrCall struct {
Pubkey []byte `json:"pubkey"`
Signature []byte `json:"signature"`
Expand Down

0 comments on commit bdcd6cb

Please sign in to comment.