diff --git a/waddrmgr/db.go b/waddrmgr/db.go index bd61125172..4cf546acd6 100644 --- a/waddrmgr/db.go +++ b/waddrmgr/db.go @@ -29,7 +29,7 @@ import ( const ( // LatestMgrVersion is the most recent manager version. - LatestMgrVersion = 2 + LatestMgrVersion = 1 ) // maybeConvertDbError converts the passed error to a ManagerError with an @@ -558,7 +558,8 @@ func deserializeAddressRow(addressID, serializedAddress []byte) (*dbAddressRow, // serializeAddressRow returns the serialization of the passed address row. func serializeAddressRow(row *dbAddressRow) []byte { // The serialized address format is: - // + // + // // // 1 byte addrType + 4 bytes account + 8 bytes addTime + 1 byte // syncStatus + 4 bytes raw data length + raw data @@ -716,6 +717,17 @@ func serializeScriptAddress(encryptedHash, encryptedScript []byte) []byte { return rawData } +// fetchAddressUsed returns true if the provided address hash was flagged as used. +func fetchAddressUsed(tx walletdb.Tx, addrHash [32]byte) bool { + bucket := tx.RootBucket().Bucket(usedAddrBucketName) + + val := bucket.Get(addrHash[:]) + if val != nil { + return true + } + return false +} + // fetchAddress loads address information for the provided address id from // the database. The returned value is one of the address rows for the specific // address type. The caller should use type assertions to ascertain the type. @@ -733,7 +745,7 @@ func fetchAddress(tx walletdb.Tx, addressID []byte) (interface{}, error) { if err != nil { return nil, err } - used := fetchAddrUsed(tx, addressID) + used := fetchAddressUsed(tx, addrHash) row.used = used switch row.addrType { @@ -766,18 +778,6 @@ func markAddressUsed(tx walletdb.Tx, addressID []byte) error { return nil } -// fetchAddrUsed returns true if the provided address id was flagged as used. -func fetchAddrUsed(tx walletdb.Tx, addressID []byte) bool { - bucket := tx.RootBucket().Bucket(usedAddrBucketName) - - addrHash := fastsha256.Sum256(addressID) - val := bucket.Get(addrHash[:]) - if val != nil { - return true - } - return false -} - // putAddress stores the provided address information to the database. This // is used a common base for storing the various address types. func putAddress(tx walletdb.Tx, addressID []byte, row *dbAddressRow) error { @@ -1262,6 +1262,7 @@ func upgradeManager(namespace walletdb.Namespace) error { return managerError(ErrDatabase, str, err) } + // usedAddrBucketName bucket was added after manager version 1 release _, err = rootBucket.CreateBucketIfNotExists(usedAddrBucketName) if err != nil { str := "failed to create used addresses bucket" @@ -1308,41 +1309,8 @@ func upgradeManager(namespace walletdb.Namespace) error { // Upgrade the manager as needed. if version < LatestMgrVersion { - // Upgrade addresses used flag - upgradeVersion1to2(namespace) + // No upgrades yet. } return nil } - -// upgradeVersion1to2 upgrades the database from version 1 to version 2 -// 'usedAddrBucketName' a bucket for storing addrs flagged as marked is initialized -// and it will be updated on the next rescan -func upgradeVersion1to2(namespace walletdb.Namespace) error { - err := namespace.Update(func(tx walletdb.Tx) error { - rootBucket := tx.RootBucket() - mainBucket := tx.RootBucket().Bucket(mainBucketName) - - _, err := rootBucket.CreateBucketIfNotExists(usedAddrBucketName) - if err != nil { - str := "failed to create used addresses bucket" - return managerError(ErrDatabase, str, err) - } - - var version uint32 - var buf [4]byte - version = LatestMgrVersion - binary.LittleEndian.PutUint32(buf[:], version) - err = mainBucket.Put(mgrVersionName, buf[:]) - if err != nil { - str := "failed to store latest database version" - return managerError(ErrDatabase, str, err) - } - return nil - }) - if err != nil { - str := "failed to upgrade version 1 to version 2" - return managerError(ErrDatabase, str, err) - } - return nil -}