Convert builder.Push* to builder.Add* we think this is clearer.

Also, shorter!  Discussed with @davecgh.
This commit is contained in:
Owain G. Ainsworth 2014-02-20 17:42:06 +00:00
parent 1566366346
commit 5171cb803c
5 changed files with 40 additions and 40 deletions

View file

@ -1005,16 +1005,16 @@ func getSigOpCount(pops []parsedOpcode, precise bool) int {
// output to a 20-byte pubkey hash. It is expected that the input is a valid
// hash.
func payToPubKeyHashScript(pubKeyHash []byte) []byte {
return NewScriptBuilder().PushOp(OP_DUP).PushOp(OP_HASH160).
PushData(pubKeyHash).PushOp(OP_EQUALVERIFY).PushOp(OP_CHECKSIG).
return NewScriptBuilder().AddOp(OP_DUP).AddOp(OP_HASH160).
AddData(pubKeyHash).AddOp(OP_EQUALVERIFY).AddOp(OP_CHECKSIG).
Script()
}
// payToScriptHashScript creates a new script to pay a transaction output to a
// script hash. It is expected that the input is a valid hash.
func payToScriptHashScript(scriptHash []byte) []byte {
return NewScriptBuilder().PushOp(OP_HASH160).PushData(scriptHash).
PushOp(OP_EQUAL).Script()
return NewScriptBuilder().AddOp(OP_HASH160).AddData(scriptHash).
AddOp(OP_EQUAL).Script()
}
// PayToAddrScript creates a new script to pay a transaction output to a the
@ -1078,7 +1078,7 @@ func signatureScriptCustomReader(reader io.Reader, tx *btcwire.MsgTx, idx int,
pkData = pk.SerializeUncompressed()
}
return NewScriptBuilder().PushData(sig).PushData(pkData).Script(), nil
return NewScriptBuilder().AddData(sig).AddData(pkData).Script(), nil
}
// expectedInputs returns the number of arguments required by a script.

View file

