diff --git a/api/worker.go b/api/worker.go index 05a9aca9..7d451dd1 100644 --- a/api/worker.go +++ b/api/worker.go @@ -45,6 +45,7 @@ func (w *Worker) getAddressesFromVout(vout *bchain.Vout) (bchain.AddressDescript // GetTransaction reads transaction data from txid func (w *Worker) GetTransaction(txid string, bestheight uint32, spendingTxs bool) (*Tx, error) { + start := time.Now() bchainTx, height, err := w.txCache.GetTransaction(txid, bestheight) if err != nil { return nil, NewApiError(fmt.Sprintf("Tx not found, %v", err), true) @@ -123,7 +124,39 @@ func (w *Worker) GetTransaction(txid string, bestheight uint32, spendingTxs bool if ta != nil { vout.Spent = ta.Outputs[i].Spent if spendingTxs && vout.Spent { - // TODO + // find transaction that spent this output + // there is not an index, it must be found in addresses -> txaddresses -> tx + // given that each step is more and more selective, it is not + err = w.db.GetAddrDescTransactions(vout.ScriptPubKey.AddrDesc, height, ^uint32(0), func(t string, index uint32, isOutput bool) error { + if isOutput == false { + tsp, err := w.db.GetTxAddresses(t) + if err != nil { + glog.Warning("DB inconsistency: tx ", t, ": not found in txAddresses") + } else { + if len(tsp.Inputs) > int(index) { + if tsp.Inputs[index].ValueSat.Cmp(&vout.ValueSat) == 0 { + spentTx, spentHeight, err := w.txCache.GetTransaction(t, bestheight) + if err != nil { + glog.Warning("Tx ", t, ": not found") + } else { + if len(spentTx.Vin) > int(index) { + if spentTx.Vin[index].Txid == bchainTx.Txid { + vout.SpentTxID = t + vout.SpentHeight = int(spentHeight) + vout.SpentIndex = int(index) + return &db.StopIteration{} + } + } + } + } + } + } + } + return nil + }) + if err != nil { + glog.Errorf("GetAddrDescTransactions error %v, %v, output %v", err, vout.ScriptPubKey.AddrDesc) + } } } } @@ -150,6 +183,7 @@ func (w *Worker) GetTransaction(txid string, bestheight uint32, spendingTxs bool Vin: vins, Vout: vouts, } + glog.Info("GetTransaction ", txid, " finished in ", time.Since(start)) return r, nil } @@ -238,6 +272,7 @@ func (w *Worker) txFromTxAddress(txid string, ta *db.TxAddresses, bi *db.BlockIn if err != nil { glog.Errorf("tai.Addresses error %v, tx %v, output %v, tao %+v", err, txid, i, tao) } + vout.Spent = tao.Spent } // for coinbase transactions valIn is 0 feesSat.Sub(&valInSat, &valOutSat) @@ -374,6 +409,6 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids b TotalPages: totalPages, TxsOnPage: txsOnPage, } - glog.Info(address, " finished in ", time.Since(start)) + glog.Info("GetAddress ", address, " finished in ", time.Since(start)) return r, nil } diff --git a/static/templates/txdetail.html b/static/templates/txdetail.html index ac22828b..9fafc7ec 100644 --- a/static/templates/txdetail.html +++ b/static/templates/txdetail.html @@ -4,30 +4,30 @@
{{$tx.Txid}}
- {{if $tx.Confirmations}} + {{- if $tx.Confirmations -}}
mined {{formatUnixTime $tx.Blocktime}}
- {{end}} + {{- end -}}
- {{range $vin := $tx.Vin}} + {{- range $vin := $tx.Vin -}} - {{end}} + {{- end -}}
- {{range $a := $vin.Addresses}} + {{- range $a := $vin.Addresses}} {{if and (ne $a $addr) $vin.Searchable}}{{$a}}{{else}}{{$a}}{{end}} - {{else}} + {{- else -}} No Inputs (Newly Generated Coins) - {{end}}{{if $vin.Addresses}} + {{- end -}}{{- if $vin.Addresses -}} {{formatAmount $vin.Value}} {{$cs}} - {{end}} + {{- end -}}
@@ -41,27 +41,24 @@
- {{range $vout := $tx.Vout}} + {{- range $vout := $tx.Vout -}} - {{end}} + {{- end -}}
- {{range $a := $vout.ScriptPubKey.Addresses}} + {{- range $a := $vout.ScriptPubKey.Addresses -}} - {{if and (ne $a $addr) $vout.ScriptPubKey.Searchable}}{{$a}}{{else}}{{$a}}{{end}} + {{- if and (ne $a $addr) $vout.ScriptPubKey.Searchable}}{{$a}}{{else}}{{$a}}{{- end -}} - {{else}} + {{- else -}} Unparsed address - {{end}} + {{- end -}} - {{formatAmount $vout.Value}} {{$cs}} - {{if $vout.Spent}} - (S) - {{else}} + {{formatAmount $vout.Value}} {{$cs}}{{if $vout.Spent}}{{if $vout.SpentTxID}}(S){{else}}(S){{end}}{{else -}} (U) - {{end}} + {{- end -}}
@@ -69,16 +66,16 @@
- {{if $tx.Fees}} + {{- if $tx.Fees -}} Fee: {{formatAmount $tx.Fees}} {{$cs}} - {{end}} + {{- end -}}
- {{if $tx.Confirmations}} + {{- if $tx.Confirmations -}} {{$tx.Confirmations}} Confirmations - {{else}} + {{- else -}} Unconfirmed Transaction! - {{end}} + {{- end -}} {{formatAmount $tx.ValueOut}} {{$cs}}