-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1507: Implement the LocalStateQueryServer r=mrBliss a=mrBliss Closes #1366. Co-authored-by: Thomas Winant <[email protected]>
- Loading branch information
Showing
34 changed files
with
1,886 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
ouroboros-consensus/src/Ouroboros/Consensus/LocalStateQueryServer.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
module Ouroboros.Consensus.LocalStateQueryServer | ||
( localStateQueryServer | ||
) where | ||
|
||
import Data.Functor ((<&>)) | ||
|
||
import Ouroboros.Network.Protocol.LocalStateQuery.Server | ||
import Ouroboros.Network.Protocol.LocalStateQuery.Type | ||
(AcquireFailure (..)) | ||
|
||
import Ouroboros.Network.Block (Point) | ||
|
||
import Ouroboros.Consensus.Ledger.Abstract (LedgerState (..), | ||
QueryLedger (..)) | ||
import Ouroboros.Consensus.Ledger.Extended (ExtLedgerState (..)) | ||
import Ouroboros.Consensus.Util.IOLike | ||
|
||
import Ouroboros.Storage.ChainDB (LedgerCursor (..), | ||
LedgerCursorFailure (..)) | ||
|
||
localStateQueryServer | ||
:: forall m blk. (IOLike m, QueryLedger blk) | ||
=> m (LedgerCursor m blk) | ||
-> LocalStateQueryServer blk (Query blk) m () | ||
localStateQueryServer newLedgerCursor = | ||
LocalStateQueryServer $ idle <$> newLedgerCursor | ||
where | ||
idle | ||
:: LedgerCursor m blk | ||
-> ServerStIdle blk (Query blk) m () | ||
idle ledgerCursor = ServerStIdle | ||
{ recvMsgAcquire = handleAcquire ledgerCursor | ||
, recvMsgDone = return () | ||
} | ||
|
||
handleAcquire | ||
:: LedgerCursor m blk | ||
-> Point blk | ||
-> m (ServerStAcquiring blk (Query blk) m ()) | ||
handleAcquire ledgerCursor pt = | ||
ledgerCursorMove ledgerCursor pt <&> \case | ||
Left failure -> | ||
SendMsgFailure (translateFailure failure) (idle ledgerCursor) | ||
Right ExtLedgerState { ledgerState } -> | ||
SendMsgAcquired (acquired ledgerState ledgerCursor) | ||
|
||
acquired | ||
:: LedgerState blk | ||
-> LedgerCursor m blk | ||
-> ServerStAcquired blk (Query blk) m () | ||
acquired ledgerState ledgerCursor = ServerStAcquired | ||
{ recvMsgQuery = handleQuery ledgerState ledgerCursor | ||
, recvMsgReAcquire = handleAcquire ledgerCursor | ||
, recvMsgRelease = return $ idle ledgerCursor | ||
} | ||
|
||
handleQuery | ||
:: LedgerState blk | ||
-> LedgerCursor m blk | ||
-> Query blk result | ||
-> m (ServerStQuerying blk (Query blk) m () result) | ||
handleQuery ledgerState ledgerCursor query = return $ | ||
SendMsgResult | ||
(answerQuery query ledgerState) | ||
(acquired ledgerState ledgerCursor) | ||
|
||
translateFailure | ||
:: LedgerCursorFailure | ||
-> AcquireFailure | ||
translateFailure = \case | ||
PointTooOld -> AcquireFailurePointTooOld | ||
PointNotOnChain -> AcquireFailurePointNotOnChain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.