From de709f28f6132e7df5514de5981f8c0a976d113f Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 24 Aug 2016 14:12:13 -0700 Subject: [PATCH] blockchain: add new LockTimeToSequence function This commit adds a new function to the package: `LockTimeToSequence`. The function is a simple utility function which aides the caller to mapping a targeted time or block based relative lock-time to the appropriate sequence number. --- blockchain/chain.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/blockchain/chain.go b/blockchain/chain.go index 9016767f..6e721fa1 100644 --- a/blockchain/chain.go +++ b/blockchain/chain.go @@ -828,6 +828,24 @@ func (b *BlockChain) calcSequenceLock(tx *btcutil.Tx, utxoView *UtxoViewpoint, return sequenceLock, nil } +// LockTimeToSequence converts the passed relative locktime to a sequence +// number in accordance to BIP-68. +// See: https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki +// * (Compatibility) +func LockTimeToSequence(isSeconds bool, locktime uint32) uint32 { + // If we're expressing the relative lock time in blocks, then the + // corresponding sequence number is simply the desired input age. + if !isSeconds { + return locktime + } + + // Set the 22nd bit which indicates the lock time is in seconds, then + // shift the locktime over by 9 since the time granularity is in + // 512-second intervals (2^9). This results in a max lock-time of + // 33,553,920 seconds, or 1.1 years. + return wire.SequenceLockTimeIsSeconds | uint32(locktime>>wire.SequenceLockTimeGranularity) +} + // getReorganizeNodes finds the fork point between the main chain and the passed // node and returns a list of block nodes that would need to be detached from // the main chain and a list of block nodes that would need to be attached to