Move IsFinalizedTransaction to txscript.
This change moves IsFinalizedTransaction to txscript and also changes the first argument to take a wire.MsgTx instead of btcutil.Tx. This is needed for an upcoming diff in which txscript will require IsFinalizedTransaction and we do not want to import the btcd/blockchain.
This commit is contained in:
parent
bb8333a739
commit
17da2ba7fa
4 changed files with 43 additions and 38 deletions
|
@ -127,41 +127,6 @@ func IsCoinBase(tx *btcutil.Tx) bool {
|
||||||
return IsCoinBaseTx(tx.MsgTx())
|
return IsCoinBaseTx(tx.MsgTx())
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsFinalizedTransaction determines whether or not a transaction is finalized.
|
|
||||||
func IsFinalizedTransaction(tx *btcutil.Tx, blockHeight int64, blockTime time.Time) bool {
|
|
||||||
msgTx := tx.MsgTx()
|
|
||||||
|
|
||||||
// Lock time of zero means the transaction is finalized.
|
|
||||||
lockTime := msgTx.LockTime
|
|
||||||
if lockTime == 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// The lock time field of a transaction is either a block height at
|
|
||||||
// which the transaction is finalized or a timestamp depending on if the
|
|
||||||
// value is before the txscript.LockTimeThreshold. When it is under the
|
|
||||||
// threshold it is a block height.
|
|
||||||
blockTimeOrHeight := int64(0)
|
|
||||||
if lockTime < txscript.LockTimeThreshold {
|
|
||||||
blockTimeOrHeight = blockHeight
|
|
||||||
} else {
|
|
||||||
blockTimeOrHeight = blockTime.Unix()
|
|
||||||
}
|
|
||||||
if int64(lockTime) < blockTimeOrHeight {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// At this point, the transaction's lock time hasn't occured yet, but
|
|
||||||
// the transaction might still be finalized if the sequence number
|
|
||||||
// for all transaction inputs is maxed out.
|
|
||||||
for _, txIn := range msgTx.TxIn {
|
|
||||||
if txIn.Sequence != math.MaxUint32 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// isBIP0030Node returns whether or not the passed node represents one of the
|
// isBIP0030Node returns whether or not the passed node represents one of the
|
||||||
// two blocks that violate the BIP0030 rule which prevents transactions from
|
// two blocks that violate the BIP0030 rule which prevents transactions from
|
||||||
// overwriting old ones.
|
// overwriting old ones.
|
||||||
|
@ -707,7 +672,7 @@ func (b *BlockChain) checkBlockContext(block *btcutil.Block, prevNode *blockNode
|
||||||
|
|
||||||
// Ensure all transactions in the block are finalized.
|
// Ensure all transactions in the block are finalized.
|
||||||
for _, tx := range block.Transactions() {
|
for _, tx := range block.Transactions() {
|
||||||
if !IsFinalizedTransaction(tx, blockHeight,
|
if !txscript.IsFinalizedTransaction(tx.MsgTx(), blockHeight,
|
||||||
header.Timestamp) {
|
header.Timestamp) {
|
||||||
|
|
||||||
str := fmt.Sprintf("block contains unfinalized "+
|
str := fmt.Sprintf("block contains unfinalized "+
|
||||||
|
|
|
@ -237,7 +237,7 @@ func (mp *txMemPool) checkTransactionStandard(tx *btcutil.Tx, height int64) erro
|
||||||
// The transaction must be finalized to be standard and therefore
|
// The transaction must be finalized to be standard and therefore
|
||||||
// considered for inclusion in a block.
|
// considered for inclusion in a block.
|
||||||
adjustedTime := mp.server.timeSource.AdjustedTime()
|
adjustedTime := mp.server.timeSource.AdjustedTime()
|
||||||
if !blockchain.IsFinalizedTransaction(tx, height, adjustedTime) {
|
if !txscript.IsFinalizedTransaction(tx.MsgTx(), height, adjustedTime) {
|
||||||
return txRuleError(wire.RejectNonstandard,
|
return txRuleError(wire.RejectNonstandard,
|
||||||
"transaction is not finalized")
|
"transaction is not finalized")
|
||||||
}
|
}
|
||||||
|
|
|
@ -446,7 +446,7 @@ mempoolLoop:
|
||||||
minrLog.Tracef("Skipping coinbase tx %s", tx.Sha())
|
minrLog.Tracef("Skipping coinbase tx %s", tx.Sha())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !blockchain.IsFinalizedTransaction(tx, nextBlockHeight,
|
if !txscript.IsFinalizedTransaction(tx.MsgTx(), nextBlockHeight,
|
||||||
timeSource.AdjustedTime()) {
|
timeSource.AdjustedTime()) {
|
||||||
|
|
||||||
minrLog.Tracef("Skipping non-finalized tx %s", tx.Sha())
|
minrLog.Tracef("Skipping non-finalized tx %s", tx.Sha())
|
||||||
|
|
|
@ -4,6 +4,13 @@
|
||||||
|
|
||||||
package txscript
|
package txscript
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/wire"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// LockTimeThreshold is the number below which a lock time is
|
// LockTimeThreshold is the number below which a lock time is
|
||||||
// interpreted to be a block number. Since an average of one block
|
// interpreted to be a block number. Since an average of one block
|
||||||
|
@ -12,3 +19,36 @@ const (
|
||||||
// the lock time is a uint32, the max is sometime around 2106.
|
// the lock time is a uint32, the max is sometime around 2106.
|
||||||
LockTimeThreshold uint32 = 5e8 // Tue Nov 5 00:53:20 1985 UTC
|
LockTimeThreshold uint32 = 5e8 // Tue Nov 5 00:53:20 1985 UTC
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// IsFinalizedTransaction determines whether or not a transaction is finalized.
|
||||||
|
func IsFinalizedTransaction(msgTx *wire.MsgTx, blockHeight int64, blockTime time.Time) bool {
|
||||||
|
// Lock time of zero means the transaction is finalized.
|
||||||
|
lockTime := msgTx.LockTime
|
||||||
|
if lockTime == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// The lock time field of a transaction is either a block height at
|
||||||
|
// which the transaction is finalized or a timestamp depending on if the
|
||||||
|
// value is before the LockTimeThreshold. When it is under the
|
||||||
|
// threshold it is a block height.
|
||||||
|
blockTimeOrHeight := int64(0)
|
||||||
|
if lockTime < LockTimeThreshold {
|
||||||
|
blockTimeOrHeight = blockHeight
|
||||||
|
} else {
|
||||||
|
blockTimeOrHeight = blockTime.Unix()
|
||||||
|
}
|
||||||
|
if int64(lockTime) < blockTimeOrHeight {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// At this point, the transaction's lock time hasn't occured yet, but
|
||||||
|
// the transaction might still be finalized if the sequence number
|
||||||
|
// for all transaction inputs is maxed out.
|
||||||
|
for _, txIn := range msgTx.TxIn {
|
||||||
|
if txIn.Sequence != math.MaxUint32 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue