From 6568c433fe26f7b42522aff6c3a1ce3eff883bad Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Thu, 15 Nov 2018 17:58:41 -0800 Subject: [PATCH] waddrmgr/db: store birthday block verification status In this commit, we add a new key/value pair to the waddrmgr's sync bucket to store the verification status of the birthday block. This verification status determines whether the wallet has verified the correctness of its birthday block through its sanity check on startup. --- waddrmgr/db.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/waddrmgr/db.go b/waddrmgr/db.go index e9dd41d..a7c41a0 100644 --- a/waddrmgr/db.go +++ b/waddrmgr/db.go @@ -253,10 +253,11 @@ var ( watchingOnlyName = []byte("watchonly") // Sync related key names (sync bucket). - syncedToName = []byte("syncedto") - startBlockName = []byte("startblock") - birthdayName = []byte("birthday") - birthdayBlockName = []byte("birthdayblock") + syncedToName = []byte("syncedto") + startBlockName = []byte("startblock") + birthdayName = []byte("birthday") + birthdayBlockName = []byte("birthdayblock") + birthdayBlockVerifiedName = []byte("birthdayblockverified") ) // uint32ToBytes converts a 32 bit unsigned integer into a 4-byte slice in @@ -2012,6 +2013,47 @@ func putBirthdayBlock(ns walletdb.ReadWriteBucket, block BlockStamp) error { return nil } +// fetchBirthdayBlockVerification retrieves the bit that determines whether the +// wallet has verified that its birthday block is correct. +func fetchBirthdayBlockVerification(ns walletdb.ReadBucket) bool { + bucket := ns.NestedReadBucket(syncBucketName) + verifiedValue := bucket.Get(birthdayBlockVerifiedName) + + // If there is no verification status, we can assume it has not been + // verified yet. + if verifiedValue == nil { + return false + } + + // Otherwise, we'll determine if it's verified by the value stored. + verified := binary.BigEndian.Uint16(verifiedValue[:]) + return verified != 0 +} + +// putBirthdayBlockVerification stores a bit that determines whether the +// birthday block has been verified by the wallet to be correct. +func putBirthdayBlockVerification(ns walletdb.ReadWriteBucket, verified bool) error { + // Convert the boolean to an integer in its binary representation as + // there is no way to insert a boolean directly as a value of a + // key/value pair. + verifiedValue := uint16(0) + if verified { + verifiedValue = 1 + } + + var verifiedBytes [2]byte + binary.BigEndian.PutUint16(verifiedBytes[:], verifiedValue) + + bucket := ns.NestedReadWriteBucket(syncBucketName) + err := bucket.Put(birthdayBlockVerifiedName, verifiedBytes[:]) + if err != nil { + str := "failed to store birthday block verification" + return managerError(ErrDatabase, str, err) + } + + return nil +} + // managerExists returns whether or not the manager has already been created // in the given database namespace. func managerExists(ns walletdb.ReadBucket) bool {