Skip to content

Commit

Permalink
Merge pull request #1260 from Concordium/scheduled-cooldown-queries
Browse files Browse the repository at this point in the history
Queries for accounts with scheduled releases or cooldowns
  • Loading branch information
td202 authored Nov 28, 2024
2 parents 4a2e7b8 + a235a5c commit e728385
Show file tree
Hide file tree
Showing 12 changed files with 633 additions and 50 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
of the consensus, at consensus version 1.
- Update Rust version to 1.82.
- Update GHC version to 9.6.6 (LTS-22.39).
- Add `GetScheduledReleaseAccounts` endpoint for querying the list of accounts that
have scheduled releases.
- Add `GetCooldownAccounts`, `GetPreCooldownAccounts` and `GetPrePreCooldownAccounts`
endpoints for querying the lists of accounts that have pending cooldowns in protocol
version 7 onwards.

## 7.0.5

Expand Down
4 changes: 4 additions & 0 deletions concordium-consensus/lib.def
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ EXPORTS
getBlockTransactionEventsV2
getBlockSpecialEventsV2
getBlockPendingUpdatesV2
getScheduledReleaseAccountsV2
getCooldownAccountsV2
getPreCooldownAccountsV2
getPrePreCooldownAccountsV2
getNextUpdateSequenceNumbersV2
getBlockChainParametersV2
getBlockFinalizationSummaryV2
Expand Down
187 changes: 139 additions & 48 deletions concordium-consensus/src-lib/Concordium/External/GRPC2.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}

Expand Down Expand Up @@ -147,12 +148,7 @@ getAccountListV2 ::
Ptr Word8 ->
FunPtr (Ptr SenderChannel -> Ptr Word8 -> Int64 -> IO Int32) ->
IO Int64
getAccountListV2 cptr channel blockType blockHashPtr outHash cbk = do
Ext.ConsensusRunner mvr <- deRefStablePtr cptr
let sender = callChannelSendCallback cbk
bhi <- decodeBlockHashInput blockType blockHashPtr
response <- runMVR (Q.getAccountList bhi) mvr
returnStreamWithBlock (sender channel) outHash response
getAccountListV2 = blockStreamHelper Q.getAccountList

getModuleListV2 ::
StablePtr Ext.ConsensusRunner ->
Expand All @@ -165,12 +161,7 @@ getModuleListV2 ::
Ptr Word8 ->
FunPtr (Ptr SenderChannel -> Ptr Word8 -> Int64 -> IO Int32) ->
IO Int64
getModuleListV2 cptr channel blockType blockHashPtr outHash cbk = do
Ext.ConsensusRunner mvr <- deRefStablePtr cptr
let sender = callChannelSendCallback cbk
bhi <- decodeBlockHashInput blockType blockHashPtr
response <- runMVR (Q.getModuleList bhi) mvr
returnStreamWithBlock (sender channel) outHash response
getModuleListV2 = blockStreamHelper Q.getModuleList

getModuleSourceV2 ::
StablePtr Ext.ConsensusRunner ->
Expand Down Expand Up @@ -205,12 +196,7 @@ getInstanceListV2 ::
Ptr Word8 ->
FunPtr (Ptr SenderChannel -> Ptr Word8 -> Int64 -> IO Int32) ->
IO Int64
getInstanceListV2 cptr channel blockType blockHashPtr outHash cbk = do
Ext.ConsensusRunner mvr <- deRefStablePtr cptr
let sender = callChannelSendCallback cbk
bhi <- decodeBlockHashInput blockType blockHashPtr
response <- runMVR (Q.getInstanceList bhi) mvr
returnStreamWithBlock (sender channel) outHash response
getInstanceListV2 = blockStreamHelper Q.getInstanceList