@ -18,7 +18,7 @@ import (
func TestStandardPushes(t *testing.T) {
for i := 0; i < 1000; i++ {
builder := btcscript.NewScriptBuilder()
builder.PushInt64(int64(i))
builder.AddInt64(int64(i))
if result := btcscript.IsPushOnlyScript(builder.Script()); !result {
t.Errorf("StandardPushesTests IsPushOnlyScript test #%d failed: %x\n", i, builder.Script())
}
@ -29,7 +29,7 @@ func TestStandardPushes(t *testing.T) {
}
for i := 0; i < 1000; i++ {
builder := btcscript.NewScriptBuilder()
builder.PushData(bytes.Repeat([]byte{0x49}, i))
builder.AddData(bytes.Repeat([]byte{0x49}, i))
if result := btcscript.IsPushOnlyScript(builder.Script()); !result {
t.Errorf("StandardPushesTests IsPushOnlyScript test #%d failed: %x\n", i, builder.Script())
}

View file

@ -25,23 +25,23 @@ const (
// For example, the following would build a 2-of-3 multisig script for usage in
// a pay-to-script-hash:
// builder := btcscript.NewScriptBuilder()
// builder.PushOp(btcscript.OP_2).PushData(pubKey1).PushData(pubKey2)
// builder.PushData(pubKey3).PushOp(btcscript.OP_3)
// builder.PushOp(btcscript.OP_CHECKMULTISIG)
// builder.AddOp(btcscript.OP_2).AddData(pubKey1).AddData(pubKey2)
// builder.AddData(pubKey3).AddOp(btcscript.OP_3)
// builder.AddOp(btcscript.OP_CHECKMULTISIG)
// fmt.Printf("Final multi-sig script: %x\n", builder.Script())
type ScriptBuilder struct {
script []byte
}
// PushOp pushes the passed opcode to the end of the script.
func (b *ScriptBuilder) PushOp(opcode byte) *ScriptBuilder {
// AddOp pushes the passed opcode to the end of the script.
func (b *ScriptBuilder) AddOp(opcode byte) *ScriptBuilder {
b.script = append(b.script, opcode)
return b
}
// PushData pushes the passed data to the end of the script. It automatically
// AddData pushes the passed data to the end of the script. It automatically
// chooses canonical opcodes depending on the length of the data.
func (b *ScriptBuilder) PushData(data []byte) *ScriptBuilder {
func (b *ScriptBuilder) AddData(data []byte) *ScriptBuilder {
// Don't modify the script at all if no data was passed.
dataLen := len(data)
if dataLen == 0 {
@ -85,8 +85,8 @@ func (b *ScriptBuilder) PushData(data []byte) *ScriptBuilder {
return b
}
// PushInt64 pushes the passed integer to the end of the script.
func (b *ScriptBuilder) PushInt64(val int64) *ScriptBuilder {
// AddInt64 pushes the passed integer to the end of the script.
func (b *ScriptBuilder) AddInt64(val int64) *ScriptBuilder {
// Fast path for small integers and OP_1NEGATE.
if val == 0 {
b.script = append(b.script, OP_0)
@ -97,11 +97,11 @@ func (b *ScriptBuilder) PushInt64(val int64) *ScriptBuilder {
return b
}
return b.PushData(fromInt(new(big.Int).SetInt64(val)))
return b.AddData(fromInt(new(big.Int).SetInt64(val)))
}
// PushUint64 pushes the passed integer to the end of the script.
func (b *ScriptBuilder) PushUint64(val uint64) *ScriptBuilder {
// AddUint64 pushes the passed integer to the end of the script.
func (b *ScriptBuilder) AddUint64(val uint64) *ScriptBuilder {
// Fast path for small integers.
if val == 0 {
b.script = append(b.script, OP_0)
@ -112,7 +112,7 @@ func (b *ScriptBuilder) PushUint64(val uint64) *ScriptBuilder {
return b
}
return b.PushData(fromInt(new(big.Int).SetUint64(val)))
return b.AddData(fromInt(new(big.Int).SetUint64(val)))
}
// Reset resets the script so it has no content.

View file

@ -10,9 +10,9 @@ import (
"testing"
)
// TestScriptBuilderPushOp tests that pushing opcodes to a script via the
// TestScriptBuilderAddOp tests that pushing opcodes to a script via the
// ScriptBuilder API works as expected.
func TestScriptBuilderPushOp(t *testing.T) {
func TestScriptBuilderAddOp(t *testing.T) {
tests := []struct {
name string
opcodes []byte
@ -40,11 +40,11 @@ func TestScriptBuilderPushOp(t *testing.T) {
for i, test := range tests {
builder.Reset()
for _, opcode := range test.opcodes {
builder.PushOp(opcode)
builder.AddOp(opcode)
}
result := builder.Script()
if !bytes.Equal(result, test.expected) {
t.Errorf("ScriptBuilder.PushOp #%d (%s) wrong result\n"+
t.Errorf("ScriptBuilder.AddOp #%d (%s) wrong result\n"+
"got: %x\nwant: %x", i, test.name, result,
test.expected)
continue
@ -52,9 +52,9 @@ func TestScriptBuilderPushOp(t *testing.T) {
}
}
// TestScriptBuilderPushInt64 tests that pushing signed integers to a script via
// TestScriptBuilderAddInt64 tests that pushing signed integers to a script via
// the ScriptBuilder API works as expected.
func TestScriptBuilderPushInt64(t *testing.T) {
func TestScriptBuilderAddInt64(t *testing.T) {
tests := []struct {
name string
val int64
@ -103,10 +103,10 @@ func TestScriptBuilderPushInt64(t *testing.T) {
builder := btcscript.NewScriptBuilder()
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
builder.Reset().PushInt64(test.val)
builder.Reset().AddInt64(test.val)
result := builder.Script()
if !bytes.Equal(result, test.expected) {
t.Errorf("ScriptBuilder.PushInt64 #%d (%s) wrong result\n"+
t.Errorf("ScriptBuilder.AddInt64 #%d (%s) wrong result\n"+
"got: %x\nwant: %x", i, test.name, result,
test.expected)
continue
@ -114,9 +114,9 @@ func TestScriptBuilderPushInt64(t *testing.T) {
}
}
// TestScriptBuilderPushUint64 tests that pushing unsigned integers to a script
// TestScriptBuilderAddUint64 tests that pushing unsigned integers to a script
// via the ScriptBuilder API works as expected.
func TestScriptBuilderPushUint64(t *testing.T) {
func TestScriptBuilderAddUint64(t *testing.T) {
tests := []struct {
name string
val uint64
@ -152,10 +152,10 @@ func TestScriptBuilderPushUint64(t *testing.T) {
builder := btcscript.NewScriptBuilder()
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
builder.Reset().PushUint64(test.val)
builder.Reset().AddUint64(test.val)
result := builder.Script()
if !bytes.Equal(result, test.expected) {
t.Errorf("ScriptBuilder.PushUint64 #%d (%s) wrong result\n"+
t.Errorf("ScriptBuilder.AddUint64 #%d (%s) wrong result\n"+
"got: %x\nwant: %x", i, test.name, result,
test.expected)
continue
@ -163,9 +163,9 @@ func TestScriptBuilderPushUint64(t *testing.T) {
}
}
// TestScriptBuilderPushData tests that pushing data to a script via the
// TestScriptBuilderAddData tests that pushing data to a script via the
// ScriptBuilder API works as expected.
func TestScriptBuilderPushData(t *testing.T) {
func TestScriptBuilderAddData(t *testing.T) {
tests := []struct {
name string
data []byte
@ -237,10 +237,10 @@ func TestScriptBuilderPushData(t *testing.T) {
builder := btcscript.NewScriptBuilder()
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
builder.Reset().PushData(test.data)
builder.Reset().AddData(test.data)
result := builder.Script()
if !bytes.Equal(result, test.expected) {
t.Errorf("ScriptBuilder.PushData #%d (%s) wrong result\n"+
t.Errorf("ScriptBuilder.AddData #%d (%s) wrong result\n"+
"got: %x\nwant: %x", i, test.name, result,
test.expected)
continue

View file

@ -4,7 +4,7 @@ github.com/conformal/btcscript/script.go Script.Step 100.00% (38/38)
github.com/conformal/btcscript/script.go parseScriptTemplate 100.00% (30/30)
github.com/conformal/btcscript/address.go ExtractPkScriptAddrs 100.00% (27/27)
github.com/conformal/btcscript/script.go CalcScriptInfo 100.00% (25/25)
github.com/conformal/btcscript/scriptbuilder.go ScriptBuilder.PushData 100.00% (24/24)
github.com/conformal/btcscript/scriptbuilder.go ScriptBuilder.AddData 100.00% (24/24)
github.com/conformal/btcscript/opcode.go parsedOpcode.bytes 100.00% (23/23)
github.com/conformal/btcscript/stack.go asInt 100.00% (21/21)
github.com/conformal/btcscript/script.go NewScript 100.00% (21/21)
@ -43,8 +43,8 @@ github.com/conformal/btcscript/opcode.go opcodeEqual 100.00% (8/8)
github.com/conformal/btcscript/opcode.go opcodeAdd 100.00% (8/8)
github.com/conformal/btcscript/opcode.go opcode0NotEqual 100.00% (7/7)
github.com/conformal/btcscript/opcode.go opcodeNot 100.00% (7/7)
github.com/conformal/btcscript/scriptbuilder.go ScriptBuilder.PushInt64 100.00% (7/7)
github.com/conformal/btcscript/scriptbuilder.go ScriptBuilder.PushUint64 100.00% (7/7)
github.com/conformal/btcscript/scriptbuilder.go ScriptBuilder.AddInt64 100.00% (7/7)
github.com/conformal/btcscript/scriptbuilder.go ScriptBuilder.AddUint64 100.00% (7/7)
github.com/conformal/btcscript/opcode.go opcodeRoll 100.00% (7/7)
github.com/conformal/btcscript/stack.go Stack.DropN 100.00% (7/7)
github.com/conformal/btcscript/opcode.go opcodePick 100.00% (7/7)
@ -106,7 +106,7 @@ github.com/conformal/btcscript/opcode.go opcode1Negate 100.00% (2/2)
github.com/conformal/btcscript/opcode.go calcHash 100.00% (2/2)
github.com/conformal/btcscript/opcode.go opcodeCodeSeparator 100.00% (2/2)
github.com/conformal/btcscript/script.go GetSigOpCount 100.00% (2/2)
github.com/conformal/btcscript/scriptbuilder.go ScriptBuilder.PushOp 100.00% (2/2)
github.com/conformal/btcscript/scriptbuilder.go ScriptBuilder.AddOp 100.00% (2/2)
github.com/conformal/btcscript/scriptbuilder.go ScriptBuilder.Reset 100.00% (2/2)
github.com/conformal/btcscript/script.go Script.subScript 100.00% (1/1)
github.com/conformal/btcscript/opcode.go opcode2Rot 100.00% (1/1)