Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit ce32228

Browse files
authored
Merge pull request #1734 from Bytom/dev
Dev
2 parents 7f08c11 + 3bfff50 commit ce32228

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

account/accounts.go

+3
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ func (m *Manager) deleteAccountControlPrograms(accountID string) error {
315315
m.db.Delete(ContractKey(hash))
316316
}
317317
}
318+
m.db.Delete(bip44ContractIndexKey(accountID, false))
319+
m.db.Delete(bip44ContractIndexKey(accountID, true))
320+
m.db.Delete(contractIndexKey(accountID))
318321
return nil
319322
}
320323

wallet/recovery.go

+8
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ func (m *recoveryManager) AddrResurrect(accts []*account.Account) error {
216216
}
217217

218218
m.state.StartTime = time.Now()
219+
if err := m.commitStatusInfo(); err != nil {
220+
return err
221+
}
222+
219223
m.started = true
220224
return nil
221225
}
@@ -236,6 +240,10 @@ func (m *recoveryManager) AcctResurrect(xPubs []chainkd.XPub) error {
236240
return err
237241
}
238242
m.state.StartTime = time.Now()
243+
if err := m.commitStatusInfo(); err != nil {
244+
return err
245+
}
246+
239247
m.started = true
240248
return nil
241249
}

wallet/recovery_test.go

+89
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/bytom/blockchain/pseudohsm"
1414
"github.com/bytom/blockchain/signers"
1515
"github.com/bytom/blockchain/txbuilder"
16+
"github.com/bytom/common"
1617
"github.com/bytom/consensus"
1718
"github.com/bytom/crypto/ed25519/chainkd"
1819
"github.com/bytom/errors"
@@ -603,3 +604,91 @@ func TestStateForScope(t *testing.T) {
603604
t.Fatal("state for scope test err")
604605
}
605606
}
607+
608+
func bip44ContractIndexKey(accountID string, change bool) []byte {
609+
contractIndexPrefix := []byte("ContractIndex")
610+
key := append(contractIndexPrefix, accountID...)
611+
if change {
612+
return append(key, []byte{1}...)
613+
}
614+
return append(key, []byte{0}...)
615+
}
616+
617+
func TestContractIndexResidue(t *testing.T) {
618+
dirPath, err := ioutil.TempDir(".", "")
619+
if err != nil {
620+
t.Fatal(err)
621+
}
622+
defer os.RemoveAll(dirPath)
623+
624+
testDB := dbm.NewDB("testdb", "leveldb", dirPath)
625+
hsm, err := pseudohsm.New(dirPath)
626+
if err != nil {
627+
t.Fatal(err)
628+
}
629+
630+
xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
631+
if err != nil {
632+
t.Fatal(err)
633+
}
634+
635+
contractIndexResidue := uint64(5)
636+
acctMgr := account.NewManager(testDB, nil)
637+
recoveryMgr := newRecoveryManager(testDB, acctMgr)
638+
acct := &account.Account{ID: "testA", Alias: "test1", Signer: &signers.Signer{XPubs: []chainkd.XPub{xpub1.XPub}, KeyIndex: 1, DeriveRule: signers.BIP0044}}
639+
640+
cp1 := &account.CtrlProgram{AccountID: acct.ID, Address: "address1", KeyIndex: 10, Change: false}
641+
642+
setContractIndexKey := func(acctMgr *account.Manager, accountID string, change bool) {
643+
testDB.Set(bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(contractIndexResidue))
644+
}
645+
646+
delAccount := func(acctMgr *account.Manager, accountID string, change bool) {
647+
acctMgr.DeleteAccount(accountID)
648+
}
649+
650+
recoveryMgr.state.XPubsStatus = newBranchRecoveryState(acctRecoveryWindow)
651+
recoveryMgr.state.XPubs = []chainkd.XPub{xpub1.XPub}
652+
recoveryMgr.state.stateForScope(acct)
653+
654+
cases := []struct {
655+
acct *account.Account
656+
cp *account.CtrlProgram
657+
preProcess func(acctMgr *account.Manager, accountID string, change bool)
658+
err error
659+
wantCPNum uint64
660+
}{
661+
{acct, cp1, setContractIndexKey, nil, 5},
662+
{acct, cp1, delAccount, nil, 10},
663+
}
664+
665+
for _, c := range cases {
666+
if c.preProcess != nil {
667+
c.preProcess(acctMgr, c.acct.ID, c.cp.Change)
668+
}
669+
670+
if err := acctMgr.SaveAccount(acct); err != nil {
671+
t.Fatal("ReportFound test err:", err)
672+
}
673+
674+
if err := recoveryMgr.reportFound(c.acct, c.cp); err != c.err {
675+
t.Fatal("ContractIndexResidue test err:", err, c.acct.ID)
676+
}
677+
cps, err := acctMgr.ListControlProgram()
678+
if err != nil {
679+
t.Fatal("list control program err:", err)
680+
}
681+
682+
cpNum := uint64(0)
683+
for _, cp := range cps {
684+
if cp.Address == "" || cp.AccountID != c.acct.ID {
685+
continue
686+
}
687+
cpNum++
688+
}
689+
690+
if cpNum != c.wantCPNum {
691+
t.Fatal("Test contract index residue cp num err want:", c.wantCPNum, " got:", cpNum)
692+
}
693+
}
694+
}

wallet/wallet.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ func (w *Wallet) AttachBlock(block *types.Block) error {
185185
}
186186

187187
if err := w.RecoveryMgr.FilterRecoveryTxs(block); err != nil {
188-
return err
188+
log.WithField("err", err).Error("filter recovery txs")
189+
w.RecoveryMgr.finished()
189190
}
190191

191192
storeBatch := w.DB.NewBatch()

0 commit comments

Comments
 (0)