From 26fb20e4ed73385b5bf3bbc04754831ac2f53bb5 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Mon, 5 Aug 2013 15:08:28 -0500 Subject: [PATCH] Remove need for protocol version. This commit modifies the code to no longer require a protocol version. It does this by making use of the new Serialize function in btcwire. Unfortuantely this does entail a public API change which I generally don't like to do, but eliminating the usage of the protocol version throughout the codebase was important enough to warrant the change. --- README.md | 3 +-- doc.go | 7 +++---- opcode_test.go | 4 ++-- script.go | 6 ++---- script_test.go | 6 +++--- 5 files changed, 11 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index b8472a8e..bb00df62 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,7 @@ can be found at https://en.bitcoin.it/wiki/Script ```Go pkscript := txS.TxOut[origintxidx].PkScript engine, err := btcscript.NewScript(sigScript, pkscript, txInIdx, - txValidator, pver, - timestamp.After(btcscript.Bip16Activation)) + txValidator, timestamp.After(btcscript.Bip16Activation)) err = engine.Execute() ``` diff --git a/doc.go b/doc.go index 0fb28f23..ebca20c6 100644 --- a/doc.go +++ b/doc.go @@ -40,13 +40,12 @@ engine to validate a transaction. // ValidateTx validates the txIdx'th input of tx. The output transaction // corresponding to the this input is the txInIdx'th output of txIn. The - // block timestamp of tx is timestamp and the protocol version involved - // is pver. - func ValidateTx(tx *btcwire.MsgTx, txIdx int, txIn *btcwire.MsgTx, txInIdx int, pver int, timestamp time.Time) { + // block timestamp of tx is timestamp. + func ValidateTx(tx *btcwire.MsgTx, txIdx int, txIn *btcwire.MsgTx, txInIdx int, timestamp time.Time) { pkScript := txIn.TxOut[txInIdx].PkScript sigScript := tx.txIn[TxIdx] engine, err := btcscript.NewScript(sigScript, pkScript, txInIdx, - tx, pver, timestamp.After(btcscript.Bip16Activation)) + tx, timestamp.After(btcscript.Bip16Activation)) return engine.Execute() } diff --git a/opcode_test.go b/opcode_test.go index 5df53f3c..61eb31c0 100644 --- a/opcode_test.go +++ b/opcode_test.go @@ -494,7 +494,7 @@ func testScript(t *testing.T, script []byte) (err error) { tx.TxOut[0].PkScript = script engine, err := btcscript.NewScript(tx.TxIn[0].SignatureScript, - tx.TxOut[0].PkScript, 0, tx, 1, false) + tx.TxOut[0].PkScript, 0, tx, false) if err != nil { return err } @@ -3875,7 +3875,7 @@ func testOpcode(t *testing.T, test *detailedTest) { tx.TxOut[0].PkScript = test.script engine, err := btcscript.NewScript(tx.TxIn[0].SignatureScript, - tx.TxOut[0].PkScript, 0, tx, 1, false) + tx.TxOut[0].PkScript, 0, tx, false) if err != nil { if err != test.expectedReturn { t.Errorf("Error return not expected %s: %v %v", diff --git a/script.go b/script.go index 8e6fd052..581cf1eb 100644 --- a/script.go +++ b/script.go @@ -147,7 +147,6 @@ type Script struct { astack Stack // alt stack tx btcwire.MsgTx txidx int - pver uint32 condStack []int numOps int bip16 bool // treat execution as pay-to-script-hash @@ -341,7 +340,7 @@ func unparseScript(pops []parsedOpcode) []byte { // a signature script scriptSig and a pubkeyscript scriptPubKey. If bip16 is // true then it will be treated as if the bip16 threshhold has passed and thus // pay-to-script hash transactions will be fully validated. -func NewScript(scriptSig []byte, scriptPubKey []byte, txidx int, tx *btcwire.MsgTx, pver uint32, bip16 bool) (*Script, error) { +func NewScript(scriptSig []byte, scriptPubKey []byte, txidx int, tx *btcwire.MsgTx, bip16 bool) (*Script, error) { var m Script scripts := [][]byte{scriptSig, scriptPubKey} m.scripts = make([][]parsedOpcode, len(scripts)) @@ -372,7 +371,6 @@ func NewScript(scriptSig []byte, scriptPubKey []byte, txidx int, tx *btcwire.Msg m.tx = *tx m.txidx = txidx - m.pver = pver m.condStack = []int{OpCondTrue} return &m, nil @@ -692,7 +690,7 @@ func (s *Script) calcScriptHash(script []parsedOpcode, hashType byte) []byte { } var wbuf bytes.Buffer - txCopy.BtcEncode(&wbuf, s.pver) + txCopy.Serialize(&wbuf) // Append LE 4 bytes hash type binary.Write(&wbuf, binary.LittleEndian, uint32(hashType)) diff --git a/script_test.go b/script_test.go index ac05927a..5f203792 100644 --- a/script_test.go +++ b/script_test.go @@ -1204,7 +1204,7 @@ var txTests = []txTest{ func testTx(t *testing.T, test txTest) { engine, err := btcscript.NewScript( test.tx.TxIn[test.idx].SignatureScript, test.pkScript, - test.idx, test.tx, 70001, test.bip16) + test.idx, test.tx, test.bip16) if err != nil { if err != test.parseErr { t.Errorf("Failed to parse %s: got \"%v\" expected "+ @@ -1731,7 +1731,7 @@ func TestBadPC(t *testing.T) { for _, test := range pcTests { engine, err := btcscript.NewScript(tx.TxIn[0].SignatureScript, - pkScript, 0, tx, 70001, false) + pkScript, 0, tx, false) if err != nil { t.Errorf("Failed to create script: %v", err) } @@ -1801,7 +1801,7 @@ func TestCheckErrorCondition(t *testing.T) { } engine, err := btcscript.NewScript(tx.TxIn[0].SignatureScript, pkScript, - 0, tx, 70001, false) + 0, tx, false) if err != nil { t.Errorf("failed to create script: %v", err) }