forked from decred/dcrwallet
/
open.go
65 lines (60 loc) · 2.28 KB
/
open.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) 2017 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package udb
import (
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrwallet/apperrors"
"github.com/decred/dcrwallet/walletdb"
)
// Open opens the database and returns various "manager" types that must be used
// to access and modify data in the database.
//
// A apperrors.E with an error code of ErrNoExist will be returned if the
// database has not been initialized. The recorded database version must match
// exactly with DBVersion. If the version is too low, the erorr code
// ErrNeedsUpgrade is used and the database must be upgraded. If the version is
// greater than the one understood by this code, the error code will be
// ErrUnknownVersion.
func Open(db walletdb.DB, params *chaincfg.Params, pubPass []byte) (addrMgr *Manager, txStore *Store, stakeStore *StakeStore, err error) {
err = walletdb.View(db, func(tx walletdb.ReadTx) error {
// Verify the database exists and the recorded version is supported by
// this software version.
metadataBucket := tx.ReadBucket(unifiedDBMetadata{}.rootBucketKey())
if metadataBucket == nil {
const str = "database has not been initialized"
return apperrors.E{ErrorCode: apperrors.ErrNoExist, Description: str}
}
dbVersion, err := unifiedDBMetadata{}.getVersion(metadataBucket)
if err != nil {
return err
}
if dbVersion < DBVersion {
const str = "database upgrade required"
return apperrors.E{ErrorCode: apperrors.ErrNeedsUpgrade, Description: str}
}
if dbVersion > DBVersion {
const str = "database has been upgraded to an unknown newer version"
return apperrors.E{ErrorCode: apperrors.ErrUnknownVersion, Description: str}
}
addrmgrNs := tx.ReadBucket(waddrmgrBucketKey)
stakemgrNs := tx.ReadBucket(wstakemgrBucketKey)
addrMgr, err = loadManager(addrmgrNs, pubPass, params)
if err != nil {
return err
}
txStore = &Store{
chainParams: params,
acctLookupFunc: addrMgr.AddrAccount,
}
stakeStore, err = openStakeStore(stakemgrNs, addrMgr, params)
return err
})
switch err.(type) {
case nil, apperrors.E:
default:
const str = "database view failed"
err = apperrors.E{ErrorCode: apperrors.ErrDatabase, Description: str, Err: err}
}
return
}