Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API updates for times in block notifications. #294

Merged
merged 1 commit into from
Jun 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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}
}

Expand Down
24 changes: 14 additions & 10 deletions wallet/chainntfns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -63,33 +63,37 @@ 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)
}

// 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
}

// 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)
Expand All @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion wallet/rescan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -211,15 +211,15 @@ 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
}
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
Expand Down