Drop http.Request arg from handlers, and use RegisterTCPService().

This commit is contained in:
Jonathan Moody 2022-09-27 17:47:27 -05:00
parent d03c992a25
commit 603a18f590
3 changed files with 21 additions and 22 deletions

View file

@ -9,7 +9,6 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"net/http"
"github.com/lbryio/herald.go/db" "github.com/lbryio/herald.go/db"
"github.com/lbryio/herald.go/internal" "github.com/lbryio/herald.go/internal"
@ -80,7 +79,7 @@ func newBlockHeaderElectrum(header *[HEADER_SIZE]byte, height uint32) *BlockHead
type BlockGetServerHeightReq struct{} type BlockGetServerHeightReq struct{}
type BlockGetServerHeightResp uint32 type BlockGetServerHeightResp uint32
func (s *BlockchainBlockService) Get_server_height(r *http.Request, req *BlockGetServerHeightReq, resp **BlockGetServerHeightResp) error { func (s *BlockchainBlockService) Get_server_height(req *BlockGetServerHeightReq, resp **BlockGetServerHeightResp) error {
if s.DB == nil || s.DB.LastState == nil { if s.DB == nil || s.DB.LastState == nil {
return fmt.Errorf("unknown height") return fmt.Errorf("unknown height")
} }
@ -93,7 +92,7 @@ type BlockGetChunkReq uint32
type BlockGetChunkResp string type BlockGetChunkResp string
// 'blockchain.block.get_chunk' // 'blockchain.block.get_chunk'
func (s *BlockchainBlockService) Get_chunk(r *http.Request, req *BlockGetChunkReq, resp **BlockGetChunkResp) error { func (s *BlockchainBlockService) Get_chunk(req *BlockGetChunkReq, resp **BlockGetChunkResp) error {
index := uint32(*req) index := uint32(*req)
db_headers, err := s.DB.GetHeaders(index*CHUNK_SIZE, CHUNK_SIZE) db_headers, err := s.DB.GetHeaders(index*CHUNK_SIZE, CHUNK_SIZE)
if err != nil { if err != nil {
@ -114,7 +113,7 @@ type BlockGetHeaderResp struct {
} }
// 'blockchain.block.get_header' // 'blockchain.block.get_header'
func (s *BlockchainBlockService) Get_header(r *http.Request, req *BlockGetHeaderReq, resp **BlockGetHeaderResp) error { func (s *BlockchainBlockService) Get_header(req *BlockGetHeaderReq, resp **BlockGetHeaderResp) error {
height := uint32(*req) height := uint32(*req)
headers, err := s.DB.GetHeaders(height, 1) headers, err := s.DB.GetHeaders(height, 1)
if err != nil { if err != nil {
@ -144,7 +143,7 @@ type BlockHeadersResp struct {
} }
// 'blockchain.block.headers' // 'blockchain.block.headers'
func (s *BlockchainBlockService) Headers(r *http.Request, req *BlockHeadersReq, resp **BlockHeadersResp) error { func (s *BlockchainBlockService) Headers(req *BlockHeadersReq, resp **BlockHeadersResp) error {
count := min(req.Count, MAX_CHUNK_SIZE) count := min(req.Count, MAX_CHUNK_SIZE)
db_headers, err := s.DB.GetHeaders(req.StartHeight, count) db_headers, err := s.DB.GetHeaders(req.StartHeight, count)
if err != nil { if err != nil {
@ -216,7 +215,7 @@ type AddressGetBalanceResp struct {
} }
// 'blockchain.address.get_balance' // 'blockchain.address.get_balance'
func (s *BlockchainAddressService) Get_balance(r *http.Request, req *AddressGetBalanceReq, resp **AddressGetBalanceResp) error { func (s *BlockchainAddressService) Get_balance(req *AddressGetBalanceReq, resp **AddressGetBalanceResp) error {
address, err := lbcutil.DecodeAddress(req.Address, s.Chain) address, err := lbcutil.DecodeAddress(req.Address, s.Chain)
if err != nil { if err != nil {
return err return err
@ -243,7 +242,7 @@ type ScripthashGetBalanceResp struct {
} }
// 'blockchain.scripthash.get_balance' // 'blockchain.scripthash.get_balance'
func (s *BlockchainScripthashService) Get_balance(r *http.Request, req *scripthashGetBalanceReq, resp **ScripthashGetBalanceResp) error { func (s *BlockchainScripthashService) Get_balance(req *scripthashGetBalanceReq, resp **ScripthashGetBalanceResp) error {
scripthash, err := decodeScriptHash(req.ScriptHash) scripthash, err := decodeScriptHash(req.ScriptHash)
if err != nil { if err != nil {
return err return err
@ -274,7 +273,7 @@ type AddressGetHistoryResp struct {
} }
// 'blockchain.address.get_history' // 'blockchain.address.get_history'
func (s *BlockchainAddressService) Get_history(r *http.Request, req *AddressGetHistoryReq, resp **AddressGetHistoryResp) error { func (s *BlockchainAddressService) Get_history(req *AddressGetHistoryReq, resp **AddressGetHistoryResp) error {
address, err := lbcutil.DecodeAddress(req.Address, s.Chain) address, err := lbcutil.DecodeAddress(req.Address, s.Chain)
if err != nil { if err != nil {
return err return err
@ -313,7 +312,7 @@ type ScripthashGetHistoryResp struct {
} }
// 'blockchain.scripthash.get_history' // 'blockchain.scripthash.get_history'
func (s *BlockchainScripthashService) Get_history(r *http.Request, req *ScripthashGetHistoryReq, resp **ScripthashGetHistoryResp) error { func (s *BlockchainScripthashService) Get_history(req *ScripthashGetHistoryReq, resp **ScripthashGetHistoryResp) error {
scripthash, err := decodeScriptHash(req.ScriptHash) scripthash, err := decodeScriptHash(req.ScriptHash)
if err != nil { if err != nil {
return err return err
@ -345,7 +344,7 @@ type AddressGetMempoolReq struct {
type AddressGetMempoolResp []TxInfoFee type AddressGetMempoolResp []TxInfoFee
// 'blockchain.address.get_mempool' // 'blockchain.address.get_mempool'
func (s *BlockchainAddressService) Get_mempool(r *http.Request, req *AddressGetMempoolReq, resp **AddressGetMempoolResp) error { func (s *BlockchainAddressService) Get_mempool(req *AddressGetMempoolReq, resp **AddressGetMempoolResp) error {
address, err := lbcutil.DecodeAddress(req.Address, s.Chain) address, err := lbcutil.DecodeAddress(req.Address, s.Chain)
if err != nil { if err != nil {
return err return err
@ -369,7 +368,7 @@ type ScripthashGetMempoolReq struct {
type ScripthashGetMempoolResp []TxInfoFee type ScripthashGetMempoolResp []TxInfoFee
// 'blockchain.scripthash.get_mempool' // 'blockchain.scripthash.get_mempool'
func (s *BlockchainScripthashService) Get_mempool(r *http.Request, req *ScripthashGetMempoolReq, resp **ScripthashGetMempoolResp) error { func (s *BlockchainScripthashService) Get_mempool(req *ScripthashGetMempoolReq, resp **ScripthashGetMempoolResp) error {
scripthash, err := decodeScriptHash(req.ScriptHash) scripthash, err := decodeScriptHash(req.ScriptHash)
if err != nil { if err != nil {
return err return err
@ -395,7 +394,7 @@ type TXOInfo struct {
type AddressListUnspentResp []TXOInfo type AddressListUnspentResp []TXOInfo
// 'blockchain.address.listunspent' // 'blockchain.address.listunspent'
func (s *BlockchainAddressService) Listunspent(r *http.Request, req *AddressListUnspentReq, resp **AddressListUnspentResp) error { func (s *BlockchainAddressService) Listunspent(req *AddressListUnspentReq, resp **AddressListUnspentResp) error {
address, err := lbcutil.DecodeAddress(req.Address, s.Chain) address, err := lbcutil.DecodeAddress(req.Address, s.Chain)
if err != nil { if err != nil {
return err return err
@ -427,7 +426,7 @@ type ScripthashListUnspentReq struct {
type ScripthashListUnspentResp []TXOInfo type ScripthashListUnspentResp []TXOInfo
// 'blockchain.scripthash.listunspent' // 'blockchain.scripthash.listunspent'
func (s *BlockchainScripthashService) Listunspent(r *http.Request, req *ScripthashListUnspentReq, resp **ScripthashListUnspentResp) error { func (s *BlockchainScripthashService) Listunspent(req *ScripthashListUnspentReq, resp **ScripthashListUnspentResp) error {
scripthash, err := decodeScriptHash(req.ScriptHash) scripthash, err := decodeScriptHash(req.ScriptHash)
if err != nil { if err != nil {
return err return err

View file

@ -65,7 +65,7 @@ func TestServerGetHeight(t *testing.T) {
req := BlockGetServerHeightReq{} req := BlockGetServerHeightReq{}
var resp *BlockGetServerHeightResp var resp *BlockGetServerHeightResp
err = s.Get_server_height(nil, &req, &resp) err = s.Get_server_height(&req, &resp)
if err != nil { if err != nil {
t.Errorf("handler err: %v", err) t.Errorf("handler err: %v", err)
} }
@ -96,7 +96,7 @@ func TestGetChunk(t *testing.T) {
for index := 0; index < 10; index++ { for index := 0; index < 10; index++ {
req := BlockGetChunkReq(index) req := BlockGetChunkReq(index)
var resp *BlockGetChunkResp var resp *BlockGetChunkResp
err := s.Get_chunk(nil, &req, &resp) err := s.Get_chunk(&req, &resp)
if err != nil { if err != nil {
t.Errorf("index: %v handler err: %v", index, err) t.Errorf("index: %v handler err: %v", index, err)
} }
@ -139,7 +139,7 @@ func TestGetHeader(t *testing.T) {
for height := 0; height < 700; height += 100 { for height := 0; height < 700; height += 100 {
req := BlockGetHeaderReq(height) req := BlockGetHeaderReq(height)
var resp *BlockGetHeaderResp var resp *BlockGetHeaderResp
err := s.Get_header(nil, &req, &resp) err := s.Get_header(&req, &resp)
if err != nil && height <= 500 { if err != nil && height <= 500 {
t.Errorf("height: %v handler err: %v", height, err) t.Errorf("height: %v handler err: %v", height, err)
} }
@ -170,7 +170,7 @@ func TestGetBalance(t *testing.T) {
for _, addr := range regTestAddrs { for _, addr := range regTestAddrs {
req := AddressGetBalanceReq{addr} req := AddressGetBalanceReq{addr}
var resp *AddressGetBalanceResp var resp *AddressGetBalanceResp
err := s.Get_balance(nil, &req, &resp) err := s.Get_balance(&req, &resp)
if err != nil { if err != nil {
t.Errorf("address: %v handler err: %v", addr, err) t.Errorf("address: %v handler err: %v", addr, err)
} }
@ -201,7 +201,7 @@ func TestGetHistory(t *testing.T) {
for _, addr := range regTestAddrs { for _, addr := range regTestAddrs {
req := AddressGetHistoryReq{addr} req := AddressGetHistoryReq{addr}
var resp *AddressGetHistoryResp var resp *AddressGetHistoryResp
err := s.Get_history(nil, &req, &resp) err := s.Get_history(&req, &resp)
if err != nil { if err != nil {
t.Errorf("address: %v handler err: %v", addr, err) t.Errorf("address: %v handler err: %v", addr, err)
} }
@ -232,7 +232,7 @@ func TestListUnspent(t *testing.T) {
for _, addr := range regTestAddrs { for _, addr := range regTestAddrs {
req := AddressListUnspentReq{addr} req := AddressListUnspentReq{addr}
var resp *AddressListUnspentResp var resp *AddressListUnspentResp
err := s.Listunspent(nil, &req, &resp) err := s.Listunspent(&req, &resp)
if err != nil { if err != nil {
t.Errorf("address: %v handler err: %v", addr, err) t.Errorf("address: %v handler err: %v", addr, err)
} }

View file

@ -64,15 +64,15 @@ func (s *Server) StartJsonRPC() error {
// Register other "blockchain.{block,address,scripthash}.*" handlers. // Register other "blockchain.{block,address,scripthash}.*" handlers.
blockchainSvc := &BlockchainBlockService{s.DB, s.Chain} blockchainSvc := &BlockchainBlockService{s.DB, s.Chain}
err = s1.RegisterService(blockchainSvc, "blockchain_block") err = s1.RegisterTCPService(blockchainSvc, "blockchain_block")
if err != nil { if err != nil {
log.Errorf("RegisterService: %v\n", err) log.Errorf("RegisterService: %v\n", err)
} }
err = s1.RegisterService(&BlockchainAddressService{*blockchainSvc}, "blockchain_address") err = s1.RegisterTCPService(&BlockchainAddressService{*blockchainSvc}, "blockchain_address")
if err != nil { if err != nil {
log.Errorf("RegisterService: %v\n", err) log.Errorf("RegisterService: %v\n", err)
} }
err = s1.RegisterService(&BlockchainScripthashService{*blockchainSvc}, "blockchain_scripthash") err = s1.RegisterTCPService(&BlockchainScripthashService{*blockchainSvc}, "blockchain_scripthash")
if err != nil { if err != nil {
log.Errorf("RegisterService: %v\n", err) log.Errorf("RegisterService: %v\n", err)
} }