Skip to content

Commit e3bd922

Browse files
authored
Merge pull request #63 from stroomnetwork/feat/add-manual-rollback
feat: Add manual rollback
2 parents 743f278 + 44cc3b5 commit e3bd922

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

run/btcwallet.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ type BtcwalletConfig struct {
3939
InitTimeout time.Duration
4040
FeeCoefficient float64
4141
// RescanStartBlock is the block height to start rescan from; 0 means from the wallet sync block
42-
RescanStartBlock uint64
42+
RescanStartBlock uint64
43+
RollbackStateFromBlock uint64
4344
}
4445

4546
func SafeInitWallet(config *BtcwalletConfig) (*wallet.Wallet, error) {
@@ -184,11 +185,18 @@ func doInit(config *BtcwalletConfig) (*wallet.Wallet, error) {
184185
w.FeeCoefficient = config.FeeCoefficient
185186

186187
if config.RescanStartBlock != 0 {
187-
rescanStartStamp, err := w.GetBlockStamp(config.RescanStartBlock)
188+
stamp, err := w.GetBlockStamp(config.RescanStartBlock)
188189
if err != nil {
189190
return nil, fmt.Errorf("cannot get rescan block stamp: %w", err)
190191
}
191-
w.RescanStartStamp = rescanStartStamp
192+
w.RescanStartStamp = stamp
193+
}
194+
if config.RollbackStateFromBlock != 0 {
195+
stamp, err := w.GetBlockStamp(config.RollbackStateFromBlock)
196+
if err != nil {
197+
return nil, fmt.Errorf("cannot get rescan block stamp: %w", err)
198+
}
199+
w.RollbackFromStamp = stamp
192200
}
193201
}
194202

wallet/wallet.go

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ type Wallet struct {
162162
rescanFinished chan *RescanFinishedMsg
163163
rescanLock sync.Mutex
164164
RescanStartStamp *waddrmgr.BlockStamp
165+
RollbackFromStamp *waddrmgr.BlockStamp
165166

166167
// Channel for transaction creation requests.
167168
createTxRequests chan createTxRequest
@@ -507,28 +508,34 @@ func (w *Wallet) syncWithChain(birthdayStamp *waddrmgr.BlockStamp) error {
507508
addrmgrNs := tx.ReadWriteBucket(waddrmgrNamespaceKey)
508509
txmgrNs := tx.ReadWriteBucket(wtxmgrNamespaceKey)
509510

510-
for height := rollbackStamp.Height; true; height-- {
511-
hash, err := w.Manager.BlockHash(addrmgrNs, height)
512-
if err != nil {
513-
return err
514-
}
515-
chainHash, err := chainClient.GetBlockHash(int64(height))
516-
if err != nil {
517-
return err
518-
}
519-
header, err := chainClient.GetBlockHeader(chainHash)
520-
if err != nil {
521-
return err
522-
}
511+
// NB: if the rollback specified in config we will do rollback from here
512+
if w.RollbackFromStamp != nil {
513+
rollbackStamp = *w.RollbackFromStamp
514+
rollback = true
515+
} else {
516+
for height := rollbackStamp.Height; true; height-- {
517+
hash, err := w.Manager.BlockHash(addrmgrNs, height)
518+
if err != nil {
519+
return err
520+
}
521+
chainHash, err := chainClient.GetBlockHash(int64(height))
522+
if err != nil {
523+
return err
524+
}
525+
header, err := chainClient.GetBlockHeader(chainHash)
526+
if err != nil {
527+
return err
528+
}
523529

524-
rollbackStamp.Hash = *chainHash
525-
rollbackStamp.Height = height
526-
rollbackStamp.Timestamp = header.Timestamp
530+
rollbackStamp.Hash = *chainHash
531+
rollbackStamp.Height = height
532+
rollbackStamp.Timestamp = header.Timestamp
527533

528-
if bytes.Equal(hash[:], chainHash[:]) {
529-
break
534+
if bytes.Equal(hash[:], chainHash[:]) {
535+
break
536+
}
537+
rollback = true
530538
}
531-
rollback = true
532539
}
533540

534541
// If a rollback did not happen, we can proceed safely.

0 commit comments

Comments
 (0)