Skip to content

Commit

Permalink
Merge pull request #67 from matheusLazaroCC-UFG/main
Browse files Browse the repository at this point in the history
Correction of the getBlockByHash function - QueryQSCC Handler: hashBytes, err := hex.DecodeString(hash), passed as a parameter to the chaincode.QueryGateway function, assigned to result.
  • Loading branch information
samuelvenzi authored Jul 8, 2024
2 parents c77a8f4 + dc740e5 commit 286cccd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
36 changes: 36 additions & 0 deletions ccapi/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,42 @@ paths:
- application/json
produces:
- application/json
/{channelName}/qscc/getBlockByHash:
get:
summary: Get block by hash
description: Retrieves a block by its hash from the specified channel.
tags:
- Blockchain
security:
- basicAuth: []
parameters:
- name: channelName
in: path
required: true
schema:
type: string
example: mainchannel
description: Name of the channel.
- name: hash
in: query
required: true
schema:
type: string
example: dbd2b14fb3d61b7aeac3add76f99cd9b47850c7c95ca5e489696a6b543fc6b2d
description: The hash of the block to be retrieved.
responses:
"200":
description: Successful response
content:
application/json:
schema:
type: object
"400":
description: Bad request
"404":
description: Block not found
"500":
description: Internal server error
/{channelName}/qscc/getChainInfo:
get:
summary: Get chain info
Expand Down
18 changes: 13 additions & 5 deletions ccapi/handlers/qscc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/hex"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/hyperledger-labs/ccapi/chaincode"
"github.com/hyperledger-labs/ccapi/common"
Expand All @@ -24,8 +24,8 @@ func QueryQSCC(c *gin.Context) {
switch txname {
case "getBlockByNumber":
getBlockByNumber(c, channelName)
// case "getBlockByHash":
// getBlockByHash(c, channelName)
case "getBlockByHash":
getBlockByHash(c, channelName)
case "getTransactionByID":
getTransactionByID(c, channelName)
case "getChainInfo":
Expand Down Expand Up @@ -139,7 +139,15 @@ func getBlockByHash(c *gin.Context, channelName string) {
return
}

result, err := chaincode.QueryGateway(channelName, "qscc", "GetBlockByHash", user, []string{channelName, hash})
hashBytes, err := hex.DecodeString(hash)

if err != nil {
common.Abort(c, http.StatusBadRequest, fmt.Errorf("invalid hash format: %s", hash))
return
}

result, err := chaincode.QueryGateway(channelName, "qscc", "GetBlockByHash", user, []string{channelName, string(hashBytes)})

if err != nil {
err, status := common.ParseError(err)
common.Abort(c, status, err)
Expand Down Expand Up @@ -440,4 +448,4 @@ func decodeProcessedTransaction(t []byte) (map[string]interface{}, error) {
}

return processedTransactionMap, nil
}
}

0 comments on commit 286cccd

Please sign in to comment.