From e7caccc866b2caef61aefc9790a952c94cd04fd5 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sun, 15 May 2016 13:22:21 +0300 Subject: [PATCH] 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. --- mempool/mempool.go | 8 +++----- mempool/policy.go | 8 +++++--- mempool/policy_test.go | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/mempool/mempool.go b/mempool/mempool.go index d8ce31e74..545335bce 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -67,9 +67,6 @@ type Config struct { // SigCache defines a signature cache to use. SigCache *txscript.SigCache - // TimeSource defines the timesource to use. - TimeSource blockchain.MedianTimeSource - // AddrIndex defines the optional address index instance to use for // indexing the unconfirmed transactions in the memory pool. // 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 // forbid their relaying. if !mp.cfg.Policy.RelayNonStd { - err := checkTransactionStandard(tx, nextBlockHeight, - mp.cfg.TimeSource, mp.cfg.Policy.MinRelayTxFee) + medianTimePast := mp.cfg.MedianTimePast() + err = checkTransactionStandard(tx, nextBlockHeight, + medianTimePast, mp.cfg.Policy.MinRelayTxFee) if err != nil { // Attempt to extract a reject code from the error so // it can be retained. When not possible, fall back to diff --git a/mempool/policy.go b/mempool/policy.go index 69fc0ef26..229471d49 100644 --- a/mempool/policy.go +++ b/mempool/policy.go @@ -6,6 +6,7 @@ package mempool import ( "fmt" + "time" "github.com/btcsuite/btcd/blockchain" "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 // of recognized forms, and not containing "dust" outputs (those that are // 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. msgTx := tx.MsgTx() 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 // considered for inclusion in a block. - adjustedTime := timeSource.AdjustedTime() - if !blockchain.IsFinalizedTransaction(tx, height, adjustedTime) { + if !blockchain.IsFinalizedTransaction(tx, height, medianTimePast) { return txRuleError(wire.RejectNonstandard, "transaction is not finalized") } diff --git a/mempool/policy_test.go b/mempool/policy_test.go index b3c7faac9..8637181eb 100644 --- a/mempool/policy_test.go +++ b/mempool/policy_test.go @@ -7,8 +7,8 @@ package mempool import ( "bytes" "testing" + "time" - "github.com/btcsuite/btcd/blockchain" "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/chaincfg" "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 { // Ensure standardness is as expected. err := checkTransactionStandard(btcutil.NewTx(&test.tx), - test.height, timeSource, DefaultMinRelayTxFee) + test.height, pastMedianTime, DefaultMinRelayTxFee) if err == nil && test.isStandard { // Test passes since function returned standard for a // transaction which is intended to be standard.