mirror of
https://github.com/FlipsideCrypto/dcrd.git
synced 2026-02-06 10:56:47 +00:00
This commit is the first stage of several that are planned to convert
the blockchain package into a concurrent safe package that will
ultimately allow support for multi-peer download and concurrent chain
processing. The goal is to update btcd proper after each step so it can
take advantage of the enhancements as they are developed.
In addition to the aforementioned benefit, this staged approach has been
chosen since it is absolutely critical to maintain consensus.
Separating the changes into several stages makes it easier for reviewers
to logically follow what is happening and therefore helps prevent
consensus bugs. Naturally there are significant automated tests to help
prevent consensus issues as well.
The main focus of this stage is to convert the blockchain package to use
the new database interface and implement the chain-related functionality
which it no longer handles. It also aims to improve efficiency in
various areas by making use of the new database and chain capabilities.
The following is an overview of the chain changes:
- Update to use the new database interface
- Add chain-related functionality that the old database used to handle
- Main chain structure and state
- Transaction spend tracking
- Implement a new pruned unspent transaction output (utxo) set
- Provides efficient direct access to the unspent transaction outputs
- Uses a domain specific compression algorithm that understands the
standard transaction scripts in order to significantly compress them
- Removes reliance on the transaction index and paves the way toward
eventually enabling block pruning
- Modify the New function to accept a Config struct instead of
inidividual parameters
- Replace the old TxStore type with a new UtxoViewpoint type that makes
use of the new pruned utxo set
- Convert code to treat the new UtxoViewpoint as a rolling view that is
used between connects and disconnects to improve efficiency
- Make best chain state always set when the chain instance is created
- Remove now unnecessary logic for dealing with unset best state
- Make all exported functions concurrent safe
- Currently using a single chain state lock as it provides a straight
forward and easy to review path forward however this can be improved
with more fine grained locking
- Optimize various cases where full blocks were being loaded when only
the header is needed to help reduce the I/O load
- Add the ability for callers to get a snapshot of the current best
chain stats in a concurrent safe fashion
- Does not block callers while new blocks are being processed
- Make error messages that reference transaction outputs consistently
use <transaction hash>:<output index>
- Introduce a new AssertError type an convert internal consistency
checks to use it
- Update tests and examples to reflect the changes
- Add a full suite of tests to ensure correct functionality of the new
code
The following is an overview of the btcd changes:
- Update to use the new database and chain interfaces
- Temporarily remove all code related to the transaction index
- Temporarily remove all code related to the address index
- Convert all code that uses transaction stores to use the new utxo
view
- Rework several calls that required the block manager for safe
concurrency to use the chain package directly now that it is
concurrent safe
- Change all calls to obtain the best hash to use the new best state
snapshot capability from the chain package
- Remove workaround for limits on fetching height ranges since the new
database interface no longer imposes them
- Correct the gettxout RPC handler to return the best chain hash as
opposed the hash the txout was found in
- Optimize various RPC handlers:
- Change several of the RPC handlers to use the new chain snapshot
capability to avoid needlessly loading data
- Update several handlers to use new functionality to avoid accessing
the block manager so they are able to return the data without
blocking when the server is busy processing blocks
- Update non-verbose getblock to avoid deserialization and
serialization overhead
- Update getblockheader to request the block height directly from
chain and only load the header
- Update getdifficulty to use the new cached data from chain
- Update getmininginfo to use the new cached data from chain
- Update non-verbose getrawtransaction to avoid deserialization and
serialization overhead
- Update gettxout to use the new utxo store versus loading
full transactions using the transaction index
The following is an overview of the utility changes:
- Update addblock to use the new database and chain interfaces
- Update findcheckpoint to use the new database and chain interfaces
- Remove the dropafter utility which is no longer supported
NOTE: The transaction index and address index will be reimplemented in
another commit.
523 lines
13 KiB
Go
523 lines
13 KiB
Go
// Copyright (c) 2013-2015 The btcsuite developers
|
|
// Copyright (c) 2016 The Decred developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/decred/dcrd/blockchain"
|
|
"github.com/decred/dcrd/blockchain/stake"
|
|
"github.com/decred/dcrd/chaincfg"
|
|
"github.com/decred/dcrd/chaincfg/chainec"
|
|
"github.com/decred/dcrd/chaincfg/chainhash"
|
|
"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.
|
|
}{
|
|
{
|
|
// 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,
|
|
},
|
|
{
|
|
"1000 bytes with default minimum relay fee",
|
|
1000,
|
|
defaultMinRelayTxFee,
|
|
1000000,
|
|
},
|
|
{
|
|
"max standard tx size with default minimum relay fee",
|
|
maxStandardTxSize,
|
|
defaultMinRelayTxFee,
|
|
100000000,
|
|
},
|
|
{
|
|
"max standard tx size with max satoshi relay fee",
|
|
maxStandardTxSize,
|
|
dcrutil.MaxAmount,
|
|
dcrutil.MaxAmount,
|
|
},
|
|
{
|
|
"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,
|
|
},
|
|
}
|
|
|
|
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 := 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,
|
|
true,
|
|
},
|
|
{
|
|
// 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
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestCheckTransactionStandard tests the checkTransactionStandard API.
|
|
func TestCheckTransactionStandard(t *testing.T) {
|
|
// Create some dummy, but otherwise standard, data for transactions.
|
|
prevOutHash, err := chainhash.NewHashFromStr("01")
|
|
if err != nil {
|
|
t.Fatalf("NewShaHashFromStr: unexpected error: %v", err)
|
|
}
|
|
dummyPrevOut := wire.OutPoint{Hash: *prevOutHash, Index: 1, Tree: 0}
|
|
dummySigScript := bytes.Repeat([]byte{0x00}, 65)
|
|
dummyTxIn := wire.TxIn{
|
|
PreviousOutPoint: dummyPrevOut,
|
|
Sequence: wire.MaxTxInSequenceNum,
|
|
ValueIn: 0,
|
|
BlockHeight: 0,
|
|
BlockIndex: 0,
|
|
SignatureScript: dummySigScript,
|
|
}
|
|
addrHash := [20]byte{0x01}
|
|
addr, err := dcrutil.NewAddressPubKeyHash(addrHash[:],
|
|
&chaincfg.TestNetParams, chainec.ECTypeSecp256k1)
|
|
if err != nil {
|
|
t.Fatalf("NewAddressPubKeyHash: unexpected error: %v", err)
|
|
}
|
|
dummyPkScript, err := txscript.PayToAddrScript(addr)
|
|
if err != nil {
|
|
t.Fatalf("PayToAddrScript: unexpected error: %v", err)
|
|
}
|
|
dummyTxOut := wire.TxOut{
|
|
Value: 100000000, // 1 BTC
|
|
Version: 0,
|
|
PkScript: dummyPkScript,
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
tx wire.MsgTx
|
|
height int64
|
|
isStandard bool
|
|
code wire.RejectCode
|
|
}{
|
|
{
|
|
name: "Typical pay-to-pubkey-hash transaction",
|
|
tx: wire.MsgTx{
|
|
Version: 1,
|
|
TxIn: []*wire.TxIn{&dummyTxIn},
|
|
TxOut: []*wire.TxOut{&dummyTxOut},
|
|
LockTime: 0,
|
|
},
|
|
height: 300000,
|
|
isStandard: true,
|
|
},
|
|
{
|
|
name: "Transaction version too high",
|
|
tx: wire.MsgTx{
|
|
Version: int32(wire.TxVersion + 1),
|
|
TxIn: []*wire.TxIn{&dummyTxIn},
|
|
TxOut: []*wire.TxOut{&dummyTxOut},
|
|
LockTime: 0,
|
|
},
|
|
height: 300000,
|
|
isStandard: false,
|
|
code: wire.RejectNonstandard,
|
|
},
|
|
{
|
|
name: "Transaction is not finalized",
|
|
tx: wire.MsgTx{
|
|
Version: 1,
|
|
TxIn: []*wire.TxIn{{
|
|
PreviousOutPoint: dummyPrevOut,
|
|
SignatureScript: dummySigScript,
|
|
Sequence: 0,
|
|
}},
|
|
TxOut: []*wire.TxOut{&dummyTxOut},
|
|
LockTime: 300001,
|
|
},
|
|
height: 300000,
|
|
isStandard: false,
|
|
code: wire.RejectNonstandard,
|
|
},
|
|
{
|
|
name: "Transaction size is too large",
|
|
tx: wire.MsgTx{
|
|
Version: 1,
|
|
TxIn: []*wire.TxIn{&dummyTxIn},
|
|
TxOut: []*wire.TxOut{{
|
|
Value: 0,
|
|
PkScript: bytes.Repeat([]byte{0x00},
|
|
maxStandardTxSize+1),
|
|
}},
|
|
LockTime: 0,
|
|
},
|
|
height: 300000,
|
|
isStandard: false,
|
|
code: wire.RejectNonstandard,
|
|
},
|
|
{
|
|
name: "Signature script size is too large",
|
|
tx: wire.MsgTx{
|
|
Version: 1,
|
|
TxIn: []*wire.TxIn{{
|
|
PreviousOutPoint: dummyPrevOut,
|
|
SignatureScript: bytes.Repeat([]byte{0x00},
|
|
maxStandardSigScriptSize+1),
|
|
Sequence: wire.MaxTxInSequenceNum,
|
|
}},
|
|
TxOut: []*wire.TxOut{&dummyTxOut},
|
|
LockTime: 0,
|
|
},
|
|
height: 300000,
|
|
isStandard: false,
|
|
code: wire.RejectNonstandard,
|
|
},
|
|
{
|
|
name: "Signature script that does more than push data",
|
|
tx: wire.MsgTx{
|
|
Version: 1,
|
|
TxIn: []*wire.TxIn{{
|
|
PreviousOutPoint: dummyPrevOut,
|
|
SignatureScript: []byte{
|
|
txscript.OP_CHECKSIGVERIFY},
|
|
Sequence: wire.MaxTxInSequenceNum,
|
|
}},
|
|
TxOut: []*wire.TxOut{&dummyTxOut},
|
|
LockTime: 0,
|
|
},
|
|
height: 300000,
|
|
isStandard: false,
|
|
code: wire.RejectNonstandard,
|
|
},
|
|
{
|
|
name: "Valid but non standard public key script",
|
|
tx: wire.MsgTx{
|
|
Version: 1,
|
|
TxIn: []*wire.TxIn{&dummyTxIn},
|
|
TxOut: []*wire.TxOut{{
|
|
Value: 100000000,
|
|
PkScript: []byte{txscript.OP_TRUE},
|
|
}},
|
|
LockTime: 0,
|
|
},
|
|
height: 300000,
|
|
isStandard: false,
|
|
code: wire.RejectNonstandard,
|
|
},
|
|
{
|
|
name: "More than four nulldata outputs",
|
|
tx: wire.MsgTx{
|
|
Version: 1,
|
|
TxIn: []*wire.TxIn{&dummyTxIn},
|
|
TxOut: []*wire.TxOut{{
|
|
Value: 0,
|
|
PkScript: []byte{txscript.OP_RETURN},
|
|
}, {
|
|
Value: 0,
|
|
PkScript: []byte{txscript.OP_RETURN},
|
|
}, {
|
|
Value: 0,
|
|
PkScript: []byte{txscript.OP_RETURN},
|
|
}, {
|
|
Value: 0,
|
|
PkScript: []byte{txscript.OP_RETURN},
|
|
}, {
|
|
Value: 0,
|
|
PkScript: []byte{txscript.OP_RETURN},
|
|
}},
|
|
LockTime: 0,
|
|
},
|
|
height: 300000,
|
|
isStandard: false,
|
|
code: wire.RejectNonstandard,
|
|
},
|
|
{
|
|
name: "Dust output",
|
|
tx: wire.MsgTx{
|
|
Version: 1,
|
|
TxIn: []*wire.TxIn{&dummyTxIn},
|
|
TxOut: []*wire.TxOut{{
|
|
Value: 0,
|
|
PkScript: dummyPkScript,
|
|
}},
|
|
LockTime: 0,
|
|
},
|
|
height: 300000,
|
|
isStandard: false,
|
|
code: wire.RejectDust,
|
|
},
|
|
{
|
|
name: "One nulldata output with 0 amount (standard)",
|
|
tx: wire.MsgTx{
|
|
Version: 1,
|
|
TxIn: []*wire.TxIn{&dummyTxIn},
|
|
TxOut: []*wire.TxOut{{
|
|
Value: 0,
|
|
PkScript: []byte{txscript.OP_RETURN},
|
|
}},
|
|
LockTime: 0,
|
|
},
|
|
height: 300000,
|
|
isStandard: true,
|
|
},
|
|
}
|
|
|
|
timeSource := blockchain.NewMedianTime()
|
|
for _, test := range tests {
|
|
// Ensure standardness is as expected.
|
|
tx := dcrutil.NewTx(&test.tx)
|
|
err := checkTransactionStandard(tx, stake.DetermineTxType(tx),
|
|
test.height, timeSource, defaultMinRelayTxFee)
|
|
if err == nil && test.isStandard {
|
|
// Test passes since function returned standard for a
|
|
// transaction which is intended to be standard.
|
|
continue
|
|
}
|
|
if err == nil && !test.isStandard {
|
|
t.Errorf("checkTransactionStandard (%s): standard when "+
|
|
"it should not be", test.name)
|
|
continue
|
|
}
|
|
if err != nil && test.isStandard {
|
|
t.Errorf("checkTransactionStandard (%s): nonstandard "+
|
|
"when it should not be: %v", test.name, err)
|
|
continue
|
|
}
|
|
|
|
// Ensure error type is a TxRuleError inside of a RuleError.
|
|
rerr, ok := err.(RuleError)
|
|
if !ok {
|
|
t.Errorf("checkTransactionStandard (%s): unexpected "+
|
|
"error type - got %T", test.name, err)
|
|
continue
|
|
}
|
|
txrerr, ok := rerr.Err.(TxRuleError)
|
|
if !ok {
|
|
t.Errorf("checkTransactionStandard (%s): unexpected "+
|
|
"error type - got %T", test.name, rerr.Err)
|
|
continue
|
|
}
|
|
|
|
// Ensure the reject code is the expected one.
|
|
if txrerr.RejectCode != test.code {
|
|
t.Errorf("checkTransactionStandard (%s): unexpected "+
|
|
"error code - got %v, want %v", test.name,
|
|
txrerr.RejectCode, test.code)
|
|
continue
|
|
}
|
|
}
|
|
}
|