mirror of
https://github.com/kaspanet/kaspad.git
synced 2026-03-19 06:39:08 +00:00
* [NOD-350] Implement testnet faucet * [NOD-350] Add JSON annotations to api server response types * [NOD-350] Fix IP check query, update IP usage with upsert, and make IP a primary key * [NOD-377] Remove redundant float conversion * [NOD-377] Change not current database error message * [NOD-377] change API route from /money_request to /request_money * [NOD-377] Add a constant for 24 hours * [NOD-377] Remove redundant call for getWalletUTXOSet() * [NOD-377] Condition refactoring * [NOD-377] Fix POST request to API server content type * [NOD-350] Rename day -> timeBetweenRequests * [NOD-377] Rename timeBetweenRequests -> minRequestInterval, timeBefore24Hours -> minRequestInterval * [NOD-350] Rename file responsetypes -> response_types * [NOD-350] Rename convertTxModelToTxResponse -> convertTxDBModelToTxResponse * [NOD-350] Explicitly select blue_score in fetchSelectedTipBlueScore * [NOD-350] Refactor and add comments * [NOD-350] Make calcFee use MassPerTxByte * [NOD-350] Convert IP column to varchar(39) to allow ipv6 addresses * [NOD-350] Add comments to isFundedAndIsChangeOutputRequired * [NOD-350] Remove approximateConfirmationsForCoinbaseMaturity * [NOD-350] Fix comments
81 lines
2.8 KiB
Go
81 lines
2.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"github.com/daglabs/btcd/apiserver/apimodels"
|
|
"github.com/daglabs/btcd/apiserver/dbmodels"
|
|
"github.com/daglabs/btcd/httpserverutils"
|
|
"net/http"
|
|
|
|
"github.com/daglabs/btcd/apiserver/database"
|
|
"github.com/daglabs/btcd/util/daghash"
|
|
)
|
|
|
|
const (
|
|
// OrderAscending is parameter that can be used
|
|
// in a get list handler to get a list ordered
|
|
// in an ascending order.
|
|
OrderAscending = "asc"
|
|
|
|
// OrderDescending is parameter that can be used
|
|
// in a get list handler to get a list ordered
|
|
// in an ascending order.
|
|
OrderDescending = "desc"
|
|
)
|
|
|
|
const maxGetBlocksLimit = 100
|
|
|
|
// GetBlockByHashHandler returns a block by a given hash.
|
|
func GetBlockByHashHandler(blockHash string) (interface{}, *httpserverutils.HandlerError) {
|
|
if bytes, err := hex.DecodeString(blockHash); err != nil || len(bytes) != daghash.HashSize {
|
|
return nil, httpserverutils.NewHandlerError(http.StatusUnprocessableEntity,
|
|
fmt.Sprintf("The given block hash is not a hex-encoded %d-byte hash.", daghash.HashSize))
|
|
}
|
|
|
|
db, err := database.DB()
|
|
if err != nil {
|
|
return nil, httpserverutils.NewInternalServerHandlerError(err.Error())
|
|
}
|
|
|
|
block := &dbmodels.Block{}
|
|
dbResult := db.Where(&dbmodels.Block{BlockHash: blockHash}).Preload("AcceptingBlock").First(block)
|
|
dbErrors := dbResult.GetErrors()
|
|
if httpserverutils.IsDBRecordNotFoundError(dbErrors) {
|
|
return nil, httpserverutils.NewHandlerError(http.StatusNotFound, "No block with the given block hash was found.")
|
|
}
|
|
if httpserverutils.HasDBError(dbErrors) {
|
|
return nil, httpserverutils.NewHandlerErrorFromDBErrors("Some errors were encountered when loading transactions from the database:", dbResult.GetErrors())
|
|
}
|
|
return convertBlockModelToBlockResponse(block), nil
|
|
}
|
|
|
|
// GetBlocksHandler searches for all blocks
|
|
func GetBlocksHandler(order string, skip uint64, limit uint64) (interface{}, *httpserverutils.HandlerError) {
|
|
if limit > maxGetBlocksLimit {
|
|
return nil, httpserverutils.NewHandlerError(http.StatusUnprocessableEntity, fmt.Sprintf("The maximum allowed value for the limit is %d", maxGetTransactionsLimit))
|
|
}
|
|
blocks := []*dbmodels.Block{}
|
|
db, err := database.DB()
|
|
if err != nil {
|
|
return nil, httpserverutils.NewHandlerError(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
|
|
}
|
|
query := db.
|
|
Limit(limit).
|
|
Offset(skip).
|
|
Preload("AcceptingBlock")
|
|
if order == OrderAscending {
|
|
query = query.Order("`id` ASC")
|
|
} else if order == OrderDescending {
|
|
query = query.Order("`id` DESC")
|
|
} else {
|
|
return nil, httpserverutils.NewHandlerError(http.StatusUnprocessableEntity, fmt.Sprintf("'%s' is not a valid order", order))
|
|
}
|
|
query.Find(&blocks)
|
|
blockResponses := make([]*apimodels.BlockResponse, len(blocks))
|
|
for i, block := range blocks {
|
|
blockResponses[i] = convertBlockModelToBlockResponse(block)
|
|
}
|
|
return blockResponses, nil
|
|
}
|