dcrd/policy_test.go
Alex Yocom-Piatt 14c7cc99d6 Move non-mempool specific functions to new file. (#140)
No functional change. Add tests.

Conflicts:
	mempool.go
2016-05-02 15:47:14 -05:00

237 lines
5.9 KiB
Go

// 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 (
"math/big"
"testing"
"github.com/decred/dcrd/chaincfg/chainec"
"github.com/decred/dcrd/txscript"
"github.com/decred/dcrd/wire"
"github.com/decred/dcrutil"
)
// TestCalcMinRequiredTxRelayFee tests the calcMinRequiredTxRelayFee API.
func TestCalcMinRequiredTxRelayFee(t *testing.T) {
tests := []struct {
name string // test description.
size int64 // Transaction size in bytes.
relayFee dcrutil.Amount // minimum relay transaction fee.
want int64 // Expected fee.
}{
{
"zero value with default minimum relay fee",
0,
minTxRelayFeeMainNet,
int64(minTxRelayFeeMainNet),
},
{
"1000 bytes with default minimum relay fee",
1000,
minTxRelayFeeMainNet,
int64(minTxRelayFeeMainNet),
},
{
"max standard tx size with default minimum relay fee",
maxStandardTxSize,
minTxRelayFeeMainNet,
100000000,
},
{
"max standard tx size with max satoshi relay fee",
maxStandardTxSize,
dcrutil.MaxAmount,
dcrutil.MaxAmount,
},
}
for _, test := range tests {
got := calcMinRequiredTxRelayFee(test.size, int64(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 := chainec.Secp256k1.NewPrivateKey(big.NewInt(int64(chainec.ECTypeSecp256k1)))
pubKeys = append(pubKeys, chainec.Secp256k1.NewPublicKey(pk.Public()).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(0, script)
got := checkPkScriptStandard(0, 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, 0x14, 0xb1, 0x2d, 0x0f, 0xca,
0xeb, 0x46, 0x14, 0xa3, 0x4b, 0x1e, 0x88, 0x61, 0xe7,
0x55, 0x4f, 0xd4, 0x13, 0xf7, 0xa6, 0x47, 0x88, 0xac}
tests := []struct {
name string // test description
txOut wire.TxOut
relayFee dcrutil.Amount // minimum relay transaction fee.
isDust bool
}{
{
// Any value is allowed with a zero relay fee.
"zero value with zero relay fee",
wire.TxOut{Value: 0, Version: 0, PkScript: pkScript},
0,
false,
},
{
// Zero value is dust with any relay fee"
"zero value with very small tx fee",
wire.TxOut{Value: 0, Version: 0, PkScript: pkScript},
1,
true,
},
{
"25 byte public key script with value 602",
wire.TxOut{Value: 602, Version: 0, PkScript: pkScript},
1000,
true,
},
{
"25 byte public key script with value 603",
wire.TxOut{Value: 603, Version: 0, PkScript: pkScript},
1000,
false,
},
{
// Maximum allowed value is never dust.
"max satoshi amount is never dust",
wire.TxOut{Value: dcrutil.MaxAmount, Version: 0, PkScript: pkScript},
dcrutil.MaxAmount,
false,
},
{
// Maximum int64 value causes overflow.
"maximum int64 value",
wire.TxOut{Value: 1<<63 - 1, Version: 0, PkScript: pkScript},
1<<63 - 1,
true,
},
{
// Unspendable pkScript due to an invalid public key
// script.
"unspendable pkScript",
wire.TxOut{Value: 5000, Version: 0, PkScript: []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
}
}
}