2015-10-21 19:53:25 +02:00
|
|
|
// Copyright (c) 2013-2015 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestCalcMinRequiredTxRelayFee tests the calcMinRequiredTxRelayFee API.
|
|
|
|
func TestCalcMinRequiredTxRelayFee(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string // test description.
|
|
|
|
size int64 // Transaction size in bytes.
|
|
|
|
relayFee btcutil.Amount // minimum relay transaction fee.
|
|
|
|
want int64 // Expected fee.
|
|
|
|
}{
|
|
|
|
{
|
2015-10-29 08:08:27 +01:00
|
|
|
// Ensure combination of size and fee that are less than 1000
|
|
|
|
// produce a non-zero fee.
|
|
|
|
"250 bytes with relay fee of 3",
|
|
|
|
250,
|
|
|
|
3,
|
|
|
|
3,
|
2015-10-21 19:53:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"100 bytes with default minimum relay fee",
|
|
|
|
100,
|
|
|
|
defaultMinRelayTxFee,
|
2015-10-29 08:08:27 +01:00
|
|
|
100,
|
2015-10-21 19:53:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"max standard tx size with default minimum relay fee",
|
|
|
|
maxStandardTxSize,
|
|
|
|
defaultMinRelayTxFee,
|
2015-10-29 08:08:27 +01:00
|
|
|
100000,
|
2015-10-21 19:53:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"max standard tx size with max satoshi relay fee",
|
|
|
|
maxStandardTxSize,
|
|
|
|
btcutil.MaxSatoshi,
|
|
|
|
btcutil.MaxSatoshi,
|
|
|
|
},
|
2015-10-29 08:08:27 +01:00
|
|
|
{
|
|
|
|
"1500 bytes with 5000 relay fee",
|
|
|
|
1500,
|
|
|
|
5000,
|
|
|
|
7500,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1500 bytes with 3000 relay fee",
|
|
|
|
1500,
|
|
|
|
3000,
|
|
|
|
4500,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"782 bytes with 5000 relay fee",
|
|
|
|
782,
|
|
|
|
5000,
|
|
|
|
3910,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"782 bytes with 3000 relay fee",
|
|
|
|
782,
|
|
|
|
3000,
|
|
|
|
2346,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"782 bytes with 2550 relay fee",
|
|
|
|
782,
|
|
|
|
2550,
|
|
|
|
1994,
|
|
|
|
},
|
2015-10-21 19:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
got := calcMinRequiredTxRelayFee(test.size, test.relayFee)
|
|
|
|
if got != test.want {
|
|
|
|
t.Errorf("TestCalcMinRequiredTxRelayFee test '%s' "+
|
|
|
|
"failed: got %v want %v", test.name, got,
|
|
|
|
test.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCheckPkScriptStandard tests the checkPkScriptStandard API.
|
|
|
|
func TestCheckPkScriptStandard(t *testing.T) {
|
|
|
|
var pubKeys [][]byte
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
pk, err := btcec.NewPrivateKey(btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("TestCheckPkScriptStandard NewPrivateKey failed: %v",
|
|
|
|
err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pubKeys = append(pubKeys, pk.PubKey().SerializeCompressed())
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string // test description.
|
|
|
|
script *txscript.ScriptBuilder
|
|
|
|
isStandard bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"key1 and key2",
|
|
|
|
txscript.NewScriptBuilder().AddOp(txscript.OP_2).
|
|
|
|
AddData(pubKeys[0]).AddData(pubKeys[1]).
|
|
|
|
AddOp(txscript.OP_2).AddOp(txscript.OP_CHECKMULTISIG),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key1 or key2",
|
|
|
|
txscript.NewScriptBuilder().AddOp(txscript.OP_1).
|
|
|
|
AddData(pubKeys[0]).AddData(pubKeys[1]).
|
|
|
|
AddOp(txscript.OP_2).AddOp(txscript.OP_CHECKMULTISIG),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"escrow",
|
|
|
|
txscript.NewScriptBuilder().AddOp(txscript.OP_2).
|
|
|
|
AddData(pubKeys[0]).AddData(pubKeys[1]).
|
|
|
|
AddData(pubKeys[2]).
|
|
|
|
AddOp(txscript.OP_3).AddOp(txscript.OP_CHECKMULTISIG),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"one of four",
|
|
|
|
txscript.NewScriptBuilder().AddOp(txscript.OP_1).
|
|
|
|
AddData(pubKeys[0]).AddData(pubKeys[1]).
|
|
|
|
AddData(pubKeys[2]).AddData(pubKeys[3]).
|
|
|
|
AddOp(txscript.OP_4).AddOp(txscript.OP_CHECKMULTISIG),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"malformed1",
|
|
|
|
txscript.NewScriptBuilder().AddOp(txscript.OP_3).
|
|
|
|
AddData(pubKeys[0]).AddData(pubKeys[1]).
|
|
|
|
AddOp(txscript.OP_2).AddOp(txscript.OP_CHECKMULTISIG),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"malformed2",
|
|
|
|
txscript.NewScriptBuilder().AddOp(txscript.OP_2).
|
|
|
|
AddData(pubKeys[0]).AddData(pubKeys[1]).
|
|
|
|
AddOp(txscript.OP_3).AddOp(txscript.OP_CHECKMULTISIG),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"malformed3",
|
|
|
|
txscript.NewScriptBuilder().AddOp(txscript.OP_0).
|
|
|
|
AddData(pubKeys[0]).AddData(pubKeys[1]).
|
|
|
|
AddOp(txscript.OP_2).AddOp(txscript.OP_CHECKMULTISIG),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"malformed4",
|
|
|
|
txscript.NewScriptBuilder().AddOp(txscript.OP_1).
|
|
|
|
AddData(pubKeys[0]).AddData(pubKeys[1]).
|
|
|
|
AddOp(txscript.OP_0).AddOp(txscript.OP_CHECKMULTISIG),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"malformed5",
|
|
|
|
txscript.NewScriptBuilder().AddOp(txscript.OP_1).
|
|
|
|
AddData(pubKeys[0]).AddData(pubKeys[1]).
|
|
|
|
AddOp(txscript.OP_CHECKMULTISIG),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"malformed6",
|
|
|
|
txscript.NewScriptBuilder().AddOp(txscript.OP_1).
|
|
|
|
AddData(pubKeys[0]).AddData(pubKeys[1]),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
script, err := test.script.Script()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("TestCheckPkScriptStandard test '%s' "+
|
|
|
|
"failed: %v", test.name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
scriptClass := txscript.GetScriptClass(script)
|
|
|
|
got := checkPkScriptStandard(script, scriptClass)
|
|
|
|
if (test.isStandard && got != nil) ||
|
|
|
|
(!test.isStandard && got == nil) {
|
|
|
|
|
|
|
|
t.Fatalf("TestCheckPkScriptStandard test '%s' failed",
|
|
|
|
test.name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestDust tests the isDust API.
|
|
|
|
func TestDust(t *testing.T) {
|
|
|
|
pkScript := []byte{0x76, 0xa9, 0x21, 0x03, 0x2f, 0x7e, 0x43,
|
|
|
|
0x0a, 0xa4, 0xc9, 0xd1, 0x59, 0x43, 0x7e, 0x84, 0xb9,
|
|
|
|
0x75, 0xdc, 0x76, 0xd9, 0x00, 0x3b, 0xf0, 0x92, 0x2c,
|
|
|
|
0xf3, 0xaa, 0x45, 0x28, 0x46, 0x4b, 0xab, 0x78, 0x0d,
|
|
|
|
0xba, 0x5e, 0x88, 0xac}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string // test description
|
|
|
|
txOut wire.TxOut
|
|
|
|
relayFee btcutil.Amount // minimum relay transaction fee.
|
|
|
|
isDust bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// Any value is allowed with a zero relay fee.
|
|
|
|
"zero value with zero relay fee",
|
|
|
|
wire.TxOut{0, pkScript},
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Zero value is dust with any relay fee"
|
|
|
|
"zero value with very small tx fee",
|
|
|
|
wire.TxOut{0, pkScript},
|
|
|
|
1,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"38 byte public key script with value 584",
|
|
|
|
wire.TxOut{584, pkScript},
|
|
|
|
1000,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"38 byte public key script with value 585",
|
|
|
|
wire.TxOut{585, pkScript},
|
|
|
|
1000,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Maximum allowed value is never dust.
|
|
|
|
"max satoshi amount is never dust",
|
|
|
|
wire.TxOut{btcutil.MaxSatoshi, pkScript},
|
|
|
|
btcutil.MaxSatoshi,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Maximum int64 value causes overflow.
|
|
|
|
"maximum int64 value",
|
|
|
|
wire.TxOut{1<<63 - 1, pkScript},
|
|
|
|
1<<63 - 1,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Unspendable pkScript due to an invalid public key
|
|
|
|
// script.
|
|
|
|
"unspendable pkScript",
|
|
|
|
wire.TxOut{5000, []byte{0x01}},
|
|
|
|
0, // no relay fee
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
res := isDust(&test.txOut, test.relayFee)
|
|
|
|
if res != test.isDust {
|
|
|
|
t.Fatalf("Dust test '%s' failed: want %v got %v",
|
|
|
|
test.name, test.isDust, res)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|