dcrd/txscript/consensus_test.go
Dave Collins 75432b3c96
multi: Break coinbase dep on standardness rules.
Standardness rules are not the same as consensus rules and it is
important to keep clear separation between them, because standardness
rules can and do change, while the consensus rules must not change
without a specific vote, and even then, the old rules must be kept
around for validation of all blocks prior to any changes introduced by a
successful vote.

Prior to this commit, the blockchain code which enforces the consensus
rule that requires the second output of the coinbase transaction to
contain the height of the block in a provably pruneable nulldata script
push was relying on code in txscript/standard.go, which, as the name of
the file suggests, is specifically intended for code related to
standardness checks.

This introduces a new function in the txscript package named
ExtractCoinbaseNullData which does not rely on any of the code related
to standardness checks and modifies the blockchain code accordingly to
make use of the new function instead.  It also removes the
standardness-dependent GetNullDataContent function which is no longer
used by anything.

Finally, it adds tests in the txscript package to ensure the new
function has the required semantics.
2018-05-08 12:49:40 -05:00

76 lines
2.2 KiB
Go

// Copyright (c) 2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package txscript
import (
"bytes"
"testing"
)
// TestExtractCoinbaseNullData ensures the ExtractCoinbaseNullData function
// produces the expected extracted data under both valid and invalid scenarios.
func TestExtractCoinbaseNullData(t *testing.T) {
tests := []struct {
name string
script []byte
valid bool
result []byte
}{{
name: "block 2, height only",
script: mustParseShortForm("RETURN DATA_4 0x02000000"),
valid: true,
result: hexToBytes("02000000"),
}, {
name: "block 2, height and extra nonce data",
script: mustParseShortForm("RETURN DATA_36 0x02000000000000000000000000000000000000000000000000000000ffa310d9a6a9588e"),
valid: true,
result: hexToBytes("02000000000000000000000000000000000000000000000000000000ffa310d9a6a9588e"),
}, {
name: "block 2, height and reduced extra nonce data",
script: mustParseShortForm("RETURN DATA_12 0x02000000ffa310d9a6a9588e"),
valid: true,
result: hexToBytes("02000000ffa310d9a6a9588e"),
}, {
name: "no push",
script: mustParseShortForm("RETURN"),
valid: true,
result: nil,
}, {
// Normal nulldata scripts support special handling of small data,
// however the coinbase nulldata in question does not.
name: "small data",
script: mustParseShortForm("RETURN OP_2"),
valid: false,
result: nil,
}, {
name: "almost correct",
script: mustParseShortForm("OP_TRUE RETURN DATA_12 0x02000000ffa310d9a6a9588e"),
valid: false,
result: nil,
}, {
name: "almost correct 2",
script: mustParseShortForm("DATA_12 0x02000000 0xffa310d9a6a9588e"),
valid: false,
result: nil,
}}
for _, test := range tests {
nullData, err := ExtractCoinbaseNullData(test.script)
if test.valid && err != nil {
t.Errorf("test '%s' unexpected error: %v", test.name, err)
continue
} else if !test.valid && err == nil {
t.Errorf("test '%s' passed when it should have failed", test.name)
continue
}
if !bytes.Equal(nullData, test.result) {
t.Errorf("test '%s' mismatched result - got %x, want %x", test.name,
nullData, test.result)
continue
}
}
}