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.
This commit is contained in:
Dave Collins 2013-08-05 15:08:28 -05:00
parent 118014a5b4
commit 26fb20e4ed
5 changed files with 11 additions and 15 deletions

View file

@ -25,8 +25,7 @@ can be found at https://en.bitcoin.it/wiki/Script
```Go ```Go
pkscript := txS.TxOut[origintxidx].PkScript pkscript := txS.TxOut[origintxidx].PkScript
engine, err := btcscript.NewScript(sigScript, pkscript, txInIdx, engine, err := btcscript.NewScript(sigScript, pkscript, txInIdx,
txValidator, pver, txValidator, timestamp.After(btcscript.Bip16Activation))
timestamp.After(btcscript.Bip16Activation))
err = engine.Execute() err = engine.Execute()
``` ```

7
doc.go
View file

@ -40,13 +40,12 @@ engine to validate a transaction.
// ValidateTx validates the txIdx'th input of tx. The output 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 // corresponding to the this input is the txInIdx'th output of txIn. The
// block timestamp of tx is timestamp and the protocol version involved // block timestamp of tx is timestamp.
// is pver. func ValidateTx(tx *btcwire.MsgTx, txIdx int, txIn *btcwire.MsgTx, txInIdx int, timestamp time.Time) {
func ValidateTx(tx *btcwire.MsgTx, txIdx int, txIn *btcwire.MsgTx, txInIdx int, pver int, timestamp time.Time) {
pkScript := txIn.TxOut[txInIdx].PkScript pkScript := txIn.TxOut[txInIdx].PkScript
sigScript := tx.txIn[TxIdx] sigScript := tx.txIn[TxIdx]
engine, err := btcscript.NewScript(sigScript, pkScript, txInIdx, engine, err := btcscript.NewScript(sigScript, pkScript, txInIdx,
tx, pver, timestamp.After(btcscript.Bip16Activation)) tx, timestamp.After(btcscript.Bip16Activation))
return engine.Execute() return engine.Execute()
} }

View file

@ -494,7 +494,7 @@ func testScript(t *testing.T, script []byte) (err error) {
tx.TxOut[0].PkScript = script tx.TxOut[0].PkScript = script
engine, err := btcscript.NewScript(tx.TxIn[0].SignatureScript, 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 != nil {
return err return err
} }
@ -3875,7 +3875,7 @@ func testOpcode(t *testing.T, test *detailedTest) {
tx.TxOut[0].PkScript = test.script tx.TxOut[0].PkScript = test.script
engine, err := btcscript.NewScript(tx.TxIn[0].SignatureScript, 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 != nil {
if err != test.expectedReturn { if err != test.expectedReturn {
t.Errorf("Error return not expected %s: %v %v", t.Errorf("Error return not expected %s: %v %v",

View file

@ -147,7 +147,6 @@ type Script struct {
astack Stack // alt stack astack Stack // alt stack
tx btcwire.MsgTx tx btcwire.MsgTx
txidx int txidx int
pver uint32
condStack []int condStack []int
numOps int numOps int
bip16 bool // treat execution as pay-to-script-hash 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 // 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 // true then it will be treated as if the bip16 threshhold has passed and thus
// pay-to-script hash transactions will be fully validated. // 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 var m Script
scripts := [][]byte{scriptSig, scriptPubKey} scripts := [][]byte{scriptSig, scriptPubKey}
m.scripts = make([][]parsedOpcode, len(scripts)) 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.tx = *tx
m.txidx = txidx m.txidx = txidx
m.pver = pver
m.condStack = []int{OpCondTrue} m.condStack = []int{OpCondTrue}
return &m, nil return &m, nil
@ -692,7 +690,7 @@ func (s *Script) calcScriptHash(script []parsedOpcode, hashType byte) []byte {
} }
var wbuf bytes.Buffer var wbuf bytes.Buffer
txCopy.BtcEncode(&wbuf, s.pver) txCopy.Serialize(&wbuf)
// Append LE 4 bytes hash type // Append LE 4 bytes hash type
binary.Write(&wbuf, binary.LittleEndian, uint32(hashType)) binary.Write(&wbuf, binary.LittleEndian, uint32(hashType))

View file

@ -1204,7 +1204,7 @@ var txTests = []txTest{
func testTx(t *testing.T, test txTest) { func testTx(t *testing.T, test txTest) {
engine, err := btcscript.NewScript( engine, err := btcscript.NewScript(
test.tx.TxIn[test.idx].SignatureScript, test.pkScript, 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 != nil {
if err != test.parseErr { if err != test.parseErr {
t.Errorf("Failed to parse %s: got \"%v\" expected "+ t.Errorf("Failed to parse %s: got \"%v\" expected "+
@ -1731,7 +1731,7 @@ func TestBadPC(t *testing.T) {
for _, test := range pcTests { for _, test := range pcTests {
engine, err := btcscript.NewScript(tx.TxIn[0].SignatureScript, engine, err := btcscript.NewScript(tx.TxIn[0].SignatureScript,
pkScript, 0, tx, 70001, false) pkScript, 0, tx, false)
if err != nil { if err != nil {
t.Errorf("Failed to create script: %v", err) 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, engine, err := btcscript.NewScript(tx.TxIn[0].SignatureScript, pkScript,
0, tx, 70001, false) 0, tx, false)
if err != nil { if err != nil {
t.Errorf("failed to create script: %v", err) t.Errorf("failed to create script: %v", err)
} }