2022-08-29 21:51:00 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/zlib"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2022-09-02 23:46:35 +02:00
|
|
|
"fmt"
|
2022-08-29 21:51:00 +02:00
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
"github.com/lbryio/herald.go/db"
|
2022-09-01 00:23:47 +02:00
|
|
|
"github.com/lbryio/herald.go/internal"
|
2022-08-29 21:51:00 +02:00
|
|
|
"github.com/lbryio/lbcd/chaincfg"
|
2022-09-01 22:26:37 +02:00
|
|
|
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
2022-08-29 21:51:00 +02:00
|
|
|
"github.com/lbryio/lbcd/txscript"
|
2022-09-01 00:23:47 +02:00
|
|
|
"github.com/lbryio/lbcd/wire"
|
2022-08-29 21:51:00 +02:00
|
|
|
"github.com/lbryio/lbcutil"
|
|
|
|
"golang.org/x/exp/constraints"
|
2022-10-04 19:25:44 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-08-29 21:51:00 +02:00
|
|
|
)
|
|
|
|
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
// BlockchainBlockService methods handle "blockchain.block.*" RPCs
|
|
|
|
type BlockchainBlockService struct {
|
|
|
|
DB *db.ReadOnlyDBColumnFamily
|
|
|
|
Chain *chaincfg.Params
|
2022-09-07 20:49:34 +02:00
|
|
|
}
|
|
|
|
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
// BlockchainBlockService methods handle "blockchain.headers.*" RPCs
|
|
|
|
type BlockchainHeadersService struct {
|
2022-09-07 20:49:34 +02:00
|
|
|
DB *db.ReadOnlyDBColumnFamily
|
|
|
|
Chain *chaincfg.Params
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
// needed for subscribe/unsubscribe
|
|
|
|
sessionMgr *sessionManager
|
|
|
|
session *session
|
2022-09-07 20:49:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlockchainAddressService methods handle "blockchain.address.*" RPCs
|
|
|
|
type BlockchainAddressService struct {
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
DB *db.ReadOnlyDBColumnFamily
|
|
|
|
Chain *chaincfg.Params
|
|
|
|
// needed for subscribe/unsubscribe
|
|
|
|
sessionMgr *sessionManager
|
|
|
|
session *session
|
2022-09-07 20:49:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlockchainScripthashService methods handle "blockchain.scripthash.*" RPCs
|
|
|
|
type BlockchainScripthashService struct {
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
DB *db.ReadOnlyDBColumnFamily
|
|
|
|
Chain *chaincfg.Params
|
|
|
|
// needed for subscribe/unsubscribe
|
|
|
|
sessionMgr *sessionManager
|
|
|
|
session *session
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-10-20 20:05:16 +02:00
|
|
|
// BlockchainTransactionService methods handle "blockchain.transaction.*" RPCs
|
|
|
|
type BlockchainTransactionService struct {
|
|
|
|
DB *db.ReadOnlyDBColumnFamily
|
|
|
|
Chain *chaincfg.Params
|
|
|
|
// needed for broadcast TX
|
|
|
|
sessionMgr *sessionManager
|
|
|
|
}
|
|
|
|
|
2022-08-29 21:51:00 +02:00
|
|
|
const CHUNK_SIZE = 96
|
|
|
|
const MAX_CHUNK_SIZE = 40960
|
2022-09-01 00:23:47 +02:00
|
|
|
const HEADER_SIZE = wire.MaxBlockHeaderPayload
|
|
|
|
const HASHX_LEN = 11
|
2022-08-29 21:51:00 +02:00
|
|
|
|
|
|
|
func min[Ord constraints.Ordered](x, y Ord) Ord {
|
|
|
|
if x < y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
func max[Ord constraints.Ordered](x, y Ord) Ord {
|
|
|
|
if x > y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlockHeaderElectrum struct {
|
|
|
|
Version uint32 `json:"version"`
|
|
|
|
PrevBlockHash string `json:"prev_block_hash"`
|
|
|
|
MerkleRoot string `json:"merkle_root"`
|
|
|
|
ClaimTrieRoot string `json:"claim_trie_root"`
|
|
|
|
Timestamp uint32 `json:"timestamp"`
|
|
|
|
Bits uint32 `json:"bits"`
|
|
|
|
Nonce uint32 `json:"nonce"`
|
|
|
|
BlockHeight uint32 `json:"block_height"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBlockHeaderElectrum(header *[HEADER_SIZE]byte, height uint32) *BlockHeaderElectrum {
|
|
|
|
var h1, h2, h3 chainhash.Hash
|
|
|
|
h1.SetBytes(header[4:36])
|
|
|
|
h2.SetBytes(header[36:68])
|
|
|
|
h3.SetBytes(header[68:100])
|
|
|
|
return &BlockHeaderElectrum{
|
|
|
|
Version: binary.LittleEndian.Uint32(header[0:]),
|
|
|
|
PrevBlockHash: h1.String(),
|
|
|
|
MerkleRoot: h2.String(),
|
|
|
|
ClaimTrieRoot: h3.String(),
|
|
|
|
Timestamp: binary.LittleEndian.Uint32(header[100:]),
|
|
|
|
Bits: binary.LittleEndian.Uint32(header[104:]),
|
|
|
|
Nonce: binary.LittleEndian.Uint32(header[108:]),
|
|
|
|
BlockHeight: height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-08 20:17:52 +02:00
|
|
|
type BlockGetServerHeightReq struct{}
|
|
|
|
type BlockGetServerHeightResp uint32
|
|
|
|
|
2022-10-25 07:48:13 +02:00
|
|
|
// blockchain.block.get_server_height
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
func (s *BlockchainBlockService) Get_server_height(req *BlockGetServerHeightReq, resp **BlockGetServerHeightResp) error {
|
2022-09-08 20:17:52 +02:00
|
|
|
if s.DB == nil || s.DB.LastState == nil {
|
|
|
|
return fmt.Errorf("unknown height")
|
|
|
|
}
|
|
|
|
result := BlockGetServerHeightResp(s.DB.LastState.Height)
|
|
|
|
*resp = &result
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type BlockGetChunkReq uint32
|
2022-09-08 18:50:06 +02:00
|
|
|
type BlockGetChunkResp string
|
2022-08-29 21:51:00 +02:00
|
|
|
|
|
|
|
// 'blockchain.block.get_chunk'
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
func (s *BlockchainBlockService) Get_chunk(req *BlockGetChunkReq, resp **BlockGetChunkResp) error {
|
2022-08-29 21:51:00 +02:00
|
|
|
index := uint32(*req)
|
|
|
|
db_headers, err := s.DB.GetHeaders(index*CHUNK_SIZE, CHUNK_SIZE)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
raw := make([]byte, 0, HEADER_SIZE*len(db_headers))
|
|
|
|
for _, h := range db_headers {
|
|
|
|
raw = append(raw, h[:]...)
|
|
|
|
}
|
2022-09-08 18:50:06 +02:00
|
|
|
headers := BlockGetChunkResp(hex.EncodeToString(raw))
|
2022-09-07 20:49:34 +02:00
|
|
|
*resp = &headers
|
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type BlockGetHeaderReq uint32
|
|
|
|
type BlockGetHeaderResp struct {
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
BlockHeaderElectrum
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.block.get_header'
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
func (s *BlockchainBlockService) Get_header(req *BlockGetHeaderReq, resp **BlockGetHeaderResp) error {
|
2022-08-29 21:51:00 +02:00
|
|
|
height := uint32(*req)
|
|
|
|
headers, err := s.DB.GetHeaders(height, 1)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
if len(headers) < 1 {
|
2022-09-07 20:49:34 +02:00
|
|
|
return errors.New("not found")
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
*resp = &BlockGetHeaderResp{*newBlockHeaderElectrum(&headers[0], height)}
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type BlockHeadersReq struct {
|
2022-08-29 21:51:00 +02:00
|
|
|
StartHeight uint32 `json:"start_height"`
|
|
|
|
Count uint32 `json:"count"`
|
|
|
|
CpHeight uint32 `json:"cp_height"`
|
|
|
|
B64 bool `json:"b64"`
|
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type BlockHeadersResp struct {
|
2022-08-29 21:51:00 +02:00
|
|
|
Base64 string `json:"base64,omitempty"`
|
|
|
|
Hex string `json:"hex,omitempty"`
|
|
|
|
Count uint32 `json:"count"`
|
|
|
|
Max uint32 `json:"max"`
|
|
|
|
Branch string `json:"branch,omitempty"`
|
|
|
|
Root string `json:"root,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.block.headers'
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
func (s *BlockchainBlockService) Headers(req *BlockHeadersReq, resp **BlockHeadersResp) error {
|
2022-08-29 21:51:00 +02:00
|
|
|
count := min(req.Count, MAX_CHUNK_SIZE)
|
|
|
|
db_headers, err := s.DB.GetHeaders(req.StartHeight, count)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
count = uint32(len(db_headers))
|
|
|
|
raw := make([]byte, 0, HEADER_SIZE*count)
|
|
|
|
for _, h := range db_headers {
|
|
|
|
raw = append(raw, h[:]...)
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
result := &BlockHeadersResp{
|
2022-08-29 21:51:00 +02:00
|
|
|
Count: count,
|
|
|
|
Max: MAX_CHUNK_SIZE,
|
|
|
|
}
|
|
|
|
if req.B64 {
|
|
|
|
zipped := bytes.Buffer{}
|
|
|
|
w := zlib.NewWriter(&zipped)
|
|
|
|
w.Write(raw)
|
|
|
|
w.Close()
|
|
|
|
result.Base64 = base64.StdEncoding.EncodeToString(zipped.Bytes())
|
|
|
|
} else {
|
|
|
|
result.Hex = hex.EncodeToString(raw)
|
|
|
|
}
|
|
|
|
if count > 0 && req.CpHeight > 0 {
|
|
|
|
// TODO
|
|
|
|
//last_height := height + count - 1
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
*resp = result
|
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
type HeadersSubscribeReq struct {
|
|
|
|
Raw bool `json:"raw"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type HeadersSubscribeResp struct {
|
|
|
|
BlockHeaderElectrum
|
|
|
|
}
|
|
|
|
type HeadersSubscribeRawResp struct {
|
|
|
|
Hex string `json:"hex"`
|
|
|
|
Height uint32 `json:"height"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.headers.subscribe'
|
|
|
|
func (s *BlockchainHeadersService) Subscribe(req *HeadersSubscribeReq, resp *interface{}) error {
|
|
|
|
if s.sessionMgr == nil || s.session == nil {
|
|
|
|
return errors.New("no session, rpc not supported")
|
|
|
|
}
|
|
|
|
s.sessionMgr.headersSubscribe(s.session, req.Raw, true /*subscribe*/)
|
|
|
|
height := s.DB.Height
|
|
|
|
if s.DB.LastState != nil {
|
|
|
|
height = s.DB.LastState.Height
|
|
|
|
}
|
|
|
|
headers, err := s.DB.GetHeaders(height, 1)
|
|
|
|
if err != nil {
|
|
|
|
s.sessionMgr.headersSubscribe(s.session, req.Raw, false /*subscribe*/)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(headers) < 1 {
|
|
|
|
return errors.New("not found")
|
|
|
|
}
|
|
|
|
if req.Raw {
|
|
|
|
*resp = &HeadersSubscribeRawResp{
|
|
|
|
Hex: hex.EncodeToString(headers[0][:]),
|
|
|
|
Height: height,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*resp = &HeadersSubscribeResp{*newBlockHeaderElectrum(&headers[0], height)}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-02 23:46:35 +02:00
|
|
|
func decodeScriptHash(scripthash string) ([]byte, error) {
|
|
|
|
sh, err := hex.DecodeString(scripthash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(sh) != chainhash.HashSize {
|
|
|
|
return nil, fmt.Errorf("invalid scripthash: %v (length %v)", scripthash, len(sh))
|
|
|
|
}
|
2022-09-01 00:23:47 +02:00
|
|
|
internal.ReverseBytesInPlace(sh)
|
2022-09-02 23:46:35 +02:00
|
|
|
return sh, nil
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
|
2022-09-02 23:46:35 +02:00
|
|
|
func hashX(scripthash []byte) []byte {
|
|
|
|
return scripthash[:HASHX_LEN]
|
2022-09-01 00:23:47 +02:00
|
|
|
}
|
|
|
|
|
2022-08-29 21:51:00 +02:00
|
|
|
func hashXScript(script []byte, coin *chaincfg.Params) []byte {
|
|
|
|
if _, err := txscript.ExtractClaimScript(script); err == nil {
|
|
|
|
baseScript := txscript.StripClaimScriptPrefix(script)
|
|
|
|
if class, addrs, _, err := txscript.ExtractPkScriptAddrs(baseScript, coin); err == nil {
|
|
|
|
switch class {
|
|
|
|
case txscript.PubKeyHashTy, txscript.ScriptHashTy, txscript.PubKeyTy:
|
|
|
|
script, _ := txscript.PayToAddrScript(addrs[0])
|
|
|
|
return hashXScript(script, coin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sum := sha256.Sum256(script)
|
2022-09-01 00:23:47 +02:00
|
|
|
return sum[:HASHX_LEN]
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressGetBalanceReq struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
Address string `json:"address"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressGetBalanceResp struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
Confirmed uint64 `json:"confirmed"`
|
|
|
|
Unconfirmed uint64 `json:"unconfirmed"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.address.get_balance'
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
func (s *BlockchainAddressService) Get_balance(req *AddressGetBalanceReq, resp **AddressGetBalanceResp) error {
|
2022-09-01 00:23:47 +02:00
|
|
|
address, err := lbcutil.DecodeAddress(req.Address, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(address)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-01 00:23:47 +02:00
|
|
|
hashX := hashXScript(script, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
confirmed, unconfirmed, err := s.DB.GetBalance(hashX)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
*resp = &AddressGetBalanceResp{confirmed, unconfirmed}
|
2022-10-04 19:25:44 +02:00
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 23:46:35 +02:00
|
|
|
type scripthashGetBalanceReq struct {
|
|
|
|
ScriptHash string `json:"scripthash"`
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashGetBalanceResp struct {
|
2022-09-02 23:46:35 +02:00
|
|
|
Confirmed uint64 `json:"confirmed"`
|
|
|
|
Unconfirmed uint64 `json:"unconfirmed"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.scripthash.get_balance'
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
func (s *BlockchainScripthashService) Get_balance(req *scripthashGetBalanceReq, resp **ScripthashGetBalanceResp) error {
|
2022-09-02 23:46:35 +02:00
|
|
|
scripthash, err := decodeScriptHash(req.ScriptHash)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
hashX := hashX(scripthash)
|
|
|
|
confirmed, unconfirmed, err := s.DB.GetBalance(hashX)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
*resp = &ScripthashGetBalanceResp{confirmed, unconfirmed}
|
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressGetHistoryReq struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
Address string `json:"address"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
type TxInfo struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
TxHash string `json:"tx_hash"`
|
|
|
|
Height uint32 `json:"height"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
type TxInfoFee struct {
|
|
|
|
TxInfo
|
2022-09-01 00:23:47 +02:00
|
|
|
Fee uint64 `json:"fee"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressGetHistoryResp struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
Confirmed []TxInfo `json:"confirmed"`
|
|
|
|
Unconfirmed []TxInfoFee `json:"unconfirmed"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.address.get_history'
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
func (s *BlockchainAddressService) Get_history(req *AddressGetHistoryReq, resp **AddressGetHistoryResp) error {
|
2022-09-01 00:23:47 +02:00
|
|
|
address, err := lbcutil.DecodeAddress(req.Address, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(address)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-01 00:23:47 +02:00
|
|
|
hashX := hashXScript(script, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
dbTXs, err := s.DB.GetHistory(hashX)
|
2022-09-02 23:46:35 +02:00
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
2022-08-29 21:51:00 +02:00
|
|
|
confirmed := make([]TxInfo, 0, len(dbTXs))
|
|
|
|
for _, tx := range dbTXs {
|
|
|
|
confirmed = append(confirmed,
|
|
|
|
TxInfo{
|
2022-09-01 22:26:37 +02:00
|
|
|
TxHash: tx.TxHash.String(),
|
2022-08-29 21:51:00 +02:00
|
|
|
Height: tx.Height,
|
|
|
|
})
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
result := &AddressGetHistoryResp{
|
2022-08-29 21:51:00 +02:00
|
|
|
Confirmed: confirmed,
|
|
|
|
Unconfirmed: []TxInfoFee{}, // TODO
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
*resp = result
|
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashGetHistoryReq struct {
|
2022-09-02 23:46:35 +02:00
|
|
|
ScriptHash string `json:"scripthash"`
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashGetHistoryResp struct {
|
2022-09-02 23:46:35 +02:00
|
|
|
Confirmed []TxInfo `json:"confirmed"`
|
|
|
|
Unconfirmed []TxInfoFee `json:"unconfirmed"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.scripthash.get_history'
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
func (s *BlockchainScripthashService) Get_history(req *ScripthashGetHistoryReq, resp **ScripthashGetHistoryResp) error {
|
2022-09-02 23:46:35 +02:00
|
|
|
scripthash, err := decodeScriptHash(req.ScriptHash)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
hashX := hashX(scripthash)
|
|
|
|
dbTXs, err := s.DB.GetHistory(hashX)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
confirmed := make([]TxInfo, 0, len(dbTXs))
|
|
|
|
for _, tx := range dbTXs {
|
|
|
|
confirmed = append(confirmed,
|
|
|
|
TxInfo{
|
|
|
|
TxHash: tx.TxHash.String(),
|
|
|
|
Height: tx.Height,
|
|
|
|
})
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
result := &ScripthashGetHistoryResp{
|
2022-09-02 23:46:35 +02:00
|
|
|
Confirmed: confirmed,
|
|
|
|
Unconfirmed: []TxInfoFee{}, // TODO
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
*resp = result
|
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressGetMempoolReq struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
Address string `json:"address"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressGetMempoolResp []TxInfoFee
|
2022-08-29 21:51:00 +02:00
|
|
|
|
|
|
|
// 'blockchain.address.get_mempool'
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
func (s *BlockchainAddressService) Get_mempool(req *AddressGetMempoolReq, resp **AddressGetMempoolResp) error {
|
2022-09-01 00:23:47 +02:00
|
|
|
address, err := lbcutil.DecodeAddress(req.Address, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(address)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-01 00:23:47 +02:00
|
|
|
hashX := hashXScript(script, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
// TODO...
|
2022-09-01 00:23:47 +02:00
|
|
|
internal.ReverseBytesInPlace(hashX)
|
2022-08-29 21:51:00 +02:00
|
|
|
unconfirmed := make([]TxInfoFee, 0, 100)
|
2022-09-07 20:49:34 +02:00
|
|
|
result := AddressGetMempoolResp(unconfirmed)
|
|
|
|
*resp = &result
|
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashGetMempoolReq struct {
|
2022-09-02 23:46:35 +02:00
|
|
|
ScriptHash string `json:"scripthash"`
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashGetMempoolResp []TxInfoFee
|
2022-09-02 23:46:35 +02:00
|
|
|
|
|
|
|
// 'blockchain.scripthash.get_mempool'
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
func (s *BlockchainScripthashService) Get_mempool(req *ScripthashGetMempoolReq, resp **ScripthashGetMempoolResp) error {
|
2022-09-02 23:46:35 +02:00
|
|
|
scripthash, err := decodeScriptHash(req.ScriptHash)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
hashX := hashX(scripthash)
|
|
|
|
// TODO...
|
|
|
|
internal.ReverseBytesInPlace(hashX)
|
|
|
|
unconfirmed := make([]TxInfoFee, 0, 100)
|
2022-09-07 20:49:34 +02:00
|
|
|
result := ScripthashGetMempoolResp(unconfirmed)
|
|
|
|
*resp = &result
|
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressListUnspentReq struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
Address string `json:"address"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
type TXOInfo struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
TxHash string `json:"tx_hash"`
|
|
|
|
TxPos uint16 `json:"tx_pos"`
|
|
|
|
Height uint32 `json:"height"`
|
|
|
|
Value uint64 `json:"value"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressListUnspentResp []TXOInfo
|
2022-08-29 21:51:00 +02:00
|
|
|
|
|
|
|
// 'blockchain.address.listunspent'
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
func (s *BlockchainAddressService) Listunspent(req *AddressListUnspentReq, resp **AddressListUnspentResp) error {
|
2022-09-01 00:23:47 +02:00
|
|
|
address, err := lbcutil.DecodeAddress(req.Address, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(address)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-01 00:23:47 +02:00
|
|
|
hashX := hashXScript(script, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
dbTXOs, err := s.DB.GetUnspent(hashX)
|
|
|
|
unspent := make([]TXOInfo, 0, len(dbTXOs))
|
|
|
|
for _, txo := range dbTXOs {
|
|
|
|
unspent = append(unspent,
|
|
|
|
TXOInfo{
|
2022-09-01 22:26:37 +02:00
|
|
|
TxHash: txo.TxHash.String(),
|
2022-08-29 21:51:00 +02:00
|
|
|
TxPos: txo.TxPos,
|
|
|
|
Height: txo.Height,
|
|
|
|
Value: txo.Value,
|
|
|
|
})
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
result := AddressListUnspentResp(unspent)
|
|
|
|
*resp = &result
|
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-02 23:46:35 +02:00
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashListUnspentReq struct {
|
2022-09-02 23:46:35 +02:00
|
|
|
ScriptHash string `json:"scripthash"`
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashListUnspentResp []TXOInfo
|
2022-09-02 23:46:35 +02:00
|
|
|
|
|
|
|
// 'blockchain.scripthash.listunspent'
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
func (s *BlockchainScripthashService) Listunspent(req *ScripthashListUnspentReq, resp **ScripthashListUnspentResp) error {
|
2022-09-02 23:46:35 +02:00
|
|
|
scripthash, err := decodeScriptHash(req.ScriptHash)
|
|
|
|
if err != nil {
|
2022-10-04 19:25:44 +02:00
|
|
|
log.Warn(err)
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
hashX := hashX(scripthash)
|
|
|
|
dbTXOs, err := s.DB.GetUnspent(hashX)
|
|
|
|
unspent := make([]TXOInfo, 0, len(dbTXOs))
|
|
|
|
for _, txo := range dbTXOs {
|
|
|
|
unspent = append(unspent,
|
|
|
|
TXOInfo{
|
|
|
|
TxHash: txo.TxHash.String(),
|
|
|
|
TxPos: txo.TxPos,
|
|
|
|
Height: txo.Height,
|
|
|
|
Value: txo.Value,
|
|
|
|
})
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
result := ScripthashListUnspentResp(unspent)
|
|
|
|
*resp = &result
|
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
Add subscribe/unsubscribe RPCs. Add session, sessionManager, and serve JSON RPC (without HTTP). (#66)
* Move and rename BlockchainCodec, BlockchainCodecRequest.
These are not specifically "blockchain", rather they are
specific to how gorilla/rpc works.
* Move claimtrie-related service/handlers to jsonrpc_claimtrie.go.
* Pull out decode logic into named func newBlockHeaderElectrum().
* Rename BlockchainService -> BlockchainBlockService.
* Drop http.Request arg from handlers, and use RegisterTCPService().
* Implement GetStatus() to pull data from HashXStatus table.
* Make the service objects independent, so we don't have inheritance.
* Add core session/subscription logic (session.go).
Implement subsribe/unsubscribe handlers.
* Support both pure JSON and JSON-over-HTTP services.
Forward NotifierChan messages to sessionManager.
* Only assign default port (50001) if neither --json-rpc-port nor
--json-rpc-http-port are specified.
* Handle failures with goto instead of break. Update error logging.
* Add --max-sessions, --session-timeout args. Enforce max sessions.
* Changes to make session.go testable. Conn created with Pipe()
used in testing has no unique Addr.
* Add tests for headers, headers.subscribe, address.subscribe.
* HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().
* Use time.Ticker object to drive management activity.
2022-10-04 16:05:06 +02:00
|
|
|
|
|
|
|
type AddressSubscribeReq []string
|
|
|
|
type AddressSubscribeResp []string
|
|
|
|
|
|
|
|
// 'blockchain.address.subscribe'
|
|
|
|
func (s *BlockchainAddressService) Subscribe(req *AddressSubscribeReq, resp **AddressSubscribeResp) error {
|
|
|
|
if s.sessionMgr == nil || s.session == nil {
|
|
|
|
return errors.New("no session, rpc not supported")
|
|
|
|
}
|
|
|
|
result := make([]string, 0, len(*req))
|
|
|
|
for _, addr := range *req {
|
|
|
|
address, err := lbcutil.DecodeAddress(addr, s.Chain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hashX := hashXScript(script, s.Chain)
|
|
|
|
s.sessionMgr.hashXSubscribe(s.session, hashX, addr, true /*subscribe*/)
|
|
|
|
status, err := s.DB.GetStatus(hashX)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
result = append(result, hex.EncodeToString(status))
|
|
|
|
}
|
|
|
|
*resp = (*AddressSubscribeResp)(&result)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.address.unsubscribe'
|
|
|
|
func (s *BlockchainAddressService) Unsubscribe(req *AddressSubscribeReq, resp **AddressSubscribeResp) error {
|
|
|
|
if s.sessionMgr == nil || s.session == nil {
|
|
|
|
return errors.New("no session, rpc not supported")
|
|
|
|
}
|
|
|
|
for _, addr := range *req {
|
|
|
|
address, err := lbcutil.DecodeAddress(addr, s.Chain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hashX := hashXScript(script, s.Chain)
|
|
|
|
s.sessionMgr.hashXSubscribe(s.session, hashX, addr, false /*subscribe*/)
|
|
|
|
}
|
|
|
|
*resp = (*AddressSubscribeResp)(nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ScripthashSubscribeReq string
|
|
|
|
type ScripthashSubscribeResp string
|
|
|
|
|
|
|
|
// 'blockchain.scripthash.subscribe'
|
|
|
|
func (s *BlockchainScripthashService) Subscribe(req *ScripthashSubscribeReq, resp **ScripthashSubscribeResp) error {
|
|
|
|
if s.sessionMgr == nil || s.session == nil {
|
|
|
|
return errors.New("no session, rpc not supported")
|
|
|
|
}
|
|
|
|
var result string
|
|
|
|
scripthash, err := decodeScriptHash(string(*req))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hashX := hashX(scripthash)
|
|
|
|
s.sessionMgr.hashXSubscribe(s.session, hashX, string(*req), true /*subscribe*/)
|
|
|
|
|
|
|
|
status, err := s.DB.GetStatus(hashX)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
result = hex.EncodeToString(status)
|
|
|
|
*resp = (*ScripthashSubscribeResp)(&result)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.scripthash.unsubscribe'
|
|
|
|
func (s *BlockchainScripthashService) Unsubscribe(req *ScripthashSubscribeReq, resp **ScripthashSubscribeResp) error {
|
|
|
|
if s.sessionMgr == nil || s.session == nil {
|
|
|
|
return errors.New("no session, rpc not supported")
|
|
|
|
}
|
|
|
|
scripthash, err := decodeScriptHash(string(*req))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hashX := hashX(scripthash)
|
|
|
|
s.sessionMgr.hashXSubscribe(s.session, hashX, string(*req), false /*subscribe*/)
|
|
|
|
*resp = (*ScripthashSubscribeResp)(nil)
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-20 20:05:16 +02:00
|
|
|
|
|
|
|
type TransactionBroadcastReq string
|
|
|
|
type TransactionBroadcastResp string
|
|
|
|
|
|
|
|
// 'blockchain.transaction.broadcast'
|
|
|
|
func (s *BlockchainTransactionService) Broadcast(req *TransactionBroadcastReq, resp **TransactionBroadcastResp) error {
|
|
|
|
strTx := string(*req)
|
|
|
|
rawTx, err := hex.DecodeString(strTx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
txhash, err := s.sessionMgr.broadcastTx(rawTx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
result := txhash.String()
|
|
|
|
*resp = (*TransactionBroadcastResp)(&result)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransactionGetReq string
|
|
|
|
type TXFullDetail struct {
|
|
|
|
Height uint32 `json:"block_height"`
|
|
|
|
Merkle string `json:"merkle"`
|
|
|
|
Pos uint64 `json:"pos"`
|
|
|
|
}
|
|
|
|
type TXDetail struct {
|
|
|
|
Height uint32 `json:"block_height"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransactionResp is a pair consisting of:
|
|
|
|
// resp[0]: Raw transaction as hex string
|
|
|
|
// resp[1]: TXFullDetail or TXDetail structure
|
|
|
|
type TXGetResp [2]interface{}
|
|
|
|
type TransactionGetResp TXGetResp
|
|
|
|
|
|
|
|
// 'blockchain.transaction.get'
|
|
|
|
func (s *BlockchainTransactionService) Get(req *TransactionGetReq, resp **TransactionGetResp) error {
|
|
|
|
txids := [1]string{string(*req)}
|
|
|
|
request := TransactionGetBatchReq(txids[:])
|
|
|
|
var response *TransactionGetBatchResp
|
|
|
|
err := s.Get_batch(&request, &response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(*response) < 1 {
|
|
|
|
return errors.New("tx not found")
|
|
|
|
}
|
|
|
|
switch (*response)[0][1].(type) {
|
|
|
|
case TXFullDetail:
|
|
|
|
break
|
|
|
|
case TXDetail:
|
|
|
|
default:
|
|
|
|
return errors.New("tx not confirmed")
|
|
|
|
}
|
|
|
|
*resp = (*TransactionGetResp)(&(*response)[0])
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransactionGetBatchReq []string
|
|
|
|
type TransactionGetBatchResp []TXGetResp
|
|
|
|
|
|
|
|
// 'blockchain.transaction.get_batch'
|
|
|
|
func (s *BlockchainTransactionService) Get_batch(req *TransactionGetBatchReq, resp **TransactionGetBatchResp) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransactionGetMerkleReq string
|
|
|
|
type TransactionGetMerkleResp TXGetResp
|
|
|
|
|
|
|
|
// 'blockchain.transaction.get_merkle'
|
|
|
|
func (s *BlockchainTransactionService) Get_merkle(req *TransactionGetMerkleReq, resp **TransactionGetMerkleResp) error {
|
|
|
|
txids := [1]string{string(*req)}
|
|
|
|
request := TransactionGetBatchReq(txids[:])
|
|
|
|
var response *TransactionGetBatchResp
|
|
|
|
err := s.Get_batch(&request, &response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(*response) < 1 {
|
|
|
|
return errors.New("tx not found")
|
|
|
|
}
|
|
|
|
switch (*response)[0][1].(type) {
|
|
|
|
case TXFullDetail:
|
|
|
|
break
|
|
|
|
case TXDetail:
|
|
|
|
default:
|
|
|
|
return errors.New("tx not confirmed")
|
|
|
|
}
|
|
|
|
*resp = (*TransactionGetMerkleResp)(&(*response)[0])
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransactionGetHeightReq string
|
|
|
|
type TransactionGetHeightResp uint32
|
|
|
|
|
|
|
|
// 'blockchain.transaction.get_height'
|
|
|
|
func (s *BlockchainTransactionService) Get_height(req *TransactionGetHeightReq, resp **TransactionGetHeightResp) error {
|
|
|
|
txid := string(*(req))
|
|
|
|
txhash, err := chainhash.NewHashFromStr(txid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
height, err := s.DB.GetTxHeight(txhash)
|
|
|
|
*resp = (*TransactionGetHeightResp)(&height)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransactionInfoReq string
|
|
|
|
type TransactionInfoResp TXGetResp
|
|
|
|
|
|
|
|
// 'blockchain.transaction.info'
|
|
|
|
func (s *BlockchainTransactionService) Info(req *TransactionInfoReq, resp **TransactionInfoResp) error {
|
|
|
|
txids := [1]string{string(*req)}
|
|
|
|
request := TransactionGetBatchReq(txids[:])
|
|
|
|
var response *TransactionGetBatchResp
|
|
|
|
err := s.Get_batch(&request, &response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(*response) < 1 {
|
|
|
|
return errors.New("tx not found")
|
|
|
|
}
|
|
|
|
switch (*response)[0][1].(type) {
|
|
|
|
case TXFullDetail:
|
|
|
|
break
|
|
|
|
case TXDetail:
|
|
|
|
default:
|
|
|
|
if (*response)[0][0] == nil {
|
|
|
|
return errors.New("no such mempool or blockchain transaction")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*resp = (*TransactionInfoResp)(&(*response)[0])
|
|
|
|
return err
|
|
|
|
}
|