Commit ce32228f authored by Paladz's avatar Paladz Committed by GitHub

Merge pull request #1734 from Bytom/dev

Dev
parents 7f08c11f 3bfff50d
......@@ -315,6 +315,9 @@ func (m *Manager) deleteAccountControlPrograms(accountID string) error {
m.db.Delete(ContractKey(hash))
}
}
m.db.Delete(bip44ContractIndexKey(accountID, false))
m.db.Delete(bip44ContractIndexKey(accountID, true))
m.db.Delete(contractIndexKey(accountID))
return nil
}
......
......@@ -216,6 +216,10 @@ func (m *recoveryManager) AddrResurrect(accts []*account.Account) error {
}
m.state.StartTime = time.Now()
if err := m.commitStatusInfo(); err != nil {
return err
}
m.started = true
return nil
}
......@@ -236,6 +240,10 @@ func (m *recoveryManager) AcctResurrect(xPubs []chainkd.XPub) error {
return err
}
m.state.StartTime = time.Now()
if err := m.commitStatusInfo(); err != nil {
return err
}
m.started = true
return nil
}
......
......@@ -13,6 +13,7 @@ import (
"github.com/bytom/blockchain/pseudohsm"
"github.com/bytom/blockchain/signers"
"github.com/bytom/blockchain/txbuilder"
"github.com/bytom/common"
"github.com/bytom/consensus"
"github.com/bytom/crypto/ed25519/chainkd"
"github.com/bytom/errors"
......@@ -603,3 +604,91 @@ func TestStateForScope(t *testing.T) {
t.Fatal("state for scope test err")
}
}
func bip44ContractIndexKey(accountID string, change bool) []byte {
contractIndexPrefix := []byte("ContractIndex")
key := append(contractIndexPrefix, accountID...)
if change {
return append(key, []byte{1}...)
}
return append(key, []byte{0}...)
}
func TestContractIndexResidue(t *testing.T) {
dirPath, err := ioutil.TempDir(".", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dirPath)
testDB := dbm.NewDB("testdb", "leveldb", dirPath)
hsm, err := pseudohsm.New(dirPath)
if err != nil {
t.Fatal(err)
}
xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
if err != nil {
t.Fatal(err)
}
contractIndexResidue := uint64(5)
acctMgr := account.NewManager(testDB, nil)
recoveryMgr := newRecoveryManager(testDB, acctMgr)
acct := &account.Account{ID: "testA", Alias: "test1", Signer: &signers.Signer{XPubs: []chainkd.XPub{xpub1.XPub}, KeyIndex: 1, DeriveRule: signers.BIP0044}}
cp1 := &account.CtrlProgram{AccountID: acct.ID, Address: "address1", KeyIndex: 10, Change: false}
setContractIndexKey := func(acctMgr *account.Manager, accountID string, change bool) {
testDB.Set(bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(contractIndexResidue))
}
delAccount := func(acctMgr *account.Manager, accountID string, change bool) {
acctMgr.DeleteAccount(accountID)
}
recoveryMgr.state.XPubsStatus = newBranchRecoveryState(acctRecoveryWindow)
recoveryMgr.state.XPubs = []chainkd.XPub{xpub1.XPub}
recoveryMgr.state.stateForScope(acct)
cases := []struct {
acct *account.Account
cp *account.CtrlProgram
preProcess func(acctMgr *account.Manager, accountID string, change bool)
err error
wantCPNum uint64
}{
{acct, cp1, setContractIndexKey, nil, 5},
{acct, cp1, delAccount, nil, 10},
}
for _, c := range cases {
if c.preProcess != nil {
c.preProcess(acctMgr, c.acct.ID, c.cp.Change)
}
if err := acctMgr.SaveAccount(acct); err != nil {
t.Fatal("ReportFound test err:", err)
}
if err := recoveryMgr.reportFound(c.acct, c.cp); err != c.err {
t.Fatal("ContractIndexResidue test err:", err, c.acct.ID)
}
cps, err := acctMgr.ListControlProgram()
if err != nil {
t.Fatal("list control program err:", err)
}
cpNum := uint64(0)
for _, cp := range cps {
if cp.Address == "" || cp.AccountID != c.acct.ID {
continue
}
cpNum++
}
if cpNum != c.wantCPNum {
t.Fatal("Test contract index residue cp num err want:", c.wantCPNum, " got:", cpNum)
}
}
}
......@@ -185,7 +185,8 @@ func (w *Wallet) AttachBlock(block *types.Block) error {
}
if err := w.RecoveryMgr.FilterRecoveryTxs(block); err != nil {
return err
log.WithField("err", err).Error("filter recovery txs")
w.RecoveryMgr.finished()
}
storeBatch := w.DB.NewBatch()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment