Use min of now and blocktime for tx recv time.

This commit is contained in:
Josh Rickmar 2014-03-18 15:31:06 -05:00
parent 998a29b0e6
commit d179af8ecf
4 changed files with 33 additions and 28 deletions

View file

@ -24,6 +24,7 @@ import (
"github.com/conformal/btcwallet/tx" "github.com/conformal/btcwallet/tx"
"github.com/conformal/btcwallet/wallet" "github.com/conformal/btcwallet/wallet"
"github.com/conformal/btcwire" "github.com/conformal/btcwire"
"time"
) )
// Errors relating to accounts. // 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 // the full information about the newly-mined tx, and the TxStore is
// scheduled to be written to disk.. // scheduled to be written to disk..
func (am *AccountManager) RecordSpendingTx(tx_ *btcutil.Tx, block *tx.BlockDetails) { 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() { for _, a := range am.AllAccounts() {
// TODO(jrick): This needs to iterate through each txout's // TODO(jrick): This needs to iterate through each txout's
// addresses and find whether this account's keystore contains // addresses and find whether this account's keystore contains
// any of the addresses this tx sends to. // any of the addresses this tx sends to.
a.TxStore.InsertSignedTx(tx_, block) a.TxStore.InsertSignedTx(tx_, created, block)
am.ds.ScheduleTxStoreWrite(a) am.ds.ScheduleTxStoreWrite(a)
} }
} }

View file

@ -105,11 +105,18 @@ func NtfnRecvTx(n btcjson.Cmd) error {
SendTxHistSyncChans.remove <- *tx_.Sha() 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) // For every output, find all accounts handling that output address (if any)
// and record the received txout. // and record the received txout.
for outIdx, txout := range tx_.MsgTx().TxOut { for outIdx, txout := range tx_.MsgTx().TxOut {
var accounts []*Account var accounts []*Account
var received time.Time
_, addrs, _, _ := btcscript.ExtractPkScriptAddrs(txout.PkScript, cfg.Net()) _, addrs, _, _ := btcscript.ExtractPkScriptAddrs(txout.PkScript, cfg.Net())
for _, addr := range addrs { for _, addr := range addrs {
aname, err := LookupAccountByAddress(addr.EncodeAddress()) aname, err := LookupAccountByAddress(addr.EncodeAddress())
@ -119,12 +126,6 @@ func NtfnRecvTx(n btcjson.Cmd) error {
// This cannot reasonably fail if the above succeeded. // This cannot reasonably fail if the above succeeded.
a, _ := AcctMgr.Account(aname) a, _ := AcctMgr.Account(aname)
accounts = append(accounts, a) accounts = append(accounts, a)
if block != nil {
received = block.Time
} else {
received = time.Now()
}
} }
for _, a := range accounts { for _, a := range accounts {

View file

@ -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) { func handleSendRawTxReply(icmd btcjson.Cmd, txIDStr string, a *Account, txInfo *CreatedTx) (interface{}, *btcjson.Error) {
// Add to transaction store. // 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 { if err != nil {
log.Warnf("Error adding sent tx history: %v", err) log.Warnf("Error adding sent tx history: %v", err)
return nil, &btcjson.ErrInternal return nil, &btcjson.ErrInternal

View file

@ -375,20 +375,15 @@ func (s *Store) WriteTo(w io.Writer) (int64, error) {
// store, returning the record. Duplicates and double spend correction is // store, returning the record. Duplicates and double spend correction is
// handled automatically. Transactions may be added without block details, // handled automatically. Transactions may be added without block details,
// and later added again with block details once the tx has been mined. // and later added again with block details once the tx has been mined.
func (s *Store) InsertSignedTx(tx *btcutil.Tx, block *BlockDetails) (*SignedTx, error) { func (s *Store) InsertSignedTx(tx *btcutil.Tx, created time.Time,
var created time.Time block *BlockDetails) (*SignedTx, error) {
if block == nil {
created = time.Now()
} else {
created = block.Time
}
// Partially create the signedTx. Everything is set except the // Partially create the signedTx. Everything is set except the
// total btc input, which is set below. // total btc input, which is set below.
st := &signedTx{ st := &signedTx{
txSha: *tx.Sha(), txSha: *tx.Sha(),
timeCreated: created, created: created,
block: block, block: block,
} }
err := s.insertTx(tx, st) err := s.insertTx(tx, st)
@ -733,10 +728,10 @@ func (tx *msgTx) writeTo(w io.Writer) (int64, error) {
} }
type signedTx struct { type signedTx struct {
txSha btcwire.ShaHash txSha btcwire.ShaHash
timeCreated time.Time created time.Time
totalIn int64 totalIn int64
block *BlockDetails // nil if unmined block *BlockDetails // nil if unmined
} }
func (st *signedTx) blockTx() blockTx { func (st *signedTx) blockTx() blockTx {
@ -769,7 +764,7 @@ func (st *signedTx) readFrom(r io.Reader) (int64, error) {
if err != nil { if err != nil {
return n64, err 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 // Read total BTC in
totalInBytes := make([]byte, 8) totalInBytes := make([]byte, 8)
@ -823,7 +818,7 @@ func (st *signedTx) writeTo(w io.Writer) (int64, error) {
// Write creation time // Write creation time
timeBytes := make([]byte, 8) 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) n, err = w.Write(timeBytes)
n64 += int64(n) n64 += int64(n)
if err != nil { if err != nil {
@ -867,7 +862,7 @@ func (st *signedTx) TxSha() *btcwire.ShaHash {
} }
func (st *signedTx) Time() time.Time { func (st *signedTx) Time() time.Time {
return st.timeCreated return st.created
} }
func (st *signedTx) setBlock(details *BlockDetails) { 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), "fee": float64(st.Fee()) / float64(btcutil.SatoshiPerBitcoin),
"confirmations": float64(confirmations), "confirmations": float64(confirmations),
"txid": st.txSha.String(), "txid": st.txSha.String(),
"time": float64(st.timeCreated.Unix()), "time": float64(st.created.Unix()),
"timereceived": float64(st.timeCreated.Unix()), "timereceived": float64(st.created.Unix()),
} }
if st.block != nil { if st.block != nil {
info["blockhash"] = st.block.Hash.String() info["blockhash"] = st.block.Hash.String()