mirror of
https://github.com/kaspanet/kaspad.git
synced 2026-03-14 20:35:18 +00:00
* [NOD-215] Implement difficulty adjustment algorithm * [NOD-215] Handle blocks with genesis parent, and fix adjustment factor calculation * [NOD-215] Fix tests * [NOD-215] fix calcNextRequiredDifficulty * [NOD-215] Add TestDifficulty * [NOD-215] Fix delay to be positive, and add tests for delayed blocks * [NOD-215] Split calcBlockWindowMinMaxAndMedianTimestamps to two functions * [NOD-215] Make explicit loop for padding blue block window with genesis * [NOD-215] Name return values * [NOD-215] Fix delay != 0 error messages * [NOD-215] Fix comments * [NOD-215] Fix blueBlockWindow * [NOD-215] Add TestBlueBlockWindow * [NOD-215] Rename PowLimit -> PowMax * [NOD-215] Fix delay != 0 error messages * [NOD-215] Move PowMaxBits to BlockDAG * [NOD-215] Make blockWindow type * [NOD-215] Make blueBlockWindow always pad with genesis * [NOD-215] Remove redundant line in checkWindowIDs * [NOD-215] Make medianTimestamp return error for empty window
76 lines
2.7 KiB
Go
76 lines
2.7 KiB
Go
// Copyright (c) 2014-2016 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package blockdag_test
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/daglabs/btcd/blockdag"
|
|
"github.com/daglabs/btcd/dagconfig"
|
|
"github.com/daglabs/btcd/database"
|
|
_ "github.com/daglabs/btcd/database/ffldb"
|
|
"github.com/daglabs/btcd/util"
|
|
)
|
|
|
|
// This example demonstrates how to create a new chain instance and use
|
|
// ProcessBlock to attempt to 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 ExampleBlockDAG_ProcessBlock() {
|
|
// Create a new database to store the accepted blocks into. Typically
|
|
// this would be opening an existing database and would not be deleting
|
|
// and creating a new database like this, but it is done here so this is
|
|
// a complete working example and does not leave temporary files laying
|
|
// around.
|
|
dbPath := filepath.Join(os.TempDir(), "exampleprocessblock")
|
|
_ = os.RemoveAll(dbPath)
|
|
db, err := database.Create("ffldb", dbPath, dagconfig.MainNetParams.Net)
|
|
if err != nil {
|
|
fmt.Printf("Failed to create database: %v\n", err)
|
|
return
|
|
}
|
|
defer os.RemoveAll(dbPath)
|
|
defer db.Close()
|
|
|
|
// Create a new BlockDAG instance using the underlying database for
|
|
// the main bitcoin network. This example does not demonstrate some
|
|
// of the other available configuration options such as specifying a
|
|
// notification callback and signature cache. Also, the caller would
|
|
// ordinarily keep a reference to the median time source and add time
|
|
// values obtained from other peers on the network so the local time is
|
|
// adjusted to be in agreement with other peers.
|
|
chain, err := blockdag.New(&blockdag.Config{
|
|
DB: db,
|
|
DAGParams: &dagconfig.MainNetParams,
|
|
TimeSource: blockdag.NewMedianTime(),
|
|
})
|
|
if err != nil {
|
|
fmt.Printf("Failed to create chain instance: %v\n", err)
|
|
return
|
|
}
|
|
|
|
// Process a block. For this example, we are going to intentionally
|
|
// cause an error by trying to process the genesis block which already
|
|
// exists.
|
|
genesisBlock := util.NewBlock(dagconfig.MainNetParams.GenesisBlock)
|
|
isOrphan, delay, err := chain.ProcessBlock(genesisBlock,
|
|
blockdag.BFNone)
|
|
if err != nil {
|
|
fmt.Printf("Failed to process block: %v\n", err)
|
|
return
|
|
}
|
|
if delay != 0 {
|
|
fmt.Printf("Block is too far in the future")
|
|
return
|
|
}
|
|
fmt.Printf("Block accepted. Is it an orphan?: %v", isOrphan)
|
|
|
|
// Output:
|
|
// Failed to process block: already have block 0f2bd52ea69aac311a59428cd5d17e3833e62706f68a1c7c50bef0664459229b
|
|
}
|