getInstanceInfoV2 ::
StablePtr Ext.ConsensusRunner ->
Expand Down Expand Up @@ -541,12 +527,7 @@ getBakerListV2 ::
Ptr Word8 ->
FunPtr ChannelSendCallback ->
IO Int64
getBakerListV2 cptr channel blockType blockHashPtr outHash cbk = do
Ext.ConsensusRunner mvr <- deRefStablePtr cptr
let sender = callChannelSendCallback cbk
bhi <- decodeBlockHashInput blockType blockHashPtr
response <- runMVR (Q.getRegisteredBakers bhi) mvr
returnStreamWithBlock (sender channel) outHash response
getBakerListV2 = blockStreamHelper Q.getRegisteredBakers

getPoolInfoV2 ::
StablePtr Ext.ConsensusRunner ->
Expand Down Expand Up @@ -803,12 +784,7 @@ getIdentityProvidersV2 ::
Ptr Word8 ->
FunPtr ChannelSendCallback ->
IO Int64
getIdentityProvidersV2 cptr channel blockType blockHashPtr outHash cbk = do
Ext.ConsensusRunner mvr <- deRefStablePtr cptr
let sender = callChannelSendCallback cbk
bhi <- decodeBlockHashInput blockType blockHashPtr
response <- runMVR (Q.getAllIdentityProviders bhi) mvr
returnStreamWithBlock (sender channel) outHash response
getIdentityProvidersV2 = blockStreamHelper Q.getAllIdentityProviders

getAnonymityRevokersV2 ::
StablePtr Ext.ConsensusRunner ->
Expand All @@ -821,12 +797,7 @@ getAnonymityRevokersV2 ::
Ptr Word8 ->
FunPtr ChannelSendCallback ->
IO Int64
getAnonymityRevokersV2 cptr channel blockType blockHashPtr outHash cbk = do
Ext.ConsensusRunner mvr <- deRefStablePtr cptr
let sender = callChannelSendCallback cbk
bhi <- decodeBlockHashInput blockType blockHashPtr
response <- runMVR (Q.getAllAnonymityRevokers bhi) mvr
returnStreamWithBlock (sender channel) outHash response
getAnonymityRevokersV2 = blockStreamHelper Q.getAllAnonymityRevokers

getAccountNonFinalizedTransactionsV2 ::
StablePtr Ext.ConsensusRunner ->
Expand Down Expand Up @@ -855,12 +826,7 @@ getBlockItemsV2 ::
Ptr Word8 ->
FunPtr ChannelSendCallback ->
IO Int64
getBlockItemsV2 cptr channel blockType blockHashPtr outHash cbk = do
Ext.ConsensusRunner mvr <- deRefStablePtr cptr
let sender = callChannelSendCallback cbk
bhi <- decodeBlockHashInput blockType blockHashPtr
response <- runMVR (Q.getBlockItems bhi) mvr
returnStreamWithBlock (sender channel) outHash response
getBlockItemsV2 = blockStreamHelper Q.getBlockItems

getBlockTransactionEventsV2 ::
StablePtr Ext.ConsensusRunner ->
Expand Down Expand Up @@ -918,12 +884,7 @@ getBlockPendingUpdatesV2 ::
Ptr Word8 ->
FunPtr ChannelSendCallback ->
IO Int64
getBlockPendingUpdatesV2 cptr channel blockType blockHashPtr outHash cbk = do
Ext.ConsensusRunner mvr <- deRefStablePtr cptr
let sender = callChannelSendCallback cbk
bhi <- decodeBlockHashInput blockType blockHashPtr
response <- runMVR (Q.getBlockPendingUpdates bhi) mvr
returnStreamWithBlock (sender channel) outHash response
getBlockPendingUpdatesV2 = blockStreamHelper Q.getBlockPendingUpdates

getNextUpdateSequenceNumbersV2 ::
StablePtr Ext.ConsensusRunner ->
Expand All @@ -944,6 +905,58 @@ getNextUpdateSequenceNumbersV2 cptr blockType blockHashPtr outHash outVec copier
result <- runMVR (Q.getNextUpdateSequenceNumbers bhi) mvr
returnMessageWithBlock (copier outVec) outHash result

