Skip to content

Commit

Permalink
Address review items:
Browse files Browse the repository at this point in the history
* add comment that usedAddrBucketName was added after v1 release
* use addrHash instead of addressID for fetchAddressUsed
* define fetchAddressUsed before use
* don't bump version number
* remove unnecessary local var for used
  • Loading branch information
tuxcanfly committed Mar 4, 2015
1 parent 46820f6 commit 33bd671
Showing 1 changed file with 16 additions and 48 deletions.
64 changes: 16 additions & 48 deletions waddrmgr/db.go
Expand Up @@ -29,7 +29,7 @@ import (

const (
// LatestMgrVersion is the most recent manager version.
LatestMgrVersion = 2
LatestMgrVersion = 1
)

var (
Expand Down Expand Up @@ -577,7 +577,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:
// <addrType><account><addedTime><syncStatus><rawdata>
// <addrType><account><addedTime><syncStatus><commentlen><comment>
// <rawdata>
//
// 1 byte addrType + 4 bytes account + 8 bytes addTime + 1 byte
// syncStatus + 4 bytes raw data length + raw data
Expand Down Expand Up @@ -735,6 +736,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.
Expand All @@ -752,8 +764,7 @@ func fetchAddress(tx walletdb.Tx, addressID []byte) (interface{}, error) {
if err != nil {
return nil, err
}
used := fetchAddrUsed(tx, addressID)
row.used = used
row.used = fetchAddressUsed(tx, addrHash)

switch row.addrType {
case adtChain:
Expand Down Expand Up @@ -785,18 +796,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 {
Expand Down Expand Up @@ -1277,6 +1276,7 @@ func createManagerNS(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"
Expand Down Expand Up @@ -1364,35 +1364,3 @@ func upgradeManager(namespace walletdb.Namespace) error {

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
}

0 comments on commit 33bd671

Please sign in to comment.