mempool: transaction finality checks now use median-time-past

This coincides with the mempool only, policy change which enforces
transaction finality according to the median-time-past rather than
blockheader timestamps. The behavior is pre-cursor to full blown BIP
113 consensus deployment, and subsequent activation.

As a result, the TimeSource field in the mempoolConfig is no longer
needed so it has been removed. Additionally, checkTransactionStandard has been
modified to instead take a time.Time as the mempool is no longer explicitly
dependant on a Chain instance.
This commit is contained in:
Olaoluwa Osuntokun 2016-05-15 13:22:21 +03:00
parent a82f67b538
commit e7caccc866
No known key found for this signature in database
GPG key ID: 9CC5B105D03521A2
3 changed files with 11 additions and 11 deletions

View file

@ -67,9 +67,6 @@ type Config struct {
// SigCache defines a signature cache to use. // SigCache defines a signature cache to use.
SigCache *txscript.SigCache SigCache *txscript.SigCache
// TimeSource defines the timesource to use.
TimeSource blockchain.MedianTimeSource
// AddrIndex defines the optional address index instance to use for // AddrIndex defines the optional address index instance to use for
// indexing the unconfirmed transactions in the memory pool. // indexing the unconfirmed transactions in the memory pool.
// This can be nil if the address index is not enabled. // This can be nil if the address index is not enabled.
@ -551,8 +548,9 @@ func (mp *TxPool) maybeAcceptTransaction(tx *btcutil.Tx, isNew, rateLimit bool)
// Don't allow non-standard transactions if the network parameters // Don't allow non-standard transactions if the network parameters
// forbid their relaying. // forbid their relaying.
if !mp.cfg.Policy.RelayNonStd { if !mp.cfg.Policy.RelayNonStd {
err := checkTransactionStandard(tx, nextBlockHeight, medianTimePast := mp.cfg.MedianTimePast()
mp.cfg.TimeSource, mp.cfg.Policy.MinRelayTxFee) err = checkTransactionStandard(tx, nextBlockHeight,
medianTimePast, mp.cfg.Policy.MinRelayTxFee)
if err != nil { if err != nil {
// Attempt to extract a reject code from the error so // Attempt to extract a reject code from the error so
// it can be retained. When not possible, fall back to // it can be retained. When not possible, fall back to

View file

@ -6,6 +6,7 @@ package mempool
import ( import (
"fmt" "fmt"
"time"
"github.com/btcsuite/btcd/blockchain" "github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/txscript"
@ -326,7 +327,9 @@ func isDust(txOut *wire.TxOut, minRelayTxFee btcutil.Amount) bool {
// finalized, conforming to more stringent size constraints, having scripts // finalized, conforming to more stringent size constraints, having scripts
// of recognized forms, and not containing "dust" outputs (those that are // of recognized forms, and not containing "dust" outputs (those that are
// so small it costs more to process them than they are worth). // so small it costs more to process them than they are worth).
func checkTransactionStandard(tx *btcutil.Tx, height int32, timeSource blockchain.MedianTimeSource, minRelayTxFee btcutil.Amount) error { func checkTransactionStandard(tx *btcutil.Tx, height int32,
medianTimePast time.Time, minRelayTxFee btcutil.Amount) error {
// The transaction must be a currently supported version. // The transaction must be a currently supported version.
msgTx := tx.MsgTx() msgTx := tx.MsgTx()
if msgTx.Version > wire.TxVersion || msgTx.Version < 1 { if msgTx.Version > wire.TxVersion || msgTx.Version < 1 {
@ -338,8 +341,7 @@ func checkTransactionStandard(tx *btcutil.Tx, height int32, timeSource blockchai
// 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 := timeSource.AdjustedTime() if !blockchain.IsFinalizedTransaction(tx, height, medianTimePast) {
if !blockchain.IsFinalizedTransaction(tx, height, adjustedTime) {
return txRuleError(wire.RejectNonstandard, return txRuleError(wire.RejectNonstandard,
"transaction is not finalized") "transaction is not finalized")
} }

View file

@ -7,8 +7,8 @@ package mempool
import ( import (
"bytes" "bytes"
"testing" "testing"
"time"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
@ -466,11 +466,11 @@ func TestCheckTransactionStandard(t *testing.T) {
}, },
} }
timeSource := blockchain.NewMedianTime() pastMedianTime := time.Now()
for _, test := range tests { for _, test := range tests {
// Ensure standardness is as expected. // Ensure standardness is as expected.
err := checkTransactionStandard(btcutil.NewTx(&test.tx), err := checkTransactionStandard(btcutil.NewTx(&test.tx),
test.height, timeSource, DefaultMinRelayTxFee) test.height, pastMedianTime, DefaultMinRelayTxFee)
if err == nil && test.isStandard { if err == nil && test.isStandard {
// Test passes since function returned standard for a // Test passes since function returned standard for a
// transaction which is intended to be standard. // transaction which is intended to be standard.