From ce2decb2753db9453ff4616c2ef6afaf9f48e659 Mon Sep 17 00:00:00 2001 From: "Owain G. Ainsworth" Date: Thu, 23 Jan 2014 00:00:10 +0000 Subject: [PATCH] make TxStore use generic interface Means we can replace a bunch of type assertions with generic code. --- account.go | 24 +++++------------------- btcdrpc.go | 2 +- tx/tx.go | 21 ++++++++++++++------- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/account.go b/account.go index 82b9991..05028f6 100644 --- a/account.go +++ b/account.go @@ -247,15 +247,8 @@ func (a *Account) ListTransactions(from, count int) ([]map[string]interface{}, e lastLookupIdx := len(a.TxStore.s) - count // Search in reverse order: lookup most recently-added first. for i := len(a.TxStore.s) - 1; i >= from && i >= lastLookupIdx; i-- { - switch e := a.TxStore.s[i].(type) { - case *tx.SendTx: - infos := e.TxInfo(a.name, bs.Height, a.Net()) - txInfoList = append(txInfoList, infos...) - - case *tx.RecvTx: - info := e.TxInfo(a.name, bs.Height, a.Net()) - txInfoList = append(txInfoList, info) - } + txInfoList = append(txInfoList, + a.TxStore.s[i].TxInfo(a.name, bs.Height, a.Net())...) } a.mtx.RUnlock() a.TxStore.RUnlock() @@ -287,7 +280,7 @@ func (a *Account) ListAddressTransactions(pkHashes map[string]struct{}) ( } if _, ok := pkHashes[string(rtx.ReceiverHash[:])]; ok { info := rtx.TxInfo(a.name, bs.Height, a.Net()) - txInfoList = append(txInfoList, info) + txInfoList = append(txInfoList, info...) } } a.mtx.RUnlock() @@ -313,15 +306,8 @@ func (a *Account) ListAllTransactions() ([]map[string]interface{}, error) { // Search in reverse order: lookup most recently-added first. for i := len(a.TxStore.s) - 1; i >= 0; i-- { - switch e := a.TxStore.s[i].(type) { - case *tx.SendTx: - infos := e.TxInfo(a.name, bs.Height, a.Net()) - txInfoList = append(txInfoList, infos...) - - case *tx.RecvTx: - info := e.TxInfo(a.name, bs.Height, a.Net()) - txInfoList = append(txInfoList, info) - } + txInfoList = append(txInfoList, + a.TxStore.s[i].TxInfo(a.name, bs.Height, a.Net())...) } a.mtx.RUnlock() a.TxStore.RUnlock() diff --git a/btcdrpc.go b/btcdrpc.go index 51061d2..4ab5d29 100644 --- a/btcdrpc.go +++ b/btcdrpc.go @@ -389,7 +389,7 @@ func NtfnProcessedTx(n btcjson.Cmd, marshaled []byte) { // Notify frontends of new recv tx and mark as notified. NotifiedRecvTxChans.add <- *recvTxOP NotifyNewTxDetails(frontendNotificationMaster, a.Name(), t.TxInfo(a.Name(), - ptn.BlockHeight, a.Wallet.Net())) + ptn.BlockHeight, a.Wallet.Net())[0]) } if !ptn.Spent { diff --git a/tx/tx.go b/tx/tx.go index d0cc996..f609622 100644 --- a/tx/tx.go +++ b/tx/tx.go @@ -102,8 +102,15 @@ type OutPoint btcwire.OutPoint // of variable length. type PkScript []byte +// Tx is a generic type that can be used in place of either of the tx types in +// a TxStore. +type Tx interface { + io.WriterTo + ReadFromVersion(uint32, io.Reader) (int64, error) + TxInfo(string, int32, btcwire.BitcoinNet) []map[string]interface{} +} // TxStore is a slice holding RecvTx and SendTx pointers. -type TxStore []interface{} +type TxStore []Tx const ( addressUnknown byte = iota @@ -725,7 +732,7 @@ func (txs *TxStore) ReadFrom(r io.Reader) (int64, error) { vers := binary.LittleEndian.Uint32(versionBytes) read += int64(n) - store := []interface{}{} + store := []Tx{} defer func() { *txs = store }() @@ -742,7 +749,7 @@ func (txs *TxStore) ReadFrom(r io.Reader) (int64, error) { } read += n - var tx io.ReaderFrom + var tx Tx // Read tx. switch header { case recvTxHeader: @@ -788,7 +795,7 @@ func (txs *TxStore) WriteTo(w io.Writer) (int64, error) { } written = int64(n) - store := ([]interface{})(*txs) + store := ([]Tx)(*txs) for _, tx := range store { // Write header for tx. var header byte @@ -861,7 +868,7 @@ func (txs *TxStore) InsertRecvTx(tx *RecvTx) { // Correct results rely on txs being sorted by block height in // increasing order. func (txs *TxStore) Rollback(height int32, hash *btcwire.ShaHash) (modified bool) { - s := ([]interface{})(*txs) + s := ([]Tx)(*txs) // endlen specifies the final length of the rolled-back TxStore. // Past endlen, array elements are nilled. We do this instead of @@ -1023,7 +1030,7 @@ func (tx *RecvTx) WriteTo(w io.Writer) (n int64, err error) { // TxInfo returns a slice of maps that may be marshaled as a JSON array // of JSON objects for a listtransactions RPC reply. func (tx *RecvTx) TxInfo(account string, curheight int32, - net btcwire.BitcoinNet) map[string]interface{} { + net btcwire.BitcoinNet) []map[string]interface{} { address := "Unknown" addr, err := btcutil.NewAddressPubKeyHash(tx.ReceiverHash, net) @@ -1049,7 +1056,7 @@ func (tx *RecvTx) TxInfo(account string, curheight int32, txInfo["confirmations"] = 0 } - return txInfo + return []map[string]interface{}{txInfo} } func (tx *SendTx) ReadFromVersion(vers uint32, r io.Reader) (n int64, err error) {