Return address array of txids in api call GetAddress
This commit is contained in:
parent
63ad3ffff2
commit
2033dba169
@ -84,8 +84,8 @@ type Address struct {
|
|||||||
UnconfirmedBalance string `json:"unconfirmedBalance"`
|
UnconfirmedBalance string `json:"unconfirmedBalance"`
|
||||||
UnconfirmedTxApperances int `json:"unconfirmedTxApperances"`
|
UnconfirmedTxApperances int `json:"unconfirmedTxApperances"`
|
||||||
TxApperances int `json:"txApperances"`
|
TxApperances int `json:"txApperances"`
|
||||||
Transactions []*Tx `json:"transactions,omitempty"`
|
Transactions []*Tx `json:"txs,omitempty"`
|
||||||
Txids []string `json:"transactions,omitempty"` // this is intentional, we return either Transactions or Txids
|
Txids []string `json:"transactions,omitempty"`
|
||||||
Page int `json:"page"`
|
Page int `json:"page"`
|
||||||
TotalPages int `json:"totalPages"`
|
TotalPages int `json:"totalPages"`
|
||||||
TxsOnPage int `json:"txsOnPage"`
|
TxsOnPage int `json:"txsOnPage"`
|
||||||
|
|||||||
@ -126,7 +126,6 @@ func (w *Worker) GetTransaction(txid string, bestheight uint32, spendingTxs bool
|
|||||||
if spendingTxs && vout.Spent {
|
if spendingTxs && vout.Spent {
|
||||||
// find transaction that spent this output
|
// find transaction that spent this output
|
||||||
// there is not an index, it must be found in addresses -> txaddresses -> tx
|
// 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 {
|
err = w.db.GetAddrDescTransactions(vout.ScriptPubKey.AddrDesc, height, ^uint32(0), func(t string, index uint32, isOutput bool) error {
|
||||||
if isOutput == false {
|
if isOutput == false {
|
||||||
tsp, err := w.db.GetTxAddresses(t)
|
tsp, err := w.db.GetTxAddresses(t)
|
||||||
@ -321,18 +320,16 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids b
|
|||||||
}
|
}
|
||||||
txc = UniqueTxidsInReverse(txc)
|
txc = UniqueTxidsInReverse(txc)
|
||||||
var txm []string
|
var txm []string
|
||||||
// mempool only on the first page or if there are no confirmed transactions
|
// if there are only unconfirmed transactions, ba is nil
|
||||||
if page == 0 || ba == nil {
|
if ba == nil {
|
||||||
if ba == nil {
|
ba = &db.AddrBalance{}
|
||||||
ba = &db.AddrBalance{}
|
page = 0
|
||||||
page = 0
|
|
||||||
}
|
|
||||||
txm, err = w.getAddressTxids(addrDesc, true)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Annotatef(err, "getAddressTxids %v true", address)
|
|
||||||
}
|
|
||||||
txm = UniqueTxidsInReverse(txm)
|
|
||||||
}
|
}
|
||||||
|
txm, err = w.getAddressTxids(addrDesc, true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Annotatef(err, "getAddressTxids %v true", address)
|
||||||
|
}
|
||||||
|
txm = UniqueTxidsInReverse(txm)
|
||||||
// check if the address exist
|
// check if the address exist
|
||||||
if len(txc)+len(txm) == 0 {
|
if len(txc)+len(txm) == 0 {
|
||||||
return nil, NewApiError("Address not found", true)
|
return nil, NewApiError("Address not found", true)
|
||||||
@ -355,7 +352,13 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids b
|
|||||||
if to > len(txc) {
|
if to > len(txc) {
|
||||||
to = len(txc)
|
to = len(txc)
|
||||||
}
|
}
|
||||||
txs := make([]*Tx, len(txm)+to-from)
|
var txs []*Tx
|
||||||
|
var txids []string
|
||||||
|
if onlyTxids {
|
||||||
|
txids = make([]string, len(txm)+to-from)
|
||||||
|
} else {
|
||||||
|
txs = make([]*Tx, len(txm)+to-from)
|
||||||
|
}
|
||||||
txi := 0
|
txi := 0
|
||||||
// load mempool transactions
|
// load mempool transactions
|
||||||
var uBalSat big.Int
|
var uBalSat big.Int
|
||||||
@ -367,8 +370,14 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids b
|
|||||||
} else {
|
} else {
|
||||||
uBalSat.Add(&uBalSat, tx.getAddrVoutValue(addrDesc))
|
uBalSat.Add(&uBalSat, tx.getAddrVoutValue(addrDesc))
|
||||||
uBalSat.Sub(&uBalSat, tx.getAddrVinValue(addrDesc))
|
uBalSat.Sub(&uBalSat, tx.getAddrVinValue(addrDesc))
|
||||||
txs[txi] = tx
|
if page > 0 {
|
||||||
txi++
|
if onlyTxids {
|
||||||
|
txids[txi] = tx.Txid
|
||||||
|
} else {
|
||||||
|
txs[txi] = tx
|
||||||
|
}
|
||||||
|
txi++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(txc) != int(ba.Txs) {
|
if len(txc) != int(ba.Txs) {
|
||||||
@ -376,25 +385,32 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids b
|
|||||||
}
|
}
|
||||||
for i := from; i < to; i++ {
|
for i := from; i < to; i++ {
|
||||||
txid := txc[i]
|
txid := txc[i]
|
||||||
ta, err := w.db.GetTxAddresses(txid)
|
if onlyTxids {
|
||||||
if err != nil {
|
txids[txi] = txid
|
||||||
return nil, errors.Annotatef(err, "GetTxAddresses %v", txid)
|
} else {
|
||||||
|
ta, err := w.db.GetTxAddresses(txid)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Annotatef(err, "GetTxAddresses %v", txid)
|
||||||
|
}
|
||||||
|
if ta == nil {
|
||||||
|
glog.Warning("DB inconsistency: tx ", txid, ": not found in txAddresses")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
bi, err := w.db.GetBlockInfo(ta.Height)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Annotatef(err, "GetBlockInfo %v", ta.Height)
|
||||||
|
}
|
||||||
|
if bi == nil {
|
||||||
|
glog.Warning("DB inconsistency: block height ", ta.Height, ": not found in db")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
txs[txi] = w.txFromTxAddress(txid, ta, bi, bestheight)
|
||||||
}
|
}
|
||||||
if ta == nil {
|
|
||||||
glog.Warning("DB inconsistency: tx ", txid, ": not found in txAddresses")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
bi, err := w.db.GetBlockInfo(ta.Height)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Annotatef(err, "GetBlockInfo %v", ta.Height)
|
|
||||||
}
|
|
||||||
if bi == nil {
|
|
||||||
glog.Warning("DB inconsistency: block height ", ta.Height, ": not found in db")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
txs[txi] = w.txFromTxAddress(txid, ta, bi, bestheight)
|
|
||||||
txi++
|
txi++
|
||||||
}
|
}
|
||||||
|
if !onlyTxids {
|
||||||
|
txs = txs[:txi]
|
||||||
|
}
|
||||||
r := &Address{
|
r := &Address{
|
||||||
AddrStr: address,
|
AddrStr: address,
|
||||||
Balance: w.chainParser.AmountToDecimalString(&ba.BalanceSat),
|
Balance: w.chainParser.AmountToDecimalString(&ba.BalanceSat),
|
||||||
@ -403,8 +419,8 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids b
|
|||||||
TxApperances: len(txc),
|
TxApperances: len(txc),
|
||||||
UnconfirmedBalance: w.chainParser.AmountToDecimalString(&uBalSat),
|
UnconfirmedBalance: w.chainParser.AmountToDecimalString(&uBalSat),
|
||||||
UnconfirmedTxApperances: len(txm),
|
UnconfirmedTxApperances: len(txm),
|
||||||
Transactions: txs[:txi],
|
Transactions: txs,
|
||||||
Txids: nil,
|
Txids: txids,
|
||||||
Page: page,
|
Page: page,
|
||||||
TotalPages: totalPages,
|
TotalPages: totalPages,
|
||||||
TxsOnPage: txsOnPage,
|
TxsOnPage: txsOnPage,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user