From d179af8ecfb40c4272a4acc999cef99c9f6d65b8 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Tue, 18 Mar 2014 15:31:06 -0500 Subject: [PATCH] Use min of now and blocktime for tx recv time. --- acctmgr.go | 11 ++++++++++- ntfns.go | 15 ++++++++------- rpcserver.go | 2 +- tx/tx.go | 33 ++++++++++++++------------------- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/acctmgr.go b/acctmgr.go index 3e72803..a2d0d31 100644 --- a/acctmgr.go +++ b/acctmgr.go @@ -24,6 +24,7 @@ import ( "github.com/conformal/btcwallet/tx" "github.com/conformal/btcwallet/wallet" "github.com/conformal/btcwire" + "time" ) // Errors relating to accounts. @@ -253,11 +254,19 @@ func (am *AccountManager) BlockNotify(bs *wallet.BlockStamp) { // the full information about the newly-mined tx, and the TxStore is // scheduled to be written to disk.. func (am *AccountManager) RecordSpendingTx(tx_ *btcutil.Tx, block *tx.BlockDetails) { + now := time.Now() + var created time.Time + if block != nil && now.After(block.Time) { + created = block.Time + } else { + created = now + } + for _, a := range am.AllAccounts() { // TODO(jrick): This needs to iterate through each txout's // addresses and find whether this account's keystore contains // any of the addresses this tx sends to. - a.TxStore.InsertSignedTx(tx_, block) + a.TxStore.InsertSignedTx(tx_, created, block) am.ds.ScheduleTxStoreWrite(a) } } diff --git a/ntfns.go b/ntfns.go index ed505ea..f4c7c3a 100644 --- a/ntfns.go +++ b/ntfns.go @@ -105,11 +105,18 @@ func NtfnRecvTx(n btcjson.Cmd) error { SendTxHistSyncChans.remove <- *tx_.Sha() } + now := time.Now() + var received time.Time + if block != nil && now.After(block.Time) { + received = block.Time + } else { + received = now + } + // For every output, find all accounts handling that output address (if any) // and record the received txout. for outIdx, txout := range tx_.MsgTx().TxOut { var accounts []*Account - var received time.Time _, addrs, _, _ := btcscript.ExtractPkScriptAddrs(txout.PkScript, cfg.Net()) for _, addr := range addrs { aname, err := LookupAccountByAddress(addr.EncodeAddress()) @@ -119,12 +126,6 @@ func NtfnRecvTx(n btcjson.Cmd) error { // This cannot reasonably fail if the above succeeded. a, _ := AcctMgr.Account(aname) accounts = append(accounts, a) - - if block != nil { - received = block.Time - } else { - received = time.Now() - } } for _, a := range accounts { diff --git a/rpcserver.go b/rpcserver.go index fce3347..c0f00b7 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -1365,7 +1365,7 @@ func SendBeforeReceiveHistorySync(add, done, remove chan btcwire.ShaHash, func handleSendRawTxReply(icmd btcjson.Cmd, txIDStr string, a *Account, txInfo *CreatedTx) (interface{}, *btcjson.Error) { // Add to transaction store. - stx, err := a.TxStore.InsertSignedTx(txInfo.tx, nil) + stx, err := a.TxStore.InsertSignedTx(txInfo.tx, time.Now(), nil) if err != nil { log.Warnf("Error adding sent tx history: %v", err) return nil, &btcjson.ErrInternal diff --git a/tx/tx.go b/tx/tx.go index 4ade4ad..11afa5c 100644 --- a/tx/tx.go +++ b/tx/tx.go @@ -375,20 +375,15 @@ func (s *Store) WriteTo(w io.Writer) (int64, error) { // store, returning the record. Duplicates and double spend correction is // handled automatically. Transactions may be added without block details, // and later added again with block details once the tx has been mined. -func (s *Store) InsertSignedTx(tx *btcutil.Tx, block *BlockDetails) (*SignedTx, error) { - var created time.Time - if block == nil { - created = time.Now() - } else { - created = block.Time - } +func (s *Store) InsertSignedTx(tx *btcutil.Tx, created time.Time, + block *BlockDetails) (*SignedTx, error) { // Partially create the signedTx. Everything is set except the // total btc input, which is set below. st := &signedTx{ - txSha: *tx.Sha(), - timeCreated: created, - block: block, + txSha: *tx.Sha(), + created: created, + block: block, } err := s.insertTx(tx, st) @@ -733,10 +728,10 @@ func (tx *msgTx) writeTo(w io.Writer) (int64, error) { } type signedTx struct { - txSha btcwire.ShaHash - timeCreated time.Time - totalIn int64 - block *BlockDetails // nil if unmined + txSha btcwire.ShaHash + created time.Time + totalIn int64 + block *BlockDetails // nil if unmined } func (st *signedTx) blockTx() blockTx { @@ -769,7 +764,7 @@ func (st *signedTx) readFrom(r io.Reader) (int64, error) { if err != nil { return n64, err } - st.timeCreated = time.Unix(int64(binary.LittleEndian.Uint64(timeBytes)), 0) + st.created = time.Unix(int64(binary.LittleEndian.Uint64(timeBytes)), 0) // Read total BTC in totalInBytes := make([]byte, 8) @@ -823,7 +818,7 @@ func (st *signedTx) writeTo(w io.Writer) (int64, error) { // Write creation time timeBytes := make([]byte, 8) - binary.LittleEndian.PutUint64(timeBytes, uint64(st.timeCreated.Unix())) + binary.LittleEndian.PutUint64(timeBytes, uint64(st.created.Unix())) n, err = w.Write(timeBytes) n64 += int64(n) if err != nil { @@ -867,7 +862,7 @@ func (st *signedTx) TxSha() *btcwire.ShaHash { } func (st *signedTx) Time() time.Time { - return st.timeCreated + return st.created } func (st *signedTx) setBlock(details *BlockDetails) { @@ -952,8 +947,8 @@ func (st *SignedTx) TxInfo(account string, chainHeight int32, net btcwire.Bitcoi "fee": float64(st.Fee()) / float64(btcutil.SatoshiPerBitcoin), "confirmations": float64(confirmations), "txid": st.txSha.String(), - "time": float64(st.timeCreated.Unix()), - "timereceived": float64(st.timeCreated.Unix()), + "time": float64(st.created.Unix()), + "timereceived": float64(st.created.Unix()), } if st.block != nil { info["blockhash"] = st.block.Hash.String()