X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=wallet%2Frecovery_test.go;h=32a0063a5a70fdcd2f1423b9fe13372e40444b94;hb=b3c1fe7a33650df6e14ae64bca2a3e8d46664aaa;hp=eff5850770d51e124075264a70d3162eb40f2ed0;hpb=a8fcbab90cbbfcc0acea0a4bd1389e39ece3da56;p=bytom%2Fvapor.git diff --git a/wallet/recovery_test.go b/wallet/recovery_test.go index eff58507..32a0063a 100644 --- a/wallet/recovery_test.go +++ b/wallet/recovery_test.go @@ -9,17 +9,18 @@ import ( "testing" "time" - "github.com/vapor/account" - "github.com/vapor/blockchain/pseudohsm" - "github.com/vapor/blockchain/signers" - "github.com/vapor/blockchain/txbuilder" - "github.com/vapor/common" - "github.com/vapor/consensus" - "github.com/vapor/crypto/ed25519/chainkd" - dbm "github.com/vapor/database/leveldb" - "github.com/vapor/errors" - "github.com/vapor/protocol/bc" - "github.com/vapor/protocol/bc/types" + "github.com/bytom/vapor/account" + acc "github.com/bytom/vapor/account" + "github.com/bytom/vapor/blockchain/pseudohsm" + "github.com/bytom/vapor/blockchain/signers" + "github.com/bytom/vapor/blockchain/txbuilder" + "github.com/bytom/vapor/common" + "github.com/bytom/vapor/consensus" + "github.com/bytom/vapor/crypto/ed25519/chainkd" + dbm "github.com/bytom/vapor/database/leveldb" + "github.com/bytom/vapor/errors" + "github.com/bytom/vapor/protocol/bc" + "github.com/bytom/vapor/protocol/bc/types" ) // MockBlock mock a block @@ -57,9 +58,12 @@ func MockSimpleUtxo(index uint64, assetID *bc.AssetID, amount uint64, ctrlProg * return utxo } +func AddInput(sourceID bc.Hash, assetID bc.AssetID, amount uint64, pos uint64, controlProgram []byte) *types.TxInput { + return types.NewSpendInput(nil, sourceID, assetID, amount, pos, controlProgram) +} + func AddTxOutput(assetID bc.AssetID, amount uint64, controlProgram []byte) *types.TxOutput { - out := types.NewIntraChainOutput(assetID, amount, controlProgram) - return out + return types.NewIntraChainOutput(assetID, amount, controlProgram) } func BuildTx(baseUtxo *account.UTXO, signer *signers.Signer) (*txbuilder.Template, error) { @@ -78,7 +82,9 @@ func BuildTx(baseUtxo *account.UTXO, signer *signers.Signer) (*txbuilder.Templat func CreateTxBuilder(baseUtxo *account.UTXO, signer *signers.Signer) (*txbuilder.TemplateBuilder, error) { tplBuilder := txbuilder.NewBuilder(time.Now()) + txInput := AddInput(bc.Hash{V0: 1}, baseUtxo.AssetID, 10000, uint64(1), baseUtxo.ControlProgram) txOutput := AddTxOutput(baseUtxo.AssetID, 100, baseUtxo.ControlProgram) + tplBuilder.AddInput(txInput, &txbuilder.SigningInstruction{Position: 0}) tplBuilder.AddOutput(txOutput) return tplBuilder, nil } @@ -136,6 +142,7 @@ func TestXPubsRecoveryLock(t *testing.T) { defer os.RemoveAll(dirPath) testDB := dbm.NewDB("testdb", "leveldb", dirPath) + walletStore := NewMockWalletStore(testDB) hsm, err := pseudohsm.New(dirPath) if err != nil { t.Fatal(err) @@ -146,8 +153,9 @@ func TestXPubsRecoveryLock(t *testing.T) { t.Fatal(err) } - acctMgr := account.NewManager(testDB, nil) - recoveryMgr := newRecoveryManager(testDB, acctMgr) + acctStore := NewMockAccountStore(testDB) + acctMgr := account.NewManager(acctStore, nil) + recoveryMgr := NewRecoveryManager(walletStore, acctMgr) recoveryMgr.state = newRecoveryState() recoveryMgr.state.XPubs = []chainkd.XPub{xpub.XPub} recoveryMgr.state.XPubsStatus = newBranchRecoveryState(acctRecoveryWindow) @@ -185,6 +193,7 @@ func TestExtendScanAddresses(t *testing.T) { defer os.RemoveAll(dirPath) testDB := dbm.NewDB("testdb", "leveldb", dirPath) + walletStore := NewMockWalletStore(testDB) hsm, err := pseudohsm.New(dirPath) if err != nil { t.Fatal(err) @@ -195,8 +204,9 @@ func TestExtendScanAddresses(t *testing.T) { t.Fatal(err) } - acctMgr := account.NewManager(testDB, nil) - recoveryMgr := newRecoveryManager(testDB, acctMgr) + acctStore := NewMockAccountStore(testDB) + acctMgr := account.NewManager(acctStore, nil) + recoveryMgr := NewRecoveryManager(walletStore, acctMgr) acc1 := &account.Account{ID: "testA", Alias: "test1", Signer: &signers.Signer{XPubs: []chainkd.XPub{xpub.XPub}, KeyIndex: 1, DeriveRule: signers.BIP0044}} acc2 := &account.Account{ID: "testB", Alias: "test2"} acc3 := &account.Account{ID: "testC", Alias: "test3", Signer: &signers.Signer{XPubs: []chainkd.XPub{xpub.XPub}, KeyIndex: 2, DeriveRule: 3}} @@ -241,6 +251,7 @@ func TestRecoveryFromXPubs(t *testing.T) { testDB := dbm.NewDB("testdb", "leveldb", dirPath) recoveryDB := dbm.NewDB("recdb", "leveldb", dirPath) + recoveryStore := NewMockWalletStore(recoveryDB) hsm, err := pseudohsm.New(dirPath) if err != nil { t.Fatal(err) @@ -251,10 +262,12 @@ func TestRecoveryFromXPubs(t *testing.T) { t.Fatal(err) } - acctMgr := account.NewManager(testDB, nil) + acctStore := NewMockAccountStore(testDB) + acctMgr := account.NewManager(acctStore, nil) txs, err := MockTxsP2PKH(acctMgr, xpub.XPub, false) - recAcctMgr := account.NewManager(recoveryDB, nil) - recoveryMgr := newRecoveryManager(recoveryDB, recAcctMgr) + recActStore := NewMockAccountStore(recoveryDB) + recAcctMgr := account.NewManager(recActStore, nil) + recoveryMgr := NewRecoveryManager(recoveryStore, recAcctMgr) cases := []struct { xPubs []chainkd.XPub @@ -285,7 +298,7 @@ func TestRecoveryFromXPubs(t *testing.T) { for _, acct := range Accounts { tmp, err := recAcctMgr.GetAccountByXPubsIndex(acct.XPubs, acct.KeyIndex) - if err != nil { + if err != nil && err != acc.ErrFindAccount { t.Fatal("recovery from XPubs err:", err) } @@ -315,6 +328,7 @@ func TestRecoveryByRescanAccount(t *testing.T) { testDB := dbm.NewDB("testdb", "leveldb", dirPath) recoveryDB := dbm.NewDB("recdb", "leveldb", dirPath) + recoveryStore := NewMockWalletStore(recoveryDB) hsm, err := pseudohsm.New(dirPath) if err != nil { t.Fatal(err) @@ -325,7 +339,8 @@ func TestRecoveryByRescanAccount(t *testing.T) { t.Fatal(err) } - acctMgr := account.NewManager(testDB, nil) + acctStore := NewMockAccountStore(testDB) + acctMgr := account.NewManager(acctStore, nil) txs, err := MockTxsP2PKH(acctMgr, xpub.XPub, true) if err != nil { t.Fatal("recovery by rescan account err:", err) @@ -336,14 +351,15 @@ func TestRecoveryByRescanAccount(t *testing.T) { t.Fatal("recovery by rescan account err:", err) } - recAcctMgr := account.NewManager(recoveryDB, nil) + recActStore := NewMockAccountStore(recoveryDB) + recAcctMgr := account.NewManager(recActStore, nil) for _, acct := range allAccounts { if err := recAcctMgr.SaveAccount(acct); err != nil { t.Fatal("recovery by rescan account err:", err) } } - recoveryMgr := newRecoveryManager(recoveryDB, recAcctMgr) + recoveryMgr := NewRecoveryManager(recoveryStore, recAcctMgr) acct := &account.Account{ID: "testA", Alias: "test1", Signer: &signers.Signer{XPubs: []chainkd.XPub{xpub.XPub}, KeyIndex: 1, DeriveRule: 3}} @@ -371,7 +387,7 @@ func TestRecoveryByRescanAccount(t *testing.T) { for _, acct := range accounts { tmp, err := recAcctMgr.GetAccountByXPubsIndex(acct.XPubs, acct.KeyIndex) - if err != nil { + if err != nil && err != acc.ErrFindAccount { t.Fatal("recovery from XPubs err:", err) } @@ -403,6 +419,7 @@ func TestReportFound(t *testing.T) { defer os.RemoveAll(dirPath) testDB := dbm.NewDB("testdb", "leveldb", dirPath) + testStore := NewMockWalletStore(testDB) hsm, err := pseudohsm.New(dirPath) if err != nil { t.Fatal(err) @@ -418,8 +435,9 @@ func TestReportFound(t *testing.T) { t.Fatal(err) } - acctMgr := account.NewManager(testDB, nil) - recoveryMgr := newRecoveryManager(testDB, acctMgr) + acctStore := NewMockAccountStore(testDB) + acctMgr := account.NewManager(acctStore, nil) + recoveryMgr := NewRecoveryManager(testStore, acctMgr) acc1 := &account.Account{ID: "testA", Alias: "test1", Signer: &signers.Signer{XPubs: []chainkd.XPub{xpub1.XPub}, KeyIndex: 1, DeriveRule: signers.BIP0044}} acc2 := &account.Account{ID: "testB", Alias: "test2", Signer: &signers.Signer{XPubs: []chainkd.XPub{xpub2.XPub}, KeyIndex: 1, DeriveRule: signers.BIP0032}} acc3 := &account.Account{ID: "testC", Alias: "test3", Signer: &signers.Signer{XPubs: []chainkd.XPub{xpub2.XPub}, KeyIndex: 2, DeriveRule: signers.BIP0044}} @@ -490,6 +508,7 @@ func TestLoadStatusInfo(t *testing.T) { defer os.RemoveAll(dirPath) testDB := dbm.NewDB("testdb", "leveldb", "temp") + testStore := NewMockWalletStore(testDB) defer os.RemoveAll("temp") hsm, err := pseudohsm.New(dirPath) @@ -502,8 +521,9 @@ func TestLoadStatusInfo(t *testing.T) { t.Fatal(err) } - acctMgr := account.NewManager(testDB, nil) - recoveryMgr := newRecoveryManager(testDB, acctMgr) + acctStore := NewMockAccountStore(testDB) + acctMgr := account.NewManager(acctStore, nil) + recoveryMgr := NewRecoveryManager(testStore, acctMgr) // StatusInit init recovery status manager. recoveryMgr.state = newRecoveryState() recoveryMgr.state.XPubs = []chainkd.XPub{xpub.XPub} @@ -516,7 +536,7 @@ func TestLoadStatusInfo(t *testing.T) { recoveryMgr.commitStatusInfo() - recoveryMgrRestore := newRecoveryManager(testDB, acctMgr) + recoveryMgrRestore := NewRecoveryManager(testStore, acctMgr) if err := recoveryMgrRestore.LoadStatusInfo(); err != nil { t.Fatal("TestLoadStatusInfo err:", err) } @@ -564,10 +584,12 @@ func TestLock(t *testing.T) { defer os.RemoveAll(dirPath) testDB := dbm.NewDB("testdb", "leveldb", "temp") + testStore := NewMockWalletStore(testDB) defer os.RemoveAll("temp") - acctMgr := account.NewManager(testDB, nil) - recoveryMgr := newRecoveryManager(testDB, acctMgr) + acctStore := NewMockAccountStore(testDB) + acctMgr := account.NewManager(acctStore, nil) + recoveryMgr := NewRecoveryManager(testStore, acctMgr) if !recoveryMgr.tryStartXPubsRec() { t.Fatal("recovery manager try lock test err") } @@ -605,15 +627,6 @@ func TestStateForScope(t *testing.T) { } } -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 { @@ -622,6 +635,7 @@ func TestContractIndexResidue(t *testing.T) { defer os.RemoveAll(dirPath) testDB := dbm.NewDB("testdb", "leveldb", dirPath) + testStore := NewMockWalletStore(testDB) hsm, err := pseudohsm.New(dirPath) if err != nil { t.Fatal(err) @@ -633,14 +647,15 @@ func TestContractIndexResidue(t *testing.T) { } contractIndexResidue := uint64(5) - acctMgr := account.NewManager(testDB, nil) - recoveryMgr := newRecoveryManager(testDB, acctMgr) + acctStore := NewMockAccountStore(testDB) + acctMgr := account.NewManager(acctStore, nil) + recoveryMgr := NewRecoveryManager(testStore, 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)) + testDB.Set(Bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(contractIndexResidue)) } delAccount := func(acctMgr *account.Manager, accountID string, change bool) {