getScheduledReleaseAccountsV2 ::
StablePtr Ext.ConsensusRunner ->
Ptr SenderChannel ->
-- | Block type.
Word8 ->
-- | Block hash.
Ptr Word8 ->
-- | Out pointer for writing the block hash that was used.
Ptr Word8 ->
FunPtr ChannelSendCallback ->
IO Int64
getScheduledReleaseAccountsV2 = blockStreamHelper Q.getScheduledReleaseAccounts

getCooldownAccountsV2 ::
StablePtr Ext.ConsensusRunner ->
Ptr SenderChannel ->
-- | Block type.
Word8 ->
-- | Block hash.
Ptr Word8 ->
-- | Out pointer for writing the block hash that was used.
Ptr Word8 ->
FunPtr ChannelSendCallback ->
IO Int64
getCooldownAccountsV2 = blockStreamHelper Q.getCooldownAccounts

getPreCooldownAccountsV2 ::
StablePtr Ext.ConsensusRunner ->
Ptr SenderChannel ->
-- | Block type.
Word8 ->
-- | Block hash.
Ptr Word8 ->
-- | Out pointer for writing the block hash that was used.
Ptr Word8 ->
FunPtr ChannelSendCallback ->
IO Int64
getPreCooldownAccountsV2 = blockStreamHelper Q.getPreCooldownAccounts

getPrePreCooldownAccountsV2 ::
StablePtr Ext.ConsensusRunner ->
Ptr SenderChannel ->
-- | Block type.
Word8 ->
-- | Block hash.
Ptr Word8 ->
-- | Out pointer for writing the block hash that was used.
Ptr Word8 ->
FunPtr ChannelSendCallback ->
IO Int64
getPrePreCooldownAccountsV2 = blockStreamHelper Q.getPrePreCooldownAccounts

getBlockChainParametersV2 ::
StablePtr Ext.ConsensusRunner ->
-- | Block type.
Expand Down Expand Up @@ -1130,6 +1143,32 @@ returnMessageRaw copier v = do
BS.unsafeUseAsCStringLen encoded (\(ptr, len) -> copier (castPtr ptr) (fromIntegral len))
return $ queryResultCode QRSuccess

-- | A helper for defining queries that take a block hash input and return a stream of
-- protobuf messages.
blockStreamHelper ::
(Proto.Message (Output a), ToProto a) =>
-- | Wrapped query function.
(forall finconf. BlockHashInput -> MVR finconf (Q.BHIQueryResponse [a])) ->
-- | Consensus pointer.
StablePtr Ext.ConsensusRunner ->
-- | Channel to send messages to.
Ptr SenderChannel ->
-- | Block type.
Word8 ->
-- | Block hash.
Ptr Word8 ->
-- | Out pointer for writing the block hash that was used.
Ptr Word8 ->
-- | Callback to output data to the channel.
FunPtr ChannelSendCallback ->
IO Int64
blockStreamHelper query cptr channel blockType blockHashPtr outHash cbk = do
Ext.ConsensusRunner mvr <- deRefStablePtr cptr
let sender = callChannelSendCallback cbk
bhi <- decodeBlockHashInput blockType blockHashPtr
response <- runMVR (query bhi) mvr
returnStreamWithBlock (sender channel) outHash response

returnStreamWithBlock ::
(Proto.Message (Output a), ToProto a) =>
(Ptr Word8 -> Int64 -> IO Int32) ->
Expand Down Expand Up @@ -1667,6 +1706,58 @@ foreign export ccall
FunPtr CopyToVecCallback ->
IO Int64

foreign export ccall
getScheduledReleaseAccountsV2 ::
StablePtr Ext.ConsensusRunner ->
Ptr SenderChannel ->
-- | Block type.
Word8 ->
-- | Block hash.
Ptr Word8 ->
-- | Out pointer for writing the block hash that was used.
Ptr Word8 ->
FunPtr ChannelSendCallback ->
IO Int64

