From e4c053e5047c1e55d869372cf5fd1362cfad67b1 Mon Sep 17 00:00:00 2001 From: Dave Collins <davec@conformal.com> Date: Mon, 28 Sep 2015 13:27:09 -0500 Subject: [PATCH] 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 --- rpcserver.go | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/rpcserver.go b/rpcserver.go index a021f29b..7fc4a669 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -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) {