mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-12 00:56:42 +00:00
txscript: Cleanup and improve NullDataScript tests.
This modifies the recently-added NullDataScript function in several ways in an effort to make them more consistent with the tests in the rest of the code base and improve/correct the logic: - Use the hexToBytes and mustParseShortForm functions - Consistently format the test errors - Replace the valid bool flag with an expected error and test against it - Ensure the returned script type is the expected type in all cases
This commit is contained in:
parent
b77654f8d4
commit
0731f2ddc9
@ -351,8 +351,8 @@ func PayToAddrScript(addr btcutil.Address) ([]byte, error) {
|
|||||||
return nil, ErrUnsupportedAddress
|
return nil, ErrUnsupportedAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
// NullDataScript creates a provably prunable script
|
// NullDataScript creates a provably-prunable script containing OP_RETURN
|
||||||
// containing OP_RETURN followed by the passed data.
|
// followed by the passed data.
|
||||||
func NullDataScript(data []byte) ([]byte, error) {
|
func NullDataScript(data []byte) ([]byte, error) {
|
||||||
if len(data) > MaxDataCarrierSize {
|
if len(data) > MaxDataCarrierSize {
|
||||||
return nil, ErrStackLongScript
|
return nil, ErrStackLongScript
|
||||||
|
@ -1031,86 +1031,85 @@ func TestStringifyClass(t *testing.T) {
|
|||||||
|
|
||||||
// TestNullDataScript tests whether NullDataScript returns a valid script.
|
// TestNullDataScript tests whether NullDataScript returns a valid script.
|
||||||
func TestNullDataScript(t *testing.T) {
|
func TestNullDataScript(t *testing.T) {
|
||||||
tiny := hexToBytes("01")
|
|
||||||
short := hexToBytes("0102030405060708090a0b0c0d0e0f1" +
|
|
||||||
"01112131415161718")
|
|
||||||
shortExp := hexToBytes("6a180102030405060708090a0b0c0d0e0f1" +
|
|
||||||
"01112131415161718")
|
|
||||||
justRight := hexToBytes("000102030405060708090a0b0c0d0e0f" +
|
|
||||||
"101112131415161718191a1b1c1d1e1f20212223242526272829" +
|
|
||||||
"2a2b2c2d2e2f303132333435363738393a3b3c3d3e3f40414243" +
|
|
||||||
"4445464748494a4b4c4d4e4f")
|
|
||||||
tooLong := hexToBytes("000102030405060708090a0b0c0d0e0f" +
|
|
||||||
"101112131415161718191a1b1c1d1e1f20212223242526272829" +
|
|
||||||
"2a2b2c2d2e2f303132333435363738393a3b3c3d3e3f40414243" +
|
|
||||||
"4445464748494a4b4c4d4e4f50")
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
data []byte
|
data []byte
|
||||||
valid bool
|
|
||||||
expected []byte
|
expected []byte
|
||||||
|
err error
|
||||||
|
class ScriptClass
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "small data",
|
name: "small int",
|
||||||
data: tiny,
|
data: hexToBytes("01"),
|
||||||
valid: true,
|
expected: mustParseShortForm("RETURN 1"),
|
||||||
expected: []byte{0x6a, 0x51},
|
err: nil,
|
||||||
|
class: NullDataTy,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "data of size before OP_PUSHDATA1 is needed",
|
name: "max small int",
|
||||||
data: short,
|
data: hexToBytes("10"),
|
||||||
valid: true,
|
expected: mustParseShortForm("RETURN 16"),
|
||||||
expected: shortExp,
|
err: nil,
|
||||||
|
class: NullDataTy,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "just right",
|
name: "data of size before OP_PUSHDATA1 is needed",
|
||||||
data: justRight,
|
data: hexToBytes("0102030405060708090a0b0c0d0e0f10111" +
|
||||||
valid: true,
|
"2131415161718"),
|
||||||
expected: append([]byte{
|
expected: mustParseShortForm("RETURN 0x18 0x01020304" +
|
||||||
OP_RETURN, OP_PUSHDATA1, 80},
|
"05060708090a0b0c0d0e0f101112131415161718"),
|
||||||
justRight...),
|
err: nil,
|
||||||
|
class: NullDataTy,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "too big",
|
name: "just right",
|
||||||
data: tooLong,
|
data: hexToBytes("000102030405060708090a0b0c0d0e0f101" +
|
||||||
valid: false,
|
"112131415161718191a1b1c1d1e1f202122232425262" +
|
||||||
|
"728292a2b2c2d2e2f303132333435363738393a3b3c3" +
|
||||||
|
"d3e3f404142434445464748494a4b4c4d4e4f"),
|
||||||
|
expected: mustParseShortForm("RETURN PUSHDATA1 0x50 " +
|
||||||
|
"0x000102030405060708090a0b0c0d0e0f101112131" +
|
||||||
|
"415161718191a1b1c1d1e1f20212223242526272829" +
|
||||||
|
"2a2b2c2d2e2f303132333435363738393a3b3c3d3e3" +
|
||||||
|
"f404142434445464748494a4b4c4d4e4f"),
|
||||||
|
err: nil,
|
||||||
|
class: NullDataTy,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "too big",
|
||||||
|
data: hexToBytes("000102030405060708090a0b0c0d0e0f101" +
|
||||||
|
"112131415161718191a1b1c1d1e1f202122232425262" +
|
||||||
|
"728292a2b2c2d2e2f303132333435363738393a3b3c3" +
|
||||||
|
"d3e3f404142434445464748494a4b4c4d4e4f50"),
|
||||||
expected: nil,
|
expected: nil,
|
||||||
|
err: ErrStackLongScript,
|
||||||
|
class: NonStandardTy,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for i, test := range tests {
|
||||||
script, err := NullDataScript(test.data)
|
script, err := NullDataScript(test.data)
|
||||||
|
if err != test.err {
|
||||||
if !test.valid {
|
t.Errorf("NullDataScript: #%d (%s) unexpected error: "+
|
||||||
if err == nil {
|
"got %v, want %v", i, test.name, err, test.err)
|
||||||
t.Errorf("NullDataScript test %v: expected error but none was returned. ",
|
|
||||||
test.name)
|
|
||||||
}
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("NullDataScript test %v: Unexpected error returned %v ",
|
|
||||||
test.name, err)
|
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the expected result was returned.
|
// Check that the expected result was returned.
|
||||||
if !bytes.Equal(script, test.expected) {
|
if !bytes.Equal(script, test.expected) {
|
||||||
t.Errorf("NullDataScript test %v: Expected %x, got %x",
|
t.Errorf("NullDataScript: #%d (%s) wrong result\n"+
|
||||||
test.name, test.expected, script)
|
"got: %x\nwant: %x", i, test.name, script,
|
||||||
|
test.expected)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the script has the correct type.
|
// Check that the script has the correct type.
|
||||||
scriptType := GetScriptClass(script)
|
scriptType := GetScriptClass(script)
|
||||||
if scriptType != NullDataTy {
|
if scriptType != test.class {
|
||||||
t.Errorf("NullDataScript test %v: Expected NullDataTy, got %v",
|
t.Errorf("GetScriptClass: #%d (%s) wrong result -- "+
|
||||||
test.name, scriptType)
|
"got: %v, want: %v", i, test.name, scriptType,
|
||||||
|
test.class)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user