// setupManager creates a new address manager and returns a teardown function // that should be invoked to ensure it is closed and removed upon completion. func setupManager(t *testing.T) (tearDownFunc func(), mgr *waddrmgr.Manager) { t.Parallel() // Create a new manager in a temp directory. dirName, err := ioutil.TempDir("", "mgrtest") if err != nil { t.Fatalf("Failed to create db temp dir: %v", err) } dbPath := filepath.Join(dirName, "mgrtest.db") db, namespace, err := createDbNamespace(dbPath) if err != nil { _ = os.RemoveAll(dirName) t.Fatalf("createDbNamespace: unexpected error: %v", err) } err = waddrmgr.Create(namespace, seed, pubPassphrase, privPassphrase, &chaincfg.MainNetParams, fastScrypt) if err == nil { mgr, err = waddrmgr.Open(namespace, pubPassphrase, &chaincfg.MainNetParams, nil) } if err != nil { db.Close() _ = os.RemoveAll(dirName) t.Fatalf("Failed to create Manager: %v", err) } tearDownFunc = func() { mgr.Close() db.Close() _ = os.RemoveAll(dirName) } return tearDownFunc, mgr }
func createWaddrmgr(ns walletdb.Namespace, params *chaincfg.Params) (*waddrmgr.Manager, error) { err := waddrmgr.Create(ns, seed, pubPassphrase, privPassphrase, params, fastScrypt) if err != nil { return nil, err } return waddrmgr.Open(ns, pubPassphrase, params, nil) }
// TstCreatePool creates a Pool on a fresh walletdb and returns it. It also // returns the pool's waddrmgr.Manager (which uses the same walletdb, but with a // different namespace) as a convenience, and a teardown function that closes // the Manager and removes the directory used to store the database. func TstCreatePool(t *testing.T) (tearDownFunc func(), mgr *waddrmgr.Manager, pool *Pool) { // This should be moved somewhere else eventually as not all of our tests // call this function, but right now the only option would be to have the // t.Parallel() call in each of our tests. t.Parallel() // Create a new wallet DB and addr manager. dir, err := ioutil.TempDir("", "pool_test") if err != nil { t.Fatalf("Failed to create db dir: %v", err) } db, err := walletdb.Create("bdb", filepath.Join(dir, "wallet.db")) if err != nil { t.Fatalf("Failed to create wallet DB: %v", err) } mgrNamespace, err := db.Namespace([]byte("waddrmgr")) if err != nil { t.Fatalf("Failed to create addr manager DB namespace: %v", err) } var fastScrypt = &waddrmgr.ScryptOptions{N: 16, R: 8, P: 1} err = waddrmgr.Create(mgrNamespace, seed, pubPassphrase, privPassphrase, &chaincfg.MainNetParams, fastScrypt) if err == nil { mgr, err = waddrmgr.Open(mgrNamespace, pubPassphrase, &chaincfg.MainNetParams, nil) } if err != nil { t.Fatalf("Failed to create addr manager: %v", err) } // Create a walletdb for votingpools. vpNamespace, err := db.Namespace([]byte("votingpool")) if err != nil { t.Fatalf("Failed to create VotingPool DB namespace: %v", err) } pool, err = Create(vpNamespace, mgr, []byte{0x00}) if err != nil { t.Fatalf("Voting Pool creation failed: %v", err) } tearDownFunc = func() { db.Close() mgr.Close() os.RemoveAll(dir) } return tearDownFunc, mgr, pool }