kaspad/server/rpc/handle_get_tx_out.go
Dan Aharoni 47214121a7 [NOD-423] Implement get selected tip RPC command (#469)
* [NOD-423] Rename BestBlock to SelectedTip

* [NOD-423] Implement GetSelectedTip RPC command

* [NOD-423] Add help to getSelectedTip command

* [NOD-423] Fix getSelectedTip test

* [NOD-423] Fix tests so they would compile. These tests will need to be rewriten at some point.

* [NOD-423] Make integration test compile. Test need to be revisited

* [NOD-423] Rename variables

* [NOD-423] Change comment s about best block to selected tip.

* [NOD-423] Update comment

* [NOD-423] Change height to bluescore
2019-11-20 12:04:22 +02:00

125 lines
3.6 KiB
Go

package rpc
import (
"encoding/hex"
"fmt"
"github.com/daglabs/btcd/btcjson"
"github.com/daglabs/btcd/txscript"
"github.com/daglabs/btcd/util"
"github.com/daglabs/btcd/util/daghash"
"github.com/daglabs/btcd/wire"
)
// handleGetTxOut handles getTxOut commands.
func handleGetTxOut(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
c := cmd.(*btcjson.GetTxOutCmd)
// Convert the provided transaction hash hex to a Hash.
txID, err := daghash.NewTxIDFromStr(c.TxID)
if err != nil {
return nil, rpcDecodeHexError(c.TxID)
}
// If requested and the tx is available in the mempool try to fetch it
// from there, otherwise attempt to fetch from the block database.
var selectedTipHash string
var confirmations *uint64
var value uint64
var scriptPubKey []byte
var isCoinbase bool
isInMempool := false
includeMempool := true
if c.IncludeMempool != nil {
includeMempool = *c.IncludeMempool
}
// TODO: This is racy. It should attempt to fetch it directly and check
// the error.
if includeMempool && s.cfg.TxMemPool.HaveTransaction(txID) {
tx, err := s.cfg.TxMemPool.FetchTransaction(txID)
if err != nil {
return nil, rpcNoTxInfoError(txID)
}
mtx := tx.MsgTx()
if c.Vout > uint32(len(mtx.TxOut)-1) {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCInvalidTxVout,
Message: "Output index number (vout) does not " +
"exist for transaction.",
}
}
txOut := mtx.TxOut[c.Vout]
if txOut == nil {
errStr := fmt.Sprintf("Output index: %d for txid: %s "+
"does not exist", c.Vout, txID)
return nil, internalRPCError(errStr, "")
}
selectedTipHash = s.cfg.DAG.SelectedTipHash().String()
value = txOut.Value
scriptPubKey = txOut.ScriptPubKey
isCoinbase = mtx.IsCoinBase()
isInMempool = true
} else {
out := wire.Outpoint{TxID: *txID, Index: c.Vout}
entry, ok := s.cfg.DAG.GetUTXOEntry(out)
if !ok {
return nil, rpcNoTxInfoError(txID)
}
// To match the behavior of the reference client, return nil
// (JSON null) if the transaction output is spent by another
// transaction already in the main chain. Mined transactions
// that are spent by a mempool transaction are not affected by
// this.
if entry == nil {
return nil, nil
}
if s.cfg.TxIndex != nil {
txConfirmations, err := txConfirmations(s, txID)
if err != nil {
return nil, internalRPCError("Output index number (vout) does not "+
"exist for transaction.", "")
}
confirmations = &txConfirmations
}
selectedTipHash = s.cfg.DAG.SelectedTipHash().String()
value = entry.Amount()
scriptPubKey = entry.ScriptPubKey()
isCoinbase = entry.IsCoinbase()
}
// Disassemble script into single line printable format.
// The disassembled string will contain [error] inline if the script
// doesn't fully parse, so ignore the error here.
disbuf, _ := txscript.DisasmString(scriptPubKey)
// Get further info about the script.
// Ignore the error here since an error means the script couldn't parse
// and there is no additional information about it anyways.
scriptClass, addr, _ := txscript.ExtractScriptPubKeyAddress(scriptPubKey,
s.cfg.DAGParams)
var address *string
if addr != nil {
address = btcjson.String(addr.EncodeAddress())
}
txOutReply := &btcjson.GetTxOutResult{
SelectedTip: selectedTipHash,
Confirmations: confirmations,
IsInMempool: isInMempool,
Value: util.Amount(value).ToBTC(),
ScriptPubKey: btcjson.ScriptPubKeyResult{
Asm: disbuf,
Hex: hex.EncodeToString(scriptPubKey),
Type: scriptClass.String(),
Address: address,
},
Coinbase: isCoinbase,
}
return txOutReply, nil
}