mirror of
https://github.com/LBRYFoundation/lbcd.git
synced 2025-08-23 17:47:24 +00:00
Return 0 for bad scripts from sigops code instead of an error.
matches how bitcoind behaves.
This commit is contained in:
parent
eb5de559ff
commit
e7f9415e4f
3 changed files with 15 additions and 69 deletions
|
@ -4004,20 +4004,7 @@ func TestDisasmStrings(t *testing.T) {
|
||||||
// us coverage over a wider range of opcodes.
|
// us coverage over a wider range of opcodes.
|
||||||
func TestSigOps(t *testing.T) {
|
func TestSigOps(t *testing.T) {
|
||||||
for _, test := range detailedTests {
|
for _, test := range detailedTests {
|
||||||
count, err := btcscript.GetSigOpCount(test.script)
|
count := btcscript.GetSigOpCount(test.script)
|
||||||
if err != nil {
|
|
||||||
if err != test.disassemblyerr {
|
|
||||||
t.Errorf("%s: unexpected error. exp \"%v\""+
|
|
||||||
"got \"%v\"", test.name,
|
|
||||||
test.disassemblyerr, err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if test.disassemblyerr != nil {
|
|
||||||
t.Errorf("%s: no error when expected \"%v\"",
|
|
||||||
test.name, test.disassemblyerr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if count != test.nSigOps {
|
if count != test.nSigOps {
|
||||||
t.Errorf("%s: expected count of %d, got %d", test.name,
|
t.Errorf("%s: expected count of %d, got %d", test.name,
|
||||||
test.nSigOps, count)
|
test.nSigOps, count)
|
||||||
|
@ -4035,21 +4022,8 @@ func TestSigOps(t *testing.T) {
|
||||||
// using real transactions to provide a bit more coverage.
|
// using real transactions to provide a bit more coverage.
|
||||||
func TestPreciseSigOps(t *testing.T) {
|
func TestPreciseSigOps(t *testing.T) {
|
||||||
for _, test := range detailedTests {
|
for _, test := range detailedTests {
|
||||||
count, err := btcscript.GetPreciseSigOpCount(
|
count := btcscript.GetPreciseSigOpCount(
|
||||||
[]byte{btcscript.OP_1}, test.script, false)
|
[]byte{btcscript.OP_1}, test.script, false)
|
||||||
if err != nil {
|
|
||||||
if err != test.disassemblyerr {
|
|
||||||
t.Errorf("%s: unexpected error. exp \"%v\""+
|
|
||||||
"got \"%v\"", test.name,
|
|
||||||
test.disassemblyerr, err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if test.disassemblyerr != nil {
|
|
||||||
t.Errorf("%s: no error when expected \"%v\"",
|
|
||||||
test.name, test.disassemblyerr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if count != test.nPreciseSigOps {
|
if count != test.nPreciseSigOps {
|
||||||
t.Errorf("%s: expected count of %d, got %d", test.name,
|
t.Errorf("%s: expected count of %d, got %d", test.name,
|
||||||
test.nPreciseSigOps, count)
|
test.nPreciseSigOps, count)
|
||||||
|
|
22
script.go
22
script.go
|
@ -763,51 +763,51 @@ func (s *Script) SetAltStack(data [][]byte) {
|
||||||
|
|
||||||
// GetSigOpCount provides a quick count of the number of signature operations
|
// GetSigOpCount provides a quick count of the number of signature operations
|
||||||
// in a script. a CHECKSIG operations counts for 1, and a CHECK_MULTISIG for 20.
|
// in a script. a CHECKSIG operations counts for 1, and a CHECK_MULTISIG for 20.
|
||||||
func GetSigOpCount(script []byte) (int, error) {
|
func GetSigOpCount(script []byte) int {
|
||||||
pops, err := parseScript(script)
|
pops, err := parseScript(script)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
return getSigOpCount(pops, false), nil
|
return getSigOpCount(pops, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPreciseSigOpCount returns the number of signature operations in
|
// GetPreciseSigOpCount returns the number of signature operations in
|
||||||
// scriptPubKey. If bip16 is true then scriptSig may be searched for the
|
// scriptPubKey. If bip16 is true then scriptSig may be searched for the
|
||||||
// Pay-To-Script-Hash script in order to find the precise number of signature
|
// Pay-To-Script-Hash script in order to find the precise number of signature
|
||||||
// operations in the transaction.
|
// operations in the transaction.
|
||||||
func GetPreciseSigOpCount(scriptSig, scriptPubKey []byte, bip16 bool) (int, error) {
|
func GetPreciseSigOpCount(scriptSig, scriptPubKey []byte, bip16 bool) int {
|
||||||
pops, err := parseScript(scriptPubKey)
|
pops, err := parseScript(scriptPubKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0
|
||||||
}
|
}
|
||||||
// non P2SH transactions just treated as normal.
|
// non P2SH transactions just treated as normal.
|
||||||
if !(bip16 && isScriptHash(pops)) {
|
if !(bip16 && isScriptHash(pops)) {
|
||||||
return getSigOpCount(pops, true), nil
|
return getSigOpCount(pops, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ok so this is P2SH, get the contained script and count it..
|
// Ok so this is P2SH, get the contained script and count it..
|
||||||
|
|
||||||
sigPops, err := parseScript(scriptSig)
|
sigPops, err := parseScript(scriptSig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0
|
||||||
}
|
}
|
||||||
if !isPushOnly(sigPops) || len(sigPops) == 0 {
|
if !isPushOnly(sigPops) || len(sigPops) == 0 {
|
||||||
return 0, nil
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
shScript := sigPops[len(sigPops)-1].data
|
shScript := sigPops[len(sigPops)-1].data
|
||||||
// Means that sigPops is jus OP_1 - OP_16, no sigops there.
|
// Means that sigPops is jus OP_1 - OP_16, no sigops there.
|
||||||
if shScript == nil {
|
if shScript == nil {
|
||||||
return 0, nil
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
shPops, err := parseScript(shScript)
|
shPops, err := parseScript(shScript)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
return getSigOpCount(shPops, true), nil
|
return getSigOpCount(shPops, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSigOpCount is the implementation function for counting the number of
|
// getSigOpCount is the implementation function for counting the number of
|
||||||
|
|
|
@ -21,7 +21,6 @@ type txTest struct {
|
||||||
err error // Failure of Executre
|
err error // Failure of Executre
|
||||||
shouldFail bool // Execute should fail with nonspecified error.
|
shouldFail bool // Execute should fail with nonspecified error.
|
||||||
nSigOps int // result of GetPreciseSigOpsCount
|
nSigOps int // result of GetPreciseSigOpsCount
|
||||||
sigOpsErr error // failure of GetPreciseSigOpsCount()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var txTests = []txTest{
|
var txTests = []txTest{
|
||||||
|
@ -1079,7 +1078,6 @@ var txTests = []txTest{
|
||||||
idx: 0,
|
idx: 0,
|
||||||
err: btcscript.StackErrShortScript,
|
err: btcscript.StackErrShortScript,
|
||||||
bip16: true,
|
bip16: true,
|
||||||
sigOpsErr: btcscript.StackErrShortScript,
|
|
||||||
},
|
},
|
||||||
txTest{
|
txTest{
|
||||||
// sigscript changed so to be non pushonly.
|
// sigscript changed so to be non pushonly.
|
||||||
|
@ -1247,22 +1245,9 @@ func TestGetPreciseSignOps(t *testing.T) {
|
||||||
// First we go over the range of tests in testTx and count the sigops in
|
// First we go over the range of tests in testTx and count the sigops in
|
||||||
// them.
|
// them.
|
||||||
for _, test := range txTests {
|
for _, test := range txTests {
|
||||||
count, err := btcscript.GetPreciseSigOpCount(
|
count := btcscript.GetPreciseSigOpCount(
|
||||||
test.tx.TxIn[test.idx].SignatureScript, test.pkScript,
|
test.tx.TxIn[test.idx].SignatureScript, test.pkScript,
|
||||||
test.bip16)
|
test.bip16)
|
||||||
// all tx currently parse
|
|
||||||
if err != nil {
|
|
||||||
if err != test.sigOpsErr {
|
|
||||||
t.Errorf("%s: unexpected error. got \"%v\"",
|
|
||||||
test.name, err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if test.sigOpsErr != nil {
|
|
||||||
t.Errorf("%s: expected error \"%v\" but got success",
|
|
||||||
test.name, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if count != test.nSigOps {
|
if count != test.nSigOps {
|
||||||
t.Errorf("%s: expected count of %d, got %d", test.name,
|
t.Errorf("%s: expected count of %d, got %d", test.name,
|
||||||
test.nSigOps, count)
|
test.nSigOps, count)
|
||||||
|
@ -1321,21 +1306,8 @@ func TestGetPreciseSignOps(t *testing.T) {
|
||||||
btcscript.OP_EQUAL,
|
btcscript.OP_EQUAL,
|
||||||
}
|
}
|
||||||
for _, test := range psocTests {
|
for _, test := range psocTests {
|
||||||
count, err := btcscript.GetPreciseSigOpCount(
|
count := btcscript.GetPreciseSigOpCount(
|
||||||
test.scriptSig, pkScript, true)
|
test.scriptSig, pkScript, true)
|
||||||
// all tx currently parse
|
|
||||||
if err != nil {
|
|
||||||
if err != test.err {
|
|
||||||
t.Errorf("%s: unexpected error. got \"%v\" exp: \"%v\"",
|
|
||||||
test.name, err, test.err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if test.err != nil {
|
|
||||||
t.Errorf("%s: expected error \"%v\" got none",
|
|
||||||
test.name, test.err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if count != test.nSigOps {
|
if count != test.nSigOps {
|
||||||
t.Errorf("%s: expected count of %d, got %d", test.name,
|
t.Errorf("%s: expected count of %d, got %d", test.name,
|
||||||
test.nSigOps, count)
|
test.nSigOps, count)
|
||||||
|
|
Loading…
Add table
Reference in a new issue