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

View file

@ -272,8 +272,8 @@ type rpcServer struct {
// Channels read from other components from which notifications are
// created.
connectedBlocks <-chan waddrmgr.BlockStamp
disconnectedBlocks <-chan waddrmgr.BlockStamp
connectedBlocks <-chan wtxmgr.BlockMeta
disconnectedBlocks <-chan wtxmgr.BlockMeta
relevantTxs <-chan chain.RelevantTx
managerLocked <-chan bool
confirmedBalance <-chan btcutil.Amount
@ -959,8 +959,8 @@ type (
notificationCmds(w *wallet.Wallet) []interface{}
}
blockConnected waddrmgr.BlockStamp
blockDisconnected waddrmgr.BlockStamp
blockConnected wtxmgr.BlockMeta
blockDisconnected wtxmgr.BlockMeta
relevantTx chain.RelevantTx
@ -973,12 +973,12 @@ type (
)
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}
}
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}
}

View file

@ -41,9 +41,9 @@ func (w *Wallet) handleChainNotifications() {
case chain.ClientConnected:
go sync(w)
case chain.BlockConnected:
w.connectBlock(waddrmgr.BlockStamp(n))
w.connectBlock(wtxmgr.BlockMeta(n))
case chain.BlockDisconnected:
err = w.disconnectBlock(waddrmgr.BlockStamp(n))
err = w.disconnectBlock(wtxmgr.BlockMeta(n))
case chain.RelevantTx:
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
// that's currently in-sync with the chain server as being synced up to
// the passed block.
func (w *Wallet) connectBlock(bs waddrmgr.BlockStamp) {
func (w *Wallet) connectBlock(b wtxmgr.BlockMeta) {
if !w.ChainSynced() {
return
}
bs := waddrmgr.BlockStamp{
Height: b.Height,
Hash: b.Hash,
}
if err := w.Manager.SetSyncedTo(&bs); err != nil {
log.Errorf("Failed to update address manager sync state in "+
"connect block for hash %v (height %d): %v", bs.Hash,
bs.Height, err)
"connect block for hash %v (height %d): %v", b.Hash,
b.Height, err)
}
w.notifyConnectedBlock(bs)
w.notifyConnectedBlock(b)
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
// block history from the reorged block for a wallet in-sync with the chain
// server.
func (w *Wallet) disconnectBlock(bs waddrmgr.BlockStamp) error {
func (w *Wallet) disconnectBlock(b wtxmgr.BlockMeta) error {
if !w.ChainSynced() {
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
// removed block.
iter := w.Manager.NewIterateRecentBlocks()
if iter != nil && iter.BlockStamp().Hash == bs.Hash {
if iter != nil && iter.BlockStamp().Hash == b.Hash {
if iter.Prev() {
prev := iter.BlockStamp()
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
}

View file

@ -217,7 +217,14 @@ out:
// every connected client. This is code smell and
// should be removed or replaced with a more
// 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:
break out

View file

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