API updates for times in block notifications.

This commit is contained in:
Josh Rickmar 2015-06-18 12:29:13 -04:00
parent a735e3c3e2
commit e5e239e124
5 changed files with 58 additions and 32 deletions

View file

@ -153,11 +153,11 @@ type (
// BlockConnected is a notification for a newly-attached block to the // BlockConnected is a notification for a newly-attached block to the
// best chain. // best chain.
BlockConnected waddrmgr.BlockStamp BlockConnected wtxmgr.BlockMeta
// BlockDisconnected is a notifcation that the block described by the // BlockDisconnected is a notifcation that the block described by the
// BlockStamp was reorganized out of the best chain. // BlockStamp was reorganized out of the best chain.
BlockDisconnected waddrmgr.BlockStamp BlockDisconnected wtxmgr.BlockMeta
// RelevantTx is a notification for a transaction which spends wallet // RelevantTx is a notification for a transaction which spends wallet
// inputs or pays to a watched address. // inputs or pays to a watched address.
@ -228,12 +228,24 @@ func (c *Client) onClientConnect() {
c.enqueueNotification <- ClientConnected{} c.enqueueNotification <- ClientConnected{}
} }
func (c *Client) onBlockConnected(hash *wire.ShaHash, height int32) { func (c *Client) onBlockConnected(hash *wire.ShaHash, height int32, time time.Time) {
c.enqueueNotification <- BlockConnected{Hash: *hash, Height: height} c.enqueueNotification <- BlockConnected{
Block: wtxmgr.Block{
Hash: *hash,
Height: height,
},
Time: time,
}
} }
func (c *Client) onBlockDisconnected(hash *wire.ShaHash, height int32) { func (c *Client) onBlockDisconnected(hash *wire.ShaHash, height int32, time time.Time) {
c.enqueueNotification <- BlockDisconnected{Hash: *hash, Height: height} c.enqueueNotification <- BlockDisconnected{
Block: wtxmgr.Block{
Hash: *hash,
Height: height,
},
Time: time,
}
} }
func (c *Client) onRecvTx(tx *btcutil.Tx, block *btcjson.BlockDetails) { func (c *Client) onRecvTx(tx *btcutil.Tx, block *btcjson.BlockDetails) {
@ -310,7 +322,10 @@ out:
case dequeue <- next: case dequeue <- next:
if n, ok := next.(BlockConnected); ok { if n, ok := next.(BlockConnected); ok {
bs = (*waddrmgr.BlockStamp)(&n) bs = &waddrmgr.BlockStamp{
n.Height,
n.Hash,
}
} }
notifications[0] = nil notifications[0] = nil

View file

@ -272,8 +272,8 @@ type rpcServer struct {
// Channels read from other components from which notifications are // Channels read from other components from which notifications are
// created. // created.
connectedBlocks <-chan waddrmgr.BlockStamp connectedBlocks <-chan wtxmgr.BlockMeta
disconnectedBlocks <-chan waddrmgr.BlockStamp disconnectedBlocks <-chan wtxmgr.BlockMeta
relevantTxs <-chan chain.RelevantTx relevantTxs <-chan chain.RelevantTx
managerLocked <-chan bool managerLocked <-chan bool
confirmedBalance <-chan btcutil.Amount confirmedBalance <-chan btcutil.Amount
@ -959,8 +959,8 @@ type (
notificationCmds(w *wallet.Wallet) []interface{} notificationCmds(w *wallet.Wallet) []interface{}
} }
blockConnected waddrmgr.BlockStamp blockConnected wtxmgr.BlockMeta
blockDisconnected waddrmgr.BlockStamp blockDisconnected wtxmgr.BlockMeta
relevantTx chain.RelevantTx relevantTx chain.RelevantTx
@ -973,12 +973,12 @@ type (
) )
func (b blockConnected) notificationCmds(w *wallet.Wallet) []interface{} { func (b blockConnected) notificationCmds(w *wallet.Wallet) []interface{} {
n := btcjson.NewBlockConnectedNtfn(b.Hash.String(), b.Height) n := btcjson.NewBlockConnectedNtfn(b.Hash.String(), b.Height, b.Time.Unix())
return []interface{}{n} return []interface{}{n}
} }
func (b blockDisconnected) notificationCmds(w *wallet.Wallet) []interface{} { func (b blockDisconnected) notificationCmds(w *wallet.Wallet) []interface{} {
n := btcjson.NewBlockDisconnectedNtfn(b.Hash.String(), b.Height) n := btcjson.NewBlockDisconnectedNtfn(b.Hash.String(), b.Height, b.Time.Unix())
return []interface{}{n} return []interface{}{n}
} }

View file

@ -41,9 +41,9 @@ func (w *Wallet) handleChainNotifications() {
case chain.ClientConnected: case chain.ClientConnected:
go sync(w) go sync(w)
case chain.BlockConnected: case chain.BlockConnected:
w.connectBlock(waddrmgr.BlockStamp(n)) w.connectBlock(wtxmgr.BlockMeta(n))
case chain.BlockDisconnected: case chain.BlockDisconnected:
err = w.disconnectBlock(waddrmgr.BlockStamp(n)) err = w.disconnectBlock(wtxmgr.BlockMeta(n))
case chain.RelevantTx: case chain.RelevantTx:
err = w.addRelevantTx(n.TxRecord, n.Block) err = w.addRelevantTx(n.TxRecord, n.Block)
@ -63,17 +63,21 @@ func (w *Wallet) handleChainNotifications() {
// connectBlock handles a chain server notification by marking a wallet // connectBlock handles a chain server notification by marking a wallet
// that's currently in-sync with the chain server as being synced up to // that's currently in-sync with the chain server as being synced up to
// the passed block. // the passed block.
func (w *Wallet) connectBlock(bs waddrmgr.BlockStamp) { func (w *Wallet) connectBlock(b wtxmgr.BlockMeta) {
if !w.ChainSynced() { if !w.ChainSynced() {
return return
} }
bs := waddrmgr.BlockStamp{
Height: b.Height,
Hash: b.Hash,
}
if err := w.Manager.SetSyncedTo(&bs); err != nil { if err := w.Manager.SetSyncedTo(&bs); err != nil {
log.Errorf("Failed to update address manager sync state in "+ log.Errorf("Failed to update address manager sync state in "+
"connect block for hash %v (height %d): %v", bs.Hash, "connect block for hash %v (height %d): %v", b.Hash,
bs.Height, err) b.Height, err)
} }
w.notifyConnectedBlock(bs) w.notifyConnectedBlock(b)
w.notifyBalances(bs.Height) w.notifyBalances(bs.Height)
} }
@ -81,7 +85,7 @@ func (w *Wallet) connectBlock(bs waddrmgr.BlockStamp) {
// disconnectBlock handles a chain server reorganize by rolling back all // disconnectBlock handles a chain server reorganize by rolling back all
// block history from the reorged block for a wallet in-sync with the chain // block history from the reorged block for a wallet in-sync with the chain
// server. // server.
func (w *Wallet) disconnectBlock(bs waddrmgr.BlockStamp) error { func (w *Wallet) disconnectBlock(b wtxmgr.BlockMeta) error {
if !w.ChainSynced() { if !w.ChainSynced() {
return nil return nil
} }
@ -89,7 +93,7 @@ func (w *Wallet) disconnectBlock(bs waddrmgr.BlockStamp) error {
// Disconnect the last seen block from the manager if it matches the // Disconnect the last seen block from the manager if it matches the
// removed block. // removed block.
iter := w.Manager.NewIterateRecentBlocks() iter := w.Manager.NewIterateRecentBlocks()
if iter != nil && iter.BlockStamp().Hash == bs.Hash { if iter != nil && iter.BlockStamp().Hash == b.Hash {
if iter.Prev() { if iter.Prev() {
prev := iter.BlockStamp() prev := iter.BlockStamp()
w.Manager.SetSyncedTo(&prev) w.Manager.SetSyncedTo(&prev)
@ -111,9 +115,9 @@ func (w *Wallet) disconnectBlock(bs waddrmgr.BlockStamp) error {
} }
} }
} }
w.notifyDisconnectedBlock(bs)
w.notifyBalances(bs.Height - 1) w.notifyDisconnectedBlock(b)
w.notifyBalances(b.Height - 1)
return nil return nil
} }

View file

@ -217,7 +217,14 @@ out:
// every connected client. This is code smell and // every connected client. This is code smell and
// should be removed or replaced with a more // should be removed or replaced with a more
// appropiate notification when the API is redone. // appropiate notification when the API is redone.
w.notifyConnectedBlock(bs) b := wtxmgr.BlockMeta{
Block: wtxmgr.Block{
*n.Hash,
n.Height,
},
Time: n.Time,
}
w.notifyConnectedBlock(b)
case <-w.quit: case <-w.quit:
break out break out

View file

@ -96,8 +96,8 @@ type Wallet struct {
// Notification channels so other components can listen in on wallet // Notification channels so other components can listen in on wallet
// activity. These are initialized as nil, and must be created by // activity. These are initialized as nil, and must be created by
// calling one of the Listen* methods. // calling one of the Listen* methods.
connectedBlocks chan waddrmgr.BlockStamp connectedBlocks chan wtxmgr.BlockMeta
disconnectedBlocks chan waddrmgr.BlockStamp disconnectedBlocks chan wtxmgr.BlockMeta
relevantTxs chan chain.RelevantTx relevantTxs chan chain.RelevantTx
lockStateChanges chan bool // true when locked lockStateChanges chan bool // true when locked
confirmedBalance chan btcutil.Amount confirmedBalance chan btcutil.Amount
@ -119,14 +119,14 @@ var ErrDuplicateListen = errors.New("duplicate listen")
// methods will block. // methods will block.
// //
// If this is called twice, ErrDuplicateListen is returned. // If this is called twice, ErrDuplicateListen is returned.
func (w *Wallet) ListenConnectedBlocks() (<-chan waddrmgr.BlockStamp, error) { func (w *Wallet) ListenConnectedBlocks() (<-chan wtxmgr.BlockMeta, error) {
defer w.notificationMu.Unlock() defer w.notificationMu.Unlock()
w.notificationMu.Lock() w.notificationMu.Lock()
if w.connectedBlocks != nil { if w.connectedBlocks != nil {
return nil, ErrDuplicateListen return nil, ErrDuplicateListen
} }
w.connectedBlocks = make(chan waddrmgr.BlockStamp) w.connectedBlocks = make(chan wtxmgr.BlockMeta)
return w.connectedBlocks, nil return w.connectedBlocks, nil
} }
@ -135,14 +135,14 @@ func (w *Wallet) ListenConnectedBlocks() (<-chan waddrmgr.BlockStamp, error) {
// block. // block.
// //
// If this is called twice, ErrDuplicateListen is returned. // If this is called twice, ErrDuplicateListen is returned.
func (w *Wallet) ListenDisconnectedBlocks() (<-chan waddrmgr.BlockStamp, error) { func (w *Wallet) ListenDisconnectedBlocks() (<-chan wtxmgr.BlockMeta, error) {
defer w.notificationMu.Unlock() defer w.notificationMu.Unlock()
w.notificationMu.Lock() w.notificationMu.Lock()
if w.disconnectedBlocks != nil { if w.disconnectedBlocks != nil {
return nil, ErrDuplicateListen return nil, ErrDuplicateListen
} }
w.disconnectedBlocks = make(chan waddrmgr.BlockStamp) w.disconnectedBlocks = make(chan wtxmgr.BlockMeta)
return w.disconnectedBlocks, nil return w.disconnectedBlocks, nil
} }
@ -211,7 +211,7 @@ func (w *Wallet) ListenRelevantTxs() (<-chan chain.RelevantTx, error) {
return w.relevantTxs, nil return w.relevantTxs, nil
} }
func (w *Wallet) notifyConnectedBlock(block waddrmgr.BlockStamp) { func (w *Wallet) notifyConnectedBlock(block wtxmgr.BlockMeta) {
w.notificationMu.Lock() w.notificationMu.Lock()
if w.connectedBlocks != nil { if w.connectedBlocks != nil {
w.connectedBlocks <- block w.connectedBlocks <- block
@ -219,7 +219,7 @@ func (w *Wallet) notifyConnectedBlock(block waddrmgr.BlockStamp) {
w.notificationMu.Unlock() w.notificationMu.Unlock()
} }
func (w *Wallet) notifyDisconnectedBlock(block waddrmgr.BlockStamp) { func (w *Wallet) notifyDisconnectedBlock(block wtxmgr.BlockMeta) {
w.notificationMu.Lock() w.notificationMu.Lock()
if w.disconnectedBlocks != nil { if w.disconnectedBlocks != nil {
w.disconnectedBlocks <- block w.disconnectedBlocks <- block