Factor out common message to hex in RPC server.
This commit is contained in:
parent
8477ef569a
commit
af3609d861
1 changed files with 19 additions and 12 deletions
31
rpcserver.go
31
rpcserver.go
|
@ -717,6 +717,20 @@ func handleGetBestBlockHash(s *rpcServer, cmd btcjson.Cmd, walletNotification ch
|
||||||
return sha.String(), nil
|
return sha.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// messageToHex serializes a message to the wire protocol encoding using the
|
||||||
|
// latest protocol version and returns a hex-encoded string of the result.
|
||||||
|
func messageToHex(msg btcwire.Message) (string, *btcjson.Error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := msg.BtcEncode(&buf, btcwire.ProtocolVersion)
|
||||||
|
if err != nil {
|
||||||
|
return "", &btcjson.Error{
|
||||||
|
Code: btcjson.ErrInternal.Code,
|
||||||
|
Message: err.Error(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(buf.Bytes()), nil
|
||||||
|
}
|
||||||
|
|
||||||
// handleGetBlock implements the getblock command.
|
// handleGetBlock implements the getblock command.
|
||||||
func handleGetBlock(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
func handleGetBlock(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
||||||
c := cmd.(*btcjson.GetBlockCmd)
|
c := cmd.(*btcjson.GetBlockCmd)
|
||||||
|
@ -734,15 +748,10 @@ func handleGetBlock(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byt
|
||||||
// When the verbose flag isn't set, simply return the network-serialized
|
// When the verbose flag isn't set, simply return the network-serialized
|
||||||
// block as a hex-encoded string.
|
// block as a hex-encoded string.
|
||||||
if !c.Verbose {
|
if !c.Verbose {
|
||||||
var wireBuf bytes.Buffer
|
blkHex, err := messageToHex(blk.MsgBlock())
|
||||||
err := blk.MsgBlock().BtcEncode(&wireBuf, btcwire.ProtocolVersion)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, btcjson.Error{
|
return nil, err
|
||||||
Code: btcjson.ErrInternal.Code,
|
|
||||||
Message: err.Error(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
blkHex := hex.EncodeToString(wireBuf.Bytes())
|
|
||||||
return blkHex, nil
|
return blkHex, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -983,15 +992,14 @@ func handleGetRawTransaction(s *rpcServer, cmd btcjson.Cmd, walletNotification c
|
||||||
return *rawTxn, nil
|
return *rawTxn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createTxRawResult
|
||||||
func createTxRawResult(net btcwire.BitcoinNet, txSha string, mtx *btcwire.MsgTx, blk *btcutil.Block, maxidx int64, blksha *btcwire.ShaHash, verbose bool) (*btcjson.TxRawResult, *btcjson.Error) {
|
func createTxRawResult(net btcwire.BitcoinNet, txSha string, mtx *btcwire.MsgTx, blk *btcutil.Block, maxidx int64, blksha *btcwire.ShaHash, verbose bool) (*btcjson.TxRawResult, *btcjson.Error) {
|
||||||
tx := btcutil.NewTx(mtx)
|
tx := btcutil.NewTx(mtx)
|
||||||
|
|
||||||
var buf bytes.Buffer
|
mtxHex, err := messageToHex(mtx)
|
||||||
err := mtx.BtcEncode(&buf, btcwire.ProtocolVersion)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, &btcjson.Error{Code: btcjson.ErrInternal.Code, Message: err.Error()}
|
return nil, err
|
||||||
}
|
}
|
||||||
mtxHex := hex.EncodeToString(buf.Bytes())
|
|
||||||
|
|
||||||
if !verbose {
|
if !verbose {
|
||||||
return &btcjson.TxRawResult{Hex: mtxHex}, nil
|
return &btcjson.TxRawResult{Hex: mtxHex}, nil
|
||||||
|
@ -999,7 +1007,6 @@ func createTxRawResult(net btcwire.BitcoinNet, txSha string, mtx *btcwire.MsgTx,
|
||||||
|
|
||||||
txOutList := mtx.TxOut
|
txOutList := mtx.TxOut
|
||||||
voutList := make([]btcjson.Vout, len(txOutList))
|
voutList := make([]btcjson.Vout, len(txOutList))
|
||||||
|
|
||||||
txInList := mtx.TxIn
|
txInList := mtx.TxIn
|
||||||
vinList := make([]btcjson.Vin, len(txInList))
|
vinList := make([]btcjson.Vin, len(txInList))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue