Skip to content

Commit

Permalink
rpcserver: Optimize JSON raw tx input list create.
Browse files Browse the repository at this point in the history
This 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
  • Loading branch information
davecgh committed May 17, 2016
2 parents 9c238a8 + e4c053e commit 8ff81ff
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions rpcserver.go
Expand Up @@ -1296,27 +1296,37 @@ 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) []dcrjson.Vin {
// Coinbase transactions only have a single txin by definition.
vinList := make([]dcrjson.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
vinList[i].Tree = v.PreviousOutPoint.Tree
if blockchain.IsCoinBaseTx(mtx) {
txIn := mtx.TxIn[0]
vinEntry := &vinList[0]
vinEntry.Coinbase = hex.EncodeToString(txIn.SignatureScript)
vinEntry.Sequence = txIn.Sequence
vinEntry.AmountIn = txIn.ValueIn
vinEntry.BlockHeight = txIn.BlockHeight
vinEntry.BlockIndex = txIn.BlockIndex
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(dcrjson.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.Tree = txIn.PreviousOutPoint.Tree
vinEntry.Sequence = txIn.Sequence
vinEntry.AmountIn = txIn.ValueIn
vinEntry.BlockHeight = txIn.BlockHeight
vinEntry.BlockIndex = txIn.BlockIndex
vinEntry.ScriptSig = &dcrjson.ScriptSig{
Asm: disbuf,
Hex: hex.EncodeToString(txIn.SignatureScript),
}
vinList[i].AmountIn = v.ValueIn
vinList[i].BlockHeight = v.BlockHeight
vinList[i].BlockIndex = v.BlockIndex
vinList[i].Sequence = v.Sequence
}

return vinList
Expand All @@ -1325,7 +1335,6 @@ func createVinList(mtx *wire.MsgTx) []dcrjson.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) []dcrjson.VinPrevOut {

// Coinbase transactions only have a single txin by definition.
vinList := make([]dcrjson.VinPrevOut, len(mtx.TxIn))
if blockchain.IsCoinBaseTx(mtx) {
Expand Down

0 comments on commit 8ff81ff

Please sign in to comment.