From 1702ef23a155dc3f4fb5df2a45f196932db83092 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Wed, 6 Sep 2023 13:52:08 -0400 Subject: [PATCH] walletutil: Fix genesis subscription --- internal/walletutil/manager.go | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/internal/walletutil/manager.go b/internal/walletutil/manager.go index 1519554..fc5d699 100644 --- a/internal/walletutil/manager.go +++ b/internal/walletutil/manager.go @@ -139,9 +139,19 @@ func (wm *EphemeralWalletManager) SubscribeWallet(name string, startHeight uint6 return errNoWallet } else if mw.subscribed { return errors.New("already subscribed") - } else if index, ok := wm.cm.BestIndex(startHeight); !ok { - return errors.New("invalid height") - } else if err := wm.cm.AddSubscriber(mw.w, index); err != nil { + } + // AddSubscriber applies each block *after* index, but we want to *include* + // the block at startHeight, so subtract one. + // + // NOTE: if subscribing from height 0, we must pass an empty index in order + // to receive the genesis block. + var index types.ChainIndex + if startHeight > 0 { + if index, ok = wm.cm.BestIndex(startHeight - 1); !ok { + return errors.New("invalid height") + } + } + if err := wm.cm.AddSubscriber(mw.w, index); err != nil { return err } mw.subscribed = true @@ -343,9 +353,16 @@ func (wm *JSONWalletManager) SubscribeWallet(name string, startHeight uint64) er } else if mw.subscribed { return errors.New("already subscribed") } - index, ok := wm.cm.BestIndex(startHeight) - if !ok { - return errors.New("invalid height") + // AddSubscriber applies each block *after* index, but we want to *include* + // the block at startHeight, so subtract one. + // + // NOTE: if subscribing from height 0, we must pass an empty index in order + // to receive the genesis block. + var index types.ChainIndex + if startHeight > 0 { + if index, ok = wm.cm.BestIndex(startHeight - 1); !ok { + return errors.New("invalid height") + } } if err := wm.cm.AddSubscriber(mw.w, index); err != nil { return err