rpcserver: Optimize JSON raw tx input list create.

This commit optimizes the createVinList function which is used to
generate the JSON list of transaction inputs.  It also makes it more
consistent with the createVinListPrevOut function.

In particular, it entails the following changes:
- Only do a single coinbase check and return right away instead of
  checking multiple times inside the loop over the inputs
- Use a pointer for populating the details of each entry to avoid
  multiple unnecessary array lookups and bounds checks
- Group all fields that populate the entry for better readability
This commit is contained in:
Dave Collins 2015-09-28 13:27:09 -05:00
parent c9ee3d9c5e
commit e4c053e504

View file

@ -616,23 +616,29 @@ func handleDebugLevel(s *rpcServer, cmd interface{}, closeChan <-chan struct{})
// createVinList returns a slice of JSON objects for the inputs of the passed
// transaction.
func createVinList(mtx *wire.MsgTx) []btcjson.Vin {
// Coinbase transactions only have a single txin by definition.
vinList := make([]btcjson.Vin, len(mtx.TxIn))
for i, v := range mtx.TxIn {
if blockchain.IsCoinBaseTx(mtx) {
vinList[i].Coinbase = hex.EncodeToString(v.SignatureScript)
} else {
vinList[i].Txid = v.PreviousOutPoint.Hash.String()
vinList[i].Vout = v.PreviousOutPoint.Index
if blockchain.IsCoinBaseTx(mtx) {
txIn := mtx.TxIn[0]
vinList[0].Coinbase = hex.EncodeToString(txIn.SignatureScript)
vinList[0].Sequence = txIn.Sequence
return vinList
}
// The disassembled string will contain [error] inline
// if the script doesn't fully parse, so ignore the
// error here.
disbuf, _ := txscript.DisasmString(v.SignatureScript)
vinList[i].ScriptSig = new(btcjson.ScriptSig)
vinList[i].ScriptSig.Asm = disbuf
vinList[i].ScriptSig.Hex = hex.EncodeToString(v.SignatureScript)
for i, txIn := range mtx.TxIn {
// The disassembled string will contain [error] inline
// if the script doesn't fully parse, so ignore the
// error here.
disbuf, _ := txscript.DisasmString(txIn.SignatureScript)
vinEntry := &vinList[i]
vinEntry.Txid = txIn.PreviousOutPoint.Hash.String()
vinEntry.Vout = txIn.PreviousOutPoint.Index
vinEntry.Sequence = txIn.Sequence
vinEntry.ScriptSig = &btcjson.ScriptSig{
Asm: disbuf,
Hex: hex.EncodeToString(txIn.SignatureScript),
}
vinList[i].Sequence = v.Sequence
}
return vinList
@ -641,7 +647,6 @@ func createVinList(mtx *wire.MsgTx) []btcjson.Vin {
// createVinList returns a slice of JSON objects for the inputs of the passed
// transaction.
func createVinListPrevOut(s *rpcServer, mtx *wire.MsgTx, chainParams *chaincfg.Params, vinExtra int) []btcjson.VinPrevOut {
// Coinbase transactions only have a single txin by definition.
vinList := make([]btcjson.VinPrevOut, len(mtx.TxIn))
if blockchain.IsCoinBaseTx(mtx) {