Skip to content

Commit

Permalink
Add timestamp to updateHeight txn db call
Browse files Browse the repository at this point in the history
  • Loading branch information
cpacia committed May 15, 2018
1 parent 050dde3 commit b276b39
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
6 changes: 3 additions & 3 deletions db/txns.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,19 @@ func (t *TxnsDB) Delete(txid *chainhash.Hash) error {
return nil
}

func (t *TxnsDB) UpdateHeight(txid chainhash.Hash, height int) error {
func (t *TxnsDB) UpdateHeight(txid chainhash.Hash, height int, timestamp time.Time) error {
t.lock.Lock()
defer t.lock.Unlock()
tx, err := t.db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare("update txns set height=? where txid=?")
stmt, err := tx.Prepare("update txns set height=? timestamp=? where txid=?")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(height, txid.String())
_, err = stmt.Exec(height, int(timestamp.Unix()), txid.String())
if err != nil {
tx.Rollback()
return err
Expand Down
3 changes: 2 additions & 1 deletion mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,13 @@ func (m *mockTxnStore) GetAll(includeWatchOnly bool) ([]wallet.Txn, error) {
return txns, nil
}

func (m *mockTxnStore) UpdateHeight(txid chainhash.Hash, height int) error {
func (m *mockTxnStore) UpdateHeight(txid chainhash.Hash, height int, timestamp time.Time) error {
txn, ok := m.txns[txid.String()]
if !ok {
return errors.New("Not found")
}
txn.Height = int32(height)
txn.Timestamp = timestamp
return nil
}

Expand Down
8 changes: 5 additions & 3 deletions txstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ func (ts *TxStore) Ingest(tx *wire.MsgTx, height int32) (uint32, error) {
shouldCallback := false
if err != nil {
cb.Value = value
// TODO: use blocktime if height > 0
txn.Timestamp = time.Now()
shouldCallback = true
var buf bytes.Buffer
Expand All @@ -355,7 +356,8 @@ func (ts *TxStore) Ingest(tx *wire.MsgTx, height int32) (uint32, error) {
// Let's check the height before committing so we don't allow rogue peers to send us a lose
// tx that resets our height to zero.
if txn.Height <= 0 {
ts.Txns().UpdateHeight(tx.TxHash(), int(height))
// TODO: should pass in blocktime here
ts.Txns().UpdateHeight(tx.TxHash(), int(height), txn.Timestamp)
ts.txids[tx.TxHash().String()] = height
if height > 0 {
cb.Value = txn.Value
Expand Down Expand Up @@ -385,7 +387,7 @@ func (ts *TxStore) markAsDead(txid chainhash.Hash) error {
if err != nil {
return err
}
err = ts.Txns().UpdateHeight(s.SpendTxid, -1)
err = ts.Txns().UpdateHeight(s.SpendTxid, -1, time.Now())
if err != nil {
return err
}
Expand Down Expand Up @@ -424,7 +426,7 @@ func (ts *TxStore) markAsDead(txid chainhash.Hash) error {
}
}
}
ts.Txns().UpdateHeight(txid, -1)
ts.Txns().UpdateHeight(txid, -1, time.Now())
return nil
}

Expand Down

0 comments on commit b276b39

Please sign in to comment.