foreign export ccall
getCooldownAccountsV2 ::
StablePtr Ext.ConsensusRunner ->
Ptr SenderChannel ->
-- | Block type.
Word8 ->
-- | Block hash.
Ptr Word8 ->
-- | Out pointer for writing the block hash that was used.
Ptr Word8 ->
FunPtr ChannelSendCallback ->
IO Int64

foreign export ccall
getPreCooldownAccountsV2 ::
StablePtr Ext.ConsensusRunner ->
Ptr SenderChannel ->
-- | Block type.
Word8 ->
-- | Block hash.
Ptr Word8 ->
-- | Out pointer for writing the block hash that was used.
Ptr Word8 ->
FunPtr ChannelSendCallback ->
IO Int64

foreign export ccall
getPrePreCooldownAccountsV2 ::
StablePtr Ext.ConsensusRunner ->
Ptr SenderChannel ->
-- | Block type.
Word8 ->
-- | Block hash.
Ptr Word8 ->
-- | Out pointer for writing the block hash that was used.
Ptr Word8 ->
FunPtr ChannelSendCallback ->
IO Int64

foreign export ccall
getBlockChainParametersV2 ::
StablePtr Ext.ConsensusRunner ->
Expand Down
20 changes: 20 additions & 0 deletions concordium-consensus/src/Concordium/GlobalState/BlockState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,18 @@ class (ContractStateOperations m, AccountOperations m, ModuleQuery m) => BlockSt
BlockState m ->
m PassiveDelegationStatus

-- | Get the index of accounts with scheduled releases.
getScheduledReleaseAccounts :: BlockState m -> m (Map.Map Timestamp (Set.Set AccountIndex))

-- | Get the index of accounts with stake in cooldown.
getCooldownAccounts :: BlockState m -> m (Map.Map Timestamp (Set.Set AccountIndex))

-- | Get the index of accounts in pre-cooldown.
getPreCooldownAccounts :: BlockState m -> m [AccountIndex]

-- | Get the index of accounts in pre-pre-cooldown.
getPrePreCooldownAccounts :: BlockState m -> m [AccountIndex]

-- | Distribution of newly-minted GTU.
data MintAmounts = MintAmounts
{ -- | Minted amount allocated to the BakingRewardAccount
Expand Down Expand Up @@ -1669,6 +1681,10 @@ instance (Monad (t m), MonadTrans t, BlockStateQuery m) => BlockStateQuery (MGST
getPaydayEpoch = lift . getPaydayEpoch
getPoolStatus s = lift . getPoolStatus s
getPassiveDelegationStatus = lift . getPassiveDelegationStatus
getScheduledReleaseAccounts = lift . getScheduledReleaseAccounts
getCooldownAccounts = lift . getCooldownAccounts
getPreCooldownAccounts = lift . getPreCooldownAccounts
getPrePreCooldownAccounts = lift . getPrePreCooldownAccounts
{-# INLINE getModule #-}
{-# INLINE getAccount #-}
{-# INLINE accountExists #-}
Expand Down Expand Up @@ -1703,6 +1719,10 @@ instance (Monad (t m), MonadTrans t, BlockStateQuery m) => BlockStateQuery (MGST
{-# INLINE getUpdateKeysCollection #-}
{-# INLINE getExchangeRates #-}
{-# INLINE getChainParameters #-}
{-# INLINE getScheduledReleaseAccounts #-}
{-# INLINE getCooldownAccounts #-}
{-# INLINE getPreCooldownAccounts #-}
{-# INLINE getPrePreCooldownAccounts #-}

instance (Monad (t m), MonadTrans t, AccountOperations m) => AccountOperations (MGSTrans t m) where
getAccountCanonicalAddress = lift . getAccountCanonicalAddress
Expand Down
Loading

0 comments on commit e728385

Please sign in to comment.