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.
172 lines
5.3 KiB
Go
172 lines
5.3 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 peer
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/decred/dcrd/chaincfg/chainhash"
|
|
"github.com/decred/dcrd/wire"
|
|
)
|
|
|
|
// TestMruInventoryMap ensures the MruInventoryMap behaves as expected including
|
|
// limiting, eviction of least-recently used entries, specific entry removal,
|
|
// and existence tests.
|
|
func TestMruInventoryMap(t *testing.T) {
|
|
// Create a bunch of fake inventory vectors to use in testing the mru
|
|
// inventory code.
|
|
numInvVects := 10
|
|
invVects := make([]*wire.InvVect, 0, numInvVects)
|
|
for i := 0; i < numInvVects; i++ {
|
|
hash := &chainhash.Hash{byte(i)}
|
|
iv := wire.NewInvVect(wire.InvTypeBlock, hash)
|
|
invVects = append(invVects, iv)
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
limit int
|
|
}{
|
|
{name: "limit 0", limit: 0},
|
|
{name: "limit 1", limit: 1},
|
|
{name: "limit 5", limit: 5},
|
|
{name: "limit 7", limit: 7},
|
|
{name: "limit one less than available", limit: numInvVects - 1},
|
|
{name: "limit all available", limit: numInvVects},
|
|
}
|
|
|
|
testLoop:
|
|
for i, test := range tests {
|
|
// Create a new mru inventory map limited by the specified test
|
|
// limit and add all of the test inventory vectors. This will
|
|
// cause evicition since there are more test inventory vectors
|
|
// than the limits.
|
|
mruInvMap := newMruInventoryMap(uint(test.limit))
|
|
for j := 0; j < numInvVects; j++ {
|
|
mruInvMap.Add(invVects[j])
|
|
}
|
|
|
|
// Ensure the limited number of most recent entries in the
|
|
// inventory vector list exist.
|
|
for j := numInvVects - test.limit; j < numInvVects; j++ {
|
|
if !mruInvMap.Exists(invVects[j]) {
|
|
t.Errorf("Exists #%d (%s) entry %s does not "+
|
|
"exist", i, test.name, *invVects[j])
|
|
continue testLoop
|
|
}
|
|
}
|
|
|
|
// Ensure the entries before the limited number of most recent
|
|
// entries in the inventory vector list do not exist.
|
|
for j := 0; j < numInvVects-test.limit; j++ {
|
|
if mruInvMap.Exists(invVects[j]) {
|
|
t.Errorf("Exists #%d (%s) entry %s exists", i,
|
|
test.name, *invVects[j])
|
|
continue testLoop
|
|
}
|
|
}
|
|
|
|
// Readd the entry that should currently be the least-recently
|
|
// used entry so it becomes the most-recently used entry, then
|
|
// force an eviction by adding an entry that doesn't exist and
|
|
// ensure the evicted entry is the new least-recently used
|
|
// entry.
|
|
//
|
|
// This check needs at least 2 entries.
|
|
if test.limit > 1 {
|
|
origLruIndex := numInvVects - test.limit
|
|
mruInvMap.Add(invVects[origLruIndex])
|
|
|
|
iv := wire.NewInvVect(wire.InvTypeBlock,
|
|
&chainhash.Hash{0x00, 0x01})
|
|
mruInvMap.Add(iv)
|
|
|
|
// Ensure the original lru entry still exists since it
|
|
// was updated and should've have become the mru entry.
|
|
if !mruInvMap.Exists(invVects[origLruIndex]) {
|
|
t.Errorf("MRU #%d (%s) entry %s does not exist",
|
|
i, test.name, *invVects[origLruIndex])
|
|
continue testLoop
|
|
}
|
|
|
|
// Ensure the entry that should've become the new lru
|
|
// entry was evicted.
|
|
newLruIndex := origLruIndex + 1
|
|
if mruInvMap.Exists(invVects[newLruIndex]) {
|
|
t.Errorf("MRU #%d (%s) entry %s exists", i,
|
|
test.name, *invVects[newLruIndex])
|
|
continue testLoop
|
|
}
|
|
}
|
|
|
|
// Delete all of the entries in the inventory vector list,
|
|
// including those that don't exist in the map, and ensure they
|
|
// no longer exist.
|
|
for j := 0; j < numInvVects; j++ {
|
|
mruInvMap.Delete(invVects[j])
|
|
if mruInvMap.Exists(invVects[j]) {
|
|
t.Errorf("Delete #%d (%s) entry %s exists", i,
|
|
test.name, *invVects[j])
|
|
continue testLoop
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestMruInventoryMapStringer tests the stringized output for the
|
|
// MruInventoryMap type.
|
|
func TestMruInventoryMapStringer(t *testing.T) {
|
|
// Create a couple of fake inventory vectors to use in testing the mru
|
|
// inventory stringer code.
|
|
hash1 := &chainhash.Hash{0x01}
|
|
hash2 := &chainhash.Hash{0x02}
|
|
iv1 := wire.NewInvVect(wire.InvTypeBlock, hash1)
|
|
iv2 := wire.NewInvVect(wire.InvTypeBlock, hash2)
|
|
|
|
// Create new mru inventory map and add the inventory vectors.
|
|
mruInvMap := newMruInventoryMap(uint(2))
|
|
mruInvMap.Add(iv1)
|
|
mruInvMap.Add(iv2)
|
|
|
|
// Ensure the stringer gives the expected result. Since map iteration
|
|
// is not ordered, either entry could be first, so account for both
|
|
// cases.
|
|
wantStr1 := fmt.Sprintf("<%d>[%s, %s]", 2, *iv1, *iv2)
|
|
wantStr2 := fmt.Sprintf("<%d>[%s, %s]", 2, *iv2, *iv1)
|
|
gotStr := mruInvMap.String()
|
|
if gotStr != wantStr1 && gotStr != wantStr2 {
|
|
t.Fatalf("unexpected string representation - got %q, want %q "+
|
|
"or %q", gotStr, wantStr1, wantStr2)
|
|
}
|
|
}
|
|
|
|
// BenchmarkMruInventoryList performs basic benchmarks on the most recently
|
|
// used inventory handling.
|
|
func BenchmarkMruInventoryList(b *testing.B) {
|
|
// Create a bunch of fake inventory vectors to use in benchmarking
|
|
// the mru inventory code.
|
|
b.StopTimer()
|
|
numInvVects := 100000
|
|
invVects := make([]*wire.InvVect, 0, numInvVects)
|
|
for i := 0; i < numInvVects; i++ {
|
|
hashBytes := make([]byte, chainhash.HashSize)
|
|
rand.Read(hashBytes)
|
|
hash, _ := chainhash.NewHash(hashBytes)
|
|
iv := wire.NewInvVect(wire.InvTypeBlock, hash)
|
|
invVects = append(invVects, iv)
|
|
}
|
|
b.StartTimer()
|
|
|
|
// Benchmark the add plus evicition code.
|
|
limit := 20000
|
|
mruInvMap := newMruInventoryMap(uint(limit))
|
|
for i := 0; i < b.N; i++ {
|
|
mruInvMap.Add(invVects[i%numInvVects])
|
|
}
|
|
}
|