Merge remote-tracking branch 'origin/master' into dev-20-primitive-blockdag

This commit is contained in:
Mike Zak 2018-07-01 15:58:02 +03:00
commit d92690f7e5
20 changed files with 1291 additions and 1455 deletions

View File

@ -175,11 +175,11 @@ const (
// standard pay-to-pubkey-hash script along with the pubkey hash it is paying to // standard pay-to-pubkey-hash script along with the pubkey hash it is paying to
// if it is. // if it is.
func isPubKeyHash(script []byte) (bool, []byte) { func isPubKeyHash(script []byte) (bool, []byte) {
if len(script) == 25 && script[0] == txscript.OP_DUP && if len(script) == 25 && script[0] == txscript.OpDup &&
script[1] == txscript.OP_HASH160 && script[1] == txscript.OpHash160 &&
script[2] == txscript.OP_DATA_20 && script[2] == txscript.OpData20 &&
script[23] == txscript.OP_EQUALVERIFY && script[23] == txscript.OpEqualVerify &&
script[24] == txscript.OP_CHECKSIG { script[24] == txscript.OpCheckSig {
return true, script[3:23] return true, script[3:23]
} }
@ -191,9 +191,9 @@ func isPubKeyHash(script []byte) (bool, []byte) {
// standard pay-to-script-hash script along with the script hash it is paying to // standard pay-to-script-hash script along with the script hash it is paying to
// if it is. // if it is.
func isScriptHash(script []byte) (bool, []byte) { func isScriptHash(script []byte) (bool, []byte) {
if len(script) == 23 && script[0] == txscript.OP_HASH160 && if len(script) == 23 && script[0] == txscript.OpHash160 &&
script[1] == txscript.OP_DATA_20 && script[1] == txscript.OpData20 &&
script[22] == txscript.OP_EQUAL { script[22] == txscript.OpEqual {
return true, script[2:22] return true, script[2:22]
} }
@ -212,8 +212,8 @@ func isScriptHash(script []byte) (bool, []byte) {
// to a valid compressed or uncompressed pubkey. // to a valid compressed or uncompressed pubkey.
func isPubKey(script []byte) (bool, []byte) { func isPubKey(script []byte) (bool, []byte) {
// Pay-to-compressed-pubkey script. // Pay-to-compressed-pubkey script.
if len(script) == 35 && script[0] == txscript.OP_DATA_33 && if len(script) == 35 && script[0] == txscript.OpData33 &&
script[34] == txscript.OP_CHECKSIG && (script[1] == 0x02 || script[34] == txscript.OpCheckSig && (script[1] == 0x02 ||
script[1] == 0x03) { script[1] == 0x03) {
// Ensure the public key is valid. // Ensure the public key is valid.
@ -225,8 +225,8 @@ func isPubKey(script []byte) (bool, []byte) {
} }
// Pay-to-uncompressed-pubkey script. // Pay-to-uncompressed-pubkey script.
if len(script) == 67 && script[0] == txscript.OP_DATA_65 && if len(script) == 67 && script[0] == txscript.OpData65 &&
script[66] == txscript.OP_CHECKSIG && script[1] == 0x04 { script[66] == txscript.OpCheckSig && script[1] == 0x04 {
// Ensure the public key is valid. // Ensure the public key is valid.
serializedPubKey := script[1:66] serializedPubKey := script[1:66]
@ -361,32 +361,32 @@ func decompressScript(compressedPkScript []byte) []byte {
// <OP_DUP><OP_HASH160><20 byte hash><OP_EQUALVERIFY><OP_CHECKSIG> // <OP_DUP><OP_HASH160><20 byte hash><OP_EQUALVERIFY><OP_CHECKSIG>
case cstPayToPubKeyHash: case cstPayToPubKeyHash:
pkScript := make([]byte, 25) pkScript := make([]byte, 25)
pkScript[0] = txscript.OP_DUP pkScript[0] = txscript.OpDup
pkScript[1] = txscript.OP_HASH160 pkScript[1] = txscript.OpHash160
pkScript[2] = txscript.OP_DATA_20 pkScript[2] = txscript.OpData20
copy(pkScript[3:], compressedPkScript[bytesRead:bytesRead+20]) copy(pkScript[3:], compressedPkScript[bytesRead:bytesRead+20])
pkScript[23] = txscript.OP_EQUALVERIFY pkScript[23] = txscript.OpEqualVerify
pkScript[24] = txscript.OP_CHECKSIG pkScript[24] = txscript.OpCheckSig
return pkScript return pkScript
// Pay-to-script-hash script. The resulting script is: // Pay-to-script-hash script. The resulting script is:
// <OP_HASH160><20 byte script hash><OP_EQUAL> // <OP_HASH160><20 byte script hash><OP_EQUAL>
case cstPayToScriptHash: case cstPayToScriptHash:
pkScript := make([]byte, 23) pkScript := make([]byte, 23)
pkScript[0] = txscript.OP_HASH160 pkScript[0] = txscript.OpHash160
pkScript[1] = txscript.OP_DATA_20 pkScript[1] = txscript.OpData20
copy(pkScript[2:], compressedPkScript[bytesRead:bytesRead+20]) copy(pkScript[2:], compressedPkScript[bytesRead:bytesRead+20])
pkScript[22] = txscript.OP_EQUAL pkScript[22] = txscript.OpEqual
return pkScript return pkScript
// Pay-to-compressed-pubkey script. The resulting script is: // Pay-to-compressed-pubkey script. The resulting script is:
// <OP_DATA_33><33 byte compressed pubkey><OP_CHECKSIG> // <OP_DATA_33><33 byte compressed pubkey><OP_CHECKSIG>
case cstPayToPubKeyComp2, cstPayToPubKeyComp3: case cstPayToPubKeyComp2, cstPayToPubKeyComp3:
pkScript := make([]byte, 35) pkScript := make([]byte, 35)
pkScript[0] = txscript.OP_DATA_33 pkScript[0] = txscript.OpData33
pkScript[1] = byte(encodedScriptSize) pkScript[1] = byte(encodedScriptSize)
copy(pkScript[2:], compressedPkScript[bytesRead:bytesRead+32]) copy(pkScript[2:], compressedPkScript[bytesRead:bytesRead+32])
pkScript[34] = txscript.OP_CHECKSIG pkScript[34] = txscript.OpCheckSig
return pkScript return pkScript
// Pay-to-uncompressed-pubkey script. The resulting script is: // Pay-to-uncompressed-pubkey script. The resulting script is:
@ -405,9 +405,9 @@ func decompressScript(compressedPkScript []byte) []byte {
} }
pkScript := make([]byte, 67) pkScript := make([]byte, 67)
pkScript[0] = txscript.OP_DATA_65 pkScript[0] = txscript.OpData65
copy(pkScript[1:], key.SerializeUncompressed()) copy(pkScript[1:], key.SerializeUncompressed())
pkScript[66] = txscript.OP_CHECKSIG pkScript[66] = txscript.OpCheckSig
return pkScript return pkScript
} }

View File

@ -46,7 +46,7 @@ const (
var ( var (
// opTrueScript is simply a public key script that contains the OP_TRUE // opTrueScript is simply a public key script that contains the OP_TRUE
// opcode. It is defined here to reduce garbage creation. // opcode. It is defined here to reduce garbage creation.
opTrueScript = []byte{txscript.OP_TRUE} opTrueScript = []byte{txscript.OpTrue}
// lowFee is a single satoshi and exists to make the test code more // lowFee is a single satoshi and exists to make the test code more
// readable. // readable.
@ -218,8 +218,8 @@ func makeTestGenerator(params *dagconfig.Params) (testGenerator, error) {
func payToScriptHashScript(redeemScript []byte) []byte { func payToScriptHashScript(redeemScript []byte) []byte {
redeemScriptHash := btcutil.Hash160(redeemScript) redeemScriptHash := btcutil.Hash160(redeemScript)
script, err := txscript.NewScriptBuilder(). script, err := txscript.NewScriptBuilder().
AddOp(txscript.OP_HASH160).AddData(redeemScriptHash). AddOp(txscript.OpHash160).AddData(redeemScriptHash).
AddOp(txscript.OP_EQUAL).Script() AddOp(txscript.OpEqual).Script()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -252,7 +252,7 @@ func standardCoinbaseScript(blockHeight int32, extraNonce uint64) ([]byte, error
// provided data. // provided data.
func opReturnScript(data []byte) []byte { func opReturnScript(data []byte) []byte {
builder := txscript.NewScriptBuilder() builder := txscript.NewScriptBuilder()
script, err := builder.AddOp(txscript.OP_RETURN).AddData(data).Script() script, err := builder.AddOp(txscript.OpReturn).AddData(data).Script()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -774,7 +774,7 @@ func (g *testGenerator) assertTipBlockTxOutOpReturn(txIndex, txOutIndex uint32)
} }
txOut := tx.TxOut[txOutIndex] txOut := tx.TxOut[txOutIndex]
if txOut.PkScript[0] != txscript.OP_RETURN { if txOut.PkScript[0] != txscript.OpReturn {
panic(fmt.Sprintf("transaction index %d output %d in block %q "+ panic(fmt.Sprintf("transaction index %d output %d in block %q "+
"(height %d) is not an OP_RETURN", txIndex, txOutIndex, "(height %d) is not an OP_RETURN", txIndex, txOutIndex,
g.tipName, g.tipHeight)) g.tipName, g.tipHeight))
@ -1051,7 +1051,7 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// ... -> b5(2) -> b12(3) -> b13(4) -> b15(5) // ... -> b5(2) -> b12(3) -> b13(4) -> b15(5)
// \-> b3(1) -> b4(2) // \-> b3(1) -> b4(2)
g.setTip("b13") g.setTip("b13")
manySigOps := repeatOpcode(txscript.OP_CHECKSIG, maxBlockSigOps) manySigOps := repeatOpcode(txscript.OpCheckSig, maxBlockSigOps)
g.nextBlock("b15", outs[5], replaceSpendScript(manySigOps)) g.nextBlock("b15", outs[5], replaceSpendScript(manySigOps))
g.assertTipBlockSigOpsCount(maxBlockSigOps) g.assertTipBlockSigOpsCount(maxBlockSigOps)
accepted() accepted()
@ -1062,7 +1062,7 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// ... -> b5(2) -> b12(3) -> b13(4) -> b15(5) // ... -> b5(2) -> b12(3) -> b13(4) -> b15(5)
// \ \-> b16(7) // \ \-> b16(7)
// \-> b3(1) -> b4(2) // \-> b3(1) -> b4(2)
tooManySigOps := repeatOpcode(txscript.OP_CHECKSIG, maxBlockSigOps+1) tooManySigOps := repeatOpcode(txscript.OpCheckSig, maxBlockSigOps+1)
g.nextBlock("b16", outs[6], replaceSpendScript(tooManySigOps)) g.nextBlock("b16", outs[6], replaceSpendScript(tooManySigOps))
g.assertTipBlockSigOpsCount(maxBlockSigOps + 1) g.assertTipBlockSigOpsCount(maxBlockSigOps + 1)
rejected(blockdag.ErrTooManySigOps) rejected(blockdag.ErrTooManySigOps)
@ -1203,7 +1203,7 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// ... -> b30(7) -> b31(8) // ... -> b30(7) -> b31(8)
// //
// OP_CHECKMULTISIG counts for 20 sigops. // OP_CHECKMULTISIG counts for 20 sigops.
manySigOps = repeatOpcode(txscript.OP_CHECKMULTISIG, maxBlockSigOps/20) manySigOps = repeatOpcode(txscript.OpCheckMultiSig, maxBlockSigOps/20)
g.nextBlock("b31", outs[8], replaceSpendScript(manySigOps)) g.nextBlock("b31", outs[8], replaceSpendScript(manySigOps))
g.assertTipBlockSigOpsCount(maxBlockSigOps) g.assertTipBlockSigOpsCount(maxBlockSigOps)
accepted() accepted()
@ -1215,8 +1215,8 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// \-> b32(9) // \-> b32(9)
// //
// OP_CHECKMULTISIG counts for 20 sigops. // OP_CHECKMULTISIG counts for 20 sigops.
tooManySigOps = repeatOpcode(txscript.OP_CHECKMULTISIG, maxBlockSigOps/20) tooManySigOps = repeatOpcode(txscript.OpCheckMultiSig, maxBlockSigOps/20)
tooManySigOps = append(manySigOps, txscript.OP_CHECKSIG) tooManySigOps = append(manySigOps, txscript.OpCheckSig)
g.nextBlock("b32", outs[9], replaceSpendScript(tooManySigOps)) g.nextBlock("b32", outs[9], replaceSpendScript(tooManySigOps))
g.assertTipBlockSigOpsCount(maxBlockSigOps + 1) g.assertTipBlockSigOpsCount(maxBlockSigOps + 1)
rejected(blockdag.ErrTooManySigOps) rejected(blockdag.ErrTooManySigOps)
@ -1225,7 +1225,7 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// //
// ... -> b31(8) -> b33(9) // ... -> b31(8) -> b33(9)
g.setTip("b31") g.setTip("b31")
manySigOps = repeatOpcode(txscript.OP_CHECKMULTISIGVERIFY, maxBlockSigOps/20) manySigOps = repeatOpcode(txscript.OpCheckMultiSigVerify, maxBlockSigOps/20)
g.nextBlock("b33", outs[9], replaceSpendScript(manySigOps)) g.nextBlock("b33", outs[9], replaceSpendScript(manySigOps))
g.assertTipBlockSigOpsCount(maxBlockSigOps) g.assertTipBlockSigOpsCount(maxBlockSigOps)
accepted() accepted()
@ -1236,8 +1236,8 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// ... -> b33(9) // ... -> b33(9)
// \-> b34(10) // \-> b34(10)
// //
tooManySigOps = repeatOpcode(txscript.OP_CHECKMULTISIGVERIFY, maxBlockSigOps/20) tooManySigOps = repeatOpcode(txscript.OpCheckMultiSigVerify, maxBlockSigOps/20)
tooManySigOps = append(manySigOps, txscript.OP_CHECKSIG) tooManySigOps = append(manySigOps, txscript.OpCheckSig)
g.nextBlock("b34", outs[10], replaceSpendScript(tooManySigOps)) g.nextBlock("b34", outs[10], replaceSpendScript(tooManySigOps))
g.assertTipBlockSigOpsCount(maxBlockSigOps + 1) g.assertTipBlockSigOpsCount(maxBlockSigOps + 1)
rejected(blockdag.ErrTooManySigOps) rejected(blockdag.ErrTooManySigOps)
@ -1247,7 +1247,7 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// ... -> b33(9) -> b35(10) // ... -> b33(9) -> b35(10)
// //
g.setTip("b33") g.setTip("b33")
manySigOps = repeatOpcode(txscript.OP_CHECKSIGVERIFY, maxBlockSigOps) manySigOps = repeatOpcode(txscript.OpCheckSigVerify, maxBlockSigOps)
g.nextBlock("b35", outs[10], replaceSpendScript(manySigOps)) g.nextBlock("b35", outs[10], replaceSpendScript(manySigOps))
g.assertTipBlockSigOpsCount(maxBlockSigOps) g.assertTipBlockSigOpsCount(maxBlockSigOps)
accepted() accepted()
@ -1258,7 +1258,7 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// ... -> b35(10) // ... -> b35(10)
// \-> b36(11) // \-> b36(11)
// //
tooManySigOps = repeatOpcode(txscript.OP_CHECKSIGVERIFY, maxBlockSigOps+1) tooManySigOps = repeatOpcode(txscript.OpCheckSigVerify, maxBlockSigOps+1)
g.nextBlock("b36", outs[11], replaceSpendScript(tooManySigOps)) g.nextBlock("b36", outs[11], replaceSpendScript(tooManySigOps))
g.assertTipBlockSigOpsCount(maxBlockSigOps + 1) g.assertTipBlockSigOpsCount(maxBlockSigOps + 1)
rejected(blockdag.ErrTooManySigOps) rejected(blockdag.ErrTooManySigOps)
@ -1292,9 +1292,9 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// signature operations to be used in the next three blocks. // signature operations to be used in the next three blocks.
const redeemScriptSigOps = 9 const redeemScriptSigOps = 9
redeemScript := pushDataScript(g.privKey.PubKey().SerializeCompressed()) redeemScript := pushDataScript(g.privKey.PubKey().SerializeCompressed())
redeemScript = append(redeemScript, bytes.Repeat([]byte{txscript.OP_2DUP, redeemScript = append(redeemScript, bytes.Repeat([]byte{txscript.Op2Dup,
txscript.OP_CHECKSIGVERIFY}, redeemScriptSigOps-1)...) txscript.OpCheckSigVerify}, redeemScriptSigOps-1)...)
redeemScript = append(redeemScript, txscript.OP_CHECKSIG) redeemScript = append(redeemScript, txscript.OpCheckSig)
assertScriptSigOpsCount(redeemScript, redeemScriptSigOps) assertScriptSigOpsCount(redeemScript, redeemScriptSigOps)
// Create a block that has enough pay-to-script-hash outputs such that // Create a block that has enough pay-to-script-hash outputs such that
@ -1351,7 +1351,7 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
fill := maxBlockSigOps - (txnsNeeded * redeemScriptSigOps) + 1 fill := maxBlockSigOps - (txnsNeeded * redeemScriptSigOps) + 1
finalTx := b.Transactions[len(b.Transactions)-1] finalTx := b.Transactions[len(b.Transactions)-1]
tx := createSpendTxForTx(finalTx, lowFee) tx := createSpendTxForTx(finalTx, lowFee)
tx.TxOut[0].PkScript = repeatOpcode(txscript.OP_CHECKSIG, fill) tx.TxOut[0].PkScript = repeatOpcode(txscript.OpCheckSig, fill)
b.AddTransaction(tx) b.AddTransaction(tx)
}) })
rejected(blockdag.ErrTooManySigOps) rejected(blockdag.ErrTooManySigOps)
@ -1385,7 +1385,7 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
} }
finalTx := b.Transactions[len(b.Transactions)-1] finalTx := b.Transactions[len(b.Transactions)-1]
tx := createSpendTxForTx(finalTx, lowFee) tx := createSpendTxForTx(finalTx, lowFee)
tx.TxOut[0].PkScript = repeatOpcode(txscript.OP_CHECKSIG, fill) tx.TxOut[0].PkScript = repeatOpcode(txscript.OpCheckSig, fill)
b.AddTransaction(tx) b.AddTransaction(tx)
}) })
accepted() accepted()
@ -1886,8 +1886,8 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// ... -> b69(20) // ... -> b69(20)
// \-> b70(21) // \-> b70(21)
scriptSize := maxBlockSigOps + 5 + (maxScriptElementSize + 1) + 1 scriptSize := maxBlockSigOps + 5 + (maxScriptElementSize + 1) + 1
tooManySigOps = repeatOpcode(txscript.OP_CHECKSIG, scriptSize) tooManySigOps = repeatOpcode(txscript.OpCheckSig, scriptSize)
tooManySigOps[maxBlockSigOps] = txscript.OP_PUSHDATA4 tooManySigOps[maxBlockSigOps] = txscript.OpPushData4
binary.LittleEndian.PutUint32(tooManySigOps[maxBlockSigOps+1:], binary.LittleEndian.PutUint32(tooManySigOps[maxBlockSigOps+1:],
maxScriptElementSize+1) maxScriptElementSize+1)
g.nextBlock("b70", outs[21], replaceSpendScript(tooManySigOps)) g.nextBlock("b70", outs[21], replaceSpendScript(tooManySigOps))
@ -1903,8 +1903,8 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// \-> b71(21) // \-> b71(21)
g.setTip("b69") g.setTip("b69")
scriptSize = maxBlockSigOps + 5 + maxScriptElementSize + 1 scriptSize = maxBlockSigOps + 5 + maxScriptElementSize + 1
tooManySigOps = repeatOpcode(txscript.OP_CHECKSIG, scriptSize) tooManySigOps = repeatOpcode(txscript.OpCheckSig, scriptSize)
tooManySigOps[maxBlockSigOps+1] = txscript.OP_PUSHDATA4 tooManySigOps[maxBlockSigOps+1] = txscript.OpPushData4
binary.LittleEndian.PutUint32(tooManySigOps[maxBlockSigOps+2:], 0xffffffff) binary.LittleEndian.PutUint32(tooManySigOps[maxBlockSigOps+2:], 0xffffffff)
g.nextBlock("b71", outs[21], replaceSpendScript(tooManySigOps)) g.nextBlock("b71", outs[21], replaceSpendScript(tooManySigOps))
g.assertTipBlockSigOpsCount(maxBlockSigOps + 1) g.assertTipBlockSigOpsCount(maxBlockSigOps + 1)
@ -1919,8 +1919,8 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// ... -> b69(20) -> b72(21) // ... -> b69(20) -> b72(21)
g.setTip("b69") g.setTip("b69")
scriptSize = maxBlockSigOps + 5 + maxScriptElementSize scriptSize = maxBlockSigOps + 5 + maxScriptElementSize
manySigOps = repeatOpcode(txscript.OP_CHECKSIG, scriptSize) manySigOps = repeatOpcode(txscript.OpCheckSig, scriptSize)
manySigOps[maxBlockSigOps] = txscript.OP_PUSHDATA4 manySigOps[maxBlockSigOps] = txscript.OpPushData4
binary.LittleEndian.PutUint32(manySigOps[maxBlockSigOps+1:], 0xffffffff) binary.LittleEndian.PutUint32(manySigOps[maxBlockSigOps+1:], 0xffffffff)
g.nextBlock("b72", outs[21], replaceSpendScript(manySigOps)) g.nextBlock("b72", outs[21], replaceSpendScript(manySigOps))
g.assertTipBlockSigOpsCount(maxBlockSigOps) g.assertTipBlockSigOpsCount(maxBlockSigOps)
@ -1933,8 +1933,8 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// //
// ... -> b72(21) -> b73(22) // ... -> b72(21) -> b73(22)
scriptSize = maxBlockSigOps + 5 + (maxScriptElementSize + 1) scriptSize = maxBlockSigOps + 5 + (maxScriptElementSize + 1)
manySigOps = repeatOpcode(txscript.OP_CHECKSIG, scriptSize) manySigOps = repeatOpcode(txscript.OpCheckSig, scriptSize)
manySigOps[maxBlockSigOps] = txscript.OP_PUSHDATA4 manySigOps[maxBlockSigOps] = txscript.OpPushData4
g.nextBlock("b73", outs[22], replaceSpendScript(manySigOps)) g.nextBlock("b73", outs[22], replaceSpendScript(manySigOps))
g.assertTipBlockSigOpsCount(maxBlockSigOps) g.assertTipBlockSigOpsCount(maxBlockSigOps)
accepted() accepted()
@ -1946,12 +1946,12 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// Create block with an invalid opcode in a dead execution path. // Create block with an invalid opcode in a dead execution path.
// //
// ... -> b73(22) -> b74(23) // ... -> b73(22) -> b74(23)
script := []byte{txscript.OP_IF, txscript.OP_INVALIDOPCODE, script := []byte{txscript.OpIf, txscript.OpInvalidOpCode,
txscript.OP_ELSE, txscript.OP_TRUE, txscript.OP_ENDIF} txscript.OpElse, txscript.OpTrue, txscript.OpEndIf}
g.nextBlock("b74", outs[23], replaceSpendScript(script), func(b *wire.MsgBlock) { g.nextBlock("b74", outs[23], replaceSpendScript(script), func(b *wire.MsgBlock) {
tx2 := b.Transactions[1] tx2 := b.Transactions[1]
tx3 := createSpendTxForTx(tx2, lowFee) tx3 := createSpendTxForTx(tx2, lowFee)
tx3.TxIn[0].SignatureScript = []byte{txscript.OP_FALSE} tx3.TxIn[0].SignatureScript = []byte{txscript.OpFalse}
b.AddTransaction(tx3) b.AddTransaction(tx3)
}) })
accepted() accepted()

View File

@ -599,11 +599,11 @@ func ExtractCoinbaseHeight(coinbaseTx *btcutil.Tx) (int32, error) {
// Detect the case when the block height is a small integer encoded with // Detect the case when the block height is a small integer encoded with
// as single byte. // as single byte.
opcode := int(sigScript[0]) opcode := int(sigScript[0])
if opcode == txscript.OP_0 { if opcode == txscript.Op0 {
return 0, nil return 0, nil
} }
if opcode >= txscript.OP_1 && opcode <= txscript.OP_16 { if opcode >= txscript.Op1 && opcode <= txscript.Op16 {
return int32(opcode - (txscript.OP_1 - 1)), nil return int32(opcode - (txscript.Op1 - 1)), nil
} }
// Otherwise, the opcode is the length of the following bytes which // Otherwise, the opcode is the length of the following bytes which

View File

@ -114,72 +114,72 @@ func TestCheckPkScriptStandard(t *testing.T) {
}{ }{
{ {
"key1 and key2", "key1 and key2",
txscript.NewScriptBuilder().AddOp(txscript.OP_2). txscript.NewScriptBuilder().AddOp(txscript.Op2).
AddData(pubKeys[0]).AddData(pubKeys[1]). AddData(pubKeys[0]).AddData(pubKeys[1]).
AddOp(txscript.OP_2).AddOp(txscript.OP_CHECKMULTISIG), AddOp(txscript.Op2).AddOp(txscript.OpCheckMultiSig),
true, true,
}, },
{ {
"key1 or key2", "key1 or key2",
txscript.NewScriptBuilder().AddOp(txscript.OP_1). txscript.NewScriptBuilder().AddOp(txscript.Op1).
AddData(pubKeys[0]).AddData(pubKeys[1]). AddData(pubKeys[0]).AddData(pubKeys[1]).
AddOp(txscript.OP_2).AddOp(txscript.OP_CHECKMULTISIG), AddOp(txscript.Op2).AddOp(txscript.OpCheckMultiSig),
true, true,
}, },
{ {
"escrow", "escrow",
txscript.NewScriptBuilder().AddOp(txscript.OP_2). txscript.NewScriptBuilder().AddOp(txscript.Op2).
AddData(pubKeys[0]).AddData(pubKeys[1]). AddData(pubKeys[0]).AddData(pubKeys[1]).
AddData(pubKeys[2]). AddData(pubKeys[2]).
AddOp(txscript.OP_3).AddOp(txscript.OP_CHECKMULTISIG), AddOp(txscript.Op3).AddOp(txscript.OpCheckMultiSig),
true, true,
}, },
{ {
"one of four", "one of four",
txscript.NewScriptBuilder().AddOp(txscript.OP_1). txscript.NewScriptBuilder().AddOp(txscript.Op1).
AddData(pubKeys[0]).AddData(pubKeys[1]). AddData(pubKeys[0]).AddData(pubKeys[1]).
AddData(pubKeys[2]).AddData(pubKeys[3]). AddData(pubKeys[2]).AddData(pubKeys[3]).
AddOp(txscript.OP_4).AddOp(txscript.OP_CHECKMULTISIG), AddOp(txscript.Op4).AddOp(txscript.OpCheckMultiSig),
false, false,
}, },
{ {
"malformed1", "malformed1",
txscript.NewScriptBuilder().AddOp(txscript.OP_3). txscript.NewScriptBuilder().AddOp(txscript.Op3).
AddData(pubKeys[0]).AddData(pubKeys[1]). AddData(pubKeys[0]).AddData(pubKeys[1]).
AddOp(txscript.OP_2).AddOp(txscript.OP_CHECKMULTISIG), AddOp(txscript.Op2).AddOp(txscript.OpCheckMultiSig),
false, false,
}, },
{ {
"malformed2", "malformed2",
txscript.NewScriptBuilder().AddOp(txscript.OP_2). txscript.NewScriptBuilder().AddOp(txscript.Op2).
AddData(pubKeys[0]).AddData(pubKeys[1]). AddData(pubKeys[0]).AddData(pubKeys[1]).
AddOp(txscript.OP_3).AddOp(txscript.OP_CHECKMULTISIG), AddOp(txscript.Op3).AddOp(txscript.OpCheckMultiSig),
false, false,
}, },
{ {
"malformed3", "malformed3",
txscript.NewScriptBuilder().AddOp(txscript.OP_0). txscript.NewScriptBuilder().AddOp(txscript.Op0).
AddData(pubKeys[0]).AddData(pubKeys[1]). AddData(pubKeys[0]).AddData(pubKeys[1]).
AddOp(txscript.OP_2).AddOp(txscript.OP_CHECKMULTISIG), AddOp(txscript.Op2).AddOp(txscript.OpCheckMultiSig),
false, false,
}, },
{ {
"malformed4", "malformed4",
txscript.NewScriptBuilder().AddOp(txscript.OP_1). txscript.NewScriptBuilder().AddOp(txscript.Op1).
AddData(pubKeys[0]).AddData(pubKeys[1]). AddData(pubKeys[0]).AddData(pubKeys[1]).
AddOp(txscript.OP_0).AddOp(txscript.OP_CHECKMULTISIG), AddOp(txscript.Op0).AddOp(txscript.OpCheckMultiSig),
false, false,
}, },
{ {
"malformed5", "malformed5",
txscript.NewScriptBuilder().AddOp(txscript.OP_1). txscript.NewScriptBuilder().AddOp(txscript.Op1).
AddData(pubKeys[0]).AddData(pubKeys[1]). AddData(pubKeys[0]).AddData(pubKeys[1]).
AddOp(txscript.OP_CHECKMULTISIG), AddOp(txscript.OpCheckMultiSig),
false, false,
}, },
{ {
"malformed6", "malformed6",
txscript.NewScriptBuilder().AddOp(txscript.OP_1). txscript.NewScriptBuilder().AddOp(txscript.Op1).
AddData(pubKeys[0]).AddData(pubKeys[1]), AddData(pubKeys[0]).AddData(pubKeys[1]),
false, false,
}, },
@ -392,7 +392,7 @@ func TestCheckTransactionStandard(t *testing.T) {
TxIn: []*wire.TxIn{{ TxIn: []*wire.TxIn{{
PreviousOutPoint: dummyPrevOut, PreviousOutPoint: dummyPrevOut,
SignatureScript: []byte{ SignatureScript: []byte{
txscript.OP_CHECKSIGVERIFY}, txscript.OpCheckSigVerify},
Sequence: wire.MaxTxInSequenceNum, Sequence: wire.MaxTxInSequenceNum,
}}, }},
TxOut: []*wire.TxOut{&dummyTxOut}, TxOut: []*wire.TxOut{&dummyTxOut},
@ -409,7 +409,7 @@ func TestCheckTransactionStandard(t *testing.T) {
TxIn: []*wire.TxIn{&dummyTxIn}, TxIn: []*wire.TxIn{&dummyTxIn},
TxOut: []*wire.TxOut{{ TxOut: []*wire.TxOut{{
Value: 100000000, Value: 100000000,
PkScript: []byte{txscript.OP_TRUE}, PkScript: []byte{txscript.OpTrue},
}}, }},
LockTime: 0, LockTime: 0,
}, },
@ -424,10 +424,10 @@ func TestCheckTransactionStandard(t *testing.T) {
TxIn: []*wire.TxIn{&dummyTxIn}, TxIn: []*wire.TxIn{&dummyTxIn},
TxOut: []*wire.TxOut{{ TxOut: []*wire.TxOut{{
Value: 0, Value: 0,
PkScript: []byte{txscript.OP_RETURN}, PkScript: []byte{txscript.OpReturn},
}, { }, {
Value: 0, Value: 0,
PkScript: []byte{txscript.OP_RETURN}, PkScript: []byte{txscript.OpReturn},
}}, }},
LockTime: 0, LockTime: 0,
}, },
@ -457,7 +457,7 @@ func TestCheckTransactionStandard(t *testing.T) {
TxIn: []*wire.TxIn{&dummyTxIn}, TxIn: []*wire.TxIn{&dummyTxIn},
TxOut: []*wire.TxOut{{ TxOut: []*wire.TxOut{{
Value: 0, Value: 0,
PkScript: []byte{txscript.OP_RETURN}, PkScript: []byte{txscript.OpReturn},
}}, }},
LockTime: 0, LockTime: 0,
}, },

View File

@ -257,7 +257,7 @@ func createCoinbaseTx(params *dagconfig.Params, coinbaseScript []byte, nextBlock
} else { } else {
var err error var err error
scriptBuilder := txscript.NewScriptBuilder() scriptBuilder := txscript.NewScriptBuilder()
pkScript, err = scriptBuilder.AddOp(txscript.OP_TRUE).Script() pkScript, err = scriptBuilder.AddOp(txscript.OpTrue).Script()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -140,7 +140,7 @@ func (vm *Engine) executeOpcode(pop *parsedOpcode) error {
} }
// Note that this includes OP_RESERVED which counts as a push operation. // Note that this includes OP_RESERVED which counts as a push operation.
if pop.opcode.value > OP_16 { if pop.opcode.value > Op16 {
vm.numOps++ vm.numOps++
if vm.numOps > MaxOpsPerScript { if vm.numOps > MaxOpsPerScript {
str := fmt.Sprintf("exceeded max operation limit of %d", str := fmt.Sprintf("exceeded max operation limit of %d",
@ -163,7 +163,7 @@ func (vm *Engine) executeOpcode(pop *parsedOpcode) error {
// Ensure all executed data push opcodes use the minimal encoding when // Ensure all executed data push opcodes use the minimal encoding when
// the minimal data verification flag is set. // the minimal data verification flag is set.
if vm.dstack.verifyMinimalData && vm.isBranchExecuting() && if vm.dstack.verifyMinimalData && vm.isBranchExecuting() &&
pop.opcode.value >= 0 && pop.opcode.value <= OP_PUSHDATA4 { pop.opcode.value >= 0 && pop.opcode.value <= OpPushData4 {
if err := pop.checkMinimalDataPush(); err != nil { if err := pop.checkMinimalDataPush(); err != nil {
return err return err

View File

@ -172,7 +172,7 @@ func TestInvalidFlagCombinations(t *testing.T) {
}), }),
Index: 0, Index: 0,
}, },
SignatureScript: []uint8{OP_NOP}, SignatureScript: []uint8{OpNop},
Sequence: 4294967295, Sequence: 4294967295,
}, },
}, },
@ -184,7 +184,7 @@ func TestInvalidFlagCombinations(t *testing.T) {
}, },
LockTime: 0, LockTime: 0,
} }
pkScript := []byte{OP_NOP} pkScript := []byte{OpNop}
for i, test := range tests { for i, test := range tests {
_, err := NewEngine(pkScript, tx, 0, test, nil) _, err := NewEngine(pkScript, tx, 0, test, nil)

View File

@ -103,7 +103,7 @@ func ExampleSignTxOutput() {
// contains a single output that pays to address in the amount of 1 BTC. // contains a single output that pays to address in the amount of 1 BTC.
originTx := wire.NewMsgTx(wire.TxVersion) originTx := wire.NewMsgTx(wire.TxVersion)
prevOut := wire.NewOutPoint(&daghash.Hash{}, ^uint32(0)) prevOut := wire.NewOutPoint(&daghash.Hash{}, ^uint32(0))
txIn := wire.NewTxIn(prevOut, []byte{txscript.OP_0, txscript.OP_0}) txIn := wire.NewTxIn(prevOut, []byte{txscript.Op0, txscript.Op0})
originTx.AddTxIn(txIn) originTx.AddTxIn(txIn)
pkScript, err := txscript.PayToAddrScript(addr) pkScript, err := txscript.PayToAddrScript(addr)
if err != nil { if err != nil {

File diff suppressed because it is too large Load Diff

View File

@ -18,9 +18,9 @@ import (
func TestOpcodeDisabled(t *testing.T) { func TestOpcodeDisabled(t *testing.T) {
t.Parallel() t.Parallel()
tests := []byte{OP_CAT, OP_SUBSTR, OP_LEFT, OP_RIGHT, OP_INVERT, tests := []byte{OpCat, OpSubStr, OpLeft, OpRight, OpInvert,
OP_AND, OP_OR, OP_2MUL, OP_2DIV, OP_MUL, OP_DIV, OP_MOD, OpAnd, OpOr, Op2Mul, Op2Div, OpMul, OpDiv, OpMod,
OP_LSHIFT, OP_RSHIFT, OpLShift, OpRShift,
} }
for _, opcodeVal := range tests { for _, opcodeVal := range tests {
pop := parsedOpcode{opcode: &opcodeArray[opcodeVal], data: nil} pop := parsedOpcode{opcode: &opcodeArray[opcodeVal], data: nil}

View File

@ -82,8 +82,8 @@ func parseShortForm(script string) ([]byte, error) {
// have the same value, so detect those by name and // have the same value, so detect those by name and
// allow them. // allow them.
if (opcodeName == "OP_FALSE" || opcodeName == "OP_TRUE") || if (opcodeName == "OP_FALSE" || opcodeName == "OP_TRUE") ||
(opcodeValue != OP_0 && (opcodeValue < OP_1 || (opcodeValue != Op0 && (opcodeValue < Op1 ||
opcodeValue > OP_16)) { opcodeValue > Op16)) {
ops[strings.TrimPrefix(opcodeName, "OP_")] = opcodeValue ops[strings.TrimPrefix(opcodeName, "OP_")] = opcodeValue
} }
@ -242,7 +242,7 @@ func createSpendingTx(sigScript, pkScript []byte) *wire.MsgTx {
coinbaseTx := wire.NewMsgTx(wire.TxVersion) coinbaseTx := wire.NewMsgTx(wire.TxVersion)
outPoint := wire.NewOutPoint(&daghash.Hash{}, ^uint32(0)) outPoint := wire.NewOutPoint(&daghash.Hash{}, ^uint32(0))
txIn := wire.NewTxIn(outPoint, []byte{OP_0, OP_0}) txIn := wire.NewTxIn(outPoint, []byte{Op0, Op0})
txOut := wire.NewTxOut(0, pkScript) txOut := wire.NewTxOut(0, pkScript)
coinbaseTx.AddTxIn(txIn) coinbaseTx.AddTxIn(txIn)
coinbaseTx.AddTxOut(txOut) coinbaseTx.AddTxOut(txOut)

View File

@ -45,7 +45,7 @@ const (
// isSmallInt returns whether or not the opcode is considered a small integer, // isSmallInt returns whether or not the opcode is considered a small integer,
// which is an OP_0, or OP_1 through OP_16. // which is an OP_0, or OP_1 through OP_16.
func isSmallInt(op *opcode) bool { func isSmallInt(op *opcode) bool {
if op.value == OP_0 || (op.value >= OP_1 && op.value <= OP_16) { if op.value == Op0 || (op.value >= Op1 && op.value <= Op16) {
return true return true
} }
return false return false
@ -55,9 +55,9 @@ func isSmallInt(op *opcode) bool {
// transaction, false otherwise. // transaction, false otherwise.
func isScriptHash(pops []parsedOpcode) bool { func isScriptHash(pops []parsedOpcode) bool {
return len(pops) == 3 && return len(pops) == 3 &&
pops[0].opcode.value == OP_HASH160 && pops[0].opcode.value == OpHash160 &&
pops[1].opcode.value == OP_DATA_20 && pops[1].opcode.value == OpData20 &&
pops[2].opcode.value == OP_EQUAL pops[2].opcode.value == OpEqual
} }
// IsPayToScriptHash returns true if the script is in the standard // IsPayToScriptHash returns true if the script is in the standard
@ -81,7 +81,7 @@ func isPushOnly(pops []parsedOpcode) bool {
// NOTE: This does consider OP_RESERVED to be a data push // NOTE: This does consider OP_RESERVED to be a data push
// instruction, but execution of OP_RESERVED will fail anyways // instruction, but execution of OP_RESERVED will fail anyways
// and matches the behavior required by consensus. // and matches the behavior required by consensus.
if pop.opcode.value > OP_16 { if pop.opcode.value > Op16 {
return false return false
} }
} }
@ -246,20 +246,20 @@ func canonicalPush(pop parsedOpcode) bool {
opcode := pop.opcode.value opcode := pop.opcode.value
data := pop.data data := pop.data
dataLen := len(pop.data) dataLen := len(pop.data)
if opcode > OP_16 { if opcode > Op16 {
return true return true
} }
if opcode < OP_PUSHDATA1 && opcode > OP_0 && (dataLen == 1 && data[0] <= 16) { if opcode < OpPushData1 && opcode > Op0 && (dataLen == 1 && data[0] <= 16) {
return false return false
} }
if opcode == OP_PUSHDATA1 && dataLen < OP_PUSHDATA1 { if opcode == OpPushData1 && dataLen < OpPushData1 {
return false return false
} }
if opcode == OP_PUSHDATA2 && dataLen <= 0xff { if opcode == OpPushData2 && dataLen <= 0xff {
return false return false
} }
if opcode == OP_PUSHDATA4 && dataLen <= 0xffff { if opcode == OpPushData4 && dataLen <= 0xffff {
return false return false
} }
return true return true
@ -348,7 +348,7 @@ func calcSignatureHash(script []parsedOpcode, hashType SigHashType, tx *wire.Msg
} }
// Remove all instances of OP_CODESEPARATOR from the script. // Remove all instances of OP_CODESEPARATOR from the script.
script = removeOpcode(script, OP_CODESEPARATOR) script = removeOpcode(script, OpCodeSeparator)
// Make a shallow copy of the transaction, zeroing out the script for // Make a shallow copy of the transaction, zeroing out the script for
// all inputs that are not currently being processed. // all inputs that are not currently being processed.
@ -415,11 +415,11 @@ func calcSignatureHash(script []parsedOpcode, hashType SigHashType, tx *wire.Msg
// asSmallInt returns the passed opcode, which must be true according to // asSmallInt returns the passed opcode, which must be true according to
// isSmallInt(), as an integer. // isSmallInt(), as an integer.
func asSmallInt(op *opcode) int { func asSmallInt(op *opcode) int {
if op.value == OP_0 { if op.value == Op0 {
return 0 return 0
} }
return int(op.value - (OP_1 - 1)) return int(op.value - (Op1 - 1))
} }
// getSigOpCount is the implementation function for counting the number of // getSigOpCount is the implementation function for counting the number of
@ -430,20 +430,20 @@ func getSigOpCount(pops []parsedOpcode, precise bool) int {
nSigs := 0 nSigs := 0
for i, pop := range pops { for i, pop := range pops {
switch pop.opcode.value { switch pop.opcode.value {
case OP_CHECKSIG: case OpCheckSig:
fallthrough fallthrough
case OP_CHECKSIGVERIFY: case OpCheckSigVerify:
nSigs++ nSigs++
case OP_CHECKMULTISIG: case OpCheckMultiSig:
fallthrough fallthrough
case OP_CHECKMULTISIGVERIFY: case OpCheckMultiSigVerify:
// If we are being precise then look for familiar // If we are being precise then look for familiar
// patterns for multisig, for now all we recognize is // patterns for multisig, for now all we recognize is
// OP_1 - OP_16 to signify the number of pubkeys. // OP_1 - OP_16 to signify the number of pubkeys.
// Otherwise, we use the max of 20. // Otherwise, we use the max of 20.
if precise && i > 0 && if precise && i > 0 &&
pops[i-1].opcode.value >= OP_1 && pops[i-1].opcode.value >= Op1 &&
pops[i-1].opcode.value <= OP_16 { pops[i-1].opcode.value <= Op16 {
nSigs += asSmallInt(pops[i-1].opcode) nSigs += asSmallInt(pops[i-1].opcode)
} else { } else {
nSigs += MaxPubKeysPerMultiSig nSigs += MaxPubKeysPerMultiSig
@ -521,5 +521,5 @@ func IsUnspendable(pkScript []byte) bool {
return true return true
} }
return len(pops) > 0 && pops[0].opcode.value == OP_RETURN return len(pops) > 0 && pops[0].opcode.value == OpReturn
} }

File diff suppressed because it is too large Load Diff

View File

@ -110,7 +110,7 @@ func canonicalDataSize(data []byte) int {
return 1 return 1
} }
if dataLen < OP_PUSHDATA1 { if dataLen < OpPushData1 {
return 1 + dataLen return 1 + dataLen
} else if dataLen <= 0xff { } else if dataLen <= 0xff {
return 2 + dataLen return 2 + dataLen
@ -132,13 +132,13 @@ func (b *ScriptBuilder) addData(data []byte) *ScriptBuilder {
// by one of the "small integer" opcodes, use that opcode instead of // by one of the "small integer" opcodes, use that opcode instead of
// a data push opcode followed by the number. // a data push opcode followed by the number.
if dataLen == 0 || dataLen == 1 && data[0] == 0 { if dataLen == 0 || dataLen == 1 && data[0] == 0 {
b.script = append(b.script, OP_0) b.script = append(b.script, Op0)
return b return b
} else if dataLen == 1 && data[0] <= 16 { } else if dataLen == 1 && data[0] <= 16 {
b.script = append(b.script, (OP_1-1)+data[0]) b.script = append(b.script, (Op1-1)+data[0])
return b return b
} else if dataLen == 1 && data[0] == 0x81 { } else if dataLen == 1 && data[0] == 0x81 {
b.script = append(b.script, byte(OP_1NEGATE)) b.script = append(b.script, byte(Op1Negate))
return b return b
} }
@ -146,19 +146,19 @@ func (b *ScriptBuilder) addData(data []byte) *ScriptBuilder {
// enough so the data push instruction is only a single byte. // enough so the data push instruction is only a single byte.
// Otherwise, choose the smallest possible OP_PUSHDATA# opcode that // Otherwise, choose the smallest possible OP_PUSHDATA# opcode that
// can represent the length of the data. // can represent the length of the data.
if dataLen < OP_PUSHDATA1 { if dataLen < OpPushData1 {
b.script = append(b.script, byte((OP_DATA_1-1)+dataLen)) b.script = append(b.script, byte((OpData1-1)+dataLen))
} else if dataLen <= 0xff { } else if dataLen <= 0xff {
b.script = append(b.script, OP_PUSHDATA1, byte(dataLen)) b.script = append(b.script, OpPushData1, byte(dataLen))
} else if dataLen <= 0xffff { } else if dataLen <= 0xffff {
buf := make([]byte, 2) buf := make([]byte, 2)
binary.LittleEndian.PutUint16(buf, uint16(dataLen)) binary.LittleEndian.PutUint16(buf, uint16(dataLen))
b.script = append(b.script, OP_PUSHDATA2) b.script = append(b.script, OpPushData2)
b.script = append(b.script, buf...) b.script = append(b.script, buf...)
} else { } else {
buf := make([]byte, 4) buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, uint32(dataLen)) binary.LittleEndian.PutUint32(buf, uint32(dataLen))
b.script = append(b.script, OP_PUSHDATA4) b.script = append(b.script, OpPushData4)
b.script = append(b.script, buf...) b.script = append(b.script, buf...)
} }
@ -240,11 +240,11 @@ func (b *ScriptBuilder) AddInt64(val int64) *ScriptBuilder {
// Fast path for small integers and OP_1NEGATE. // Fast path for small integers and OP_1NEGATE.
if val == 0 { if val == 0 {
b.script = append(b.script, OP_0) b.script = append(b.script, Op0)
return b return b
} }
if val == -1 || (val >= 1 && val <= 16) { if val == -1 || (val >= 1 && val <= 16) {
b.script = append(b.script, byte((OP_1-1)+val)) b.script = append(b.script, byte((Op1-1)+val))
return b return b
} }

View File

@ -21,18 +21,18 @@ func TestScriptBuilderAddOp(t *testing.T) {
}{ }{
{ {
name: "push OP_0", name: "push OP_0",
opcodes: []byte{OP_0}, opcodes: []byte{Op0},
expected: []byte{OP_0}, expected: []byte{Op0},
}, },
{ {
name: "push OP_1 OP_2", name: "push OP_1 OP_2",
opcodes: []byte{OP_1, OP_2}, opcodes: []byte{Op1, Op2},
expected: []byte{OP_1, OP_2}, expected: []byte{Op1, Op2},
}, },
{ {
name: "push OP_HASH160 OP_EQUAL", name: "push OP_HASH160 OP_EQUAL",
opcodes: []byte{OP_HASH160, OP_EQUAL}, opcodes: []byte{OpHash160, OpEqual},
expected: []byte{OP_HASH160, OP_EQUAL}, expected: []byte{OpHash160, OpEqual},
}, },
} }
@ -88,44 +88,44 @@ func TestScriptBuilderAddInt64(t *testing.T) {
val int64 val int64
expected []byte expected []byte
}{ }{
{name: "push -1", val: -1, expected: []byte{OP_1NEGATE}}, {name: "push -1", val: -1, expected: []byte{Op1Negate}},
{name: "push small int 0", val: 0, expected: []byte{OP_0}}, {name: "push small int 0", val: 0, expected: []byte{Op0}},
{name: "push small int 1", val: 1, expected: []byte{OP_1}}, {name: "push small int 1", val: 1, expected: []byte{Op1}},
{name: "push small int 2", val: 2, expected: []byte{OP_2}}, {name: "push small int 2", val: 2, expected: []byte{Op2}},
{name: "push small int 3", val: 3, expected: []byte{OP_3}}, {name: "push small int 3", val: 3, expected: []byte{Op3}},
{name: "push small int 4", val: 4, expected: []byte{OP_4}}, {name: "push small int 4", val: 4, expected: []byte{Op4}},
{name: "push small int 5", val: 5, expected: []byte{OP_5}}, {name: "push small int 5", val: 5, expected: []byte{Op5}},
{name: "push small int 6", val: 6, expected: []byte{OP_6}}, {name: "push small int 6", val: 6, expected: []byte{Op6}},
{name: "push small int 7", val: 7, expected: []byte{OP_7}}, {name: "push small int 7", val: 7, expected: []byte{Op7}},
{name: "push small int 8", val: 8, expected: []byte{OP_8}}, {name: "push small int 8", val: 8, expected: []byte{Op8}},
{name: "push small int 9", val: 9, expected: []byte{OP_9}}, {name: "push small int 9", val: 9, expected: []byte{Op9}},
{name: "push small int 10", val: 10, expected: []byte{OP_10}}, {name: "push small int 10", val: 10, expected: []byte{Op10}},
{name: "push small int 11", val: 11, expected: []byte{OP_11}}, {name: "push small int 11", val: 11, expected: []byte{Op11}},
{name: "push small int 12", val: 12, expected: []byte{OP_12}}, {name: "push small int 12", val: 12, expected: []byte{Op12}},
{name: "push small int 13", val: 13, expected: []byte{OP_13}}, {name: "push small int 13", val: 13, expected: []byte{Op13}},
{name: "push small int 14", val: 14, expected: []byte{OP_14}}, {name: "push small int 14", val: 14, expected: []byte{Op14}},
{name: "push small int 15", val: 15, expected: []byte{OP_15}}, {name: "push small int 15", val: 15, expected: []byte{Op15}},
{name: "push small int 16", val: 16, expected: []byte{OP_16}}, {name: "push small int 16", val: 16, expected: []byte{Op16}},
{name: "push 17", val: 17, expected: []byte{OP_DATA_1, 0x11}}, {name: "push 17", val: 17, expected: []byte{OpData1, 0x11}},
{name: "push 65", val: 65, expected: []byte{OP_DATA_1, 0x41}}, {name: "push 65", val: 65, expected: []byte{OpData1, 0x41}},
{name: "push 127", val: 127, expected: []byte{OP_DATA_1, 0x7f}}, {name: "push 127", val: 127, expected: []byte{OpData1, 0x7f}},
{name: "push 128", val: 128, expected: []byte{OP_DATA_2, 0x80, 0}}, {name: "push 128", val: 128, expected: []byte{OpData2, 0x80, 0}},
{name: "push 255", val: 255, expected: []byte{OP_DATA_2, 0xff, 0}}, {name: "push 255", val: 255, expected: []byte{OpData2, 0xff, 0}},
{name: "push 256", val: 256, expected: []byte{OP_DATA_2, 0, 0x01}}, {name: "push 256", val: 256, expected: []byte{OpData2, 0, 0x01}},
{name: "push 32767", val: 32767, expected: []byte{OP_DATA_2, 0xff, 0x7f}}, {name: "push 32767", val: 32767, expected: []byte{OpData2, 0xff, 0x7f}},
{name: "push 32768", val: 32768, expected: []byte{OP_DATA_3, 0, 0x80, 0}}, {name: "push 32768", val: 32768, expected: []byte{OpData3, 0, 0x80, 0}},
{name: "push -2", val: -2, expected: []byte{OP_DATA_1, 0x82}}, {name: "push -2", val: -2, expected: []byte{OpData1, 0x82}},
{name: "push -3", val: -3, expected: []byte{OP_DATA_1, 0x83}}, {name: "push -3", val: -3, expected: []byte{OpData1, 0x83}},
{name: "push -4", val: -4, expected: []byte{OP_DATA_1, 0x84}}, {name: "push -4", val: -4, expected: []byte{OpData1, 0x84}},
{name: "push -5", val: -5, expected: []byte{OP_DATA_1, 0x85}}, {name: "push -5", val: -5, expected: []byte{OpData1, 0x85}},
{name: "push -17", val: -17, expected: []byte{OP_DATA_1, 0x91}}, {name: "push -17", val: -17, expected: []byte{OpData1, 0x91}},
{name: "push -65", val: -65, expected: []byte{OP_DATA_1, 0xc1}}, {name: "push -65", val: -65, expected: []byte{OpData1, 0xc1}},
{name: "push -127", val: -127, expected: []byte{OP_DATA_1, 0xff}}, {name: "push -127", val: -127, expected: []byte{OpData1, 0xff}},
{name: "push -128", val: -128, expected: []byte{OP_DATA_2, 0x80, 0x80}}, {name: "push -128", val: -128, expected: []byte{OpData2, 0x80, 0x80}},
{name: "push -255", val: -255, expected: []byte{OP_DATA_2, 0xff, 0x80}}, {name: "push -255", val: -255, expected: []byte{OpData2, 0xff, 0x80}},
{name: "push -256", val: -256, expected: []byte{OP_DATA_2, 0x00, 0x81}}, {name: "push -256", val: -256, expected: []byte{OpData2, 0x00, 0x81}},
{name: "push -32767", val: -32767, expected: []byte{OP_DATA_2, 0xff, 0xff}}, {name: "push -32767", val: -32767, expected: []byte{OpData2, 0xff, 0xff}},
{name: "push -32768", val: -32768, expected: []byte{OP_DATA_3, 0x00, 0x80, 0x80}}, {name: "push -32768", val: -32768, expected: []byte{OpData3, 0x00, 0x80, 0x80}},
} }
builder := NewScriptBuilder() builder := NewScriptBuilder()
@ -159,70 +159,70 @@ func TestScriptBuilderAddData(t *testing.T) {
useFull bool // use AddFullData instead of AddData. useFull bool // use AddFullData instead of AddData.
}{ }{
// BIP0062: Pushing an empty byte sequence must use OP_0. // BIP0062: Pushing an empty byte sequence must use OP_0.
{name: "push empty byte sequence", data: nil, expected: []byte{OP_0}}, {name: "push empty byte sequence", data: nil, expected: []byte{Op0}},
{name: "push 1 byte 0x00", data: []byte{0x00}, expected: []byte{OP_0}}, {name: "push 1 byte 0x00", data: []byte{0x00}, expected: []byte{Op0}},
// BIP0062: Pushing a 1-byte sequence of byte 0x01 through 0x10 must use OP_n. // BIP0062: Pushing a 1-byte sequence of byte 0x01 through 0x10 must use OP_n.
{name: "push 1 byte 0x01", data: []byte{0x01}, expected: []byte{OP_1}}, {name: "push 1 byte 0x01", data: []byte{0x01}, expected: []byte{Op1}},
{name: "push 1 byte 0x02", data: []byte{0x02}, expected: []byte{OP_2}}, {name: "push 1 byte 0x02", data: []byte{0x02}, expected: []byte{Op2}},
{name: "push 1 byte 0x03", data: []byte{0x03}, expected: []byte{OP_3}}, {name: "push 1 byte 0x03", data: []byte{0x03}, expected: []byte{Op3}},
{name: "push 1 byte 0x04", data: []byte{0x04}, expected: []byte{OP_4}}, {name: "push 1 byte 0x04", data: []byte{0x04}, expected: []byte{Op4}},
{name: "push 1 byte 0x05", data: []byte{0x05}, expected: []byte{OP_5}}, {name: "push 1 byte 0x05", data: []byte{0x05}, expected: []byte{Op5}},
{name: "push 1 byte 0x06", data: []byte{0x06}, expected: []byte{OP_6}}, {name: "push 1 byte 0x06", data: []byte{0x06}, expected: []byte{Op6}},
{name: "push 1 byte 0x07", data: []byte{0x07}, expected: []byte{OP_7}}, {name: "push 1 byte 0x07", data: []byte{0x07}, expected: []byte{Op7}},
{name: "push 1 byte 0x08", data: []byte{0x08}, expected: []byte{OP_8}}, {name: "push 1 byte 0x08", data: []byte{0x08}, expected: []byte{Op8}},
{name: "push 1 byte 0x09", data: []byte{0x09}, expected: []byte{OP_9}}, {name: "push 1 byte 0x09", data: []byte{0x09}, expected: []byte{Op9}},
{name: "push 1 byte 0x0a", data: []byte{0x0a}, expected: []byte{OP_10}}, {name: "push 1 byte 0x0a", data: []byte{0x0a}, expected: []byte{Op10}},
{name: "push 1 byte 0x0b", data: []byte{0x0b}, expected: []byte{OP_11}}, {name: "push 1 byte 0x0b", data: []byte{0x0b}, expected: []byte{Op11}},
{name: "push 1 byte 0x0c", data: []byte{0x0c}, expected: []byte{OP_12}}, {name: "push 1 byte 0x0c", data: []byte{0x0c}, expected: []byte{Op12}},
{name: "push 1 byte 0x0d", data: []byte{0x0d}, expected: []byte{OP_13}}, {name: "push 1 byte 0x0d", data: []byte{0x0d}, expected: []byte{Op13}},
{name: "push 1 byte 0x0e", data: []byte{0x0e}, expected: []byte{OP_14}}, {name: "push 1 byte 0x0e", data: []byte{0x0e}, expected: []byte{Op14}},
{name: "push 1 byte 0x0f", data: []byte{0x0f}, expected: []byte{OP_15}}, {name: "push 1 byte 0x0f", data: []byte{0x0f}, expected: []byte{Op15}},
{name: "push 1 byte 0x10", data: []byte{0x10}, expected: []byte{OP_16}}, {name: "push 1 byte 0x10", data: []byte{0x10}, expected: []byte{Op16}},
// BIP0062: Pushing the byte 0x81 must use OP_1NEGATE. // BIP0062: Pushing the byte 0x81 must use OP_1NEGATE.
{name: "push 1 byte 0x81", data: []byte{0x81}, expected: []byte{OP_1NEGATE}}, {name: "push 1 byte 0x81", data: []byte{0x81}, expected: []byte{Op1Negate}},
// BIP0062: Pushing any other byte sequence up to 75 bytes must // BIP0062: Pushing any other byte sequence up to 75 bytes must
// use the normal data push (opcode byte n, with n the number of // use the normal data push (opcode byte n, with n the number of
// bytes, followed n bytes of data being pushed). // bytes, followed n bytes of data being pushed).
{name: "push 1 byte 0x11", data: []byte{0x11}, expected: []byte{OP_DATA_1, 0x11}}, {name: "push 1 byte 0x11", data: []byte{0x11}, expected: []byte{OpData1, 0x11}},
{name: "push 1 byte 0x80", data: []byte{0x80}, expected: []byte{OP_DATA_1, 0x80}}, {name: "push 1 byte 0x80", data: []byte{0x80}, expected: []byte{OpData1, 0x80}},
{name: "push 1 byte 0x82", data: []byte{0x82}, expected: []byte{OP_DATA_1, 0x82}}, {name: "push 1 byte 0x82", data: []byte{0x82}, expected: []byte{OpData1, 0x82}},
{name: "push 1 byte 0xff", data: []byte{0xff}, expected: []byte{OP_DATA_1, 0xff}}, {name: "push 1 byte 0xff", data: []byte{0xff}, expected: []byte{OpData1, 0xff}},
{ {
name: "push data len 17", name: "push data len 17",
data: bytes.Repeat([]byte{0x49}, 17), data: bytes.Repeat([]byte{0x49}, 17),
expected: append([]byte{OP_DATA_17}, bytes.Repeat([]byte{0x49}, 17)...), expected: append([]byte{OpData17}, bytes.Repeat([]byte{0x49}, 17)...),
}, },
{ {
name: "push data len 75", name: "push data len 75",
data: bytes.Repeat([]byte{0x49}, 75), data: bytes.Repeat([]byte{0x49}, 75),
expected: append([]byte{OP_DATA_75}, bytes.Repeat([]byte{0x49}, 75)...), expected: append([]byte{OpData75}, bytes.Repeat([]byte{0x49}, 75)...),
}, },
// BIP0062: Pushing 76 to 255 bytes must use OP_PUSHDATA1. // BIP0062: Pushing 76 to 255 bytes must use OP_PUSHDATA1.
{ {
name: "push data len 76", name: "push data len 76",
data: bytes.Repeat([]byte{0x49}, 76), data: bytes.Repeat([]byte{0x49}, 76),
expected: append([]byte{OP_PUSHDATA1, 76}, bytes.Repeat([]byte{0x49}, 76)...), expected: append([]byte{OpPushData1, 76}, bytes.Repeat([]byte{0x49}, 76)...),
}, },
{ {
name: "push data len 255", name: "push data len 255",
data: bytes.Repeat([]byte{0x49}, 255), data: bytes.Repeat([]byte{0x49}, 255),
expected: append([]byte{OP_PUSHDATA1, 255}, bytes.Repeat([]byte{0x49}, 255)...), expected: append([]byte{OpPushData1, 255}, bytes.Repeat([]byte{0x49}, 255)...),
}, },
// BIP0062: Pushing 256 to 520 bytes must use OP_PUSHDATA2. // BIP0062: Pushing 256 to 520 bytes must use OP_PUSHDATA2.
{ {
name: "push data len 256", name: "push data len 256",
data: bytes.Repeat([]byte{0x49}, 256), data: bytes.Repeat([]byte{0x49}, 256),
expected: append([]byte{OP_PUSHDATA2, 0, 1}, bytes.Repeat([]byte{0x49}, 256)...), expected: append([]byte{OpPushData2, 0, 1}, bytes.Repeat([]byte{0x49}, 256)...),
}, },
{ {
name: "push data len 520", name: "push data len 520",
data: bytes.Repeat([]byte{0x49}, 520), data: bytes.Repeat([]byte{0x49}, 520),
expected: append([]byte{OP_PUSHDATA2, 0x08, 0x02}, bytes.Repeat([]byte{0x49}, 520)...), expected: append([]byte{OpPushData2, 0x08, 0x02}, bytes.Repeat([]byte{0x49}, 520)...),
}, },
// BIP0062: OP_PUSHDATA4 can never be used, as pushes over 520 // BIP0062: OP_PUSHDATA4 can never be used, as pushes over 520
@ -252,7 +252,7 @@ func TestScriptBuilderAddData(t *testing.T) {
{ {
name: "push data len 32767 (non-canonical)", name: "push data len 32767 (non-canonical)",
data: bytes.Repeat([]byte{0x49}, 32767), data: bytes.Repeat([]byte{0x49}, 32767),
expected: append([]byte{OP_PUSHDATA2, 255, 127}, bytes.Repeat([]byte{0x49}, 32767)...), expected: append([]byte{OpPushData2, 255, 127}, bytes.Repeat([]byte{0x49}, 32767)...),
useFull: true, useFull: true,
}, },
@ -260,7 +260,7 @@ func TestScriptBuilderAddData(t *testing.T) {
{ {
name: "push data len 65536 (non-canonical)", name: "push data len 65536 (non-canonical)",
data: bytes.Repeat([]byte{0x49}, 65536), data: bytes.Repeat([]byte{0x49}, 65536),
expected: append([]byte{OP_PUSHDATA4, 0, 0, 1, 0}, bytes.Repeat([]byte{0x49}, 65536)...), expected: append([]byte{OpPushData4, 0, 0, 1, 0}, bytes.Repeat([]byte{0x49}, 65536)...),
useFull: true, useFull: true,
}, },
} }
@ -312,7 +312,7 @@ func TestExceedMaxScriptSize(t *testing.T) {
// Ensure adding an opcode that would exceed the maximum size of the // Ensure adding an opcode that would exceed the maximum size of the
// script does not add the data. // script does not add the data.
builder.Reset().AddFullData(make([]byte, MaxScriptSize-3)) builder.Reset().AddFullData(make([]byte, MaxScriptSize-3))
script, err = builder.AddOp(OP_0).Script() script, err = builder.AddOp(Op0).Script()
if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil {
t.Fatalf("ScriptBuilder.AddOp unexpected modified script - "+ t.Fatalf("ScriptBuilder.AddOp unexpected modified script - "+
"got len %d, want len %d", len(script), len(origScript)) "got len %d, want len %d", len(script), len(origScript))
@ -384,7 +384,7 @@ func TestErroredScript(t *testing.T) {
} }
// Ensure adding an opcode to a script that has errored doesn't succeed. // Ensure adding an opcode to a script that has errored doesn't succeed.
script, err = builder.AddOp(OP_0).Script() script, err = builder.AddOp(Op0).Script()
if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil {
t.Fatal("ScriptBuilder.AddOp succeeded on errored script") t.Fatal("ScriptBuilder.AddOp succeeded on errored script")
} }

View File

@ -74,7 +74,7 @@ func signMultiSig(tx *wire.MsgTx, idx int, subScript []byte, hashType SigHashTyp
// We start with a single OP_FALSE to work around the (now standard) // We start with a single OP_FALSE to work around the (now standard)
// but in the reference implementation that causes a spurious pop at // but in the reference implementation that causes a spurious pop at
// the end of OP_CHECKMULTISIG. // the end of OP_CHECKMULTISIG.
builder := NewScriptBuilder().AddOp(OP_FALSE) builder := NewScriptBuilder().AddOp(OpFalse)
signed := 0 signed := 0
for _, addr := range addresses { for _, addr := range addresses {
key, _, err := kdb.GetKey(addr) key, _, err := kdb.GetKey(addr)
@ -315,7 +315,7 @@ sigLoop:
// Extra opcode to handle the extra arg consumed (due to previous bugs // Extra opcode to handle the extra arg consumed (due to previous bugs
// in the reference implementation). // in the reference implementation).
builder := NewScriptBuilder().AddOp(OP_FALSE) builder := NewScriptBuilder().AddOp(OpFalse)
doneSigs := 0 doneSigs := 0
// This assumes that addresses are in the same order as in the script. // This assumes that addresses are in the same order as in the script.
for _, addr := range addresses { for _, addr := range addresses {
@ -332,7 +332,7 @@ sigLoop:
// padding for missing ones. // padding for missing ones.
for i := doneSigs; i < nRequired; i++ { for i := doneSigs; i < nRequired; i++ {
builder.AddOp(OP_0) builder.AddOp(Op0)
} }
script, _ := builder.Script() script, _ := builder.Script()

View File

@ -1608,7 +1608,7 @@ nexttest:
for i := range sigScriptTests { for i := range sigScriptTests {
tx := wire.NewMsgTx(wire.TxVersion) tx := wire.NewMsgTx(wire.TxVersion)
output := wire.NewTxOut(500, []byte{OP_RETURN}) output := wire.NewTxOut(500, []byte{OpReturn})
tx.AddTxOut(output) tx.AddTxOut(output)
for range sigScriptTests[i].inputs { for range sigScriptTests[i].inputs {

View File

@ -80,18 +80,18 @@ func isPubkey(pops []parsedOpcode) bool {
// Valid pubkeys are either 33 or 65 bytes. // Valid pubkeys are either 33 or 65 bytes.
return len(pops) == 2 && return len(pops) == 2 &&
(len(pops[0].data) == 33 || len(pops[0].data) == 65) && (len(pops[0].data) == 33 || len(pops[0].data) == 65) &&
pops[1].opcode.value == OP_CHECKSIG pops[1].opcode.value == OpCheckSig
} }
// isPubkeyHash returns true if the script passed is a pay-to-pubkey-hash // isPubkeyHash returns true if the script passed is a pay-to-pubkey-hash
// transaction, false otherwise. // transaction, false otherwise.
func isPubkeyHash(pops []parsedOpcode) bool { func isPubkeyHash(pops []parsedOpcode) bool {
return len(pops) == 5 && return len(pops) == 5 &&
pops[0].opcode.value == OP_DUP && pops[0].opcode.value == OpDup &&
pops[1].opcode.value == OP_HASH160 && pops[1].opcode.value == OpHash160 &&
pops[2].opcode.value == OP_DATA_20 && pops[2].opcode.value == OpData20 &&
pops[3].opcode.value == OP_EQUALVERIFY && pops[3].opcode.value == OpEqualVerify &&
pops[4].opcode.value == OP_CHECKSIG pops[4].opcode.value == OpCheckSig
} }
@ -110,7 +110,7 @@ func isMultiSig(pops []parsedOpcode) bool {
if !isSmallInt(pops[l-2].opcode) { if !isSmallInt(pops[l-2].opcode) {
return false return false
} }
if pops[l-1].opcode.value != OP_CHECKMULTISIG { if pops[l-1].opcode.value != OpCheckMultiSig {
return false return false
} }
@ -136,14 +136,14 @@ func isNullData(pops []parsedOpcode) bool {
// OP_RETURN SMALLDATA (where SMALLDATA is a data push up to // OP_RETURN SMALLDATA (where SMALLDATA is a data push up to
// MaxDataCarrierSize bytes). // MaxDataCarrierSize bytes).
l := len(pops) l := len(pops)
if l == 1 && pops[0].opcode.value == OP_RETURN { if l == 1 && pops[0].opcode.value == OpReturn {
return true return true
} }
return l == 2 && return l == 2 &&
pops[0].opcode.value == OP_RETURN && pops[0].opcode.value == OpReturn &&
(isSmallInt(pops[1].opcode) || pops[1].opcode.value <= (isSmallInt(pops[1].opcode) || pops[1].opcode.value <=
OP_PUSHDATA4) && OpPushData4) &&
len(pops[1].data) <= MaxDataCarrierSize len(pops[1].data) <= MaxDataCarrierSize
} }
@ -311,23 +311,23 @@ func CalcMultiSigStats(script []byte) (int, int, error) {
// output to a 20-byte pubkey hash. It is expected that the input is a valid // output to a 20-byte pubkey hash. It is expected that the input is a valid
// hash. // hash.
func payToPubKeyHashScript(pubKeyHash []byte) ([]byte, error) { func payToPubKeyHashScript(pubKeyHash []byte) ([]byte, error) {
return NewScriptBuilder().AddOp(OP_DUP).AddOp(OP_HASH160). return NewScriptBuilder().AddOp(OpDup).AddOp(OpHash160).
AddData(pubKeyHash).AddOp(OP_EQUALVERIFY).AddOp(OP_CHECKSIG). AddData(pubKeyHash).AddOp(OpEqualVerify).AddOp(OpCheckSig).
Script() Script()
} }
// payToScriptHashScript creates a new script to pay a transaction output to a // payToScriptHashScript creates a new script to pay a transaction output to a
// script hash. It is expected that the input is a valid hash. // script hash. It is expected that the input is a valid hash.
func payToScriptHashScript(scriptHash []byte) ([]byte, error) { func payToScriptHashScript(scriptHash []byte) ([]byte, error) {
return NewScriptBuilder().AddOp(OP_HASH160).AddData(scriptHash). return NewScriptBuilder().AddOp(OpHash160).AddData(scriptHash).
AddOp(OP_EQUAL).Script() AddOp(OpEqual).Script()
} }
// payToPubkeyScript creates a new script to pay a transaction output to a // payToPubkeyScript creates a new script to pay a transaction output to a
// public key. It is expected that the input is a valid pubkey. // public key. It is expected that the input is a valid pubkey.
func payToPubKeyScript(serializedPubKey []byte) ([]byte, error) { func payToPubKeyScript(serializedPubKey []byte) ([]byte, error) {
return NewScriptBuilder().AddData(serializedPubKey). return NewScriptBuilder().AddData(serializedPubKey).
AddOp(OP_CHECKSIG).Script() AddOp(OpCheckSig).Script()
} }
// PayToAddrScript creates a new script to pay a transaction output to a the // PayToAddrScript creates a new script to pay a transaction output to a the
@ -373,7 +373,7 @@ func NullDataScript(data []byte) ([]byte, error) {
return nil, scriptError(ErrTooMuchNullData, str) return nil, scriptError(ErrTooMuchNullData, str)
} }
return NewScriptBuilder().AddOp(OP_RETURN).AddData(data).Script() return NewScriptBuilder().AddOp(OpReturn).AddData(data).Script()
} }
// MultiSigScript returns a valid script for a multisignature redemption where // MultiSigScript returns a valid script for a multisignature redemption where
@ -393,7 +393,7 @@ func MultiSigScript(pubkeys []*btcutil.AddressPubKey, nrequired int) ([]byte, er
builder.AddData(key.ScriptAddress()) builder.AddData(key.ScriptAddress())
} }
builder.AddInt64(int64(len(pubkeys))) builder.AddInt64(int64(len(pubkeys)))
builder.AddOp(OP_CHECKMULTISIG) builder.AddOp(OpCheckMultiSig)
return builder.Script() return builder.Script()
} }
@ -410,7 +410,7 @@ func PushedData(script []byte) ([][]byte, error) {
for _, pop := range pops { for _, pop := range pops {
if pop.data != nil { if pop.data != nil {
data = append(data, pop.data) data = append(data, pop.data)
} else if pop.opcode.value == OP_0 { } else if pop.opcode.value == Op0 {
data = append(data, nil) data = append(data, nil)
} }
} }
@ -528,26 +528,26 @@ func ExtractAtomicSwapDataPushes(version uint16, pkScript []byte) (*AtomicSwapDa
if len(pops) != 20 { if len(pops) != 20 {
return nil, nil return nil, nil
} }
isAtomicSwap := pops[0].opcode.value == OP_IF && isAtomicSwap := pops[0].opcode.value == OpIf &&
pops[1].opcode.value == OP_SIZE && pops[1].opcode.value == OpSize &&
canonicalPush(pops[2]) && canonicalPush(pops[2]) &&
pops[3].opcode.value == OP_EQUALVERIFY && pops[3].opcode.value == OpEqualVerify &&
pops[4].opcode.value == OP_SHA256 && pops[4].opcode.value == OpSHA256 &&
pops[5].opcode.value == OP_DATA_32 && pops[5].opcode.value == OpData32 &&
pops[6].opcode.value == OP_EQUALVERIFY && pops[6].opcode.value == OpEqualVerify &&
pops[7].opcode.value == OP_DUP && pops[7].opcode.value == OpDup &&
pops[8].opcode.value == OP_HASH160 && pops[8].opcode.value == OpHash160 &&
pops[9].opcode.value == OP_DATA_20 && pops[9].opcode.value == OpData20 &&
pops[10].opcode.value == OP_ELSE && pops[10].opcode.value == OpElse &&
canonicalPush(pops[11]) && canonicalPush(pops[11]) &&
pops[12].opcode.value == OP_CHECKLOCKTIMEVERIFY && pops[12].opcode.value == OpCheckLockTimeVerify &&
pops[13].opcode.value == OP_DROP && pops[13].opcode.value == OpDrop &&
pops[14].opcode.value == OP_DUP && pops[14].opcode.value == OpDup &&
pops[15].opcode.value == OP_HASH160 && pops[15].opcode.value == OpHash160 &&
pops[16].opcode.value == OP_DATA_20 && pops[16].opcode.value == OpData20 &&
pops[17].opcode.value == OP_ENDIF && pops[17].opcode.value == OpEndIf &&
pops[18].opcode.value == OP_EQUALVERIFY && pops[18].opcode.value == OpEqualVerify &&
pops[19].opcode.value == OP_CHECKSIG pops[19].opcode.value == OpCheckSig
if !isAtomicSwap { if !isAtomicSwap {
return nil, nil return nil, nil
} }

View File

@ -332,7 +332,7 @@ func TestExtractPkScriptAddrs(t *testing.T) {
}, },
{ {
name: "script that does not parse", name: "script that does not parse",
script: []byte{OP_DATA_45}, script: []byte{OpData45},
addrs: nil, addrs: nil,
reqSigs: 0, reqSigs: 0,
class: NonStandardTy, class: NonStandardTy,
@ -529,22 +529,22 @@ func TestPayToAddrScript(t *testing.T) {
} }
// mainnet p2pk 13CG6SJ3yHUXo4Cr2RY4THLLJrNFuG3gUg // mainnet p2pk 13CG6SJ3yHUXo4Cr2RY4THLLJrNFuG3gUg
p2pkCompressedMain, err := btcutil.NewAddressPubKey(hexToBytes("02192d"+ p2pkCompressedMain, err := btcutil.NewAddressPubKey(hexToBytes("02192d" +
"74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4")) "74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4"))
if err != nil { if err != nil {
t.Fatalf("Unable to create pubkey address (compressed): %v", t.Fatalf("Unable to create pubkey address (compressed): %v",
err) err)
} }
p2pkCompressed2Main, err := btcutil.NewAddressPubKey(hexToBytes("03b0b"+ p2pkCompressed2Main, err := btcutil.NewAddressPubKey(hexToBytes("03b0b" +
"d634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e65")) "d634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e65"))
if err != nil { if err != nil {
t.Fatalf("Unable to create pubkey address (compressed 2): %v", t.Fatalf("Unable to create pubkey address (compressed 2): %v",
err) err)
} }
p2pkUncompressedMain, err := btcutil.NewAddressPubKey(hexToBytes("0411"+ p2pkUncompressedMain, err := btcutil.NewAddressPubKey(hexToBytes("0411" +
"db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5"+ "db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5" +
"cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b4"+ "cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b4" +
"12a3")) "12a3"))
if err != nil { if err != nil {
t.Fatalf("Unable to create pubkey address (uncompressed): %v", t.Fatalf("Unable to create pubkey address (uncompressed): %v",
@ -631,22 +631,22 @@ func TestMultiSigScript(t *testing.T) {
t.Parallel() t.Parallel()
// mainnet p2pk 13CG6SJ3yHUXo4Cr2RY4THLLJrNFuG3gUg // mainnet p2pk 13CG6SJ3yHUXo4Cr2RY4THLLJrNFuG3gUg
p2pkCompressedMain, err := btcutil.NewAddressPubKey(hexToBytes("02192d"+ p2pkCompressedMain, err := btcutil.NewAddressPubKey(hexToBytes("02192d" +
"74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4")) "74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4"))
if err != nil { if err != nil {
t.Fatalf("Unable to create pubkey address (compressed): %v", t.Fatalf("Unable to create pubkey address (compressed): %v",
err) err)
} }
p2pkCompressed2Main, err := btcutil.NewAddressPubKey(hexToBytes("03b0b"+ p2pkCompressed2Main, err := btcutil.NewAddressPubKey(hexToBytes("03b0b" +
"d634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e65")) "d634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e65"))
if err != nil { if err != nil {
t.Fatalf("Unable to create pubkey address (compressed 2): %v", t.Fatalf("Unable to create pubkey address (compressed 2): %v",
err) err)
} }
p2pkUncompressedMain, err := btcutil.NewAddressPubKey(hexToBytes("0411"+ p2pkUncompressedMain, err := btcutil.NewAddressPubKey(hexToBytes("0411" +
"db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5"+ "db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5" +
"cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b4"+ "cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b4" +
"12a3")) "12a3"))
if err != nil { if err != nil {
t.Fatalf("Unable to create pubkey address (uncompressed): %v", t.Fatalf("Unable to create pubkey address (uncompressed): %v",

View File

@ -4,172 +4,8 @@
package main package main
import (
"io"
"os"
"path/filepath"
)
// dirEmpty returns whether or not the specified directory path is empty.
func dirEmpty(dirPath string) (bool, error) {
f, err := os.Open(dirPath)
if err != nil {
return false, err
}
defer f.Close()
// Read the names of a max of one entry from the directory. When the
// directory is empty, an io.EOF error will be returned, so allow it.
names, err := f.Readdirnames(1)
if err != nil && err != io.EOF {
return false, err
}
return len(names) == 0, nil
}
// oldBtcdHomeDir returns the OS specific home directory btcd used prior to
// version 0.3.3. This has since been replaced with btcutil.AppDataDir, but
// this function is still provided for the automatic upgrade path.
func oldBtcdHomeDir() string {
// Search for Windows APPDATA first. This won't exist on POSIX OSes.
appData := os.Getenv("APPDATA")
if appData != "" {
return filepath.Join(appData, "btcd")
}
// Fall back to standard HOME directory that works for most POSIX OSes.
home := os.Getenv("HOME")
if home != "" {
return filepath.Join(home, ".btcd")
}
// In the worst case, use the current directory.
return "."
}
// upgradeDBPathNet moves the database for a specific network from its
// location prior to btcd version 0.2.0 and uses heuristics to ascertain the old
// database type to rename to the new format.
func upgradeDBPathNet(oldDbPath, netName string) error {
// Prior to version 0.2.0, the database was named the same thing for
// both sqlite and leveldb. Use heuristics to figure out the type
// of the database and move it to the new path and name introduced with
// version 0.2.0 accordingly.
fi, err := os.Stat(oldDbPath)
if err == nil {
oldDbType := "sqlite"
if fi.IsDir() {
oldDbType = "leveldb"
}
// The new database name is based on the database type and
// resides in a directory named after the network type.
newDbRoot := filepath.Join(filepath.Dir(cfg.DataDir), netName)
newDbName := blockDbNamePrefix + "_" + oldDbType
if oldDbType == "sqlite" {
newDbName = newDbName + ".db"
}
newDbPath := filepath.Join(newDbRoot, newDbName)
// Create the new path if needed.
err = os.MkdirAll(newDbRoot, 0700)
if err != nil {
return err
}
// Move and rename the old database.
err := os.Rename(oldDbPath, newDbPath)
if err != nil {
return err
}
}
return nil
}
// upgradeDBPaths moves the databases from their locations prior to btcd
// version 0.2.0 to their new locations.
func upgradeDBPaths() error {
// Prior to version 0.2.0, the databases were in the "db" directory and
// their names were suffixed by "testnet" and "regtest" for their
// respective networks. Check for the old database and update it to the
// new path introduced with version 0.2.0 accordingly.
oldDbRoot := filepath.Join(oldBtcdHomeDir(), "db")
upgradeDBPathNet(filepath.Join(oldDbRoot, "btcd.db"), "mainnet")
upgradeDBPathNet(filepath.Join(oldDbRoot, "btcd_testnet.db"), "testnet")
upgradeDBPathNet(filepath.Join(oldDbRoot, "btcd_regtest.db"), "regtest")
// Remove the old db directory.
return os.RemoveAll(oldDbRoot)
}
// upgradeDataPaths moves the application data from its location prior to btcd
// version 0.3.3 to its new location.
func upgradeDataPaths() error {
// No need to migrate if the old and new home paths are the same.
oldHomePath := oldBtcdHomeDir()
newHomePath := defaultHomeDir
if oldHomePath == newHomePath {
return nil
}
// Only migrate if the old path exists and the new one doesn't.
if fileExists(oldHomePath) && !fileExists(newHomePath) {
// Create the new path.
btcdLog.Infof("Migrating application home path from '%s' to '%s'",
oldHomePath, newHomePath)
err := os.MkdirAll(newHomePath, 0700)
if err != nil {
return err
}
// Move old btcd.conf into new location if needed.
oldConfPath := filepath.Join(oldHomePath, defaultConfigFilename)
newConfPath := filepath.Join(newHomePath, defaultConfigFilename)
if fileExists(oldConfPath) && !fileExists(newConfPath) {
err := os.Rename(oldConfPath, newConfPath)
if err != nil {
return err
}
}
// Move old data directory into new location if needed.
oldDataPath := filepath.Join(oldHomePath, defaultDataDirname)
newDataPath := filepath.Join(newHomePath, defaultDataDirname)
if fileExists(oldDataPath) && !fileExists(newDataPath) {
err := os.Rename(oldDataPath, newDataPath)
if err != nil {
return err
}
}
// Remove the old home if it is empty or show a warning if not.
ohpEmpty, err := dirEmpty(oldHomePath)
if err != nil {
return err
}
if ohpEmpty {
err := os.Remove(oldHomePath)
if err != nil {
return err
}
} else {
btcdLog.Warnf("Not removing '%s' since it contains files "+
"not created by this application. You may "+
"want to manually move them or delete them.",
oldHomePath)
}
}
return nil
}
// doUpgrades performs upgrades to btcd as new versions require it. // doUpgrades performs upgrades to btcd as new versions require it.
// currently it's a placeholder we got from btcd upstream, that does nothing
func doUpgrades() error { func doUpgrades() error {
err := upgradeDBPaths() return nil
if err != nil {
return err
}
return upgradeDataPaths()
} }