Skip to content

Commit

Permalink
wallet: deprecate exported header extraction funcs
Browse files Browse the repository at this point in the history
This deprecates  udb.ExtractBlockHeaderParentHash and
udb.ExtractBlockHeaderHeight in preference of using an
 unexported copy of the extract function at the call site.
  • Loading branch information
dnldd committed Aug 6, 2019
1 parent d206287 commit 6c79b06
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
17 changes: 16 additions & 1 deletion wallet/rescan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package wallet

import (
"encoding/binary"
"context"
"time"

Expand Down Expand Up @@ -62,6 +63,20 @@ func NewRescanFilter(addresses []dcrutil.Address, unspentOutPoints []*wire.OutPo
return filter
}

// extractBlockHeaderHeight fetches the block height from wire encoded block
// header bytes.
func extractBlockHeaderHeight(header []byte) int32 {
const heightOffset = 128
return int32(binary.LittleEndian.Uint32(header[heightOffset:]))
}

// extractBlockHeaderUnixTime fetches the unix timestamp from wire encoded
// block header bytes.
func extractBlockHeaderUnixTime(header []byte) uint32 {
const timestampOffset = 136
return binary.LittleEndian.Uint32(header[timestampOffset:])
}

// AddAddress adds an address to the filter if it does not already exist.
func (f *RescanFilter) AddAddress(a dcrutil.Address) {
switch a := a.(type) {
Expand Down Expand Up @@ -269,7 +284,7 @@ func (w *Wallet) Rescan(ctx context.Context, n NetworkBackend, startHash *chainh
if err != nil {
return err
}
startHeight = udb.ExtractBlockHeaderHeight(header)
startHeight = extractBlockHeaderHeight(header)
return nil
})
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions wallet/udb/txmined.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func extractBlockHeaderParentHash(header []byte) []byte {
// ExtractBlockHeaderParentHash subslices the header to return the bytes of the
// parent block's hash. Must only be called on known good input.
//
// TODO: This really should not be exported by this package.
// DEPRECATED: to be removed in the next major release.
func ExtractBlockHeaderParentHash(header []byte) []byte {
return extractBlockHeaderParentHash(header)
}
Expand All @@ -476,7 +476,7 @@ func extractBlockHeaderHeight(header []byte) int32 {
// ExtractBlockHeaderHeight returns the height field that is encoded in the
// header. Must only be called on known good input.
//
// TODO: This really should not be exported by this package.
// DEPRECATED: to be removed in the next major release.
func ExtractBlockHeaderHeight(header []byte) int32 {
return extractBlockHeaderHeight(header)
}
Expand All @@ -490,7 +490,7 @@ func extractBlockHeaderUnixTime(header []byte) uint32 {
// header. Must only be called on known good input. Header timestamps are only
// 4 bytes and this value is actually limited to a maximum unix time of 2^32-1.
//
// TODO: This really should not be exported by this package.
// DEPRECATED: to be removed in the next major release.
func ExtractBlockHeaderTime(header []byte) int64 {
return int64(extractBlockHeaderUnixTime(header))
}
Expand Down
4 changes: 2 additions & 2 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2223,7 +2223,7 @@ func (w *Wallet) BlockInfo(blockID *BlockIdentifier) (*BlockInfo, error) {
if err != nil {
return err
}
height := udb.ExtractBlockHeaderHeight(header)
height := extractBlockHeaderHeight(header)
inMainChain, invalidated := w.TxStore.BlockInMainChain(dbtx, blockHash)
var confs int32
if inMainChain {
Expand All @@ -2234,7 +2234,7 @@ func (w *Wallet) BlockInfo(blockID *BlockIdentifier) (*BlockInfo, error) {
Height: height,
Confirmations: confs,
Header: header,
Timestamp: udb.ExtractBlockHeaderTime(header),
Timestamp: int64(extractBlockHeaderUnixTime(header)),
StakeInvalidated: invalidated,
}
return nil
Expand Down

0 comments on commit 6c79b06

Please sign in to comment.