haskoin-core 2.0.0 → 2.0.1
raw patch · 6 files changed
+171/−23 lines, 6 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Haskoin.Block.Headers: nextWorkRequiredWith :: BlockHeaders m => Network -> Maybe BlockNode -> BlockNode -> BlockHeader -> m Word32
Files
- CHANGELOG.md +18/−0
- README.md +9/−1
- bench/Bench.hs +43/−2
- haskoin-core.cabal +1/−1
- src/Haskoin/Block/Headers.hs +77/−19
- test/Haskoin/BlockSpec.hs +23/−0
CHANGELOG.md view
@@ -5,6 +5,24 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [2.0.1] - 2026-11-18++### Fixed++- **`connectBlocks` is fast on Bitcoin Cash again.** It searched for the+ aserti3-2d anchor block for every header: a binary search over the whole+ chain with a median time past at each step, some hundreds of reads. A node+ syncing Bitcoin Cash headers from a database took about 10 ms per header,+ 20 seconds for each `headers` message of 2,000. The anchor of a block is+ also the anchor of all its descendants, so `connectBlocks` now finds it once+ for a run of headers. The new benchmark `headers` connects 2,000 headers on+ top of 100,000 in memory: 4.7 s before, 72 ms after, at `-O1`.++### Added++- `nextWorkRequiredWith`: `nextWorkRequired` with the anchor block of the+ parent already found, for callers that connect many headers.+ ## [2.0.0] - 2026-11-15 This release breaks backwards compatibility on purpose: the secp256k1 context
README.md view
@@ -724,6 +724,11 @@ a `BlockHeaders` instance. `StateT HeaderMemory` is the in-memory one. Write your own instance to keep headers in a database. +Give `connectBlocks` all the headers of a `headers` message at once rather than+one by one with `connectBlock`. On Bitcoin Cash the difficulty depends on an+anchor block far down the chain, and finding it takes some hundreds of reads+from the store. `connectBlocks` finds it once for the whole run.+ Once the best block has the `minimumChainWork` of the network, a fork with less work is refused. Below that work nothing bounds the side chains a peer can send, so a node that is still syncing should not keep headers from a peer it does not@@ -856,7 +861,10 @@ Base58. The group `serial` times the binary encoding of hashes, outpoints, transactions, headers, blocks and addresses, written and read through cereal, through `runPutS` and `runGetS`, and through `encodeSerial` and `decodeSerial`,-and the hashing of transactions and headers.+and the hashing of transactions and headers. The group `headers` times+`connectBlocks` on a batch of 2,000 headers, the most that a `headers` message+holds, on top of a Bitcoin Cash chain of 100,000 that uses the aserti3-2d+difficulty rule, the case where a node syncing headers spends its time. ```sh stack bench # at -O1, as the package builds by default
bench/Bench.hs view
@@ -1,4 +1,5 @@ {-# LANGUAGE ImportQualifiedPost #-}+{-# LANGUAGE OverloadedRecordDot #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} @@ -45,6 +46,7 @@ import Control.DeepSeq (NFData) import Control.Monad (unless, (<=<))+import Control.Monad.State.Strict (runState) import Data.Aeson (ToJSON (toEncoding)) import Data.Aeson.Encoding (Encoding, encodingToLazyByteString, unsafeToEncoding) import Data.Base16.Types (extractBase16)@@ -60,7 +62,7 @@ import Data.Bytes.Put (runPutL, runPutS) import Data.Bytes.Serial (Serial (deserialize, serialize)) import Data.Char (chr)-import Data.List (intercalate)+import Data.List (foldl', intercalate) import Data.Maybe (fromMaybe) import Data.Serialize qualified as Cereal import Data.Text (Text)@@ -78,7 +80,8 @@ bgroup "address" addressBenchmarks, bgroup "json" jsonBenchmarks, bgroup "base58" base58Benchmarks,- bgroup "serial" serialBenchmarks+ bgroup "serial" serialBenchmarks,+ bgroup "headers" headerBenchmarks ] -- | The benchmark of a function of the library, named @now@, after the same@@ -478,3 +481,41 @@ TxOut (5000000 + k) (hex "5120" <> bytes (k + 2) 32) ] witness seed = [bytes (seed + 600) 72, bytes (seed + 700) 33]++-- | Connecting a batch of 2,000 headers, the most a peer sends in one+-- @headers@ message, on top of a chain of 100,000 headers that already uses+-- the aserti3-2d difficulty algorithm of Bitcoin Cash. That algorithm needs+-- the anchor block of the chain, which is far below the tip, and a node+-- syncing headers spends its time here. The headers are mined at the easiest+-- target of a test network, so that the chain can be built in a moment, and+-- kept in the memory store of the library, so that the figure is the cost of+-- the validation and not of a database.+headerBenchmarks :: [Benchmark]+headerBenchmarks =+ [ env (return (chain, batch)) $ \ ~(mem, hs) ->+ bench "connectBlocks 2000 at 100000" $+ nf (\hs' -> fst (runState (connectBlocks net now hs') mem)) hs+ ]+ where+ base = 100000+ (old, batch) = splitAt base (appendBlocks net 0 net.genesisHeader (base + 2000))+ now = net.genesisHeader.timestamp + 600 * fromIntegral (base + 3000)+ net =+ bchRegTest+ { powNoRetarget = False,+ minDiffBlocks = False,+ edaHeight = Nothing,+ daaHeight = Nothing,+ asertActivationTime = Just (net.genesisHeader.timestamp + 600 * 20),+ checkpoints = []+ }+ -- The store holding the first headers, built directly, without+ -- validation, so that building it is quick.+ chain = foldl' add (initialChain net) old+ add :: HeaderMemory -> BlockHeader -> HeaderMemory+ add mem bh =+ let par = mem.best+ h = par.height + 1+ sk = fromMaybe (error "no skip") (fst (runState (getAncestor (skipHeight h) par) mem))+ node = BlockNode bh h (par.work + headerWork bh) (headerHash sk.header)+ in snd (runState (addBlockHeader node >> setBestBlockHeader node) mem)
haskoin-core.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: haskoin-core-version: 2.0.0+version: 2.0.1 synopsis: Bitcoin & Bitcoin Cash library for Haskell description: Please see the README on GitHub at <https://github.com/jprupp/haskoin-core#readme> category: Bitcoin, Finance, Network
src/Haskoin/Block/Headers.hs view
@@ -57,6 +57,7 @@ -- ** Difficulty nextWorkRequired,+ nextWorkRequiredWith, nextPowWorkRequired, calcNextWork, lastNoMinDiff,@@ -338,17 +339,17 @@ | not (chained bhs) = return $ Left "Blocks to connect do not form a chain" | otherwise = do best <- getBestBlockHeader- (result, nodes, best') <- go best [] bhs+ (result, nodes, best') <- go Nothing best [] bhs when (best' /= best) $ setBestBlockHeader best' return $ either Left (const (Right nodes)) result where chained (h1 : h2 : hs) = headerHash h1 == h2.prev && chained (h2 : hs) chained _ = True- go best nodes [] = return (Right (), nodes, best)- go best nodes (bh : rest) =- connectHeader net t best bh >>= \case+ go _ best nodes [] = return (Right (), nodes, best)+ go cache best nodes (bh : rest) =+ connectHeader net t cache best bh >>= \case Left err -> return (Left err, nodes, best)- Right node -> go (chooseBest best node) (node : nodes) rest+ Right (node, cache') -> go cache' (chooseBest best node) (node : nodes) rest -- | Block's parent. If the block header is in the store, its parent must also -- be there. No block header get deleted or pruned from the store.@@ -370,34 +371,66 @@ connectBlock net t bh = runExceptT $ do best <- lift getBestBlockHeader- node <- ExceptT $ connectHeader net t best bh+ (node, _) <- ExceptT $ connectHeader net t Nothing best bh let best' = chooseBest best node when (best' /= best) $ lift $ setBestBlockHeader best' return node +-- | The aserti3-2d anchor found for a block, kept while a run of headers is+-- connected. Finding the anchor is a binary search over the whole chain, with+-- a median time past at each step, and it cost milliseconds for every header+-- when each one searched again. The anchor of a block is also the anchor of+-- all of its descendants, since the median time past never decreases along a+-- chain, so one search serves a whole run of headers. The cache holds the+-- hash of the block that the anchor was found for, and the anchor.+type AnchorCache = Maybe (BlockHash, BlockNode)++-- | The anchor for the chain that ends at the given block, from the cache when+-- the block is the one cached or its child, and from 'asertAnchor' otherwise.+-- Returns the cache to use for the next header.+cachedAnchor ::+ (BlockHeaders m) =>+ Network ->+ AnchorCache ->+ BlockNode ->+ m (Maybe BlockNode, AnchorCache)+cachedAnchor net cache par =+ case cache of+ Just (k, anchor)+ | k == parHash || k == par.header.prev ->+ return (Just anchor, Just (parHash, anchor))+ _ ->+ asertAnchor net par >>= \case+ Just anchor -> return (Just anchor, Just (parHash, anchor))+ Nothing -> return (Nothing, Nothing)+ where+ parHash = headerHash par.header+ -- | Validate one header against the store and add it to the store. The best -- block passed in is only used by the checkpoint rule and is not updated. connectHeader :: (BlockHeaders m) => Network -> Timestamp ->+ AnchorCache -> BlockNode -> BlockHeader ->- m (Either String BlockNode)-connectHeader net t best bh =+ m (Either String (BlockNode, AnchorCache))+connectHeader net t cache best bh = getBlockHeader (headerHash bh) >>= \case- Just known -> return (Right known)- Nothing -> connectNewHeader net t best bh+ Just known -> return (Right (known, cache))+ Nothing -> connectNewHeader net t cache best bh -- | Validate a header that is not in the store yet, and add it. connectNewHeader :: (BlockHeaders m) => Network -> Timestamp ->+ AnchorCache -> BlockNode -> BlockHeader ->- m (Either String BlockNode)-connectNewHeader net t best bh =+ m (Either String (BlockNode, AnchorCache))+connectNewHeader net t cache best bh = runExceptT $ do par <- maybeToExceptT@@ -411,10 +444,14 @@ throwError $ "BUG: Could not get skip for block " ++ show (headerHash par.header)- requiredBits <- lift $ nextWorkRequired net par bh+ (anchor, cache') <-+ if needsAnchor net+ then lift $ cachedAnchor net cache par+ else return (Nothing, cache)+ requiredBits <- lift $ nextWorkRequiredWith net anchor par bh node <- ExceptT . return $ validBlock net t best par pars bh skipNode requiredBits lift $ addBlockHeader node- return node+ return (node, cache') -- | Validate this block header. Build a 'BlockNode' if successful. The checks -- follow @CheckBlockHeader@ and @ContextualCheckBlockHeader@ in Bitcoin Core.@@ -645,18 +682,39 @@ BlockHeader -> m Word32 nextWorkRequired net par bh+ | needsAnchor net = asertAnchor net par >>= \a -> nextWorkRequiredWith net a par bh+ | otherwise = nextWorkRequiredWith net Nothing par bh++-- | Does the network need the aserti3-2d anchor to work out the difficulty?+needsAnchor :: Network -> Bool+needsAnchor net = isJust net.asertActivationTime && not (usesCashRules net && net.powNoRetarget)++-- | Does the network follow the difficulty rules of Bitcoin Cash?+usesCashRules :: Network -> Bool+usesCashRules net =+ isJust net.edaHeight || isJust net.daaHeight || isJust net.asertActivationTime++-- | 'nextWorkRequired' with the result of 'asertAnchor' for the parent+-- already found, so that a caller connecting many headers can find it once.+nextWorkRequiredWith ::+ (BlockHeaders m) =>+ Network ->+ -- | 'asertAnchor' of the parent+ Maybe BlockNode ->+ BlockNode ->+ BlockHeader ->+ m Word32+nextWorkRequiredWith net anchor par bh -- Bitcoin Cash Node never retargets on regtest, before picking an algorithm.- | usesCashRules && net.powNoRetarget = return par.header.bits+ | usesCashRules net && net.powNoRetarget = return par.header.bits | otherwise =- asertAnchor net par >>= \case- Just anchor -> nextAsertWorkRequired net anchor par bh+ case anchor of+ Just a -> nextAsertWorkRequired net a par bh Nothing | activeAt net.daaHeight -> nextDaaWorkRequired net par bh | activeAt net.edaHeight -> nextEdaWorkRequired net par bh | otherwise -> nextPowWorkRequired net par bh where- usesCashRules =- isJust net.edaHeight || isJust net.daaHeight || isJust net.asertActivationTime activeAt = maybe False (\h -> par.height + 1 >= h) -- | Find out the next amount of work required according to the Emergency
test/Haskoin/BlockSpec.hs view
@@ -947,6 +947,17 @@ -- 1793800487 - 1605447844 = 188352643 seconds and 975898 - 661647 = -- 314251 blocks after the anchor. computeAsertBits mainnetLimit 172800 0x1804dafe 188352643 314251 `shouldBe` 0x1802308e+ it "connects a run of headers that crosses the activation as one batch" $+ -- 'connectBlocks' finds the anchor once for a run of headers. The run+ -- starts before the activation and comes faster than the schedule, so+ -- the bits change on every block; each must match what the headers+ -- connected one by one required.+ let fastNet = (asertNet :: Network) {asertHalfLife = 3600}+ hs = mineAsert fastNet 60+ batch = withChain fastNet (connectBlocks fastNet myTime hs)+ in do+ length (filter (/= 0x207fffff) (map (.bits) hs)) `shouldSatisfy` (> 20)+ fmap (map (.height)) batch `shouldBe` Right [60, 59 .. 1] describe "aserti3-2d test vectors" $ mapM_ ( \x ->@@ -962,6 +973,18 @@ let parent = (last nodes).header t0 = asertNet.genesisHeader.timestamp in connectHeight asertNet myTime (mineOn asertNet parent (t0 + 600 * 30 + 428) 0x1f400000)+ -- Mine headers 300 seconds apart, each with the bits that the chain+ -- connected so far one by one requires.+ mineAsert :: Network -> Int -> [BlockHeader]+ mineAsert net n = withChain net (replicateM n next)+ where+ next = do+ par <- getBestBlockHeader+ let time = par.header.timestamp + 300+ bits <- nextWorkRequired net par (headerOn par.header time 0)+ let h = mineOn net par.header time bits+ _ <- either error id <$> connectBlock net myTime h+ return h anchorHeights = withChain asertNet $ do nodes <- storeChain (evenlySpaced asertNet 600 30 0x1f400000) forM [nodes !! 13, nodes !! 14, nodes !! 19, last nodes] $ \n ->