Skip to content

Commit

Permalink
multi-account: update listtransactions
Browse files Browse the repository at this point in the history
  • Loading branch information
roylee17 committed Sep 28, 2022
1 parent 4acd03b commit 2e8dcc4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
17 changes: 2 additions & 15 deletions rpc/legacyrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -1487,23 +1487,10 @@ func listSinceBlock(icmd interface{}, w *wallet.Wallet, chainClient *chain.RPCCl
// listTransactions handles a listtransactions request by returning an
// array of maps with details of sent and recevied wallet transactions.
func listTransactions(icmd interface{}, w *wallet.Wallet) (interface{}, error) {
cmd := icmd.(*btcjson.ListTransactionsCmd)

// TODO: ListTransactions does not currently understand the difference
// between transactions pertaining to one account from another. This
// will be resolved when wtxmgr is combined with the waddrmgr namespace.

if cmd.Account != nil && *cmd.Account != "*" && *cmd.Account != "default" {
// For now, don't bother trying to continue if the user
// specified an account, since this can't be (easily or
// efficiently) calculated.
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCWallet,
Message: "Transactions are not yet grouped by account",
}
}
cmd := icmd.(*btcjson.ListTransactionsCmd)

return w.ListTransactions(*cmd.From, *cmd.Count)
return w.ListTransactions(*cmd.Account, *cmd.From, *cmd.Count)
}

// listAddressTransactions handles a listaddresstransactions request by
Expand Down
34 changes: 18 additions & 16 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -1916,7 +1916,8 @@ func RecvCategory(details *wtxmgr.TxDetails, syncHeight int32, net *chaincfg.Par
// for a listtransactions RPC.
//
// TODO: This should be moved to the legacyrpc package.
func listTransactions(tx walletdb.ReadTx, details *wtxmgr.TxDetails, addrMgr *waddrmgr.Manager,
func listTransactions(accountName string, tx walletdb.ReadTx,
details *wtxmgr.TxDetails, addrMgr *waddrmgr.Manager,
syncHeight int32, net *chaincfg.Params) []btcjson.ListTransactionsResult {

addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
Expand Down Expand Up @@ -1977,19 +1978,22 @@ outputs:
}

var address string
var accountName string
var name string
_, addrs, _, _ := txscript.ExtractPkScriptAddrs(output.PkScript, net)
if len(addrs) == 1 {
addr := addrs[0]
address = addr.EncodeAddress()
mgr, account, err := addrMgr.AddrAccount(addrmgrNs, addrs[0])
if err == nil {
accountName, err = mgr.AccountName(addrmgrNs, account)
name, err = mgr.AccountName(addrmgrNs, account)
if err != nil {
accountName = ""
name = ""
}
}
}
if accountName != "*" && accountName != name {
continue
}

amountF64 := btcutil.Amount(output.Value).ToBTC()
result := btcjson.ListTransactionsResult{
Expand All @@ -1998,7 +2002,7 @@ outputs:
// BlockIndex
//
// Fields set below:
// Account (only for non-"send" categories)
// Account
// Category
// Amount
// Fee
Expand All @@ -2025,13 +2029,14 @@ outputs:
// with debits are grouped under the send category.

if send || spentCredit {
result.Account = name
result.Category = "send"
result.Amount = -amountF64
result.Fee = &feeF64
results = append(results, result)
}
if isCredit {
result.Account = accountName
result.Account = name
result.Category = recvCat
result.Amount = amountF64
result.Fee = nil
Expand Down Expand Up @@ -2070,7 +2075,7 @@ func (w *Wallet) ListSinceBlock(start, end, syncHeight int32) ([]btcjson.ListTra
// ListTransactions returns a slice of objects with details about a recorded
// transaction. This is intended to be used for listtransactions RPC
// replies.
func (w *Wallet) ListTransactions(from, count int) ([]btcjson.ListTransactionsResult, error) {
func (w *Wallet) ListTransactions(accountName string, from, count int) ([]btcjson.ListTransactionsResult, error) {
txList := []btcjson.ListTransactionsResult{}

err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
Expand All @@ -2083,7 +2088,6 @@ func (w *Wallet) ListTransactions(from, count int) ([]btcjson.ListTransactionsRe
// Need to skip the first from transactions, and after those, only
// include the next count transactions.
skipped := 0
n := 0
rangeFn := func(details []wtxmgr.TxDetails) (bool, error) {

for _, detail := range details {
Expand All @@ -2092,18 +2096,16 @@ func (w *Wallet) ListTransactions(from, count int) ([]btcjson.ListTransactionsRe
continue
}

n++
if n > count {
if len(txList) >= count {
txList = txList[:count]
return true, nil
}

jsonResults := listTransactions(tx, &detail,
w.Manager, syncBlock.Height, w.chainParams)
txList = append(txList, jsonResults...)
jsonResults := listTransactions(accountName,
tx, &detail, w.Manager,
syncBlock.Height, w.chainParams)

if len(jsonResults) > 0 {
n++
}
txList = append(txList, jsonResults...)
}

return false, nil
Expand Down

0 comments on commit 2e8dcc4

Please sign in to comment.