Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blockchain, fullblocktests, workmath, testhelper: add InvalidateBlock() method to BlockChain #2155

Merged
merged 10 commits into from
May 22, 2024
Merged
Prev Previous commit
Next Next commit
fullblocktests, testhelper: move uniqueOpReturnScript to testhelper
uniqueOpReturnScript is moved to testhelper and is exported so that the
code and be reused in package blockchain without introducing import
cycles.  The test code for invalidateblock and reconsiderblock that are
gonna be added in later commits uses the functions.
  • Loading branch information
kcalvinalvin committed Apr 22, 2024
commit 9093243d8bcc40c46ad612e1921e7556cecd636f
35 changes: 9 additions & 26 deletions blockchain/fullblocktests/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,30 +228,6 @@ func standardCoinbaseScript(blockHeight int32, extraNonce uint64) ([]byte, error
AddInt64(int64(extraNonce)).Script()
}

// opReturnScript returns a provably-pruneable OP_RETURN script with the
// provided data.
func opReturnScript(data []byte) []byte {
builder := txscript.NewScriptBuilder()
script, err := builder.AddOp(txscript.OP_RETURN).AddData(data).Script()
if err != nil {
panic(err)
}
return script
}

// uniqueOpReturnScript returns a standard provably-pruneable OP_RETURN script
// with a random uint64 encoded as the data.
func uniqueOpReturnScript() []byte {
rand, err := wire.RandomUint64()
if err != nil {
panic(err)
}

data := make([]byte, 8)
binary.LittleEndian.PutUint64(data[0:8], rand)
return opReturnScript(data)
}

// createCoinbaseTx returns a coinbase transaction paying an appropriate
// subsidy based on the passed block height. The coinbase signature script
// conforms to the requirements of version 2 blocks.
Expand Down Expand Up @@ -358,7 +334,11 @@ func createSpendTx(spend *testhelper.SpendableOut, fee btcutil.Amount) *wire.Msg
})
spendTx.AddTxOut(wire.NewTxOut(int64(spend.Amount-fee),
testhelper.OpTrueScript))
spendTx.AddTxOut(wire.NewTxOut(0, uniqueOpReturnScript()))
opRetScript, err := testhelper.UniqueOpReturnScript()
if err != nil {
panic(err)
}
spendTx.AddTxOut(wire.NewTxOut(0, opRetScript))

return spendTx
}
Expand Down Expand Up @@ -1959,7 +1939,10 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
const zeroCoin = int64(0)
spendTx := b.Transactions[1]
for i := 0; i < numAdditionalOutputs; i++ {
opRetScript := uniqueOpReturnScript()
opRetScript, err := testhelper.UniqueOpReturnScript()
if err != nil {
panic(err)
}
spendTx.AddTxOut(wire.NewTxOut(zeroCoin, opRetScript))
}
})
Expand Down
25 changes: 25 additions & 0 deletions blockchain/internal/testhelper/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testhelper

import (
"encoding/binary"
"math"
"runtime"

Expand All @@ -20,6 +21,30 @@ var (
LowFee = btcutil.Amount(1)
)

// OpReturnScript returns a provably-pruneable OP_RETURN script with the
// provided data.
func OpReturnScript(data []byte) ([]byte, error) {
builder := txscript.NewScriptBuilder()
script, err := builder.AddOp(txscript.OP_RETURN).AddData(data).Script()
if err != nil {
return nil, err
}
return script, nil
}

// UniqueOpReturnScript returns a standard provably-pruneable OP_RETURN script
// with a random uint64 encoded as the data.
func UniqueOpReturnScript() ([]byte, error) {
rand, err := wire.RandomUint64()
if err != nil {
return nil, err
}

data := make([]byte, 8)
binary.LittleEndian.PutUint64(data[0:8], rand)
return OpReturnScript(data)
}

// SpendableOut represents a transaction output that is spendable along with
// additional metadata such as the block its in and how much it pays.
type SpendableOut struct {
Expand Down