From b64d76d8f92ceee862a8a795f9b82327ae482cd1 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Wed, 3 Apr 2019 14:08:36 +0200 Subject: [PATCH] Show first seen date of mempool transaction in explorer --- api/worker.go | 4 ++++ api/xpub.go | 26 +++++++++++++-------- bchain/coins/blockchain.go | 4 ++++ bchain/types.go | 1 + static/templates/txdetail.html | 4 +--- static/templates/txdetail_ethereumtype.html | 4 +--- 6 files changed, 27 insertions(+), 16 deletions(-) diff --git a/api/worker.go b/api/worker.go index b19b1e11..8209c83b 100644 --- a/api/worker.go +++ b/api/worker.go @@ -294,6 +294,10 @@ func (w *Worker) GetTransactionFromBchainTx(bchainTx *bchain.Tx, height uint32, return nil, err } } + // for mempool transaction get first seen time + if bchainTx.Confirmations == 0 { + bchainTx.Blocktime = int64(w.mempool.GetTransactionTime(bchainTx.Txid)) + } r := &Tx{ Blockhash: blockhash, Blockheight: int(height), diff --git a/api/xpub.go b/api/xpub.go index 7508f24a..664a12ff 100644 --- a/api/xpub.go +++ b/api/xpub.go @@ -383,13 +383,13 @@ func (w *Worker) GetXpubAddress(xpub string, page int, txsOnPage int, option Acc return nil, err } // setup filtering of txids - var useTxids func(txid *xpubTxid, ad *xpubAddress) bool + var txidFilter func(txid *xpubTxid, ad *xpubAddress) bool if !(filter.FromHeight == 0 && filter.ToHeight == 0 && filter.Vout == AddressFilterVoutOff) { toHeight := maxUint32 if filter.ToHeight != 0 { toHeight = filter.ToHeight } - useTxids = func(txid *xpubTxid, ad *xpubAddress) bool { + txidFilter = func(txid *xpubTxid, ad *xpubAddress) bool { if txid.height < filter.FromHeight || txid.height > toHeight { return false } @@ -406,6 +406,7 @@ func (w *Worker) GetXpubAddress(xpub string, page int, txsOnPage int, option Acc // process mempool, only if ToHeight is not specified if filter.ToHeight == 0 && !filter.OnlyConfirmed { txmMap = make(map[string]*Tx) + mempoolEntries := make(bchain.MempoolTxidEntries, 0) for _, da := range [][]xpubAddress{data.addresses, data.changeAddresses} { for i := range da { ad := &da[i] @@ -432,18 +433,23 @@ func (w *Worker) GetXpubAddress(xpub string, page int, txsOnPage int, option Acc } uBalSat.Add(&uBalSat, tx.getAddrVoutValue(ad.addrDesc)) uBalSat.Sub(&uBalSat, tx.getAddrVinValue(ad.addrDesc)) - if page == 0 && !foundTx && (useTxids == nil || useTxids(&txid, ad)) { - if option == AccountDetailsTxidHistory { - txids = append(txids, tx.Txid) - } else if option >= AccountDetailsTxHistoryLight { - txs = append(txs, tx) - } + // mempool txs are returned only on the first page, uniquely and filtered + if page == 0 && !foundTx && (txidFilter == nil || txidFilter(&txid, ad)) { + mempoolEntries = append(mempoolEntries, bchain.MempoolTxidEntry{Txid: txid.txid, Time: uint32(tx.Blocktime)}) } } - } } } + // sort the entries by time descending + sort.Sort(mempoolEntries) + for _, entry := range mempoolEntries { + if option == AccountDetailsTxidHistory { + txids = append(txids, entry.Txid) + } else if option >= AccountDetailsTxHistoryLight { + txs = append(txs, txmMap[entry.Txid]) + } + } } if option >= AccountDetailsTxidHistory { txcMap := make(map[string]bool) @@ -459,7 +465,7 @@ func (w *Worker) GetXpubAddress(xpub string, page int, txsOnPage int, option Acc } // add tx only once if !added { - add := useTxids == nil || useTxids(&txid, ad) + add := txidFilter == nil || txidFilter(&txid, ad) txcMap[txid.txid] = add if add { txc = append(txc, txid) diff --git a/bchain/coins/blockchain.go b/bchain/coins/blockchain.go index 2567c77f..38224311 100644 --- a/bchain/coins/blockchain.go +++ b/bchain/coins/blockchain.go @@ -316,3 +316,7 @@ func (c *mempoolWithMetrics) GetAllEntries() (v bchain.MempoolTxidEntries) { defer func(s time.Time) { c.observeRPCLatency("GetAllEntries", s, nil) }(time.Now()) return c.mempool.GetAllEntries() } + +func (c *mempoolWithMetrics) GetTransactionTime(txid string) uint32 { + return c.mempool.GetTransactionTime(txid) +} diff --git a/bchain/types.go b/bchain/types.go index 58b1acf9..4c072574 100644 --- a/bchain/types.go +++ b/bchain/types.go @@ -291,4 +291,5 @@ type Mempool interface { GetTransactions(address string) ([]Outpoint, error) GetAddrDescTransactions(addrDesc AddressDescriptor) ([]Outpoint, error) GetAllEntries() MempoolTxidEntries + GetTransactionTime(txid string) uint32 } diff --git a/static/templates/txdetail.html b/static/templates/txdetail.html index 6b3a3f82..6e7327c8 100644 --- a/static/templates/txdetail.html +++ b/static/templates/txdetail.html @@ -4,9 +4,7 @@
{{$tx.Txid}}
- {{- if $tx.Confirmations -}} -
mined {{formatUnixTime $tx.Blocktime}}
- {{- end -}} +
{{if $tx.Confirmations}}mined{{else}}first seen{{end}} {{formatUnixTime $tx.Blocktime}}
diff --git a/static/templates/txdetail_ethereumtype.html b/static/templates/txdetail_ethereumtype.html index bb71a8cc..78cde7bd 100644 --- a/static/templates/txdetail_ethereumtype.html +++ b/static/templates/txdetail_ethereumtype.html @@ -5,9 +5,7 @@ {{$tx.Txid}} {{if eq $tx.EthereumSpecific.Status 1}}{{end}}{{if eq $tx.EthereumSpecific.Status 0}}{{end}}
- {{- if $tx.Confirmations -}} -
mined {{formatUnixTime $tx.Blocktime}}
- {{- end -}} +
{{if $tx.Confirmations}}mined{{else}}first seen{{end}} {{formatUnixTime $tx.Blocktime}}