mirror of
https://github.com/FlipsideCrypto/dcrd.git
synced 2026-02-06 19:06:51 +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.
457 lines
11 KiB
Go
457 lines
11 KiB
Go
// Copyright (c) 2013-2015 The btcsuite developers
|
|
// Copyright (c) 2015-2016 The Decred developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package txscript_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/decred/dcrd/chaincfg/chainhash"
|
|
"github.com/decred/dcrd/txscript"
|
|
"github.com/decred/dcrd/wire"
|
|
)
|
|
|
|
// TestBadPC sets the pc to a deliberately bad result then confirms that Step()
|
|
// and Disasm fail correctly.
|
|
func TestBadPC(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
type pcTest struct {
|
|
script, off int
|
|
}
|
|
pcTests := []pcTest{
|
|
{
|
|
script: 2,
|
|
off: 0,
|
|
},
|
|
{
|
|
script: 0,
|
|
off: 2,
|
|
},
|
|
}
|
|
// tx with almost empty scripts.
|
|
tx := &wire.MsgTx{
|
|
Version: 1,
|
|
TxIn: []*wire.TxIn{
|
|
{
|
|
PreviousOutPoint: wire.OutPoint{
|
|
Hash: chainhash.Hash([32]byte{
|
|
0xc9, 0x97, 0xa5, 0xe5,
|
|
0x6e, 0x10, 0x41, 0x02,
|
|
0xfa, 0x20, 0x9c, 0x6a,
|
|
0x85, 0x2d, 0xd9, 0x06,
|
|
0x60, 0xa2, 0x0b, 0x2d,
|
|
0x9c, 0x35, 0x24, 0x23,
|
|
0xed, 0xce, 0x25, 0x85,
|
|
0x7f, 0xcd, 0x37, 0x04,
|
|
}),
|
|
Index: 0,
|
|
},
|
|
SignatureScript: []uint8{txscript.OP_NOP},
|
|
Sequence: 4294967295,
|
|
},
|
|
},
|
|
TxOut: []*wire.TxOut{
|
|
{
|
|
Value: 1000000000,
|
|
PkScript: nil,
|
|
},
|
|
},
|
|
LockTime: 0,
|
|
}
|
|
pkScript := []byte{txscript.OP_NOP}
|
|
|
|
for _, test := range pcTests {
|
|
vm, err := txscript.NewEngine(pkScript, tx, 0, 0, 0, nil)
|
|
if err != nil {
|
|
t.Errorf("Failed to create script: %v", err)
|
|
}
|
|
|
|
// set to after all scripts
|
|
vm.TstSetPC(test.script, test.off)
|
|
|
|
_, err = vm.Step()
|
|
if err == nil {
|
|
t.Errorf("Step with invalid pc (%v) succeeds!", test)
|
|
continue
|
|
}
|
|
|
|
_, err = vm.DisasmPC()
|
|
if err == nil {
|
|
t.Errorf("DisasmPC with invalid pc (%v) succeeds!",
|
|
test)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestCheckErrorCondition tests the execute early test in CheckErrorCondition()
|
|
// since most code paths are tested elsewhere.
|
|
func TestCheckErrorCondition(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// tx with almost empty scripts.
|
|
tx := &wire.MsgTx{
|
|
Version: 1,
|
|
TxIn: []*wire.TxIn{
|
|
{
|
|
PreviousOutPoint: wire.OutPoint{
|
|
Hash: chainhash.Hash([32]byte{
|
|
0xc9, 0x97, 0xa5, 0xe5,
|
|
0x6e, 0x10, 0x41, 0x02,
|
|
0xfa, 0x20, 0x9c, 0x6a,
|
|
0x85, 0x2d, 0xd9, 0x06,
|
|
0x60, 0xa2, 0x0b, 0x2d,
|
|
0x9c, 0x35, 0x24, 0x23,
|
|
0xed, 0xce, 0x25, 0x85,
|
|
0x7f, 0xcd, 0x37, 0x04,
|
|
}),
|
|
Index: 0,
|
|
},
|
|
SignatureScript: []uint8{},
|
|
Sequence: 4294967295,
|
|
},
|
|
},
|
|
TxOut: []*wire.TxOut{
|
|
{
|
|
Value: 1000000000,
|
|
PkScript: nil,
|
|
},
|
|
},
|
|
LockTime: 0,
|
|
}
|
|
pkScript := []byte{
|
|
txscript.OP_NOP,
|
|
txscript.OP_NOP,
|
|
txscript.OP_NOP,
|
|
txscript.OP_NOP,
|
|
txscript.OP_NOP,
|
|
txscript.OP_NOP,
|
|
txscript.OP_NOP,
|
|
txscript.OP_NOP,
|
|
txscript.OP_NOP,
|
|
txscript.OP_NOP,
|
|
txscript.OP_TRUE,
|
|
}
|
|
|
|
vm, err := txscript.NewEngine(pkScript, tx, 0, 0, 0, nil)
|
|
if err != nil {
|
|
t.Errorf("failed to create script: %v", err)
|
|
}
|
|
|
|
for i := 0; i < len(pkScript)-1; i++ {
|
|
done, err := vm.Step()
|
|
if err != nil {
|
|
t.Errorf("failed to step %dth time: %v", i, err)
|
|
return
|
|
}
|
|
if done {
|
|
t.Errorf("finshed early on %dth time", i)
|
|
return
|
|
}
|
|
|
|
err = vm.CheckErrorCondition(false)
|
|
if err != txscript.ErrStackScriptUnfinished {
|
|
t.Errorf("got unexepected error %v on %dth iteration",
|
|
err, i)
|
|
return
|
|
}
|
|
}
|
|
done, err := vm.Step()
|
|
if err != nil {
|
|
t.Errorf("final step failed %v", err)
|
|
return
|
|
}
|
|
if !done {
|
|
t.Errorf("final step isn't done!")
|
|
return
|
|
}
|
|
|
|
err = vm.CheckErrorCondition(false)
|
|
if err != nil {
|
|
t.Errorf("unexpected error %v on final check", err)
|
|
}
|
|
}
|
|
|
|
// TestInvalidFlagCombinations ensures the script engine returns the expected
|
|
// error when disallowed flag combinations are specified.
|
|
func TestInvalidFlagCombinations(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []txscript.ScriptFlags{
|
|
txscript.ScriptVerifyCleanStack,
|
|
}
|
|
|
|
// tx with almost empty scripts.
|
|
tx := &wire.MsgTx{
|
|
Version: 1,
|
|
TxIn: []*wire.TxIn{
|
|
{
|
|
PreviousOutPoint: wire.OutPoint{
|
|
Hash: chainhash.Hash([32]byte{
|
|
0xc9, 0x97, 0xa5, 0xe5,
|
|
0x6e, 0x10, 0x41, 0x02,
|
|
0xfa, 0x20, 0x9c, 0x6a,
|
|
0x85, 0x2d, 0xd9, 0x06,
|
|
0x60, 0xa2, 0x0b, 0x2d,
|
|
0x9c, 0x35, 0x24, 0x23,
|
|
0xed, 0xce, 0x25, 0x85,
|
|
0x7f, 0xcd, 0x37, 0x04,
|
|
}),
|
|
Index: 0,
|
|
},
|
|
SignatureScript: []uint8{txscript.OP_NOP},
|
|
Sequence: 4294967295,
|
|
},
|
|
},
|
|
TxOut: []*wire.TxOut{
|
|
{
|
|
Value: 1000000000,
|
|
PkScript: nil,
|
|
},
|
|
},
|
|
LockTime: 0,
|
|
}
|
|
pkScript := []byte{txscript.OP_NOP}
|
|
|
|
for i, test := range tests {
|
|
_, err := txscript.NewEngine(pkScript, tx, 0, test, 0, nil)
|
|
if err != txscript.ErrInvalidFlags {
|
|
t.Fatalf("TestInvalidFlagCombinations #%d unexpected "+
|
|
"error: %v", i, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestCheckPubKeyEncoding ensures the internal checkPubKeyEncoding function
|
|
// works as expected.
|
|
func TestCheckPubKeyEncoding(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
key []byte
|
|
isValid bool
|
|
}{
|
|
{
|
|
name: "uncompressed ok",
|
|
key: decodeHex("0411db93e1dcdb8a016b49840f8c53bc1eb68" +
|
|
"a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf" +
|
|
"9744464f82e160bfa9b8b64f9d4c03f999b8643f656b" +
|
|
"412a3"),
|
|
isValid: true,
|
|
},
|
|
{
|
|
name: "compressed ok",
|
|
key: decodeHex("02ce0b14fb842b1ba549fdd675c98075f12e9" +
|
|
"c510f8ef52bd021a9a1f4809d3b4d"),
|
|
isValid: true,
|
|
},
|
|
{
|
|
name: "compressed ok",
|
|
key: decodeHex("032689c7c2dab13309fb143e0e8fe39634252" +
|
|
"1887e976690b6b47f5b2a4b7d448e"),
|
|
isValid: true,
|
|
},
|
|
{
|
|
name: "hybrid",
|
|
key: decodeHex("0679be667ef9dcbbac55a06295ce870b07029" +
|
|
"bfcdb2dce28d959f2815b16f81798483ada7726a3c46" +
|
|
"55da4fbfc0e1108a8fd17b448a68554199c47d08ffb1" +
|
|
"0d4b8"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "empty",
|
|
key: nil,
|
|
isValid: false,
|
|
},
|
|
}
|
|
|
|
flags := txscript.ScriptVerifyStrictEncoding
|
|
for _, test := range tests {
|
|
err := txscript.TstCheckPubKeyEncoding(test.key, flags)
|
|
if err != nil && test.isValid {
|
|
t.Errorf("checkSignatureEncoding test '%s' failed "+
|
|
"when it should have succeeded: %v", test.name,
|
|
err)
|
|
} else if err == nil && !test.isValid {
|
|
t.Errorf("checkSignatureEncooding test '%s' succeeded "+
|
|
"when it should have failed", test.name)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// TestCheckSignatureEncoding ensures the internal checkSignatureEncoding
|
|
// function works as expected.
|
|
func TestCheckSignatureEncoding(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
sig []byte
|
|
isValid bool
|
|
}{
|
|
{
|
|
name: "valid signature",
|
|
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
|
|
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
|
|
"82221a8768d1d09"),
|
|
isValid: true,
|
|
},
|
|
{
|
|
name: "empty.",
|
|
sig: nil,
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "bad magic",
|
|
sig: decodeHex("314402204e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
|
|
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
|
|
"82221a8768d1d09"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "bad 1st int marker magic",
|
|
sig: decodeHex("304403204e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
|
|
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
|
|
"82221a8768d1d09"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "bad 2nd int marker",
|
|
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd41032018152" +
|
|
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
|
|
"82221a8768d1d09"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "short len",
|
|
sig: decodeHex("304302204e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
|
|
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
|
|
"82221a8768d1d09"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "long len",
|
|
sig: decodeHex("304502204e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
|
|
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
|
|
"82221a8768d1d09"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "long X",
|
|
sig: decodeHex("304402424e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
|
|
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
|
|
"82221a8768d1d09"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "long Y",
|
|
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022118152" +
|
|
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
|
|
"82221a8768d1d09"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "short Y",
|
|
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd41021918152" +
|
|
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
|
|
"82221a8768d1d09"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "trailing crap",
|
|
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
|
|
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
|
|
"82221a8768d1d0901"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "X == N ",
|
|
sig: decodeHex("30440220fffffffffffffffffffffffffffff" +
|
|
"ffebaaedce6af48a03bbfd25e8cd0364141022018152" +
|
|
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
|
|
"82221a8768d1d09"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "X == N ",
|
|
sig: decodeHex("30440220fffffffffffffffffffffffffffff" +
|
|
"ffebaaedce6af48a03bbfd25e8cd0364142022018152" +
|
|
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
|
|
"82221a8768d1d09"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "Y == N",
|
|
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd410220fffff" +
|
|
"ffffffffffffffffffffffffffebaaedce6af48a03bb" +
|
|
"fd25e8cd0364141"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "Y > N",
|
|
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd410220fffff" +
|
|
"ffffffffffffffffffffffffffebaaedce6af48a03bb" +
|
|
"fd25e8cd0364142"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "0 len X",
|
|
sig: decodeHex("302402000220181522ec8eca07de4860a4acd" +
|
|
"d12909d831cc56cbbac4622082221a8768d1d09"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "0 len Y",
|
|
sig: decodeHex("302402204e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd410200"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "extra R padding",
|
|
sig: decodeHex("30450221004e45e16932b8af514961a1d3a1a" +
|
|
"25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181" +
|
|
"522ec8eca07de4860a4acdd12909d831cc56cbbac462" +
|
|
"2082221a8768d1d09"),
|
|
isValid: false,
|
|
},
|
|
{
|
|
name: "extra S padding",
|
|
sig: decodeHex("304502204e45e16932b8af514961a1d3a1a25" +
|
|
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022100181" +
|
|
"522ec8eca07de4860a4acdd12909d831cc56cbbac462" +
|
|
"2082221a8768d1d09"),
|
|
isValid: false,
|
|
},
|
|
}
|
|
|
|
flags := txscript.ScriptVerifyStrictEncoding
|
|
for _, test := range tests {
|
|
err := txscript.TstCheckSignatureEncoding(test.sig, flags)
|
|
if err != nil && test.isValid {
|
|
t.Errorf("checkSignatureEncoding test '%s' failed "+
|
|
"when it should have succeeded: %v", test.name,
|
|
err)
|
|
} else if err == nil && !test.isValid {
|
|
t.Errorf("checkSignatureEncooding test '%s' succeeded "+
|
|
"when it should have failed", test.name)
|
|
}
|
|
}
|
|
}
|