Skip to content

Commit

Permalink
feat: Add RescanStartBlock (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
bullet-tooth authored Dec 25, 2024
1 parent 016f76c commit 79b976f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
24 changes: 10 additions & 14 deletions run/btcwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,8 @@ type BtcwalletConfig struct {
Config *Config
InitTimeout time.Duration
FeeCoefficient float64
}

func NewBtcwalletConfig(signer frost.Signer, pk1, pk2 *btcec.PublicKey, bitcoindConfig *chain.BitcoindConfig,
config *Config, initTimeout time.Duration, feeCoefficient float64) *BtcwalletConfig {

return &BtcwalletConfig{
Signer: signer,
Pk1: pk1,
Pk2: pk2,
BitcoindConfig: bitcoindConfig,
Config: config,
InitTimeout: initTimeout,
FeeCoefficient: feeCoefficient,
}
// RescanStartBlock is the block height to start rescan from; 0 means from the wallet sync block
RescanStartBlock uint64
}

func SafeInitWallet(config *BtcwalletConfig) (*wallet.Wallet, error) {
Expand Down Expand Up @@ -194,6 +182,14 @@ func doInit(config *BtcwalletConfig) (*wallet.Wallet, error) {

w.ChangeAddressKey = changeAddressKey
w.FeeCoefficient = config.FeeCoefficient

if config.RescanStartBlock != 0 {
rescanStartStamp, err := w.GetBlockStamp(config.RescanStartBlock)
if err != nil {
return nil, fmt.Errorf("cannot get rescan block stamp: %w", err)
}
w.RescanStartStamp = rescanStartStamp
}
}

// Add interrupt handlers to shut down the various process components
Expand Down
13 changes: 9 additions & 4 deletions run/signer_with_wallet_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ func Example() {
return
}

w, err := SafeInitWallet(
NewBtcwalletConfig(validators[0], pk1, pk2,
chain.NewBitcoindConfig("127.0.0.1:38332", "rpcuser", "rpcpassword"),
nil, 2, 1))
w, err := SafeInitWallet(&BtcwalletConfig{
Signer: validators[0],
Pk1: pk1,
Pk2: pk2,
BitcoindConfig: chain.NewBitcoindConfig("127.0.0.1:38332", "rpcuser", "rpcpassword"),
Config: nil,
InitTimeout: 2,
FeeCoefficient: 1,
})

if err != nil {
os.Exit(1)
Expand Down
25 changes: 21 additions & 4 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ type Wallet struct {
rescanProgress chan *RescanProgressMsg
rescanFinished chan *RescanFinishedMsg
rescanLock sync.Mutex
RescanStartStamp *waddrmgr.BlockStamp

// Channel for transaction creation requests.
createTxRequests chan createTxRequest
Expand Down Expand Up @@ -275,9 +276,25 @@ func (w *Wallet) requireChainClient() (chain.Interface, error) {
// the wallet.
func (w *Wallet) ChainClient() chain.Interface {
w.chainClientLock.Lock()
chainClient := w.chainClient
w.chainClientLock.Unlock()
return chainClient
defer w.chainClientLock.Unlock()
return w.chainClient
}

func (w *Wallet) GetBlockStamp(height uint64) (*waddrmgr.BlockStamp, error) {
hash, err := w.ChainClient().GetBlockHash(int64(height))
if err != nil {
return nil, err
}
header, err := w.ChainClient().GetBlockHeader(hash)
if err != nil {
return nil, err
}

return &waddrmgr.BlockStamp{
Height: int32(height),
Hash: header.BlockHash(),
Timestamp: header.Timestamp,
}, nil
}

// quitChan atomically reads the quit channel.
Expand Down Expand Up @@ -574,7 +591,7 @@ func (w *Wallet) syncWithChain(birthdayStamp *waddrmgr.BlockStamp) error {
return err
}

return w.rescanWithTarget(addrs, unspent, nil)
return w.rescanWithTarget(addrs, unspent, w.RescanStartStamp)
}

// isDevEnv determines whether the wallet is currently under a local developer
Expand Down

0 comments on commit 79b976f

Please sign in to comment.