haskoin-store 2.1.0 → 2.2.0
raw patch · 11 files changed
+341/−61 lines, 11 files
Files
- CHANGELOG.md +28/−0
- README.md +1/−1
- haskoin-store.cabal +1/−1
- src/Haskoin/Store/Fleet/Member.hs +9/−0
- src/Haskoin/Store/Ingest.hs +54/−12
- src/Haskoin/Store/Ingest/Run.hs +56/−13
- src/Haskoin/Store/Manager.hs +50/−22
- swagger.yaml +9/−5
- test/Haskoin/Store/Fleet/CacheSpec.hs +29/−0
- test/Haskoin/Store/Ingest/Model.hs +60/−7
- test/Haskoin/Store/IngestSpec.hs +44/−0
CHANGELOG.md view
@@ -6,6 +6,34 @@ ## [Unreleased] +## [2.2.0] - 2026-12-03++Two wrong words in `mempool.sync` of `/health`, found by the regtests of+haskoin-wallet. A new version under the PVP, not a patch: the machine of+`Haskoin.Store.Ingest` has a new input (`AtTip`) and its `Config` a new+field (`operated`). A program that only runs the store sees no change but+the two fixes below.++### Fixed+- **No `synced` before a long move empties the pool.** After a move of+ more than 50 blocks the store had its blocks, read its node and said+ `synced`; a second later its member moved the fleet tip and started the+ shared mempool again there, and the store said `syncing` for ten+ seconds more. The store now does not say `synced` before the fleet tip+ is at its best block (`AtTip` in `Haskoin.Store.Ingest`). The member+ counts a restart of the pool before it notes the tip that the restart+ moved to (`memberTick`), and the path into the pool reads the tip and+ then that count (`FleetPool` in `Haskoin.Store.Ingest.Run`), so it is+ told of the emptying first. In a fleet of several stores, a store that+ comes back says `syncing` until every live store has its blocks.+- **`syncing`, not `unknown`, while the store's node is away.** A store+ whose options name a node of the operator (`--node-rpc`, or `--peer`+ with `--peer-permission mempool`) says `syncing` while no such node is+ connected, as when the node restarts, and from its start until one is.+ `unknown` is for a store whose options name none (`operated` in+ `Haskoin.Store.Ingest.Config`). A client that accepted `unknown` from a+ store with no node could not tell it from one that lost its node.+ ## [2.1.0] - 2026-12-02 A store whose node is given with `--node-rpc` says that its mempool is in
README.md view
@@ -487,7 +487,7 @@ | Field | What it says | | --- | --- | | `sources` | How many nodes the store can read a whole mempool from |-| `sync` | How the mempool stands with the mempools of your nodes, in one word. `syncing`: the store has not yet read your node and taken in all that it named, since it started, since Redis lost its data, or since the chain moved by more than two blocks at once, and shows other unconfirmed transactions than the node meanwhile. After a start, a node that is a peer is given `--mempool-wait`, twenty seconds by default, to name its mempool; the node of `--node-rpc` says when it has, and a store on a regtest with it says `synced` a moment after it has its blocks. `synced`: it has. `unknown`: the store has no node of yours to read a whole mempool from, only strangers or nobody, and cannot tell |+| `sync` | How the mempool stands with the mempools of your nodes, in one word. `syncing`: the store has not yet read your node and taken in all that it named, since it started, since Redis lost its data, or since the chain moved by more than two blocks at once, or the fleet tip has not reached its best block since, and shows other unconfirmed transactions than the node meanwhile; or its options name a node of yours (`--node-rpc`, or `--peer` with `--peer-permission mempool`) and that node is away. After a start, a node that is a peer is given `--mempool-wait`, twenty seconds by default, to name its mempool; the node of `--node-rpc` says when it has, and a store on a regtest with it says `synced` a moment after it has its blocks. `synced`: it has. `unknown`: the store has no node of yours to read a whole mempool from, only strangers or nobody, its options name none, and it cannot tell | | `known`, `held` | How many transactions are on their way to the mempool, and how many of them the store holds | | `dirty` | Whether the store gave something up and is about to read its sources again |
haskoin-store.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: haskoin-store-version: 2.1.0+version: 2.2.0 synopsis: Storage and index for Bitcoin and Bitcoin Cash description: An indexer for Bitcoin and Bitcoin Cash. It downloads blocks and mempool transactions from the peer-to-peer network, indexes the chain in RocksDB and
src/Haskoin/Store/Fleet/Member.hs view
@@ -468,6 +468,15 @@ when moved (clearLeaving m) -- the tip is read after the report, as the rule of 'F.keep' asks tip <- fmap decoded <$> fleetTip m.redis m.keys+ -- A move that started the pool again ('fleetRestart'), this store's or+ -- another's, moved the tip and the count of restarts at once. The count+ -- is read after the tip, and counted as a refill before the tip is+ -- noted: the path into the pool reads the tip and then the count, and+ -- so never sees the store at a tip without the emptying that took it+ -- there. Counted at the next turn, as it was, the store said for that+ -- turn that its mempool was in sync, and then that it was syncing+ -- ("Haskoin.Store.Ingest").+ noteRestarts m atomically (writeTVar m.tip (fromMaybe Nothing tip)) prune m cacheGauges m
src/Haskoin/Store/Ingest.hs view
@@ -191,7 +191,14 @@ sweepEvery :: !Tick, -- | and this often while peers name transactions that the pool -- lacks: not more than 'sweepEvery', and never sooner than 'sweepGap'- sweepFast :: !Tick+ sweepFast :: !Tick,+ -- | whether the options of the store name a node of the operator that+ -- gives its mempool: the RPC of a node, or a peer that lets the store+ -- ask for it. While no such node is there, the pool is syncing, not+ -- unknown ('standing'): the store lost the node that it measures its+ -- pool against, and a client must not take it for a store that never+ -- had one.+ operated :: !Bool } deriving (Eq, Ord, Show) @@ -201,7 +208,8 @@ -- an orphan twenty minutes, offered again -- after two seconds and then twice as long each time, and a reading of the -- sources at most every ten seconds, every thirty while transactions--- come, and at least every minute.+-- come, and at least every minute. Its options name no node of the+-- operator ('operated'): the store sets that from its own. defaultConfig :: Config defaultConfig = Config@@ -215,7 +223,8 @@ retry = 2000, sweepGap = 10000, sweepEvery = 60000,- sweepFast = 30000+ sweepFast = 30000,+ operated = False } -- | What the machine knows of a peer.@@ -298,6 +307,10 @@ -- what it had, read the nodes of the operator and seen the pool -- decide on all that they named ('standing') caughtUp :: !Bool,+ -- | whether the fleet tip is the store's best block, as the runner+ -- was last told ('AtTip'): the machine does not catch up before it+ -- is ('settleUp')+ atTip :: !Bool, -- | how many questions to the pool about what a peer named have no -- answer yet: ids that have no place, and may get one checking :: !Int@@ -341,6 +354,13 @@ Offered !h !(Verdict h) | -- | the pool lost what it had Emptied+ | -- | whether the fleet tip is the store's best block. The pool is the+ -- mempool at the fleet tip, and a store whose tip is far behind+ -- empties it when it moves the tip ('Haskoin.Store.Fleet.Member.farthest'):+ -- until the tip is there, the machine does not catch up. The runner+ -- tells it after an emptying that the move made ('Emptied'), never+ -- before.+ AtTip !Bool deriving (Eq, Ord, Show) -- | What the pool says of an offer.@@ -359,7 +379,10 @@ Unverified deriving (Eq, Ord, Show) --- | A machine with nothing in it and no peer, at a time.+-- | A machine with nothing in it and no peer, at a time. It is at the+-- fleet tip ('AtTip'): a machine that is never told of a fleet, as in the+-- specs, has no tip to wait for. The runner of a store with a fleet tells+-- it that it is not, before anything else. start :: Config -> Tick -> Ingest p h b start config now = Ingest@@ -377,6 +400,7 @@ doubts = Set.empty, dirty = False, caughtUp = False,+ atTip = True, checking = 0 } @@ -421,15 +445,19 @@ -- | How the pool stands with the mempools of the nodes of the operator. data Standing- = -- | no node of the operator gives its mempool: the machine cannot- -- tell. What strangers name says nothing of what the pool should- -- have: a stranger may name what no honest node took, and keep quiet- -- about what every node has.+ = -- | no node of the operator gives its mempool, and the options of the+ -- store name none ('operated'): the machine cannot tell. What+ -- strangers name says nothing of what the pool should have: a+ -- stranger may name what no honest node took, and keep quiet about+ -- what every node has. PoolUnknown | -- | the machine has not yet read the nodes of the operator and seen -- the pool decide on all that they named, since it started or since- -- the pool lost what it had: the store shows fewer transactions than- -- its node+ -- the pool lost what it had, or the fleet tip has not reached the+ -- store's best block since: the store shows fewer transactions than+ -- its node, or is about to. Also while the node of the operator that+ -- the options name is away: the store has lost what it measures its+ -- pool against. PoolSyncing | -- | it has PoolSynced@@ -440,7 +468,7 @@ -- the machine depends on it. standing :: Ingest p h b -> Standing standing s- | null (trusted s) = PoolUnknown+ | null (trusted s) = if s.config.operated then PoolSyncing else PoolUnknown | s.caughtUp = PoolSynced | otherwise = PoolSyncing @@ -492,11 +520,24 @@ -- -- A machine that loses its last node of the operator has not caught up -- when one is back: what that node took meanwhile is not known.+--+-- Nor has it caught up while the fleet tip is not at the store's best+-- block ('AtTip'). The pool is the mempool at the fleet tip. After a move+-- of more than 'Haskoin.Store.Fleet.Member.farthest' blocks, the store+-- had its blocks and read its node, and said that it was in sync, for+-- the second before its member moved the tip and emptied the pool: then+-- it said that it was syncing again, for ten seconds. A tip that falls+-- behind for a block, once the machine caught up, changes nothing: a+-- move of a block or two empties nothing, and a longer one is 'Moved'.+-- The other way round, a machine that caught up after the tip reached+-- the block and then sees the pool emptied is told 'Emptied' first, by+-- the runner, before it is told that the tip is there. settleUp :: Ingest p h b -> Ingest p h b settleUp s | null nodes = if s.caughtUp then s {caughtUp = False} else s | s.caughtUp = s- | s.synced && all heard nodes && not s.dirty && s.checking == 0 && Set.null s.vouched = s {caughtUp = True}+ | s.synced && s.atTip && all heard nodes && not s.dirty && s.checking == 0 && Set.null s.vouched =+ s {caughtUp = True} | otherwise = s where nodes = trusted s@@ -616,6 +657,7 @@ done (foldl' (\acc h -> if known h acc then acc else forget Unjudged h acc) (answeredCheck s, []) hs) Offered h v -> done (offered h v (s, [])) Emptied -> (s {dirty = True, caughtUp = False}, [])+ AtTip b -> (s {atTip = b}, []) where done (s', outs) = (s', reverse outs) known h (s', _) = Map.member h (places s')
src/Haskoin/Store/Ingest/Run.hs view
@@ -71,6 +71,7 @@ ( Ingester, IngestConfig (..), IngestState (..),+ FleetPool (..), withIngest, ingestState, @@ -173,10 +174,32 @@ -- two blocks makes the store say that its mempool is syncing at once -- ('Moved'). behind :: !(IO Int),+ -- | what the fleet says of its pool, a t'FleetPool', read when the+ -- runner looks at whether the store is in sync: 'Nothing' without a+ -- fleet, in tests+ fleet :: !(Maybe (IO FleetPool)), stats :: !(Maybe Stats), logger :: !Logger } +-- | What the fleet says of its pool, for the path into it: how many+-- times the store found it emptied ('Haskoin.Store.Fleet.Member.refills'),+-- and whether the fleet tip is the store's best block.+--+-- The runner turns a new count into 'Emptied', and a change of the+-- second into 'AtTip', in that order, in the same pass. A move of the tip+-- of more than 'Haskoin.Store.Fleet.Member.farthest' blocks empties the+-- pool, and the member counts that before it notes the tip that it moved+-- to. So the action reads the tip first and the count after: a tip read+-- at the best block comes with the count of the emptying that took it+-- there, and the machine never hears that the tip is there before it+-- hears that the pool was emptied on the way.+data FleetPool = FleetPool+ { emptied :: !Int,+ atTip :: !Bool+ }+ deriving (Eq, Show)+ -- | A way to read the whole mempool of a node that is no peer of the -- store, such as its RPC. Both actions throw when the node does not -- answer, and only then: what they return is the node's word.@@ -316,10 +339,11 @@ ingestLookSTM :: Ingester -> STM () ingestLookSTM = always Look --- | The pool lost what it had: Redis lost its data, or the pool was--- started again. The machine is told the same when the runner dropped--- events of peers: in both cases something may be missing that only a--- reading of the sources brings back.+-- | The pool lost what it had. The machine is told the same when the+-- runner dropped events of peers: in both cases something may be missing+-- that only a reading of the sources brings back. What the fleet empties,+-- when Redis lost its data or the pool was started again, the runner+-- reads itself ('fleet'), in its order with the moves of the tip. ingestEmptiedSTM :: Ingester -> STM () ingestEmptiedSTM = always PoolLost @@ -442,9 +466,16 @@ metrics <- mapM newMetrics cfg.stats feeds <- newTVarIO 0 now <- milliseconds+ -- With a fleet, the machine is not at its tip until the runner has+ -- read that it is: what it reads at its first look.+ seen <- fmap (\f -> f {atTip = False}) <$> sequence cfg.fleet let ing = Ingester {events, waiting, dropped, state, feeds, logger = cfg.logger}- run0 = Run {machine = start cfg.config now, peers = Map.empty, next = 0}- withAsync (loop cfg ing metrics run0) $ \a -> link a >> action ing+ fresh = start cfg.config now+ machine0 = case seen of+ Just _ -> fst (step parentsOf (AtTip False) fresh)+ Nothing -> fresh+ run0 = Run {machine = machine0, peers = Map.empty, next = 0}+ withAsync (loop cfg ing metrics seen run0) $ \a -> link a >> action ing -- | The clock of the machine. milliseconds :: IO Tick@@ -478,13 +509,13 @@ noCosts :: Costs noCosts = Costs 0 0 0 0 0 0 0 0 0 mempty -loop :: IngestConfig -> Ingester -> Maybe Metrics -> Run -> IO a-loop cfg ing metrics run0 = do+loop :: IngestConfig -> Ingester -> Maybe Metrics -> Maybe FleetPool -> Run -> IO a+loop cfg ing metrics seen run0 = do t <- milliseconds costs <- newIORef noCosts saidAt <- newIORef t pings <- newIORef Map.empty- loopWith costs pings saidAt cfg ing metrics run0 t+ loopWith costs pings saidAt cfg ing metrics seen run0 t -- | The blocks that a move of the chain may take, back and in, for the -- store to go on saying that its mempool is in sync: a block found, or@@ -502,10 +533,12 @@ IngestConfig -> Ingester -> Maybe Metrics ->+ -- | what the fleet said of its pool when the runner last looked+ Maybe FleetPool -> Run -> Tick -> IO a-loopWith costs pings saidAt cfg ing metrics run0 t0 = go False t0 run0+loopWith costs pings saidAt cfg ing metrics seen0 run0 t0 = go False seen0 t0 run0 where -- the time that an action takes, added to what the runner spent timed :: (Costs -> Word64 -> Costs) -> IO x -> IO x@@ -557,8 +590,8 @@ shown (holding m) <> "." ] -- with whether the machine was last told that the store is in sync,- -- and when it was last told the time- go wasSynced lastTick run = do+ -- what the fleet said of its pool, and when it was last told the time+ go wasSynced seen lastTick run = do before <- milliseconds timer <- registerDelay (1000 * max 0 (lastTick + 1000 - before)) event <-@@ -579,6 +612,15 @@ -- whether the store is in sync is looked at when it may have changed, -- and at every tick; and when it is not, how far the chain moved synced <- if looks then cfg.inSync else return wasSynced+ -- and what the fleet says of its pool: an emptying, then where its+ -- tip is ('FleetPool')+ fleetNow <- case (looks, cfg.fleet) of+ (True, Just read') -> Just <$> read'+ _ -> return seen+ let fleetInputs = case (seen, fleetNow) of+ (Just before, Just after) ->+ [Emptied | after.emptied /= before.emptied] <> [AtTip after.atTip | after.atTip /= before.atTip]+ _ -> [] distance <- if looks && not synced then cfg.behind else return 0 let moved = distance > longMove when (moved && standing run'.machine == PoolSynced) . say LevelInfo . T.unwords $@@ -592,6 +634,7 @@ lostNow <- newIORef Map.empty let all' = [Emptied | lost > 0]+ <> fleetInputs <> [Synced synced | synced /= wasSynced] <> [Moved | moved] <> inputs@@ -624,7 +667,7 @@ "Gave up transactions on their way to the pool, and will read the sources again: " <> T.intercalate ", " [shown n <> " " <> cs (reasonName why) | (why, n) <- gaveUp] account done machine- go synced (if tick then now else lastTick) run' {machine}+ go synced fleetNow (if tick then now else lastTick) run' {machine} -- what an event is to the machine, with the peers named inputsOf run = \case Nothing -> return (run, [])
src/Haskoin/Store/Manager.hs view
@@ -25,14 +25,16 @@ where import Control.Concurrent.Async (link, withAsync)-import Control.Concurrent.STM (STM, TVar, atomically, newTVarIO, readTVar, retry)+import Control.Concurrent.STM (STM, TVar, atomically, newTVarIO, readTVar, readTVarIO, retry) import Control.Exception (throwIO) import Control.Monad (forM, forM_, join, unless, when) import Data.ByteString (ByteString) import Data.ByteString.Char8 qualified as C+import Data.Functor ((<&>)) import Data.IORef (IORef, newIORef, readIORef, writeIORef) import Data.Maybe (isJust, isNothing) import Data.Serialize (Serialize, decode)+import Data.Set qualified as Set import Data.String.Conversions (cs) import Data.Time.Clock (NominalDiffTime) import Data.Word (Word32, Word64)@@ -69,6 +71,7 @@ Peer, PeerEvent (..), PeerMgr,+ Permission (..), Permissions, WithConnection, chainGetBest,@@ -117,11 +120,11 @@ noteBest, ) import Haskoin.Store.Fleet.Redis (FleetEvent (..))-import Haskoin.Store.Ingest (Config (sweepWait), defaultConfig)+import Haskoin.Store.Ingest (Config (operated, sweepWait), defaultConfig) import Haskoin.Store.Ingest.Run ( IngestConfig (..), Ingester,- ingestEmptiedSTM,+ FleetPool (..), ingestLookSTM, ingestMissingSTM, ingestNamedSTM,@@ -322,7 +325,7 @@ withBlockStore (blockStoreCfg cfg node pub db member ingest) $ \block_store -> withAsync (nodeForwarder block_store ingest pub node_sub) $ \fwd -> do link fwd- withStoreFleet member ingest $+ withStoreFleet member $ withFleetEvents cfg pub member $ \events -> action $ Store@@ -383,6 +386,18 @@ given <- concat <$> mapM (toSockAddr cfg.net) cfg.initPeers return (show p `elem` map show given) +-- | Whether the options of the store name a node of the operator that+-- gives its mempool ('Haskoin.Store.Ingest.operated'): the RPC of a node,+-- or peers that are nodes of the operator and let the store ask for+-- their mempools ('Haskoin.Node.PermMempool'). A node may also let a+-- peer ask for it when it serves Bloom filters, which no option says: a+-- store that has such a node as its only one says @unknown@ while it is+-- away, as it did before.+operatorNode :: StoreConfig -> Bool+operatorNode cfg =+ isJust cfg.nodePeerRpc+ || (not cfg.foreignPeers && not (null cfg.initPeers) && Set.member PermMempool cfg.peerPermissions)+ -- | Whether a peer is the node that the store has through its RPC. isNodePeer :: Peer -> Bool isNodePeer p = show p == show nodePeerAddress@@ -446,22 +461,16 @@ -- an offer as soon as a transaction comes ('memberLoop'). Beside them, in -- a thread of its own, the member builds the entries of the fleet's cache -- of extended public keys that requests asked for.-withStoreFleet :: Maybe Member -> Ingester -> IO a -> IO a-withStoreFleet Nothing _ action = action-withStoreFleet (Just member) ingest action =+--+-- Each time the member finds that the pool lost what it had, the path+-- into the pool reads its sources again: it reads the count of that+-- itself ('Haskoin.Store.Ingest.Run.fleet'), in its order with the moves+-- of the tip.+withStoreFleet :: Maybe Member -> IO a -> IO a+withStoreFleet Nothing action = action+withStoreFleet (Just member) action = withAsync (memberLoop member) $ \a ->- withAsync (memberCacheLoop member) $ \b ->- withAsync (emptied 0) $ \c -> link a >> link b >> link c >> action- where- -- each time the member finds that the pool lost what it had, the path- -- into the pool is told, and reads its sources again- emptied seen = do- n <- atomically $ do- n <- readTVar member.refills- when (n == seen) retry- return n- atomically (ingestEmptiedSTM ingest)- emptied n+ withAsync (memberCacheLoop member) $ \b -> link a >> link b >> action -- | Run an action with the events that the store tells its clients: those -- of the fleet, which a thread reads from Redis for as long as the action@@ -553,13 +562,18 @@ -- headers of its peers too, which peers give their mempools when asked, -- as the model of each peer's node says ('judgeMessage'), which are -- nodes of the operator ('ownPeer'), which answer in order (the node--- through its RPC), and how far the chain moved, back to where the best--- block of the database and the best header split and on to the header.+-- through its RPC), how far the chain moved, back to where the best+-- block of the database and the best header split and on to the header,+-- and what the fleet says of its pool. ingestCfg :: IORef Bool -> StoreConfig -> Node -> DatabaseReader -> Maybe Member -> IngestConfig ingestCfg begun cfg node db member = IngestConfig { net = cfg.net,- config = defaultConfig {sweepWait = max 1 (round (cfg.mempoolWait * 1000))},+ config =+ defaultConfig+ { sweepWait = max 1 (round (cfg.mempoolWait * 1000)),+ operated = operatorNode cfg+ }, member, -- The first time, the headers too: while the node still takes -- them, two thousand at a time, the database catches up with the@@ -595,6 +609,20 @@ Just b -> do split <- chainGetSplitBlock node.chain b top return (fromIntegral (top.height - split.height) + fromIntegral (b.height - split.height)),+ -- The tip first, then the count of emptyings: the member counts an+ -- emptying of the pool before it notes the tip that emptied it+ -- ('memberTick'), so a tip read here comes with its emptying.+ fleet =+ member <&> \m -> do+ tip <- readTVarIO m.tip+ best <- Q.getBestBlock db+ emptied <- readTVarIO m.refills+ let atTip = case (tip, best) of+ (Just t, Just b) -> t == b+ -- no tip, or no block: the pool is at no block of this+ -- store's+ _ -> False+ return FleetPool {emptied, atTip}, stats = cfg.stats, logger = withSource "Ingest" cfg.logger }
swagger.yaml view
@@ -7048,16 +7048,20 @@ `syncing`: the server has such a node to read a whole mempool from, and has not yet read it and taken in all that it named,- since it started or since the shared mempool lost what it had.- It then shows fewer unconfirmed transactions than its node- has. It says so for twenty seconds at least after a start, the- time that a node is given to name its mempool.+ since it started, since the shared mempool lost what it had,+ or since the chain moved by more than two blocks; or the tip+ of its fleet has not reached its best block since. It then+ shows fewer unconfirmed transactions than its node has, or is+ about to. A peer is given twenty seconds after a start to name+ its mempool, unless `--mempool-wait` says otherwise; the RPC of+ a node says when it has. A server whose options name a node of+ its operator also says `syncing` while that node is away. `synced`: it has. `unknown`: the server has no node of its operator to read a whole mempool from, only peers that it does not trust or- nobody, and cannot tell.+ nobody, its options name none, and it cannot tell. The server is healthy in all three: wait for `synced` before giving a new server traffic, if unconfirmed transactions
test/Haskoin/Store/Fleet/CacheSpec.hs view
@@ -122,6 +122,7 @@ it "keeps the cache across a reorganisation, as the chain and the pool give it" (`withBed` reorgTest) it "fills the pool again after Redis lost its data" (`withBed` refillTest) it "fills the pool again after it started again, once after Redis lost its data" (`withBed` restartTest)+ it "counts a start of the pool again before it notes the tip that the start moved to" (`withBed` farTest) it "keeps what an abandoned block gave back when the chain moves under a move of the tip" $ (`withBed` unsettledTest) it "counts as settled, for every member, what the pool has decided on" (`withBed` seenTest)@@ -365,6 +366,34 @@ settle bed readTVarIO bed.a.refills `shouldReturn` 2 readTVarIO bed.b.refills `shouldReturn` 2++-- | The chain moves by more than 'farthest' blocks, and the member that+-- moves the tip starts the pool again there. It counts the refill in the+-- same turn, before it notes the new tip: the path into the pool reads+-- the tip and then the count ("Haskoin.Store.Ingest.Run"), and said that+-- the mempool was in sync for the turn in which it saw the tip without+-- the emptying. The other member counts it in its next turn, before it+-- notes the tip too.+farTest :: Bed -> IO ()+farTest bed = do+ settle bed+ readTVarIO bed.a.refills `shouldReturn` 0+ replicateM_ (farthest + 1) (mineBlock bed [])+ h <- best bed+ -- the other member reports the new blocks; this one reports them and+ -- moves the tip, the way too long for a move+ memberTick bed.b+ memberTick bed.a+ memberTip bed.a `shouldReturn` Just (encode h)+ readTVarIO bed.a.tip `shouldReturn` Just h+ readTVarIO bed.a.refills `shouldReturn` 1+ memberTick bed.b+ readTVarIO bed.b.tip `shouldReturn` Just h+ readTVarIO bed.b.refills `shouldReturn` 1+ -- and it is counted once+ settle bed+ readTVarIO bed.a.refills `shouldReturn` 1+ readTVarIO bed.b.refills `shouldReturn` 1 refillTest :: Bed -> IO () refillTest bed = do
test/Haskoin/Store/Ingest/Model.hs view
@@ -134,6 +134,10 @@ | -- | the chain moves by more than a block or two, and the store is -- out of sync until it has the blocks Moves+ | -- | the same, by more than the fleet tip goes through+ -- ('Haskoin.Store.Fleet.Member.farthest'): the tip is behind too, and+ -- when it moves there the pool may start again+ FarMoves | -- | a peer goes, and comes back PeerDrops | -- | the pool does not answer@@ -182,7 +186,9 @@ asked :: !(Seq Op), inSync :: !Bool, -- | whether the store ever had the blocks of its nodes- hadBlocks :: !Bool+ hadBlocks :: !Bool,+ -- | whether the fleet tip is the store's best block+ atTip :: !Bool } deriving (Eq, Ord, Show) @@ -225,7 +231,8 @@ toStore = Map.fromList [(p, Seq.empty) | (p, _) <- sc.peers], asked = Seq.empty, inSync = False,- hadBlocks = False+ hadBlocks = False,+ atTip = True } -- | Give the machine an input: check what the step did against the rules@@ -346,6 +353,9 @@ -- which requests get no answer in time breaks A10 on purpose, and is -- not held to this. when (Timeouts `notElem` sc.disturbances && standing before /= PoolSynced && standing after == PoolSynced) $ do+ -- the pool is the mempool at the fleet tip, and a tip that is far+ -- behind empties it when it moves there+ check "the pool is said to be in sync while the fleet tip is not at the best block" w.atTip check "the pool is said to be in sync while a node of the operator has not answered for its mempool" $ and [MSweep `notElem` toList q | (p, q) <- Map.toList w.toNode, trustedHere p] check "the pool is said to be in sync and lacks a transaction that a node of the operator named and has" $@@ -355,7 +365,9 @@ trustedHere p ] -- it says so only of a node of the operator that gives its mempool:- -- what strangers name says nothing of what the pool should have+ -- what strangers name says nothing of what the pool should have. While+ -- the options of the store name such a node, it says that it is+ -- syncing when none is there, and never that it cannot tell. -- -- A peer connects one step before the machine is told whose it is: -- between the two it is a stranger to the machine, and this is not asked.@@ -364,7 +376,8 @@ SourceUp p | p `elem` sc.owned -> return () _ -> check "the pool is said to be in sync, or syncing, with no node of the operator to measure it against" $- (standing after == PoolUnknown) == not (or [trustedHere p | p <- Map.keys w.nodes])+ (standing after == PoolUnknown)+ == (not sc.config.operated && not (or [trustedHere p | p <- Map.keys w.nodes])) -- and a pool that lost what it had is not in sync, nor one whose chain -- moved far case input of@@ -558,6 +571,28 @@ ] Desyncs -> [("the store is out of sync", feed sc (Synced False) w {inSync = False}) | w.inSync] Moves -> [("the chain moves far", feed sc Moved w {inSync = False}) | w.inSync]+ FarMoves ->+ [ ( "the chain moves farther than the fleet tip goes",+ feed sc (AtTip False) w {atTip = False} >>= feed sc Moved . \x -> x {inSync = False}+ )+ | w.inSync,+ w.atTip+ ]+ -- The member moves the fleet tip to the best block, through the+ -- blocks or by starting the pool again there; the runner says+ -- the emptying first ("Haskoin.Store.Ingest.Run"). It is a+ -- disturbance and not a step that goes well, so that time can+ -- pass while the tip is behind: a peer is heard by the time+ -- that passed, and the machine could catch up meanwhile.+ <> [ step'+ | not w.atTip,+ step' <-+ [ ("the fleet tip moves to the best block", feed sc (AtTip True) w {atTip = True}),+ ( "the fleet tip moves to the best block and the pool starts again",+ feed sc Emptied w {pool = Set.empty} >>= feed sc (AtTip True) . \x -> x {atTip = True}+ )+ ]+ ] PeerDrops -> [ ( "peer " <> show p <> " is gone", feed@@ -769,7 +804,8 @@ retry = 1, sweepGap = 2, sweepEvery = 0,- sweepFast = 0+ sweepFast = 0,+ operated = False }, txs = Map.fromList [(1, []), (2, [1]), (3, [2]), (4, []), (5, [4]), (6, [5, 3])], peers = [(1, True), (2, True), (3, False)],@@ -813,7 +849,8 @@ retry = 1, sweepGap = 1, sweepEvery = 0,- sweepFast = 0+ sweepFast = 0,+ operated = False }, txs = Map.fromList [(1, []), (2, [1])], peers = [(1, True)],@@ -890,6 +927,19 @@ feeds = [1], owned = [1] },+ -- a move longer than the fleet tip goes: the store does not say that+ -- it caught up before the tip is at its best block, whether the pool+ -- starts again there or not+ (world "a chain of two, a node of the operator, moves too long for the fleet tip" chain [(1, True)] far small)+ { owned = [1]+ },+ -- the options of the store name its node: while that node is away the+ -- store says that it is syncing, and never that it cannot tell+ (world "one transaction, the RPC of a node that the options name, which goes" one mixed wavering named)+ { oneNode = True,+ feeds = [1],+ owned = [1]+ }, -- T8: peer 1 is a stranger and peer 2 a node of the operator. The -- store cannot verify transaction 1, and 2 spends from it. (world "a stranger and a node of the operator, a transaction that cannot be verified" one both wavering small)@@ -937,6 +987,7 @@ where losing = [Mempools, PoolEmptied] moving = [Mempools, Moves]+ far = [Mempools, FarMoves] world name txs peers disturbances config = Scenario { name,@@ -961,6 +1012,7 @@ three = Map.fromList [(1, []), (2, [1]), (3, [])] pair = Map.fromList [(1, []), (2, [])] tight = small {room = 1, heldRoom = 1}+ named = small {operated = True} two = small {heldRoom = 2, maxWindow = 3} one = Map.fromList [(1, [])] chain = Map.fromList [(1, []), (2, [1])]@@ -979,5 +1031,6 @@ retry = 1, sweepGap = 1, sweepEvery = 0,- sweepFast = 0+ sweepFast = 0,+ operated = False }
test/Haskoin/Store/IngestSpec.hs view
@@ -296,6 +296,48 @@ standing gone `shouldBe` PoolUnknown -- back, it has not read what the node took meanwhile standing (fst (run' [PeerUp 1 True, Own 1] gone)) `shouldBe` PoolSyncing+ it "says that it is syncing, not unknown, while the node that its options name is away" $ do+ -- the options name the RPC of a node, or a peer that gives its+ -- mempool: syncing from the start, before the node is there+ let named = start defaultConfig {operated = True} 0 :: Ingest Int Int [Int]+ standing named `shouldBe` PoolSyncing+ standing (fst (run' [Synced True, PeerUp 2 True] named)) `shouldBe` PoolSyncing+ let rpc = fst (run' [SourceUp 1, Own 1, Synced True, Answered 1] named)+ standing rpc `shouldBe` PoolSynced+ -- the node restarts: the store has lost what it measures against+ let gone = fst (run' [PeerDown 1] rpc)+ standing gone `shouldBe` PoolSyncing+ standing (fst (run' [Tick 60000] gone)) `shouldBe` PoolSyncing+ -- back, syncing until it read the node again+ let back = fst (run' [SourceUp 3, Own 3] gone)+ standing back `shouldBe` PoolSyncing+ standing (fst (run' [Answered 3] back)) `shouldBe` PoolSynced+ -- options that name no such node: unknown, as before+ standing (fst (run' [PeerDown 1] (fst (run' [SourceUp 1, Own 1, Synced True, Answered 1] fresh))))+ `shouldBe` PoolUnknown+ it "does not say that it is in sync before the fleet tip is at its best block" $ do+ -- a store of a fleet: the runner says first that the tip is not there+ let rpc = fst (run' [AtTip False, SourceUp 1, Own 1, Synced True, Answered 1] fresh)+ standing rpc `shouldBe` PoolSyncing+ let there = fst (run' [AtTip True] rpc)+ standing there `shouldBe` PoolSynced+ -- the tip a block behind for a moment: still in sync+ standing (fst (run' [AtTip False] there)) `shouldBe` PoolSynced+ -- the chain moves by more than 'farthest' blocks: the store has the+ -- blocks and its node answered, and the tip is still behind+ let (caught, _) = run' [AtTip False, Moved, Synced True, Answered 1] there+ standing caught `shouldBe` PoolSyncing+ -- the member moves the tip and starts the pool again: the runner+ -- says the emptying first, then that the tip is there+ let (restarted, outs) = run' [Emptied, AtTip True] caught+ standing restarted `shouldBe` PoolSyncing+ outs `shouldBe` []+ -- the node is read again after the gap, and the store is in sync+ -- once it answered+ let (again, more) = run' [Tick 10000] restarted+ [p | Sweep p <- more] `shouldBe` [1]+ standing again `shouldBe` PoolSyncing+ standing (fst (run' [Answered 1] again)) `shouldBe` PoolSynced where ready :: Ingest Int Int [Int] ready = fst (run' [PeerUp 1 True, Synced True] (start defaultConfig 0))@@ -303,6 +345,8 @@ ours :: Ingest Int Int [Int] ours = fst (run' [Own 1] ready) run = flip run' ready+ fresh :: Ingest Int Int [Int]+ fresh = start defaultConfig 0 run' inputs s0 = foldl' (\(s, outs) i -> let (s', o) = step id i s in (s', outs <> o)) (s0, []) inputs -- | A store with an empty pool and one source whose mempool has so many