Skip to content

Commit

Permalink
api: add /block/.../raw endpoints
Browse files Browse the repository at this point in the history
Also add api/types.BlockRaw.

use strings.Builder to avoid copying a long string
  • Loading branch information
chappjc committed Feb 19, 2019
1 parent 0be15e3 commit faf775e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/apirouter.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func NewAPIRouter(app *appContext, useRealIP bool) apiMux {
rd.Get("/height", app.currentHeight) rd.Get("/height", app.currentHeight)
rd.Get("/hash", app.getBlockHash) rd.Get("/hash", app.getBlockHash)
rd.Get("/header", app.getBlockHeader) rd.Get("/header", app.getBlockHeader)
rd.Get("/raw", app.getBlockRaw)
rd.Get("/size", app.getBlockSize) rd.Get("/size", app.getBlockSize)
rd.Get("/subsidy", app.blockSubsidies) rd.Get("/subsidy", app.blockSubsidies)
rd.With((middleware.Compress(1))).Get("/verbose", app.getBlockVerbose) rd.With((middleware.Compress(1))).Get("/verbose", app.getBlockVerbose)
Expand All @@ -54,6 +55,7 @@ func NewAPIRouter(app *appContext, useRealIP bool) apiMux {
rd.Get("/", app.getBlockSummary) rd.Get("/", app.getBlockSummary)
rd.Get("/height", app.getBlockHeight) rd.Get("/height", app.getBlockHeight)
rd.Get("/header", app.getBlockHeader) rd.Get("/header", app.getBlockHeader)
rd.Get("/raw", app.getBlockRaw)
rd.Get("/size", app.getBlockSize) rd.Get("/size", app.getBlockSize)
rd.Get("/subsidy", app.blockSubsidies) rd.Get("/subsidy", app.blockSubsidies)
rd.With((middleware.Compress(1))).Get("/verbose", app.getBlockVerbose) rd.With((middleware.Compress(1))).Get("/verbose", app.getBlockVerbose)
Expand All @@ -69,6 +71,7 @@ func NewAPIRouter(app *appContext, useRealIP bool) apiMux {
rd.Get("/", app.getBlockSummary) rd.Get("/", app.getBlockSummary)
rd.Get("/header", app.getBlockHeader) rd.Get("/header", app.getBlockHeader)
rd.Get("/hash", app.getBlockHash) rd.Get("/hash", app.getBlockHash)
rd.Get("/raw", app.getBlockRaw)
rd.Get("/size", app.getBlockSize) rd.Get("/size", app.getBlockSize)
rd.Get("/subsidy", app.blockSubsidies) rd.Get("/subsidy", app.blockSubsidies)
rd.With((middleware.Compress(1))).Get("/verbose", app.getBlockVerbose) rd.With((middleware.Compress(1))).Get("/verbose", app.getBlockVerbose)
Expand Down
36 changes: 35 additions & 1 deletion api/apiroutes.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"encoding/csv" "encoding/csv"
"encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
Expand All @@ -23,6 +24,7 @@ import (
"github.com/decred/dcrd/chaincfg" "github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/dcrjson/v2" "github.com/decred/dcrd/dcrjson/v2"
"github.com/decred/dcrd/rpcclient/v2" "github.com/decred/dcrd/rpcclient/v2"
"github.com/decred/dcrd/wire"
apitypes "github.com/decred/dcrdata/v4/api/types" apitypes "github.com/decred/dcrdata/v4/api/types"
"github.com/decred/dcrdata/v4/db/agendadb" "github.com/decred/dcrdata/v4/db/agendadb"
"github.com/decred/dcrdata/v4/db/dbtypes" "github.com/decred/dcrdata/v4/db/dbtypes"
Expand All @@ -42,7 +44,7 @@ type DataSourceLite interface {
GetBestBlockHash() (string, error) GetBestBlockHash() (string, error)
GetBlockHash(idx int64) (string, error) GetBlockHash(idx int64) (string, error)
GetBlockHeight(hash string) (int64, error) GetBlockHeight(hash string) (int64, error)
//Get(idx int) *blockdata.BlockData GetBlockByHash(string) (*wire.MsgBlock, error)
GetHeader(idx int) *dcrjson.GetBlockHeaderVerboseResult GetHeader(idx int) *dcrjson.GetBlockHeaderVerboseResult
GetBlockVerbose(idx int, verboseTx bool) *dcrjson.GetBlockVerboseResult GetBlockVerbose(idx int, verboseTx bool) *dcrjson.GetBlockVerboseResult
GetBlockVerboseByHash(hash string, verboseTx bool) *dcrjson.GetBlockVerboseResult GetBlockVerboseByHash(hash string, verboseTx bool) *dcrjson.GetBlockVerboseResult
Expand Down Expand Up @@ -441,6 +443,38 @@ func (c *appContext) getBlockHeader(w http.ResponseWriter, r *http.Request) {
writeJSON(w, blockHeader, c.getIndentQuery(r)) writeJSON(w, blockHeader, c.getIndentQuery(r))
} }


func (c *appContext) getBlockRaw(w http.ResponseWriter, r *http.Request) {
hash := c.getBlockHashCtx(r)
if hash == "" {
http.Error(w, http.StatusText(422), 422)
return
}

msgBlock, err := c.BlockData.GetBlockByHash(hash)
if err != nil {
apiLog.Errorf("Unable to get block %s: %v", hash, err)
http.Error(w, http.StatusText(422), 422)
return
}

var hexString strings.Builder
hexString.Grow(msgBlock.SerializeSize())
err = msgBlock.Serialize(hex.NewEncoder(&hexString))
if err != nil {
apiLog.Errorf("Unable to serialize block %s: %v", hash, err)
http.Error(w, http.StatusText(422), 422)
return
}

blockRaw := &apitypes.BlockRaw{
Height: msgBlock.Header.Height,
Hash: hash,
Hex: hexString.String(),
}

writeJSON(w, blockRaw, c.getIndentQuery(r))
}

func (c *appContext) getBlockVerbose(w http.ResponseWriter, r *http.Request) { func (c *appContext) getBlockVerbose(w http.ResponseWriter, r *http.Request) {
hash := c.getBlockHashCtx(r) hash := c.getBlockHashCtx(r)
if hash == "" { if hash == "" {
Expand Down
7 changes: 7 additions & 0 deletions api/types/apitypes.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ type BlockID struct {
BlockTime int64 `json:"blocktime"` BlockTime int64 `json:"blocktime"`
} }


// BlockRaw contains the hexadecimal encoded bytes of a serialized block.
type BlockRaw struct {
Height uint32 `json:"height"`
Hash string `json:"hash"`
Hex string `json:"hex"`
}

// VoutMined appends a best block hash, number of confimations and if a // VoutMined appends a best block hash, number of confimations and if a
// transaction is a coinbase to a transaction output // transaction is a coinbase to a transaction output
type VoutMined struct { type VoutMined struct {
Expand Down
10 changes: 10 additions & 0 deletions db/dcrsqlite/apisource.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -491,6 +491,16 @@ func (db *WiredDB) GetBlockVerbose(idx int, verboseTx bool) *dcrjson.GetBlockVer
func (db *WiredDB) GetBlockVerboseByHash(hash string, verboseTx bool) *dcrjson.GetBlockVerboseResult { func (db *WiredDB) GetBlockVerboseByHash(hash string, verboseTx bool) *dcrjson.GetBlockVerboseResult {
return rpcutils.GetBlockVerboseByHash(db.client, hash, verboseTx) return rpcutils.GetBlockVerboseByHash(db.client, hash, verboseTx)
} }

func (db *WiredDB) GetBlockByHash(hash string) (*wire.MsgBlock, error) {
blockHash, err := chainhash.NewHashFromStr(hash)
if err != nil {
log.Errorf("Invalid block hash %s", hash)
return nil, err
}
return db.client.GetBlock(blockHash)
}

func (db *WiredDB) CoinSupply() (supply *apitypes.CoinSupply) { func (db *WiredDB) CoinSupply() (supply *apitypes.CoinSupply) {
coinSupply, err := db.client.GetCoinSupply() coinSupply, err := db.client.GetCoinSupply()
if err != nil { if err != nil {
Expand Down

0 comments on commit faf775e

Please sign in to comment.