mirror of
https://github.com/btcsuite/btcd.git
synced 2024-11-19 18:00:11 +01:00
0029905d43
Introduce an ECDSA signature verification into btcd in order to mitigate a certain DoS attack and as a performance optimization. The benefits of SigCache are two fold. Firstly, usage of SigCache mitigates a DoS attack wherein an attacker causes a victim's client to hang due to worst-case behavior triggered while processing attacker crafted invalid transactions. A detailed description of the mitigated DoS attack can be found here: https://bitslog.wordpress.com/2013/01/23/fixed-bitcoin-vulnerability-explanation-why-the-signature-cache-is-a-dos-protection/ Secondly, usage of the SigCache introduces a signature verification optimization which speeds up the validation of transactions within a block, if they've already been seen and verified within the mempool. The server itself manages the sigCache instance. The blockManager and txMempool respectively now receive pointers to the created sigCache instance. All read (sig triplet existence) operations on the sigCache will not block unless a separate goroutine is adding an entry (writing) to the sigCache. GetBlockTemplate generation now also utilizes the sigCache in order to avoid unnecessarily double checking signatures when generating a template after previously accepting a txn to the mempool. Consequently, the CPU miner now also employs the same optimization. The maximum number of entries for the sigCache has been introduced as a config parameter in order to allow users to configure the amount of memory consumed by this new additional caching.
103 lines
3.6 KiB
Go
103 lines
3.6 KiB
Go
// Copyright (c) 2014 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package blockchain_test
|
|
|
|
import (
|
|
"fmt"
|
|
"math/big"
|
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
"github.com/btcsuite/btcd/database"
|
|
_ "github.com/btcsuite/btcd/database/memdb"
|
|
"github.com/btcsuite/btcutil"
|
|
)
|
|
|
|
// This example demonstrates how to create a new chain instance and use
|
|
// ProcessBlock to attempt to attempt add a block to the chain. As the package
|
|
// overview documentation describes, this includes all of the Bitcoin consensus
|
|
// rules. This example intentionally attempts to insert a duplicate genesis
|
|
// block to illustrate how an invalid block is handled.
|
|
func ExampleBlockChain_ProcessBlock() {
|
|
// Create a new database to store the accepted blocks into. Typically
|
|
// this would be opening an existing database and would not use memdb
|
|
// which is a memory-only database backend, but we create a new db
|
|
// here so this is a complete working example.
|
|
db, err := database.CreateDB("memdb")
|
|
if err != nil {
|
|
fmt.Printf("Failed to create database: %v\n", err)
|
|
return
|
|
}
|
|
defer db.Close()
|
|
|
|
// Insert the main network genesis block. This is part of the initial
|
|
// database setup. Like above, this typically would not be needed when
|
|
// opening an existing database.
|
|
genesisBlock := btcutil.NewBlock(chaincfg.MainNetParams.GenesisBlock)
|
|
_, err = db.InsertBlock(genesisBlock)
|
|
if err != nil {
|
|
fmt.Printf("Failed to insert genesis block: %v\n", err)
|
|
return
|
|
}
|
|
|
|
// Create a new BlockChain instance without an initialized signature
|
|
// verification cache, using the underlying database for the main
|
|
// bitcoin network and ignore notifications.
|
|
chain := blockchain.New(db, &chaincfg.MainNetParams, nil, nil)
|
|
|
|
// Create a new median time source that is required by the upcoming
|
|
// call to ProcessBlock. Ordinarily this would also add time values
|
|
// obtained from other peers on the network so the local time is
|
|
// adjusted to be in agreement with other peers.
|
|
timeSource := blockchain.NewMedianTime()
|
|
|
|
// Process a block. For this example, we are going to intentionally
|
|
// cause an error by trying to process the genesis block which already
|
|
// exists.
|
|
isOrphan, err := chain.ProcessBlock(genesisBlock, timeSource, blockchain.BFNone)
|
|
if err != nil {
|
|
fmt.Printf("Failed to process block: %v\n", err)
|
|
return
|
|
}
|
|
fmt.Printf("Block accepted. Is it an orphan?: %v", isOrphan)
|
|
|
|
// Output:
|
|
// Failed to process block: already have block 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
|
|
}
|
|
|
|
// This example demonstrates how to convert the compact "bits" in a block header
|
|
// which represent the target difficulty to a big integer and display it using
|
|
// the typical hex notation.
|
|
func ExampleCompactToBig() {
|
|
// Convert the bits from block 300000 in the main block chain.
|
|
bits := uint32(419465580)
|
|
targetDifficulty := blockchain.CompactToBig(bits)
|
|
|
|
// Display it in hex.
|
|
fmt.Printf("%064x\n", targetDifficulty.Bytes())
|
|
|
|
// Output:
|
|
// 0000000000000000896c00000000000000000000000000000000000000000000
|
|
}
|
|
|
|
// This example demonstrates how to convert a target difficulty into the compact
|
|
// "bits" in a block header which represent that target difficulty .
|
|
func ExampleBigToCompact() {
|
|
// Convert the target difficulty from block 300000 in the main block
|
|
// chain to compact form.
|
|
t := "0000000000000000896c00000000000000000000000000000000000000000000"
|
|
targetDifficulty, success := new(big.Int).SetString(t, 16)
|
|
if !success {
|
|
fmt.Println("invalid target difficulty")
|
|
return
|
|
}
|
|
bits := blockchain.BigToCompact(targetDifficulty)
|
|
|
|
fmt.Println(bits)
|
|
|
|
// Output:
|
|
// 419465580
|
|
}
|